using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BI;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Enums;
using APT.Utility;
using APT.WebApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.PP.WebApi.Controllers.Api.PP
{
    [Route("api/PP/BISAFE")]
    [TypeFilter(typeof(CustomActionFilterAttribute))]
    public partial class BISAFEController : AuthorizeApiController<T_BI_BSSAFECHECK>
    {
        IBIBSSafeCheckService SafeCheckService { get; set; }
        IFMSyncLogDetailService SyncLogDetailService { get; set; }

        public BISAFEController(IBIBSSafeCheckService safeCheckService, IFMSyncLogDetailService syncLogDetailService)
        {
            SafeCheckService = safeCheckService;
            SyncLogDetailService = syncLogDetailService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("AddSafeInfo")]
        public JsonActionResult<bool> AddSafeInfo([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                try
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtRun = DateTime.Now;
                    if (!string.IsNullOrEmpty(filter.Parameter1))
                    {
                        try
                        {
                            //接收来自页面的参数
                            DateTime deParm = Convert.ToDateTime(filter.Parameter1);
                            dtRun = deParm;
                        }
                        catch { }
                    }

                    //获取 model
                    List<T_BI_BSSAFECHECK> listSafeCheck = new List<T_BI_BSSAFECHECK>();
                    List<T_BI_BSCHECKTYPE> listCheckType = new List<T_BI_BSCHECKTYPE>();
                    List<T_BI_SUMMARY> listSummary = new List<T_BI_SUMMARY>();
                    T_BI_RUNLOG modelLog = new T_BI_RUNLOG();

                    string Code = "BI002_BSSAFECHECK"; //编码
                    bool result = SafeCheckService.SafeCheckGet(filter.OrgId.Value, dtRun, ref listSafeCheck, ref listCheckType, ref listSummary, ref modelLog);

                    List<Guid> listIDSafeCheck = new List<Guid>();
                    List<Guid> listIDCheckType = new List<Guid>();
                    List<Guid> listIDSummary = new List<Guid>();

                    #region    查找旧数据

                    var IlistCheck = this.GetEntities<T_BI_BSSAFECHECK>(e => e.ORG_ID == filter.OrgId.Value, null);
                    if (IlistCheck != null && IlistCheck.Any())
                    {
                        listIDSafeCheck = IlistCheck.Select(x => x.ID).ToList();
                    }

                    var IlistCheckType = this.GetEntities<T_BI_BSCHECKTYPE>(e => e.ORG_ID == filter.OrgId.Value, null);
                    if (IlistCheckType != null && IlistCheckType.Any())
                    {
                        listIDCheckType = IlistCheckType.Select(x => x.ID).ToList();
                    }

                    var IlistSummary = this.GetEntities<T_BI_SUMMARY>(e => e.ORG_ID == filter.OrgId.Value && e.CODE == Code, null);
                    if (IlistSummary != null && IlistSummary.Any())
                    {
                        listIDSummary = IlistSummary.Select(x => x.ID).ToList();
                    }

                    #endregion

                    T_FM_SYNC_TASK task = null;
                    task = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                    if (task != null)
                    {
                        task.CURR_TASK_START_TIME = dtNow;//上次同步结束时间
                        task.UPDATE_SUCCES_TIME = DateTime.Now;//上次同步结束时间
                        task.SYNC_PARAM = "";//清空参数
                    }

                    this.UnifiedCommit(() =>
                    {
                        if (task != null)
                            UpdateEntityNoCommit(task);//跑批信息
                        if (modelLog != null)//日志
                            UpdateEntityNoCommit(modelLog);

                        //删除旧数据
                        if (listIDSafeCheck.Count > 0)
                            BantchDeleteEntity<T_BI_BSSAFECHECK>(listIDSafeCheck);
                        if (listIDCheckType.Count > 0)
                            BantchDeleteEntity<T_BI_BSCHECKTYPE>(listIDCheckType);
                        if (listIDSummary.Count > 0)
                            BantchDeleteEntity<T_BI_SUMMARY>(listIDSummary);

                        //插入新数据
                        if (listSafeCheck != null && listSafeCheck.Count > 0)
                            BantchSaveEntityNoCommit(listSafeCheck);
                        if (listCheckType != null && listCheckType.Count > 0)
                            BantchSaveEntityNoCommit(listCheckType);
                        if (listSummary != null && listSummary.Count > 0)
                            BantchSaveEntityNoCommit(listSummary);
                    });

                    int Count = ((listIDSafeCheck != null && listIDSafeCheck.Any()) ? listIDSafeCheck.Count : 0) + ((listIDCheckType != null && listIDCheckType.Any()) ? listIDCheckType.Count : 0) + ((listIDSummary != null && listIDSummary.Any()) ? listIDSummary.Count : 0) + ((listSafeCheck != null && listSafeCheck.Any()) ? listSafeCheck.Count : 0) + ((listCheckType != null && listCheckType.Any()) ? listCheckType.Count : 0) + ((listSummary != null && listSummary.Any()) ? listSummary.Count : 0);
                    SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);

                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrEmpty(ex.StackTrace))
                        throw new Exception("错误日志：[StackTrace]" + ex.StackTrace);
                    else
                        throw new Exception("【" + HttpContext.Request.Path + "】错误日志：[Message]" + ex.Message);
                }

                return true;
            });
        }



        /// <summary>
        /// 年度安全检查情况综合统计分析
        /// BI051
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("AddSafeInfoYear")]
        public JsonActionResult<bool> AddSafeInfoYear([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                try
                {
                    DateTime dtNow = DateTime.Now;
                    int caculateMonth = 3;//计算月份数 动态配置
                    GetParmInfo(filter.Parameter1, out dtNow, out caculateMonth);

                    #region    1 年度 计划检查数 按时已办数 超时已办数 未完成数 完成率 及时完成率

                    List<T_BI_SUMMARY> listSummery = new List<T_BI_SUMMARY>();//计算值

                    IEnumerable<T_BI_SUMMARY> listSummary = GetEntities<T_BI_SUMMARY>(e => e.CODE == "BI051_BSCOMPANYYEAR", null, null);

                    T_BI_SUMMARY summery = null;
                    if (listSummary != null && listSummary.Any())
                    {
                        summery = listSummary.FirstOrDefault(e => e.PART == 1);
                    }
                    if (summery == null)
                    {
                        summery = new T_BI_SUMMARY();
                        summery.ID = Guid.NewGuid();
                        summery.CODE = "BI051_BSCOMPANYYEAR";
                        summery.PART = 1;
                        summery.NUM = 1;
                        summery.ORG_ID = filter.OrgId;

                        summery.Parameter1 = "计划检查数";
                        summery.Parameter2 = "按时已办";
                        summery.Parameter3 = "超时已办";
                        summery.Parameter4 = "未完成数";
                        summery.Parameter5 = "完成率";
                        summery.Parameter6 = "及时完成率";
                    }

                    DateTime dtYearBegin = Convert.ToDateTime(dtNow.Year + "-01-01 00:00:00");

                    summery.Count1 = GetCount<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && !e.IS_DELETED && e.ENABLE_STATUS == 0, null);//计划检查数


                    summery.Count2 = GetCount<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && e.ISFINISHINTTIME && !e.IS_DELETED && e.ENABLE_STATUS == 0, null);//按时已办

                    summery.Count3 = GetCount<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && !e.ISFINISHINTTIME && e.STATECHECK == STATECHECK.Archived && !e.IS_DELETED && e.ENABLE_STATUS == 0, null);//完成 未及时完成

                    summery.Count4 = GetCount<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && e.STATECHECK != STATECHECK.Archived && !e.IS_DELETED && e.ENABLE_STATUS == 0, null);//未完成数
                    if (summery.Count1 < 1)
                    {
                        summery.Parameter7 = "100%";//完成率
                        summery.Parameter8 = "100%";//及时完成率
                    }
                    else
                    {
                        summery.Parameter7 = Math.Round(100.0 * (summery.Count2 + summery.Count3) / summery.Count1, 2) + "%";//完成率
                        summery.Parameter8 = Math.Round(100.0 * summery.Count2 / summery.Count1, 2) + "%";//及时完成率
                    }
                    #endregion

                    #region   2 年度每个层级对应的安全检查记录

                    var listLevel = GetEntities<T_FM_ENUMS>(e => e.CODE == "HMLevleEnum", null, null).OrderBy(e => e.NUM).ToList();

                    List<T_BS_CHECK_TYPE_LEVEL> listCheckTypeLevel = null;
                    var iListCheckTypeLevel = GetEntities<T_BS_CHECK_TYPE_LEVEL>(e => !e.IS_DELETED, null, null);
                    if (iListCheckTypeLevel != null && iListCheckTypeLevel.Any())
                    {
                        listCheckTypeLevel = iListCheckTypeLevel.ToList();
                    }

                    #region  查表 建表 按 公司 部门 车间 班组 的顺序  ROW_NO 0 => 3

                    List<T_BI_BSSAFECHECK_YEAR> listLastYearInfo = null;
                    List<T_BI_BSSAFECHECK_YEAR> listYearInfo = null;
                    var iListYearInfo = GetEntities<T_BI_BSSAFECHECK_YEAR>(e => e.YEAR == dtNow.Year, null, null);
                    if (iListYearInfo == null || !iListYearInfo.Any())
                    {
                        listYearInfo = new List<T_BI_BSSAFECHECK_YEAR>();
                        for (int i = 0; i < listLevel.Count; i++)
                        {
                            listYearInfo.Add(new T_BI_BSSAFECHECK_YEAR()
                            {
                                ID = Guid.NewGuid(),
                                MineType = BSMineTypeEnum.All,
                                YEAR = dtNow.Year,
                                //LEVEL = (FMDepartmentType)i,
                                LEVEL = (FMDepartmentType)listLevel[i].VALUE,
                                NAME = listLevel[i].NAME,
                                ROW_NO = i,
                                COUNTJANUARY = null,
                                COUNTFEBRUARY = null,
                                COUNTMARCH = null,
                                COUNTAPRIL = null,
                                COUNTMAY = null,
                                COUNTJUNE = null,
                                COUNTJULY = null,
                                COUNTAUGUST = null,
                                COUNTSEPTEMBER = null,
                                COUNTOCTOBER = null,
                                COUNTNOVEMBER = null,
                                COUNTDECEMBER = null,
                                COUNTTOTAL = null,
                                IS_DELETED = false,
                                ORG_ID = filter.OrgId,
                                //ENTITY_ORG_TPYE = ,
                                //FORM_ID = ,
                                //FLOW_STATUS = ,
                                //FLOW_SEND_STATUS = ,
                                //FLOW_ID = ,
                                CREATE_TIME = dtNow,
                                MODIFY_TIME = dtNow,
                                //CREATER_ID = ,
                                //MODIFIER_ID = ,
                            });
                        }
                    }
                    else
                    {
                        listYearInfo = iListYearInfo.OrderBy(e => e.ROW_NO).ToList();
                    }


                    #endregion

                    //如果是 1号 跑上个月和本月的数据
                    //否则  跑本月的数据
                    //枚举 的 value 和  组织枚举的值匹配

                    List<T_BS_SAFE_CHECK> listSafeCheck = null;
                    DateTime dtNow01 = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                    if (dtNow.Date.Day == 1)
                    {
                        DateTime dtNow_01 = dtNow01.AddMonths(-1);
                        if (dtNow_01.Year != dtNow01.Year)
                        {
                            iListYearInfo = GetEntities<T_BI_BSSAFECHECK_YEAR>(e => e.YEAR == dtNow_01.Year, null, null);
                            if (iListYearInfo != null)
                            {
                                listLastYearInfo = iListYearInfo.ToList();
                            }
                        }
                        var iList = GetEntities<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtNow_01 && e.CREATE_TIME < dtNow01, null, null);
                        if (iList != null && iList.Any())
                        {
                            listSafeCheck = iList.ToList();
                        }
                        //赋值

                        if (listLastYearInfo == null)
                        {
                            SetVal(listSafeCheck, listLevel, listCheckTypeLevel, dtNow_01.Month, ref listYearInfo);
                        }
                        else if (dtNow_01.Year == dtNow01.Year)
                        {
                            SetVal(listSafeCheck, listLevel, listCheckTypeLevel, dtNow_01.Month, ref listLastYearInfo);
                        }
                    }
                    DateTime dtNow31 = dtNow01.AddMonths(1);
                    var iListMonth = GetEntities<T_BS_SAFE_CHECK>(e => e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtNow01 && e.CREATE_TIME < dtNow31, null, null);
                    if (iListMonth != null && iListMonth.Any())
                    {
                        listSafeCheck = iListMonth.ToList();
                    }

                    SetVal(listSafeCheck, listLevel, listCheckTypeLevel, dtNow01.Month, ref listYearInfo);

                    #endregion


                    #region   3  安全检查隐患整改统计图  更新当前时间 对应前 3月的数据


                    List<T_BI_BSCHECKTYPE_YEAR> listModel = new List<T_BI_BSCHECKTYPE_YEAR>();
                    IEnumerable<T_BI_BSCHECKTYPE_YEAR> listModelDel = null;//需要删除的旧数据
                    IEnumerable<T_BI_BSCHECKTYPE_YEAR> listModelDelLast = null;//需要删除的旧数据


                    T_BI_BSCHECKTYPE_YEAR modelYM = null;
                    for (int i = 0; i < caculateMonth; i++)
                    {
                        DateTime dt = dtNow;
                        if (i > 0)
                        {
                            dt = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                            dt = dt.AddMonths(-1 * (i - 1)).AddSeconds(-1);
                        }
                        modelYM = new T_BI_BSCHECKTYPE_YEAR();
                        modelYM.ORG_ID = filter.OrgId.Value;
                        SetVal(dt, filter.OrgId.Value, ref modelYM);
                        listModel.Add(modelYM);
                    }

                    //listModel 判断年份 计算数据 不够的去数据库查找  

                    T_BI_SUMMARY summery3 = null;
                    if (listSummary != null && listSummary.Any())
                        summery3 = listSummary.FirstOrDefault(e => e.PART == 3);

                    if (summery3 == null)
                    {
                        summery3 = new T_BI_SUMMARY();
                        summery3.ID = Guid.NewGuid();
                        summery3.CODE = "BI051_BSCOMPANYYEAR";
                        summery3.PART = 3;
                        summery3.NUM = 1;
                        summery3.ORG_ID = filter.OrgId;


                        summery3.Parameter1 = "隐患数";
                        summery3.Parameter2 = "超整改数";
                        summery3.Parameter3 = "按期整改数";
                        summery3.Parameter4 = "整改率";
                    }
                    else
                    {
                        summery3.Count1 = 0;
                        summery3.Count2 = 0;
                        summery3.Count3 = 0;
                    }

                    foreach (var item in listModel)
                    {
                        if (item.YEAR == dtNow.Year)
                        {
                            summery3.Count1 += (item.INTTIMECOUNT + item.OUTTIMECOUNT + item.NOTFINISHCOUN);
                            summery3.Count2 += item.OUTTIMECOUNT;
                            summery3.Count3 += item.INTTIMECOUNT;
                        }
                    }


                    if (dtNow.Month >= caculateMonth)
                    {
                        //如果月份小于 往后计算月份 再从数据库中 拉取
                        var listCaculate = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.YEAR == dtNow.Year && e.MONTH < (dtNow.Month - caculateMonth), null, null);
                        if (listCaculate != null && listCaculate.Any())
                        {
                            foreach (var item in listCaculate)
                            {
                                summery3.Count1 += (item.INTTIMECOUNT + item.OUTTIMECOUNT + item.NOTFINISHCOUN);
                                summery3.Count2 += item.OUTTIMECOUNT;
                                summery3.Count3 += item.INTTIMECOUNT;
                            }
                        }

                        listModelDel = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.MineType == BSMineTypeEnum.All && e.YEAR == dtNow.Year && e.MONTH < dtNow.Month && e.MONTH >= (dtNow.Month - caculateMonth), null);//需要删除的数据
                    }
                    else
                    {
                        listModelDelLast = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.MineType == BSMineTypeEnum.All && e.YEAR == dtNow.Year - 1 && e.MONTH > (12 - (caculateMonth - dtNow.Month)), null);//需要删除的数据
                    }
                    if (summery3.Count1 < 1)
                    {
                        summery3.Parameter7 = "100%";
                    }
                    else
                    {
                        summery3.Parameter7 = Math.Round(100.0 * (summery3.Count2 + summery3.Count3) / summery3.Count1, 2).ToString() + "%";
                    }

                    listSummery.Add(summery);
                    listSummery.Add(summery3);
                    #endregion


                    #region   4 安全检查隐患整改率

                    #endregion

                    T_FM_SYNC_TASK task = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                    if (task != null)
                    {
                        task.CURR_TASK_START_TIME = dtNow;//上次同步结束时间
                        task.UPDATE_SUCCES_TIME = DateTime.Now;//上次同步结束时间
                        task.SYNC_PARAM = "";//清空参数
                    }

                    this.UnifiedCommit(() =>
                    {
                        if (task != null)
                            UpdateEntityNoCommit(task);//跑批信息
                        if (listYearInfo != null && listYearInfo.Count > 0)
                            BantchSaveEntityNoCommit(listYearInfo);
                        if (listLastYearInfo != null && listLastYearInfo.Count > 0)
                            BantchSaveEntityNoCommit(listLastYearInfo);


                        if (listModel != null && listModel.Any())//添加新的数据
                            BantchSaveEntityNoCommit(listModel);
                        if (listModelDel != null && listModelDel.Any()) //删除旧数据
                            BantchSaveEntityNoCommit(listModelDel);
                        if (listModelDelLast != null && listModelDelLast.Any()) //删除旧数据
                            BantchSaveEntityNoCommit(listModelDelLast);
                        if (listSummery != null && listSummery.Any())//计算值合并入库
                            BantchSaveEntityNoCommit(listSummery);

                    });

                    int Count = ((listYearInfo != null && listYearInfo.Any()) ? listYearInfo.Count : 0) + ((listLastYearInfo != null && listLastYearInfo.Any()) ? listLastYearInfo.Count : 0) + ((listModel != null && listModel.Any()) ? listModel.Count : 0) + ((listModelDel != null && listModelDel.Any()) ? listModelDel.Count() : 0) + ((listModelDelLast != null && listModelDelLast.Any()) ? listModelDelLast.Count() : 0) + ((listSummery != null && listSummery.Any()) ? listSummery.Count() : 0);
                    SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);

                    //this.UnifiedCommit(() =>
                    //{
                    //    if (task != null)
                    //        UpdateEntityNoCommit(task);//跑批信息
                    //    if (modelLog != null)//日志
                    //        UpdateEntityNoCommit(modelLog);
                    //    //删除旧数据
                    //    if (listIDSafeCheck.Count > 0)
                    //        BantchDeleteEntity<T_BI_BSSAFECHECK>(listIDSafeCheck);
                    //    if (listIDCheckType.Count > 0)
                    //        BantchDeleteEntity<T_BI_BSCHECKTYPE>(listIDCheckType);
                    //    if (listIDSummary.Count > 0)
                    //        BantchDeleteEntity<T_BI_SUMMARY>(listIDSummary);
                    //    //插入新数据
                    //    if (listSafeCheck != null && listSafeCheck.Count > 0)
                    //        BantchSaveEntityNoCommit(listSafeCheck);
                    //    if (listCheckType != null && listCheckType.Count > 0)
                    //        BantchSaveEntityNoCommit(listCheckType);
                    //    if (listSummary != null && listSummary.Count > 0)
                    //        BantchSaveEntityNoCommit(listSummary);
                    //});
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrEmpty(ex.StackTrace))
                        throw new Exception("错误日志：[StackTrace]" + ex.StackTrace);
                    else
                        throw new Exception("【" + HttpContext.Request.Path + "】错误日志：[Message]" + ex.Message);
                }
                return true;
            });
        }

        /// <summary>
        /// 给 listModel 赋月份值更新合计 
        /// </summary>
        /// <param name="listCheckMonth"></param>
        /// <param name="listELevel"></param>
        /// <param name="listCheckTypeLevel"></param>
        /// <param name="month"></param>
        /// <param name="listYearInfo"></param>
        private void SetVal(List<T_BS_SAFE_CHECK> listCheckMonth, List<T_FM_ENUMS> listELevel, List<T_BS_CHECK_TYPE_LEVEL> listCheckTypeLevel, int month, ref List<T_BI_BSSAFECHECK_YEAR> listYearInfo)
        {
            if (listCheckMonth == null || listCheckMonth.Count < 1 || listYearInfo == null || listYearInfo.Count < 1 || listELevel == null || listELevel.Count < 1 && listCheckTypeLevel == null || listCheckTypeLevel.Count < 1 || month < 1 || month > 12)
            {
                return;
            }

            foreach (var item in listYearInfo)
            {
                int count = 0;
                T_FM_ENUMS enumsL = listELevel.FirstOrDefault(e => e.VALUE == (int)item.LEVEL);
                if (enumsL != null)
                {
                    var listTypeLevel = listCheckTypeLevel.Where(e => e.CheckLevel_ENUMS_ID == enumsL.ID);
                    if (listTypeLevel != null && listTypeLevel.Any())
                    {
                        List<Guid> listTypeLevelID = listTypeLevel.Select(e => e.ID).ToList();
                        var listLevel = listCheckMonth.FindAll(e => e.CHECK_TYPE_LEVEL_ID.HasValue && listTypeLevelID.Contains(e.CHECK_TYPE_LEVEL_ID.Value));
                        if (listLevel != null && listLevel.Any())
                        {
                            count = listLevel.Count;
                        }
                    }
                }

                switch (month)
                {
                    case 1:
                        item.COUNTJANUARY = count;
                        break;
                    case 2:
                        item.COUNTFEBRUARY = count;
                        break;
                    case 3:
                        item.COUNTMARCH = count;
                        break;
                    case 4:
                        item.COUNTAPRIL = count;
                        break;
                    case 5:
                        item.COUNTMAY = count;
                        break;
                    case 6:
                        item.COUNTJUNE = count;
                        break;
                    case 7:
                        item.COUNTJULY = count;
                        break;
                    case 8:
                        item.COUNTAUGUST = count;
                        break;
                    case 9:
                        item.COUNTSEPTEMBER = count;
                        break;
                    case 10:
                        item.COUNTOCTOBER = count;
                        break;
                    case 11:
                        item.COUNTNOVEMBER = count;
                        break;
                    case 12:
                        item.COUNTDECEMBER = count;
                        break;
                    default:
                        break;
                }
                item.COUNTTOTAL = (item.COUNTJANUARY.HasValue ? item.COUNTJANUARY.Value : 0) + (item.COUNTFEBRUARY.HasValue ? item.COUNTFEBRUARY.Value : 0) + (item.COUNTMARCH.HasValue ? item.COUNTMARCH.Value : 0) + (item.COUNTAPRIL.HasValue ? item.COUNTAPRIL.Value : 0) + (item.COUNTMAY.HasValue ? item.COUNTMAY.Value : 0) + (item.COUNTJUNE.HasValue ? item.COUNTJUNE.Value : 0) + (item.COUNTJULY.HasValue ? item.COUNTJULY.Value : 0) + (item.COUNTAUGUST.HasValue ? item.COUNTAUGUST.Value : 0) + (item.COUNTSEPTEMBER.HasValue ? item.COUNTSEPTEMBER.Value : 0) + (item.COUNTOCTOBER.HasValue ? item.COUNTOCTOBER.Value : 0) + (item.COUNTNOVEMBER.HasValue ? item.COUNTNOVEMBER.Value : 0) + (item.COUNTDECEMBER.HasValue ? item.COUNTDECEMBER.Value : 0);

            }

        }

        /// <summary>
        /// 安全检查隐患整改统计图（近12个月）
        /// </summary>
        /// <param name="dtMax"></param>
        /// <param name="ORG_ID"></param>
        /// <param name="typeYearMonth"></param>
        /// <param name="MineType"></param>
        private void SetVal(DateTime dtMax, Guid ORG_ID, ref T_BI_BSCHECKTYPE_YEAR typeYearMonth, BSMineTypeEnum MineType = BSMineTypeEnum.All)
        {
            typeYearMonth = GetEntity<T_BI_BSCHECKTYPE_YEAR>(e => e.MineType == MineType && e.YEAR == dtMax.Year && e.MONTH == dtMax.Month);
            if (typeYearMonth == null)
            {
                //最好有 code 但是之前没有就没有了 0406 修改
                typeYearMonth = new T_BI_BSCHECKTYPE_YEAR();
                typeYearMonth.YEAR = dtMax.Year;
                typeYearMonth.MONTH = dtMax.Month;
                typeYearMonth.MineType = MineType;
                typeYearMonth.ORG_ID = ORG_ID;
            }

            DateTime dtMin = Convert.ToDateTime(dtMax.ToString("yyyy-MM-01 00:00:00"));
            Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression = e => !e.IS_DELETED && e.ISFINISHINTTIME.HasValue && e.ISFINISHINTTIME.Value && e.CREATE_TIME.HasValue && dtMin < e.CREATE_TIME.Value && e.CREATE_TIME.Value < dtMax;//按期整改
            Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression2 = e => !e.IS_DELETED && e.OKDATE.HasValue && e.LastDateUser.HasValue && e.OKDATE.Value > e.LastDateUser.Value && e.CREATE_TIME.HasValue && dtMin < e.CREATE_TIME.Value && e.CREATE_TIME.Value < dtMax;//超期整改
            Expression<Func<T_BS_RISK_SUBMIT_CONTENT, bool>> expression3 = e => !e.IS_DELETED && !e.OKDATE.HasValue && e.CREATE_TIME.HasValue && dtMin < e.CREATE_TIME.Value && e.CREATE_TIME.Value < dtMax;//未完成

            BaseFilter filter = new BaseFilter(ORG_ID);
            filter.IgnoreDataRule = true;
            if (MineType != BSMineTypeEnum.All)
            {
                int iMineType = (int)MineType;
                expression = expression.And(e => e.SAFE_CHECK_ID.HasValue && e.Nav_SafeCheck != null && e.Nav_SafeCheck.CHECKOBJECT.HasValue && e.Nav_SafeCheck.CHECKOBJECT.Value == iMineType);
                expression2 = expression2.And(e => e.SAFE_CHECK_ID.HasValue && e.Nav_SafeCheck != null && e.Nav_SafeCheck.CHECKOBJECT.HasValue && e.Nav_SafeCheck.CHECKOBJECT.Value == iMineType);
                expression3 = expression3.And(e => e.SAFE_CHECK_ID.HasValue && e.Nav_SafeCheck != null && e.Nav_SafeCheck.CHECKOBJECT.HasValue && e.Nav_SafeCheck.CHECKOBJECT.Value == iMineType);

                filter.SelectField = new List<string> { "ID", "SAFE_CHECK_ID", "Nav_SafeCheck.ID" };

                try
                {
                    //GetCount  filter.DataRule 加属性也不起作用 ！！！！！！！
                    typeYearMonth.INTTIMECOUNT = GetEntities(expression, filter, "Nav_SafeCheck").Count();//按期整改
                    typeYearMonth.OUTTIMECOUNT = GetEntities(expression2, filter, "Nav_SafeCheck").Count();//超期整改
                    typeYearMonth.NOTFINISHCOUN = GetEntities(expression3, filter, "Nav_SafeCheck").Count();//未完成
                }
                catch (Exception ex)
                {
                    throw new Exception(("MsgDIY Message: " + ex.Message) + (!string.IsNullOrEmpty(ex.StackTrace) ? ("  StackTrace: " + ex.StackTrace) : ""));
                }
            }
            else
            {
                try
                {
                    typeYearMonth.INTTIMECOUNT = GetCount<T_BS_RISK_SUBMIT_CONTENT>(expression, filter);//按期整改
                    typeYearMonth.OUTTIMECOUNT = GetCount<T_BS_RISK_SUBMIT_CONTENT>(expression2, filter);//超期整改
                    typeYearMonth.NOTFINISHCOUN = GetCount<T_BS_RISK_SUBMIT_CONTENT>(expression3, filter);//未完成
                }
                catch (Exception ex)
                {
                    throw new Exception(("MsgDIY Message: " + ex.Message) + (!string.IsNullOrEmpty(ex.StackTrace) ? ("  StackTrace: " + ex.StackTrace) : ""));
                }
            }
            typeYearMonth.ALLCOUNT = typeYearMonth.INTTIMECOUNT + typeYearMonth.OUTTIMECOUNT + typeYearMonth.NOTFINISHCOUN;

            if (typeYearMonth.ALLCOUNT == 0)
            {
                typeYearMonth.FINISHPERCENT = 100;
            }
            else if (typeYearMonth.INTTIMECOUNT + typeYearMonth.OUTTIMECOUNT == 0)
            {
                typeYearMonth.FINISHPERCENT = 0;
            }
            else
            {
                typeYearMonth.FINISHPERCENT = Math.Round(Convert.ToDecimal(100.0 * (typeYearMonth.INTTIMECOUNT + typeYearMonth.OUTTIMECOUNT) / (typeYearMonth.INTTIMECOUNT + typeYearMonth.OUTTIMECOUNT + typeYearMonth.NOTFINISHCOUN)), 2);
            }
        }


        #region     部门级年度安全检查情况综合统计分析

        /// <summary>
        /// 部门级年度安全检查情况综合统计分析
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("AddDepartmentSafeInfoYear")]
        public JsonActionResult<bool> AddDepartmentSafeInfoYear([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                try
                {
                    DateTime dtNow = DateTime.Now;
                    int caculateMonth = 3;//计算月份数 动态配置

                    GetParmInfo(filter.Parameter1, out dtNow, out caculateMonth);

                    var listCheckObjectT = GetEntities<T_BS_SAFE_CHECK>(e => e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value > 0 && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Year == dtNow.Year, null, null).Select(e => e.CHECKOBJECT.Value).GroupBy(e => e);//排除全公司

                    if (listCheckObjectT == null || !listCheckObjectT.Any())
                    {
                        return true;
                    }

                    List<int> listCheckObject = new List<int>();//矿山 选矿 尾矿 
                    listCheckObjectT.ForEach(e => listCheckObject.Add(e.Key));

                    List<T_BI_SUMMARY> listSummery = null;
                    IEnumerable<T_BI_SUMMARY> IlistSummary = null;
                    T_BI_SUMMARY summery = null;
                    DateTime dtYearBegin = Convert.ToDateTime(dtNow.Year + "-01-01 00:00:00");
                    List<Guid> listCheckTypeId = null;
                    List<Guid> listCheckTypeIdOrder = null;
                    //检查区域 检查类型 的安全检查
                    IEnumerable<T_BS_SAFE_CHECK> IlistCheckTypeOCT = null;
                    T_BI_BSSAFECHECKTYPE_YEAR modelCheckTypeResult = null;
                    List<T_BI_BSSAFECHECKTYPE_YEAR> listCheckTypeResult = new List<T_BI_BSSAFECHECKTYPE_YEAR>();


                    T_FM_SYNC_TASK task = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                    if (task != null)
                    {
                        task.CURR_TASK_START_TIME = dtNow;//上次同步结束时间
                        task.UPDATE_SUCCES_TIME = DateTime.Now;//上次同步结束时间
                        task.SYNC_PARAM = "";//清空参数
                    }
                    List<T_BS_CHECK_TYPE> ListCheckType = GetEntities<T_BS_CHECK_TYPE>(e => true
                        , null, null).OrderBy(e => e.NUM).ToList();
                    int ROW_NO = 0;
                    foreach (var item in listCheckObject)
                    {
                        //每种类型分开计算

                        listSummery = new List<T_BI_SUMMARY>();
                        #region    1 年度 计划检查数 按时已办数 超时已办数 未完成数 完成率 及时完成率

                        IlistSummary = GetEntities<T_BI_SUMMARY>(e => (int)e.MineType == item && e.CODE == "BI052_BSSafeCheckYear", null, null);

                        if (IlistSummary != null && IlistSummary.Any())
                        {
                            summery = IlistSummary.FirstOrDefault(e => e.PART == 1 && e.MineType == (BSMineTypeEditEnum)item);
                        }
                        if (summery == null)
                        {
                            summery = new T_BI_SUMMARY();
                            summery.ID = Guid.NewGuid();
                            summery.CODE = "BI052_BSSafeCheckYear";
                            summery.PART = 1;
                            summery.NUM = 1;
                            summery.ORG_ID = filter.OrgId;
                            summery.MineType = (BSMineTypeEditEnum)item;

                            summery.Parameter1 = "计划检查数";
                            summery.Parameter2 = "按时已办";
                            summery.Parameter3 = "超时已办";
                            summery.Parameter4 = "未完成数";
                            summery.Parameter5 = "完成率";
                            summery.Parameter6 = "及时完成率";
                            summery.Parameter9 = summery.MineType.GetDescription();// "矿山部";
                        }

                        summery.Count1 = GetCount<T_BS_SAFE_CHECK>(e => !e.IS_DELETED && e.ENABLE_STATUS == 0 && e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value == item && e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin, null);//计划检查数

                        summery.Count2 = GetCount<T_BS_SAFE_CHECK>(e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value == item && e.ENABLE_STATUS == 0 && e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && e.ISFINISHINTTIME, null);//按时已办

                        summery.Count3 = GetCount<T_BS_SAFE_CHECK>(e => !e.IS_DELETED && e.ENABLE_STATUS == 0 && e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value == item && e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && !e.ISFINISHINTTIME && e.STATECHECK == STATECHECK.Archived, null);//完成 未及时完成

                        summery.Count4 = GetCount<T_BS_SAFE_CHECK>(e => !e.IS_DELETED && e.ENABLE_STATUS == 0 && e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value == item && e.CREATE_TIME.HasValue && e.CREATE_TIME >= dtYearBegin && e.STATECHECK != STATECHECK.Archived, null);//未完成数

                        if (summery.Count1 < 1)
                        {
                            summery.Parameter7 = "100%";
                            summery.Parameter8 = "100%";
                        }
                        else
                        {
                            summery.Parameter7 = Math.Round(100.0 * (summery.Count2 + summery.Count3) / summery.Count1, 2) + "%";//完成率
                            summery.Parameter8 = Math.Round(100.0 * summery.Count2 / summery.Count1, 2) + "%";//及时完成率
                        }


                        #endregion

                        #region   2 年度每个层级对应的安全检查记录

                        var listCheckTypeYear = GetEntities<T_BI_BSSAFECHECKTYPE_YEAR>(e => e.YEAR == dtNow.Year && (int)e.MineType == item, null, null);

                        var listCheckTypeYearT = GetEntities<T_BS_SAFE_CHECK>(e => !e.IS_DELETED && e.CHECKOBJECT.HasValue && e.CHECKOBJECT.Value == item && e.CHECK_TYPE_ID.HasValue && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Year == dtNow.Year, null, null);//检查区域全公司

                        if (listCheckTypeYearT == null || !listCheckTypeYearT.Any())
                        {
                            continue;//今年这个检查区域没安全检查
                        }

                        var listCheckTypeT = listCheckTypeYearT.Select(e => e.CHECK_TYPE_ID.Value).GroupBy(e => e);
                        listCheckTypeId = new List<Guid>();
                        listCheckTypeIdOrder = new List<Guid>();
                        listCheckTypeT.ForEach(e => listCheckTypeId.Add(e.Key));

                        for (int i = 0; i < ListCheckType.Count; i++)
                        {
                            if (listCheckTypeId.Contains(ListCheckType[i].ID))
                                listCheckTypeIdOrder.Add(ListCheckType[i].ID);
                        }

                        //安全检查隐患整改率
                        List<T_BI_BSCHECKTYPE_YEAR> listModel = null;
                        IEnumerable<T_BI_BSCHECKTYPE_YEAR> listModelDel = null;//需要删除的旧数据
                        IEnumerable<T_BI_BSCHECKTYPE_YEAR> listModelDelLast = null;//需要删除的旧数据
                        ROW_NO = 0;
                        foreach (var itemT in listCheckTypeIdOrder)
                        {
                            IlistCheckTypeOCT = listCheckTypeYearT.Where(e => e.CHECKOBJECT.Value == item && e.CHECK_TYPE_ID.Value == itemT);//检查区域 检查类型 
                            #region   数据查找或者初始化

                            modelCheckTypeResult = listCheckTypeYear.FirstOrDefault(e => e.CHECK_TYPE_ID == itemT);

                            if (modelCheckTypeResult == null)
                            {
                                modelCheckTypeResult = new T_BI_BSSAFECHECKTYPE_YEAR();
                                modelCheckTypeResult.ID = Guid.NewGuid();
                                modelCheckTypeResult.ORG_ID = filter.OrgId;
                                modelCheckTypeResult.YEAR = dtNow.Year;
                                modelCheckTypeResult.CHECK_TYPE_ID = itemT;
                                modelCheckTypeResult.MineType = (BSMineTypeEnum)item;
                                modelCheckTypeResult.CREATE_TIME = DateTime.Now;
                                modelCheckTypeResult.NAME = ListCheckType.FirstOrDefault(e => e.ID == itemT)?.NAME;
                                modelCheckTypeResult.ROW_NO = ROW_NO;
                                ROW_NO++;
                            }
                            #endregion

                            #region     赋值 月份没到 不赋值

                            modelCheckTypeResult.COUNTJANUARY = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 1).Count();
                            if (dtNow.Month >= 2)
                                modelCheckTypeResult.COUNTFEBRUARY = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 2).Count();
                            if (dtNow.Month >= 3)
                                modelCheckTypeResult.COUNTMARCH = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 3).Count();
                            if (dtNow.Month >= 4)
                                modelCheckTypeResult.COUNTAPRIL = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 4).Count();
                            if (dtNow.Month >= 5)
                                modelCheckTypeResult.COUNTMAY = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 5).Count();
                            if (dtNow.Month >= 6)
                                modelCheckTypeResult.COUNTJUNE = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 6).Count();
                            if (dtNow.Month >= 7)
                                modelCheckTypeResult.COUNTJULY = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 7).Count();
                            if (dtNow.Month >= 8)
                                modelCheckTypeResult.COUNTAUGUST = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 8).Count();
                            if (dtNow.Month >= 9)
                                modelCheckTypeResult.COUNTSEPTEMBER = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 9).Count();
                            if (dtNow.Month >= 10)
                                modelCheckTypeResult.COUNTOCTOBER = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 10).Count();
                            if (dtNow.Month >= 11)
                                modelCheckTypeResult.COUNTNOVEMBER = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 11).Count();
                            if (dtNow.Month == 12)
                                modelCheckTypeResult.COUNTDECEMBER = IlistCheckTypeOCT.Where(e => e.CREATE_TIME.Value.Month == 12).Count();

                            modelCheckTypeResult.COUNTTOTAL = IlistCheckTypeOCT.Count();

                            listCheckTypeResult.Add(modelCheckTypeResult);//检查类型每月检查数量 入库 
                            #endregion
                        }

                        #endregion

                        #region   3  安全检查隐患整改统计图  更新当前时间 对应前 3月的数据

                        listModel = new List<T_BI_BSCHECKTYPE_YEAR>();
                        T_BI_BSCHECKTYPE_YEAR modelYM = null;

                        for (int i = 0; i < caculateMonth; i++)
                        {
                            DateTime dt = dtNow;
                            if (i > 0)
                            {
                                dt = Convert.ToDateTime(dtNow.ToString("yyyy-MM-01 00:00:00"));
                                dt = dt.AddMonths(-1 * (i - 1)).AddSeconds(-1);
                            }
                            modelYM = new T_BI_BSCHECKTYPE_YEAR();
                            modelYM.ORG_ID = filter.OrgId.Value;

                            SetVal(dt, filter.OrgId.Value, ref modelYM, (BSMineTypeEnum)item);

                            listModel.Add(modelYM);
                        }

                        //listModel 判断年份 计算数据 不够的去数据库查找  

                        T_BI_SUMMARY summery3 = null;

                        #region    隐患整改查询 统计 

                        if (IlistSummary != null && IlistSummary.Any())
                            summery3 = IlistSummary.FirstOrDefault(e => e.PART == 3 && e.MineType == (BSMineTypeEditEnum)item);

                        //初始化
                        if (summery3 == null)
                        {
                            summery3 = new T_BI_SUMMARY();
                            summery3.ID = Guid.NewGuid();
                            summery3.CODE = "BI052_BSSafeCheckYear";
                            summery3.PART = 3;
                            summery3.NUM = 1;
                            summery3.ORG_ID = filter.OrgId;
                            summery3.MineType = (BSMineTypeEditEnum)item;

                            summery3.Parameter1 = "隐患数";
                            summery3.Parameter2 = "超整改数";
                            summery3.Parameter3 = "按期整改数";
                            summery3.Parameter4 = "整改率";
                        }
                        else
                        {
                            summery3.Count1 = 0;
                            summery3.Count2 = 0;
                            summery3.Count3 = 0;
                        }

                        #region    数据统计

                        foreach (var itemTY in listModel)
                        {
                            if (itemTY.YEAR == dtNow.Year)
                            {
                                summery3.Count1 += (itemTY.INTTIMECOUNT + itemTY.OUTTIMECOUNT + itemTY.NOTFINISHCOUN);
                                summery3.Count2 += itemTY.OUTTIMECOUNT;
                                summery3.Count3 += itemTY.INTTIMECOUNT;
                            }
                        }

                        if (dtNow.Month >= caculateMonth)
                        {
                            //如果月份小于 往后计算月份 再从数据库中 拉取
                            var listCaculate = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.YEAR == dtNow.Year && e.MONTH < (dtNow.Month - caculateMonth), null, null);
                            if (listCaculate != null && listCaculate.Any())
                            {
                                foreach (var itemTT in listCaculate)
                                {
                                    summery3.Count1 += (itemTT.INTTIMECOUNT + itemTT.OUTTIMECOUNT + itemTT.NOTFINISHCOUN);
                                    summery3.Count2 += itemTT.OUTTIMECOUNT;
                                    summery3.Count3 += itemTT.INTTIMECOUNT;
                                }
                            }

                            listModelDel = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.MineType == (BSMineTypeEnum)item && e.YEAR == dtNow.Year && e.MONTH < dtNow.Month && e.MONTH >= (dtNow.Month - caculateMonth), null);//需要删除的数据
                        }
                        else
                        {
                            listModelDelLast = GetEntities<T_BI_BSCHECKTYPE_YEAR>(e => e.MineType == (BSMineTypeEnum)item && e.YEAR == dtNow.Year - 1 && e.MONTH > (12 - (caculateMonth - dtNow.Month)), null);//需要删除的数据
                        }

                        if (summery3.Count1 < 1)
                        {

                            summery3.Parameter7 = "100%";
                        }
                        else
                        {
                            summery3.Parameter7 = Math.Round(100.0 * (summery3.Count2 + summery3.Count3) / summery3.Count1, 2).ToString() + "%";
                        }

                        #endregion

                        #endregion

                        listSummery.Add(summery3);

                        #endregion

                        listSummery.Add(summery);
                        List<Guid> listDelID = null;
                        List<Guid> listDelIDLast = null;
                        if (listModelDel != null && listModelDel.Any())
                        {
                            listDelID = listModelDel.Select(e => e.ID).ToList();
                        }
                        if (listModelDelLast != null && listModelDelLast.Any())
                        {
                            listDelIDLast = listModelDelLast.Select(e => e.ID).ToList();
                        }
                        this.UnifiedCommit(() =>
                        {
                            if (task != null)
                                UpdateEntityNoCommit(task);//跑批信息
                            if (listCheckTypeResult != null && listCheckTypeResult.Any()) //年度 检查类型 每个月有多少数据
                                BantchSaveEntityNoCommit(listCheckTypeResult);
                            if (listSummery != null && listSummery.Any())//1 3 汇总信息
                                BantchSaveEntityNoCommit(listSummery);

                            if (listModel != null && listModel.Any())//添加新的数据
                                BantchSaveEntityNoCommit(listModel);
                            if (listDelID != null && listDelID.Any()) //删除旧数据
                                BantchDeleteEntityNoCommit<T_BI_BSSAFECHECKTYPE_YEAR>(listDelID);
                            if (listDelIDLast != null && listDelIDLast.Any()) //删除旧数据
                                BantchDeleteEntityNoCommit<T_BI_BSSAFECHECKTYPE_YEAR>(listDelIDLast);
                        });
                        summery = null;
                        task = null;

                        int Count = ((listCheckTypeResult != null && listCheckTypeResult.Any()) ? listCheckTypeResult.Count : 0) + ((listSummery != null && listSummery.Any()) ? listSummery.Count : 0) + ((listModel != null && listModel.Any()) ? listModel.Count : 0) + ((listDelID != null && listDelID.Any()) ? listDelID.Count() : 0) + ((listDelIDLast != null && listDelIDLast.Any()) ? listDelIDLast.Count() : 0);
                        SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);

                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.StartsWith("MsgDIY"))
                        throw new Exception(ex.Message);
                    else if (!string.IsNullOrEmpty(ex.StackTrace))
                        throw new Exception("错误日志：[StackTrace]" + ex.StackTrace);
                    else
                        throw new Exception("【" + HttpContext.Request.Path + "】错误日志：[Message]" + ex.Message);
                }
                return true;
            });
        }



        #endregion

        /// <summary>
        /// 获取 传参
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="dtNow"></param>
        /// <param name="caculateMonth"></param>
        private void GetParmInfo(string parm, out DateTime dtNow, out int caculateMonth)
        {
            dtNow = DateTime.Now;
            caculateMonth = 3;
            if (!string.IsNullOrEmpty(parm))
            {
                if (parm.Contains("|"))
                {
                    List<string> listParm = parm.Split("|").ToList();
                    foreach (var item in listParm)
                    {
                        if (item.Contains("caculateMonth"))
                        {
                            try
                            {
                                caculateMonth = int.Parse(item.Replace(" ", "").Split("=")[1]);
                            }
                            catch
                            {
                                caculateMonth = 3;
                            }
                        }
                        else
                        {
                            try
                            {
                                dtNow = Convert.ToDateTime(item);  //接收来自页面的参数
                            }
                            catch { dtNow = DateTime.Now; }
                        }
                    }
                }
                else
                {
                    if (parm.Contains("caculateMonth"))
                    {
                        try
                        {
                            caculateMonth = int.Parse(parm.Replace(" ", "").Split("=")[1]);
                        }
                        catch
                        {
                            caculateMonth = 3;
                        }
                    }
                    else
                    {
                        try
                        {
                            dtNow = Convert.ToDateTime(parm);  //接收来自页面的参数
                        }
                        catch { dtNow = DateTime.Now; }
                    }
                }
            }
        }
    }
}
