using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.PF;
using APT.MS.Domain.Entities.SC;
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;
using System.Text;
using System.Text.RegularExpressions;

namespace APT.PP.WebApi.Controllers.Api.PP
{
    [Route("api/PP/SC")]
    [TypeFilter(typeof(CustomActionFilterAttribute))]
    public partial class SCController : AuthorizeApiController<T_SC_MT_PLAN_SET>
    {

        IFMNotificationTaskService NotificationTaskService { get; set; }
        //IBSSafeCheckService SafeCheckService { get; set; }, IBSSafeCheckService safeCheckService
        IFMUserService UserService { get; set; }
        IPFSysLogService SysLogService { get; set; }
        ISCMTMeetingService SCMTMeetingService { get; set; }
        IFMSyncLogDetailService SyncLogDetailService { get; set; }
        public SCController(IFMNotificationTaskService notificationTaskService, IPFSysLogService sysLogService, IFMUserService userService, ISCMTMeetingService scMTMeetingService, IFMSyncLogDetailService syncLogDetailService)
        {
            NotificationTaskService = notificationTaskService;
            UserService = userService;
            SysLogService = sysLogService;
            SCMTMeetingService = scMTMeetingService;
            SyncLogDetailService = syncLogDetailService;
        }

        /// <summary>
        /// 判断跑批时间是否符合
        /// </summary>
        /// <param name="RUNSETTIME"></param>
        /// <param name="dtNow"></param>
        /// <param name="minAdvance"></param>
        /// <param name="minLaster"></param>
        /// <returns></returns>
        private bool ISTimeOK(DateTime? RUNSETTIME, DateTime dtNow, int minAdvance, int minLaster)
        {
            if (RUNSETTIME == null)
            {
                return false;
            }

            if (RUNSETTIME != null)
            {
                if (minAdvance != 0 || minLaster != 0)
                {
                    //DateTime dtSetTime = Convert.ToDateTime(dtNow.ToString("yyyy") + RUNSETTIME.Value.ToString("-MM-dd HH:mm:ss"));
                    DateTime dtSetTime = Convert.ToDateTime(dtNow.ToString("yyyy-MM-dd") + RUNSETTIME.Value.ToString(" HH:mm:ss"));
                    if (minAdvance > 0 && minLaster > 0)
                    {
                        if (dtNow.AddMinutes(-1 * minAdvance) <= dtSetTime && dtSetTime <= dtNow.AddMinutes(minLaster))
                        {
                            return true;
                        }
                    }
                    else if (minAdvance > 0)
                    {
                        if (dtNow.AddMinutes(-1 * minAdvance) <= dtSetTime && dtSetTime <= dtNow)
                        {
                            //如果设置时间小于当前时间  并且 设置使劲按大于 当前时间大于当前时间往前偏移分钟数
                            return true;
                        }
                    }
                    else
                    {
                        if (dtNow <= dtSetTime && dtSetTime <= dtNow.AddMinutes(minLaster))
                        {
                            //如果 设置时间大于当前时间 并且设置时间小于当前时间往后偏移分钟数
                            return true;
                        }
                    }
                }
                else
                {
                    if (RUNSETTIME.Value.ToString("HH:mm") == dtNow.ToString("HH:mm"))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 根据 会议召开计划
        /// 给触发人 添加会议召开待办
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("CreateMeetingPlanSet")]
        public JsonActionResult<bool> CreatePreShiftMeeting([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                DateTime dtNow = DateTime.Now;
                if (!string.IsNullOrEmpty(filter.Parameter1))
                {
                    try
                    {
                        //接收来自页面的参数
                        DateTime deParm = Convert.ToDateTime(filter.Parameter1);
                        dtNow = deParm;
                    }
                    catch { }
                }

                filter.Parameter1 = " ENABLE_STATUS = false ";
                Expression<Func<T_SC_MT_PLAN_SET, bool>> expression = e => e.IS_DELETED == false && e.ENABLE_STATUS == 0;

                List<T_SC_MT_PLAN_SET> listPlanSet = this.GetOrderEntities<T_SC_MT_PLAN_SET>(expression, filter, new string[] { "Nav_ContentName" }).ToList();

                List<T_SC_MT_PLAN_SET> listPlanSetRund = new List<T_SC_MT_PLAN_SET>();

                if (listPlanSet.Count < 1)
                    return true;//没找到审批信息

                T_FM_USER User = GetEntity<T_FM_USER>(e => e.CODE.StartsWith("admin") && e.ENABLE_STATUS == 0);
                if (User == null)
                {
                    User = GetEntity<T_FM_USER>(e => e.ID == listPlanSet[0].CREATER_ID.Value && e.ENABLE_STATUS == 0);//随便取一个不影响信息
                }
                int min = 5;//按设置时间 可以提前5分钟执行跑批生成代办任务

                #region   获取时间符合的设置
                List<int> listQuarter = new List<int> { 1, 4, 7, 10 };
                //5 单次 每天10,  每周20,  每月30, 每季40,
                for (int i = 0; i < listPlanSet.Count; i++)
                {
                    if (!ISTimeOK(listPlanSet[i].RUNSETTIME, dtNow, min, min))//设置时间往前后各偏移 5 分钟 判断跑批
                    { continue; }

                    switch (listPlanSet[i].PLANCHECKFREQUENCY)
                    {
                        //case BSPLANCHECKFREQUENCYEnum.OneTime:
                        case BSPLANCHECKFREQUENCYEnum.Year:
                            #region    单次检查 时间对   月 日对才加入

                            if (!string.IsNullOrEmpty(listPlanSet[i].RUNDATA))//05-01,10-01
                            {
                                List<string> listMMdd = listPlanSet[i].RUNDATA.Split(",").ToList();
                                if (listPlanSet[i].RUNDATA.Contains("-"))
                                {
                                    for (int j = 0; j < listMMdd.Count; j++)
                                    {
                                        try
                                        {
                                            if (!string.IsNullOrEmpty(listMMdd[j]) && (Convert.ToDateTime(dtNow.Year.ToString() + ("-") + listMMdd[j]) == Convert.ToDateTime(dtNow.ToString("yyyy-MM-dd")) || Convert.ToDateTime(dtNow.Year.ToString() + "-" + listMMdd[j]) == Convert.ToDateTime(dtNow.ToString("yyyy-M-d"))))
                                            {
                                                listPlanSetRund.Add(listPlanSet[i]);
                                                break;
                                            }
                                        }
                                        catch { }
                                    }
                                }
                                else if (listPlanSet[i].RUNDATA.Contains("/"))
                                {
                                    for (int j = 0; j < listMMdd.Count; j++)
                                    {
                                        try
                                        {
                                            if (!string.IsNullOrEmpty(listMMdd[j]) && (Convert.ToDateTime(dtNow.Year.ToString("yyyy/") + listMMdd[j]) == Convert.ToDateTime(dtNow.ToString("yyyy/MM/dd")) || Convert.ToDateTime(dtNow.Year.ToString("yyyy/") + listMMdd[j]) == Convert.ToDateTime(dtNow.ToString("yyyy/M/d"))))
                                            {
                                                listPlanSetRund.Add(listPlanSet[i]);
                                                break;
                                            }
                                        }
                                        catch { }
                                    }
                                }
                            }
                            #endregion
                            break;
                        case BSPLANCHECKFREQUENCYEnum.Date:
                            //每日天执行 时间匹配就添加
                            listPlanSetRund.Add(listPlanSet[i]);
                            break;
                        case BSPLANCHECKFREQUENCYEnum.Week:
                            if (listPlanSet[i].WEEKDATA != null && (int)dtNow.DayOfWeek == (int)listPlanSet[i].WEEKDATA.Value)
                            {
                                //星期匹配   DateTime.Now.DayOfWeek Thursday    (int)DateTime.Now.DayOfWeek 4
                                listPlanSetRund.Add(listPlanSet[i]);
                            }
                            break;
                        case BSPLANCHECKFREQUENCYEnum.Month:
                            if (listPlanSet[i].DATA != null && listPlanSet[i].DATA.Value == dtNow.Day)
                            {
                                //日匹配  （季度是季度第一月的某日 刚好也符合）
                                listPlanSetRund.Add(listPlanSet[i]);
                            }
                            break;
                        case BSPLANCHECKFREQUENCYEnum.Quarter:
                            if (listPlanSet[i].DATA != null && listPlanSet[i].DATA.Value == dtNow.Day && listQuarter.Contains(dtNow.Month))
                            {
                                //日匹配  （季度是季度第一月的某日 刚好也符合）
                                listPlanSetRund.Add(listPlanSet[i]);
                            }
                            break;
                        default:
                            break;
                    }
                }

                #endregion

                if (listPlanSetRund.Count < 1)//未获取到数据 直接返回
                    return true;

                #region    跑批添加数据前 

                DateTime dtFirst = dtNow.Date;
                List<Guid> listPlansetId = listPlanSetRund.Select(e => e.ID).ToList();

                // 系统生成10  用户主动创建20   今天 生成的
                //List<T_SC_MT_MEETING> listMeeting = this.GetOrderEntities<T_SC_MT_MEETING>(e => e.CREACTTYPE == CREACTTYPEEnum.System && e.CREATE_TIME >= dtFirst && e.CREATE_TIME <= dtFirst.AddDays(1) && listPlansetId.Contains(e.ID), filter).ToList();

                DateTime dtCheckMin = dtFirst.AddMinutes(-30);
                List<T_SC_MT_MEETING> listMeeting = this.GetOrderEntities<T_SC_MT_MEETING>(e => e.CREACTTYPE == CREACTTYPEEnum.System && e.CREATE_TIME >= dtCheckMin && e.CREATE_TIME <= dtFirst.AddDays(1) && e.PLANSET_ID.HasValue && listPlansetId.Contains(e.PLANSET_ID.Value), null).ToList();

                Guid PLANSET_ID = Guid.Empty;
                T_SC_MT_MEETING modelCheck = null;

                List<T_SC_MT_MEETING> listSafeCheckAdd = new List<T_SC_MT_MEETING>(); //需要插入数据库的安检记录
                List<T_SC_MT_MEETING_CONTENT> listcont = new List<T_SC_MT_MEETING_CONTENT>();
                List<T_FM_USER> listFMUserNotice = null;//每项设置的通知人员（中间表）
                List<T_FM_DEPARTMENT> listDepartment = null;

                //KeywordFilter filterDepart = new KeywordFilter();
                int DEPARTMENT_TYPE = 0;//部门类型 用于linq相接时存放 （中间值：防错）
                DateTime ChcekDateTime = dtFirst;
                DateTime dtTimeOut = DateTime.Now;

                T_PF_APPROVAL_ROLE modelRoleSafe = GetEntity<T_PF_APPROVAL_ROLE>(e => e.NAME.Contains("安环部"));
                if (modelRoleSafe == null || modelRoleSafe.ID == new Guid())
                {
                    try
                    {
                        //没有找到安全员
                        SysLogService.AddLog(listPlanSetRund[0].ORG_ID.Value, User.ID, PFSysLogTypeEnum.SafeChecker, "SC024", "未找到审批角色【安环部】", null, null);
                        //return true;//没有找到安全员 没用往下继续执行的必要 
                        // 班组给班长发送待办 需要继续执行
                    }
                    catch { }
                }

                #endregion

                #region 按逐条设置 查询信息  添加数据
                var listPlanSetID = listPlanSetRund.Select(e => e.ID);
                var listPlanSetDep = GetEntities<T_SC_MT_PLAN_SET_DEPARTMENT>(e => listPlanSetID.Contains(e.PLAN_SET_ID), null, null);
                IEnumerable<T_SC_MT_PLAN_SET_DEPARTMENT> IListPlanSetDep = null;
                T_SC_MT_PLAN_SET_DEPARTMENT depTemp = null;
                List<T_FM_DEPARTMENT> listDepAdd = new List<T_FM_DEPARTMENT>();
                T_FM_DEPARTMENT depCheck = null;
                bool isUserContent = false;
                T_FM_SYNC_TASK task = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                if (task != null)
                {
                    task.UPDATE_SUCCES_TIME = DateTime.Now;//上次同步结束时间
                    task.SYNC_PARAM = "";//清空参数
                }
                string FormCode = "";
                List<T_FM_NOTIFICATION_TASK> listTask = null;//接收会议召开计划的待办
                for (int i = 0; i < listPlanSetRund.Count; i++)
                {
                    listSafeCheckAdd = new List<T_SC_MT_MEETING>();
                    listcont = new List<T_SC_MT_MEETING_CONTENT>();
                    var noticeTitles = new List<string>();
                    var noticeDataIds = new List<Guid>();
                    var noticeUserIds = new List<Guid>();
                    var noticeUserNames = new List<string>();

                    PLANSET_ID = listPlanSetRund[i].ID;
                    modelCheck = listMeeting.FirstOrDefault(e => e.PLANSET_ID == PLANSET_ID);
                    if (modelCheck != null)
                    {
                        //有添加会议通知 返回 查找下一个
                        continue;
                    }

                    listDepartment = new List<T_FM_DEPARTMENT>();

                    listFMUserNotice = new List<T_FM_USER>();
                    List<DateTime> listEndTime = new List<DateTime>();
                    #region    获取通知触发人员

                    if (listPlanSetRund[i].APPROVE_ROLE_ID.HasValue)
                    {
                        var roleUser = GetEntities<T_FM_USER>(e => e.APPROVE_ROLE_ID == listPlanSetRund[i].APPROVE_ROLE_ID && e.ENABLE_STATUS == 0, null, new string[] { "Nav_Department" });
                        if (roleUser != null && roleUser.Any())
                        {
                            listFMUserNotice = roleUser.ToList();

                            IListPlanSetDep = listPlanSetDep.Where(e => e.PLAN_SET_ID == listPlanSetRund[i].ID);
                            if (IListPlanSetDep != null && IListPlanSetDep.Any())
                            {
                                for (int k = 0; k < listFMUserNotice.Count; k++)
                                {
                                    isUserContent = false;
                                    #region    判断 部门限制 //FMDepartmentType 部门 = 0, 车间 = 1, 班组 = 2, 公司 = 3

                                    switch (listFMUserNotice[k].Nav_Department.DEPARTMENT_TYPE)
                                    {
                                        case 0:
                                            if (listFMUserNotice[k].DEPARTMENT_ID.HasValue)
                                            {
                                                depTemp = IListPlanSetDep.FirstOrDefault(e => e.DEPARTMENT_ID == listFMUserNotice[k].DEPARTMENT_ID.Value);
                                                if (depTemp != null)
                                                    isUserContent = true;
                                            }
                                            break;
                                        case 1:
                                            if (listFMUserNotice[k].Nav_Department.PARENT_ID.HasValue)
                                            {
                                                depTemp = IListPlanSetDep.FirstOrDefault(e => e.DEPARTMENT_ID == listFMUserNotice[k].Nav_Department.PARENT_ID.Value);
                                                if (depTemp != null)
                                                    isUserContent = true;
                                            }
                                            break;
                                        case 2:
                                            if (listFMUserNotice[k].Nav_Department.PARENT_ID.HasValue)
                                            {
                                                depCheck = listDepAdd.FirstOrDefault(e => e.ID == listFMUserNotice[k].Nav_Department.PARENT_ID.Value);
                                                if (depCheck == null)
                                                {
                                                    depCheck = GetEntity<T_FM_DEPARTMENT>(listFMUserNotice[k].Nav_Department.PARENT_ID.Value);
                                                    listDepAdd.Add(depCheck);
                                                }
                                                if (depCheck != null)
                                                {
                                                    depTemp = IListPlanSetDep.FirstOrDefault(e => e.DEPARTMENT_ID == depCheck.PARENT_ID.Value);
                                                    if (depTemp != null)
                                                        isUserContent = true;
                                                }
                                            }
                                            break;
                                        case 3:
                                            isUserContent = true;
                                            break;
                                        default:
                                            break;
                                    }

                                    #endregion
                                    if (isUserContent)
                                        continue;

                                    listFMUserNotice.Remove(listFMUserNotice[k]);
                                    k--;
                                }
                            }

                            for (int k = 0; k < listFMUserNotice.Count; k++)
                            {
                                listDepartment.Add(listFMUserNotice[k].Nav_Department);
                            }
                        }
                    }
                    else
                    {
                        //标准化创建小组组长  看看怎么取值
                    }

                    if (listFMUserNotice.Count < 1)
                    {
                        //没有找到通知人员信息
                        continue;
                    }

                    #endregion
                    FormCode = (listPlanSetRund[i].MEETINGTYPE == SCMEETINGTYPE.BasicSecurity || listPlanSetRund[i].MEETINGTYPE == SCMEETINGTYPE.SafetyBoard) ? "SC028" : "SC032";
                    for (int j = 0; j < listFMUserNotice.Count; j++)
                    {
                        //添加代办 
                        T_SC_MT_MEETING modelRun = new T_SC_MT_MEETING();
                        modelRun.ID = Guid.NewGuid();
                        modelRun.ORG_ID = filter.GetOrgId();
                        modelRun.NAME = listPlanSetRund[i]?.Nav_ContentName?.NAME;
                        modelRun.CODE = "HY" + dtNow.ToString("yyyyMMddHH") + i.ToString() + j.ToString();//mmss 因为审批流 CODE 长度20 减少长度
                        modelRun.CREATER_ID = listFMUserNotice[j].ID;//安全检查的创建人 就是通知人
                        modelRun.MEETINGTYPE = listPlanSetRund[i].MEETINGTYPE;
                        modelRun.PLANSET_ID = listPlanSetRund[i].ID;
                        modelRun.CREACTTYPE = CREACTTYPEEnum.System;//系统生成
                        modelRun.STATUS = PFStandardStatus.Draft;
                        modelRun.USER_ID_ORIGINATOR = listFMUserNotice[j].ID;//制表人
                        modelRun.LEVELVALUE = listDepartment[j].DEPARTMENT_TYPE;
                        if (modelRun.MEETINGTYPE == SCMEETINGTYPE.SafetyBoard)
                        {
                            modelRun.LEVELVALUE = (int)FMDepartmentType.公司;
                        }
                        modelRun.DEPARTMENT_ID = listDepartment[j].ID;//检查部门（公司/部门/车间/班组 ID）
                        modelRun.CREATE_TIME = dtNow;
                        //会议内容
                        if (listPlanSetRund[i].CONTENTNAME_ID.HasValue)
                        {
                            listcont.Add(new T_SC_MT_MEETING_CONTENT()
                            {
                                ID = Guid.NewGuid(),
                                ORG_ID = modelRun.ORG_ID,
                                MEETING_ID = modelRun.ID,
                                CONTENTNAME_ID = listPlanSetRund[i].CONTENTNAME_ID.Value//变成非必填了
                            });
                        }

                        listSafeCheckAdd.Add(modelRun);
                        noticeDataIds.Add(modelRun.ID);
                        noticeUserIds.Add(listFMUserNotice[j].ID);
                        noticeUserNames.Add(listFMUserNotice[j].NAME);

                        //请召开＋“会议内容”＋会议
                        //手动发起
                        //在应急模块中的具体表单中已说明
                        if (modelRun.MEETINGTYPE == SCMEETINGTYPE.BasicSecurity || modelRun.MEETINGTYPE == SCMEETINGTYPE.SafetyBoard)
                        {
                            noticeTitles.Add("请召开" + modelRun.MEETINGTYPE.GetDescription());
                        }
                        else
                        {
                            noticeTitles.Add("请召开" + modelRun.MEETINGTYPE.GetDescription() + "-" + listPlanSetRund[i]?.Nav_ContentName?.NAME + "会议");
                            if (listPlanSetRund[i]?.Nav_ContentName?.NAME == "潜在紧急事件讨论")
                            {
                                modelRun.CREACTTYPE = CREACTTYPEEnum.ToCM018;
                            }
                        }
                    }

                    if (noticeTitles.Count > 0)
                    {
                        listTask = NotificationTaskService.InsertUserNoticeTaskModels(noticeTitles[0], noticeDataIds, filter.OrgId, noticeUserIds, noticeUserNames, DateTime.Now, 0, FormCode, FMTASKTYPE.MT_PLAN_Make, (int)listPlanSetRund[i].PLANCHECKFREQUENCY);
                        this.UnifiedCommit(() =>
                        {
                            if (listSafeCheckAdd != null && listSafeCheckAdd.Any())
                                BantchAddEntityNoCommit(listSafeCheckAdd);
                            if (listcont != null && listcont.Any())
                                BantchAddEntityNoCommit(listcont);
                            if (task != null)
                                UpdateEntityNoCommit(task);
                            if (listTask != null && listTask.Any())
                                this.BantchAddEntityNoCommit(listTask);
                        });

                        int Count = ((listSafeCheckAdd != null && listSafeCheckAdd.Any()) ? listSafeCheckAdd.Count : 0) + ((listcont != null && listcont.Any()) ? listcont.Count : 0) + ((listTask != null && listTask.Any()) ? listTask.Count : 0);
                        SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);
                    }
                }

                #endregion

                return true;
            });
        }

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

                //查找符合条件的会议信息   今天 已经审批过  开始时间小于当前时间 的会议
                var listMeeting = GetEntities<T_SC_MT_MEETING>(e => !e.ISBEGIN && (e.STATUS == PFStandardStatus.Approving || e.STATUS == PFStandardStatus.Archived) && e.BEGIN_TIME.HasValue && e.BEGIN_TIME.Value.Date == dtNow.Date && e.BEGIN_TIME <= dtNow && !e.IS_DELETED && !e.ISCANCEL, null, new string[] { "Nav_ListMeetingContent.Nav_ContentName.Nav_ListContent.Nav_ListContentDeal" });//"Nav_ListPAll.Nav_User" => Nav_ListPAll, "Nav_ListPAll"  "Nav_ListPReView", "Nav_UserRecorder", 
                //IS_DELETED 取消

                if (listMeeting == null || !listMeeting.Any())
                {
                    //没有符合条件的会议
                    return true;
                }
                List<T_FM_NOTIFICATION_TASK> listTask = new List<T_FM_NOTIFICATION_TASK>();
                List<T_SC_MT_MEETING_MINUTES> listMeetMinutes = new List<T_SC_MT_MEETING_MINUTES>();
                List<T_SC_MT_MEETING_PALL> listAllChange = new List<T_SC_MT_MEETING_PALL>();
                List<T_SC_MT_MINUTES_PREVIEW> listMeetMinutesPreview = new List<T_SC_MT_MINUTES_PREVIEW>();
                DateTime dtOperate = DateTime.Now; //实际时间 不随传参改变
                int NoticeNotDeal = (int)FMNoticeStatusEnum.未处理;
                List<T_FM_NOTIFICATION_TASK> listTaskEnd = new List<T_FM_NOTIFICATION_TASK>();


                List<T_SC_MT_MEETING_PJOIN> listJoin = new List<T_SC_MT_MEETING_PJOIN>();//参会
                List<T_SC_MT_MEETING_PASKFORLEAVE> listAskLevel = new List<T_SC_MT_MEETING_PASKFORLEAVE>();//请假
                List<T_SC_MT_MEETING_MINUTES_CONTENT> listMinutesContent = new List<T_SC_MT_MEETING_MINUTES_CONTENT>();
                List<T_SC_MT_MEETING_MINUTES_CONTENT_DEAL> listMinutesContentDeal = new List<T_SC_MT_MEETING_MINUTES_CONTENT_DEAL>();

                //跑批 会议开始时 1、给 记录人 发送 会议纪要 待办  2、判断 参会人员 的状态   （未完成 默认参会）
                foreach (var item in listMeeting)
                {
                    item.ISBEGIN = true;//修改是否开始

                    T_SC_MT_MEETING_MINUTES meetMinutes = new T_SC_MT_MEETING_MINUTES();
                    meetMinutes.MEETINGTYPE = item.MEETINGTYPE;
                    meetMinutes.ID = Guid.NewGuid();
                    meetMinutes.MEETING_ID = item.ID;
                    meetMinutes.STATUS = PFStandardStatus.Draft;
                    meetMinutes.CREATER_ID = item.USER_ID_RECORDER;
                    meetMinutes.CREATE_TIME = dtOperate;
                    meetMinutes.ORG_ID = item.ORG_ID;
                    meetMinutes.DEPARTMENT_ID = item.DEPARTMENT_ID;
                    listMeetMinutes.Add(meetMinutes);
                    //给记录人 添加待办

                    //判断有几个题目 
                    foreach (var itemTitle in item.Nav_ListMeetingContent)
                    {
                        if (itemTitle.Nav_ContentName.Nav_ListContent == null || !itemTitle.Nav_ContentName.Nav_ListContent.Any())
                        {
                            continue;
                        }
                        foreach (var itemNAME in itemTitle.Nav_ContentName.Nav_ListContent)
                        {
                            //itemNAME.TITLE
                            T_SC_MT_MEETING_MINUTES_CONTENT modelContent = new T_SC_MT_MEETING_MINUTES_CONTENT();
                            modelContent.ID = Guid.NewGuid();
                            modelContent.ORG_ID = item.ORG_ID;
                            modelContent.MEETING_MINUTES_ID = meetMinutes.ID;
                            modelContent.CONTENT_ID = itemNAME.ID;
                            modelContent.CREATER_ID = item.USER_ID_RECORDER;
                            modelContent.CREATE_TIME = dtOperate;
                            listMinutesContent.Add(modelContent);
                            if (itemNAME.Nav_ListContentDeal == null || !itemNAME.Nav_ListContentDeal.Any())
                            {
                                continue;
                            }

                            foreach (var itemDetail in itemNAME.Nav_ListContentDeal)
                            {
                                //选项内容
                                //itemDetail.CONTENTDETAIL
                                T_SC_MT_MEETING_MINUTES_CONTENT_DEAL modelContentDetail = new T_SC_MT_MEETING_MINUTES_CONTENT_DEAL();
                                modelContentDetail.ID = Guid.NewGuid();
                                modelContentDetail.ORG_ID = item.ORG_ID;
                                modelContentDetail.CONTENT_ID = modelContent.ID;
                                modelContentDetail.CONTENT_DEAL_ID = itemDetail.ID;
                                modelContentDetail.ISCHECK = false;
                                modelContentDetail.GETPOINT = 0;
                                modelContentDetail.CREATER_ID = item.USER_ID_RECORDER;
                                modelContentDetail.CREATE_TIME = dtOperate;
                                listMinutesContentDeal.Add(modelContentDetail);
                            }
                        }
                    }
                    //SC034 普通会议纪要编辑
                    //SC030 安委会纪要编辑
                    var Nav_UserRecorder = GetEntity<T_FM_USER>(e => e.ID == item.USER_ID_RECORDER && e.ENABLE_STATUS == 0);

                    var taskAdd = NotificationTaskService.InsertUserNoticeTaskModel(item.MEETINGTYPE.GetDescription() + "会议纪要", meetMinutes.ID, meetMinutes.ORG_ID, item.USER_ID_RECORDER.Value, Nav_UserRecorder.NAME, DateTime.Now, (int)FMNoticeTypeEnum.消息, (int)item.MEETINGTYPE >= 40 ? "SC030" : "SC034", FMTASKTYPE.MT_MINUTES);
                    listTask.Add(taskAdd);

                    var Nav_ListPAll = GetEntities<T_SC_MT_MEETING_PALL>(e => e.MEETING_ID == item.ID, null, null);
                    var Nav_ListPReView = GetEntities<T_SC_MT_MEETING_PREVIEW>(e => e.MEETING_ID == item.ID, null, null);

                    if (Nav_ListPAll != null && Nav_ListPAll.Any())
                    {
                        foreach (var itemAll in Nav_ListPAll)
                        {
                            if (itemAll.JOINRESULT == null)//参会情况未反馈
                            {
                                //如果未反馈  直接 修改状态为未完成  默认参加会议
                                itemAll.JOINRESULT = JoinTaskResult.UnCheck;//系统设置未完成
                                                                            //itemAll.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN; //未反馈的默认参会
                                listAllChange.Add(itemAll);
                            }

                            if (itemAll.PERSONNATURE == PersonNature.HOST || itemAll.PERSONNATURE == PersonNature.RECORDER)
                            {
                                //如果是主持人或者 记录人 不加信息
                                continue;
                            }
                            switch (itemAll.JOINRESULT)
                            {
                                case JoinTaskResult.Join:
                                case JoinTaskResult.UnCheck:
                                    //参加 未完成 默认 参会
                                    T_SC_MT_MEETING_PJOIN join = new T_SC_MT_MEETING_PJOIN();
                                    join.ID = Guid.NewGuid();
                                    join.MEETING_MINUTES_ID = meetMinutes.ID;


                                    if (itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.ALL || itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.JOIN)// || itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.JOIN 应该不要
                                    {
                                        if (Nav_ListPReView != null && Nav_ListPReView.Any())
                                        {
                                            var rep = Nav_ListPReView.Where(e => e.USER_ID == itemAll.USER_ID);
                                            if (rep != null && rep.Any())
                                            {
                                                join.PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW;
                                            }
                                            else
                                            {
                                                join.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
                                            }
                                        }
                                        else
                                        {
                                            join.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
                                        }
                                    }
                                    else if (itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.REVIEW)
                                    {
                                        join.PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW;
                                    }
                                    join.USER_ID = itemAll.USER_ID;
                                    join.IS_DELETED = false;
                                    join.ORG_ID = itemAll.ORG_ID;
                                    join.ISRECORDRED = false;
                                    listJoin.Add(join);
                                    break;
                                case JoinTaskResult.AskForLeave:
                                    //请假
                                    T_SC_MT_MEETING_PASKFORLEAVE asklevel = new T_SC_MT_MEETING_PASKFORLEAVE();
                                    asklevel.ID = Guid.NewGuid();
                                    asklevel.MEETING_MINUTES_ID = meetMinutes.ID;
                                    asklevel.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
                                    asklevel.USER_ID = itemAll.USER_ID;
                                    asklevel.IS_DELETED = false;
                                    asklevel.ORG_ID = itemAll.ORG_ID;
                                    listAskLevel.Add(asklevel);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    //item.Nav_ListPAll = null; 这样会把ID清空 

                    var thisNotDeal = GetEntities<T_FM_NOTIFICATION_TASK>(e => e.SOURCE_FORMCODE == "SC028_SHOWPRINT" && e.SOURCE_DATA_ID == item.ID && e.NOTICE_STATUS == NoticeNotDeal, null, null);//这边应该不会有重复数据  （这个会议 未处理的 待办）

                    if (thisNotDeal != null && thisNotDeal.Any())
                    {
                        listTaskEnd.AddRange(thisNotDeal);
                    }

                    if (Nav_ListPReView != null && Nav_ListPReView.Any())
                    {
                        //评审 人员
                        foreach (var itemP in Nav_ListPReView)
                        {
                            listMeetMinutesPreview.Add(new T_SC_MT_MINUTES_PREVIEW()
                            {
                                ID = Guid.NewGuid(),
                                ORG_ID = itemP.ORG_ID,
                                MEETING_MINUTES_ID = meetMinutes.ID,
                                PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW,
                                USER_ID = itemP.USER_ID,
                                ISRECORDRED = false
                            });
                        }
                    }
                }

                //对未处理的待办 做系统超期处理
                if (listTaskEnd != null && listTaskEnd.Count > 0)
                {
                    foreach (var item in listTaskEnd)
                    {
                        item.NOTICE_STATUS = (int)FMNoticeStatusEnum.超期办理;
                        item.MODIFY_TIME = dtOperate;
                    }
                }

                T_FM_SYNC_TASK task = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                if (task != null)
                {
                    task.UPDATE_SUCCES_TIME = DateTime.Now;//上次同步结束时间
                    task.SYNC_PARAM = "";//清空参数
                }
                foreach (var item in listMeeting)
                {
                    if (item.Nav_ListMeetingContent != null && item.Nav_ListMeetingContent.Any())
                    {
                        foreach (var itemT in item.Nav_ListMeetingContent)
                        {
                            itemT.Nav_ContentName = null;
                        }
                    }
                }
                this.UnifiedCommit(() =>
                {
                    if (listMeeting != null && listMeeting.Any()) //会议状态【是否开始】修改
                        BantchUpdateEntityNoCommit(listMeeting);
                    if (listMeetMinutes != null && listMeetMinutes.Any()) //添加会议纪要
                        BantchSaveEntityNoCommit(listMeetMinutes);
                    if (listJoin != null && listJoin.Any()) //参会人员
                        BantchSaveEntityNoCommit(listJoin);
                    if (listAskLevel != null && listAskLevel.Any()) //请假人员
                        BantchSaveEntityNoCommit(listAskLevel);
                    if (listTask != null && listTask.Any()) //给记录人发送会议纪要待办
                        BantchSaveEntityNoCommit(listTask);
                    if (listAllChange != null && listAllChange.Any()) //参会人员未反馈 状态修改
                        BantchUpdateEntityNoCommit(listAllChange);
                    if (listTaskEnd != null && listTaskEnd.Count > 0) //未完成对应待办处理
                        BantchUpdateEntityNoCommit(listTaskEnd);
                    if (listMinutesContent != null && listMinutesContent.Any()) //会议意见
                        BantchSaveEntityNoCommit(listMinutesContent);
                    if (listMinutesContentDeal != null && listMinutesContentDeal.Any()) //会议意见详情
                        BantchSaveEntityNoCommit(listMinutesContentDeal);
                    if (listMeetMinutesPreview != null && listMeetMinutesPreview.Count > 0)//审批人员
                        BantchSaveEntityNoCommit(listMeetMinutesPreview);
                    if (task != null)
                        UpdateEntityNoCommit(task);
                });

                int Count = ((listMeetMinutes != null && listMeetMinutes.Any()) ? listMeetMinutes.Count : 0) + ((listJoin != null && listJoin.Any()) ? listJoin.Count : 0) + ((listAskLevel != null && listAskLevel.Any()) ? listAskLevel.Count : 0) + ((listTask != null && listTask.Any()) ? listTask.Count : 0) + ((listAllChange != null && listAllChange.Any()) ? listAllChange.Count : 0) + ((listTaskEnd != null && listTaskEnd.Any()) ? listTaskEnd.Count : 0) + ((listMinutesContent != null && listMinutesContent.Any()) ? listMinutesContent.Count : 0) + ((listMinutesContentDeal != null && listMinutesContentDeal.Any()) ? listMinutesContentDeal.Count : 0) + ((listMeetMinutesPreview != null && listMeetMinutesPreview.Any()) ? listMeetMinutesPreview.Count : 0);
                SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);

                return true;
            });
        }

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

                var listMtEnd = GetEntities<T_SC_MT_MEETING_END>(e => !e.IS_DELETED && !e.ISEND && e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Date <= dtNow.Date, null, null);
                if (listMtEnd == null && !listMtEnd.Any())
                {
                    return true;
                }

                var listMTID = listMtEnd.Select(e => e.MEETING_ID);
                var listMT = GetEntities<T_SC_MT_MEETING>(e => !e.IS_DELETED && listMTID.Contains(e.ID), null, null);
                if (listMT == null || !listMT.Any())
                {
                    return true;
                }

                listMT = listMT.ToList();

                List<T_FM_NOTIFICATION_TASK> listTaskSend = null;//需要添加的所有待办
                T_FM_USER userSafeMaster = null; //安环部负责人

                T_FM_SYNC_TASK taskSync = null;
                #region     各处理逻辑  代码块区分 不直接 return 因为有数据要处理

                //数据挨个判断处理
                listMtEnd.ForEach(e =>
                {
                    e.RUN_COUNT++;
                    e.ISEND = true;
                    e.MODIFY_TIME = DateTime.Now;
                });



                #region     3、CM 潜在紧急事件讨论 

                var listMTFA = listMT.Where(e => e.MEETINGTYPE == SCMEETINGTYPE.Ordinary && e.CREACTTYPE == CREACTTYPEEnum.ToCM018);
                if (listMTFA != null && listMTFA.Any())
                {
                    var userIDs = listMTFA.Where(e => e.USER_ID_RECORDER.HasValue && !e.ISCANCEL && !e.IS_DELETED).Select(e => e.USER_ID_RECORDER.Value);
                    if (userIDs != null && userIDs.Any())
                    {
                        var listUser = GetEntities<T_FM_USER>(e => userIDs.Contains(e.ID), null);
                        if (listTaskSend == null)
                            listTaskSend = new List<T_FM_NOTIFICATION_TASK>();
                        foreach (var item in listUser)
                        {
                            listTaskSend.Add(NotificationTaskService.InsertUserNoticeTaskModel("潜在紧急事件", Guid.Empty, filter.OrgId, item.ID, item.NAME, DateTime.Now, 0, "CM018", FMTASKTYPE.Default));
                        }
                    }
                }

                #endregion


                #endregion

                taskSync = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);//跑批待办
                if (taskSync.SYNC_PARAM != null)
                {
                    taskSync.SYNC_PARAM = "";
                }
                taskSync.TASK_END_TIME = DateTime.Now;
                taskSync.UPDATE_SUCCES_TIME = DateTime.Now;

                this.UnifiedCommit(() =>
                {
                    if (taskSync != null) //跑批信息
                        UpdateEntityNoCommit(taskSync);
                    BantchUpdateEntityNoCommit(listMtEnd);

                    if (listTaskSend != null && listTaskSend.Any()) //添加待办 公共
                        BantchSaveEntityNoCommit(listTaskSend);
                });

                int Count = ((listMtEnd != null && listMtEnd.Any()) ? listMtEnd.Count() : 0) + ((listTaskSend != null && listTaskSend.Any()) ? listTaskSend.Count : 0);
                SyncLogDetailService.InsertSyncLogDetail(HttpContext.Items, HttpContext.TraceIdentifier, filter.OrgId, Count, filter.Parameter1, SyncLogType.BSCreateCheckPlanSet);
                return true;
            });
        }

        /// <summary>
        /// 其他模块 对接到会议开始
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("OtherToMeetingBegin")]
        public JsonActionResult<bool> OtherToMeetingBegin([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                DateTime dtNow = DateTime.Now;
                if (!string.IsNullOrEmpty(filter.Parameter1))
                {
                    try
                    {
                        //接收来自页面的参数
                        DateTime deParm = Convert.ToDateTime(filter.Parameter1);
                        dtNow = deParm;
                    }
                    catch { }
                }

                var listMtBegin = GetEntities<T_SC_MT_MEETING_BEGIN>(e => !e.IS_DELETED && !e.ISBEGIN && (!e.END_IGNORE.HasValue || e.END_IGNORE.Value > dtNow), null, null).OrderBy(e => e.CREATE_TIME.Value);
                //&& e.CREATE_TIME.HasValue && e.CREATE_TIME.Value.Date == dtNow.Date  只要没触发 就会触发

                if (listMtBegin == null && !listMtBegin.Any())
                {
                    return true;
                }


                #region    业务代码 扩展 每条一个事务处理 加强健壮性 但是性能低一些


                bool isChange = false;
                //foreach (var item in listMtBegin)
                //{
                //    switch (item.SOURCETYPE)
                //    {
                //        case SOURCETYPE.EventReport:

                //            if (!isChange)
                //                isChange = true;
                //            break;
                //        default:
                //            break;
                //    }
                //}

                #endregion

                if (isChange)
                {
                    T_FM_SYNC_TASK taskSync = GetEntity<T_FM_SYNC_TASK>(filter.Keyword);//跑批待办
                    if (taskSync.SYNC_PARAM != null)
                    {
                        taskSync.SYNC_PARAM = "";
                    }
                    taskSync.TASK_END_TIME = DateTime.Now;
                    taskSync.UPDATE_SUCCES_TIME = DateTime.Now;
                    this.UnifiedCommit(() =>
                    {
                        if (taskSync != null) //跑批信息
                            UpdateEntityNoCommit(taskSync);
                    });
                }

                return true;
            });
        }


        ///// <summary>
        ///// 会议开始   原方法 如果有多个可能会报错
        ///// 现在按每个单独处理（从数据库获取的model 不能设置导航属性为空）
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //[HttpPost, Route("MeetingBegin")]
        //public JsonActionResult<bool> MeetingBegin([FromBody] KeywordFilter filter)
        //{
        //    return SafeExecute<bool>(() =>
        //    {
        //        DateTime dtNow = DateTime.Now;
        //        if (!string.IsNullOrEmpty(filter.Parameter1))
        //        {
        //            try
        //            {
        //                //接收来自页面的参数
        //                DateTime deParm = Convert.ToDateTime(filter.Parameter1);
        //                dtNow = deParm;
        //            }
        //            catch { }
        //        }

        //        //查找符合条件的会议信息   今天 已经审批过  开始时间小于当前时间 的会议
        //        var listMeeting = GetEntities<T_SC_MT_MEETING>(e => !e.ISBEGIN && e.STATUS == PFStandardStatus.Archived && e.BEGIN_TIME.HasValue && e.BEGIN_TIME.Value.Date == dtNow.Date && e.BEGIN_TIME <= dtNow, null, new string[] { "Nav_UserRecorder", "Nav_ListPAll.Nav_User", "Nav_ListPReView", "Nav_ListMeetingContent.Nav_ContentName.Nav_ListContent.Nav_ListContentDeal" });
        //        if (listMeeting == null || !listMeeting.Any())
        //        {
        //            //没有符合条件的会议
        //            return true;
        //        }

        //        //跑批 会议开始时 1、给 记录人 发送 会议纪要 待办  2、判断 参会人员 的状态   （未完成 默认参会）
        //        foreach (var item in listMeeting)
        //        {
        //            List<T_FM_NOTIFICATION_TASK> listTask = new List<T_FM_NOTIFICATION_TASK>();
        //            List<T_SC_MT_MEETING_MINUTES> listMeetMinutes = new List<T_SC_MT_MEETING_MINUTES>();
        //            List<T_SC_MT_MEETING_PALL> listAllChange = new List<T_SC_MT_MEETING_PALL>();
        //            List<T_SC_MT_MINUTES_PREVIEW> listMeetMinutesPreview = new List<T_SC_MT_MINUTES_PREVIEW>();
        //            DateTime dtOperate = DateTime.Now; //实际时间 不随传参改变
        //            int NoticeNotDeal = (int)FMNoticeStatusEnum.未处理;
        //            List<T_FM_NOTIFICATION_TASK> listTaskEnd = new List<T_FM_NOTIFICATION_TASK>();


        //            List<T_SC_MT_MEETING_PJOIN> listJoin = new List<T_SC_MT_MEETING_PJOIN>();//参会
        //            List<T_SC_MT_MEETING_PASKFORLEAVE> listAskLevel = new List<T_SC_MT_MEETING_PASKFORLEAVE>();//请假
        //            List<T_SC_MT_MEETING_MINUTES_CONTENT> listMinutesContent = new List<T_SC_MT_MEETING_MINUTES_CONTENT>();
        //            List<T_SC_MT_MEETING_MINUTES_CONTENT_DEAL> listMinutesContentDeal = new List<T_SC_MT_MEETING_MINUTES_CONTENT_DEAL>();


        //            item.ISBEGIN = true;//修改是否开始

        //            T_SC_MT_MEETING_MINUTES meetMinutes = new T_SC_MT_MEETING_MINUTES();
        //            meetMinutes.MEETINGTYPE = item.MEETINGTYPE;
        //            meetMinutes.ID = Guid.NewGuid();
        //            meetMinutes.MEETING_ID = item.ID;
        //            meetMinutes.STATUS = PFStandardStatus.Draft;
        //            meetMinutes.CREATER_ID = item.USER_ID_RECORDER;
        //            meetMinutes.CREATE_TIME = dtOperate;
        //            meetMinutes.ORG_ID = item.ORG_ID;
        //            listMeetMinutes.Add(meetMinutes);
        //            //给记录人 添加待办

        //            //判断有几个题目 
        //            foreach (var itemTitle in item.Nav_ListMeetingContent)
        //            {
        //                if (itemTitle.Nav_ContentName.Nav_ListContent == null || !itemTitle.Nav_ContentName.Nav_ListContent.Any())
        //                {
        //                    continue;
        //                }
        //                foreach (var itemNAME in itemTitle.Nav_ContentName.Nav_ListContent)
        //                {
        //                    //itemNAME.TITLE
        //                    T_SC_MT_MEETING_MINUTES_CONTENT modelContent = new T_SC_MT_MEETING_MINUTES_CONTENT();
        //                    modelContent.ID = Guid.NewGuid();
        //                    modelContent.ORG_ID = item.ORG_ID;
        //                    modelContent.MEETING_MINUTES_ID = meetMinutes.ID;
        //                    modelContent.CONTENT_ID = itemNAME.ID;
        //                    modelContent.CREATER_ID = item.USER_ID_RECORDER;
        //                    modelContent.CREATE_TIME = dtOperate;
        //                    listMinutesContent.Add(modelContent);
        //                    if (itemNAME.Nav_ListContentDeal == null || !itemNAME.Nav_ListContentDeal.Any())
        //                    {
        //                        continue;
        //                    }

        //                    foreach (var itemDetail in itemNAME.Nav_ListContentDeal)
        //                    {
        //                        //选项内容
        //                        //itemDetail.CONTENTDETAIL
        //                        T_SC_MT_MEETING_MINUTES_CONTENT_DEAL modelContentDetail = new T_SC_MT_MEETING_MINUTES_CONTENT_DEAL();
        //                        modelContentDetail.ID = Guid.NewGuid();
        //                        modelContentDetail.ORG_ID = item.ORG_ID;
        //                        modelContentDetail.CONTENT_ID = modelContent.ID;
        //                        modelContentDetail.CONTENT_DEAL_ID = itemDetail.ID;
        //                        modelContentDetail.ISCHECK = false;
        //                        modelContentDetail.GETPOINT = 0;
        //                        modelContentDetail.CREATER_ID = item.USER_ID_RECORDER;
        //                        modelContentDetail.CREATE_TIME = dtOperate;
        //                        listMinutesContentDeal.Add(modelContentDetail);
        //                    }
        //                }
        //            }
        //            //SC034 普通会议纪要编辑
        //            //SC030 安委会纪要编辑

        //            var taskAdd = NotificationTaskService.InsertUserNoticeTaskModel(item.MEETINGTYPE.GetDescription() + "会议纪要", meetMinutes.ID, meetMinutes.ORG_ID, item.USER_ID_RECORDER.Value, item.Nav_UserRecorder.NAME, DateTime.Now, DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, (int)item.MEETINGTYPE >= 40 ? "SC030" : "SC034");
        //            listTask.Add(taskAdd);

        //            foreach (var itemAll in item.Nav_ListPAll)
        //            {
        //                if (itemAll.JOINRESULT == null)//参会情况未反馈
        //                {
        //                    //如果未反馈  直接 修改状态为未完成  默认参加会议
        //                    itemAll.JOINRESULT = JoinTaskResult.UnCheck;//系统设置未完成
        //                    //itemAll.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN; //未反馈的默认参会
        //                    listAllChange.Add(itemAll);
        //                }

        //                switch (itemAll.JOINRESULT)
        //                {
        //                    case JoinTaskResult.Join:
        //                    case JoinTaskResult.UnCheck:
        //                        //参加 未完成 默认 参会
        //                        T_SC_MT_MEETING_PJOIN join = new T_SC_MT_MEETING_PJOIN();
        //                        join.ID = Guid.NewGuid();
        //                        join.MEETING_MINUTES_ID = meetMinutes.ID;
        //                        if (itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.ALL || itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.JOIN)// || itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.JOIN 应该不要
        //                        {
        //                            if (item.Nav_ListPReView != null && item.Nav_ListPReView.Any())
        //                            {
        //                                var rep = item.Nav_ListPReView.Where(e => e.USER_ID == itemAll.USER_ID);
        //                                if (rep != null && rep.Any())
        //                                {
        //                                    join.PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW;
        //                                }
        //                                else
        //                                {
        //                                    join.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
        //                                }
        //                            }
        //                            else
        //                            {
        //                                join.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
        //                            }
        //                        }
        //                        else if (itemAll.PERSONTYPE == SCMEETINGPERSONTYPE.REVIEW)
        //                        {
        //                            join.PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW;
        //                        }
        //                        join.USER_ID = itemAll.USER_ID;
        //                        join.IS_DELETED = false;
        //                        join.ORG_ID = itemAll.ORG_ID;
        //                        join.ISRECORDRED = false;
        //                        listJoin.Add(join);
        //                        break;
        //                    case JoinTaskResult.AskForLeave:
        //                        //请假
        //                        T_SC_MT_MEETING_PASKFORLEAVE asklevel = new T_SC_MT_MEETING_PASKFORLEAVE();
        //                        asklevel.ID = Guid.NewGuid();
        //                        asklevel.MEETING_MINUTES_ID = meetMinutes.ID;
        //                        asklevel.PERSONTYPE = SCMEETINGPERSONTYPE.JOIN;
        //                        asklevel.USER_ID = itemAll.USER_ID;
        //                        asklevel.IS_DELETED = false;
        //                        asklevel.ORG_ID = itemAll.ORG_ID;
        //                        listAskLevel.Add(asklevel);
        //                        break;
        //                    default:
        //                        break;
        //                }
        //            }

        //            //item.Nav_ListPAll = null; 这样会把ID清空 

        //            var thisNotDeal = GetEntities<T_FM_NOTIFICATION_TASK>(e => e.SOURCE_FORMCODE == "SC028_SHOWPRINT" && e.SOURCE_DATA_ID == item.ID && e.NOTICE_STATUS == NoticeNotDeal, null, null);//这边应该不会有重复数据  （这个会议 未处理的 待办）

        //            if (thisNotDeal != null && thisNotDeal.Any())
        //            {
        //                listTaskEnd.AddRange(thisNotDeal);
        //            }

        //            if (item.Nav_ListPReView != null && item.Nav_ListPReView.Any())
        //            {
        //                //评审 人员
        //                foreach (var itemP in item.Nav_ListPReView)
        //                {
        //                    listMeetMinutesPreview.Add(new T_SC_MT_MINUTES_PREVIEW()
        //                    {
        //                        ID = Guid.NewGuid(),
        //                        ORG_ID = itemP.ORG_ID,
        //                        MEETING_MINUTES_ID = meetMinutes.ID,
        //                        PERSONTYPE = SCMEETINGPERSONTYPE.REVIEW,
        //                        USER_ID = itemP.USER_ID,
        //                        ISRECORDRED = false
        //                    });
        //                }
        //            }



        //            //对未处理的待办 做系统超期处理
        //            if (listTaskEnd != null && listTaskEnd.Count > 0)
        //            {
        //                foreach (var itemT in listTaskEnd)
        //                {
        //                    itemT.NOTICE_STATUS = (int)FMNoticeStatusEnum.超期办理;
        //                    itemT.MODIFY_TIME = dtOperate;
        //                }
        //            }

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

        //            this.UnifiedCommit(() =>
        //            {
        //                if (listMeeting != null && listMeeting.Any()) //会议状态【是否开始】修改
        //                    BantchUpdateEntityNoCommit(listMeeting);
        //                if (listMeetMinutes != null && listMeetMinutes.Any()) //添加会议纪要
        //                    BantchSaveEntityNoCommit(listMeetMinutes);
        //                if (listJoin != null && listJoin.Any()) //参会人员
        //                    BantchSaveEntityNoCommit(listJoin);
        //                if (listAskLevel != null && listAskLevel.Any()) //请假人员
        //                    BantchSaveEntityNoCommit(listAskLevel);
        //                if (listTask != null && listTask.Any()) //给记录人发送会议纪要待办
        //                    BantchSaveEntityNoCommit(listTask);
        //                if (listAllChange != null && listAllChange.Any()) //参会人员未反馈 状态修改
        //                    BantchUpdateEntityNoCommit(listAllChange);
        //                if (listTaskEnd != null && listTaskEnd.Count > 0) //未完成对应待办处理
        //                    BantchUpdateEntityNoCommit(listTaskEnd);
        //                if (listMinutesContent != null && listMinutesContent.Any()) //会议意见
        //                    BantchSaveEntityNoCommit(listMinutesContent);
        //                if (listMinutesContentDeal != null && listMinutesContentDeal.Any()) //会议意见详情
        //                    BantchSaveEntityNoCommit(listMinutesContentDeal);
        //                if (listMeetMinutesPreview != null && listMeetMinutesPreview.Count > 0)//审批人员
        //                    BantchSaveEntityNoCommit(listMeetMinutesPreview);
        //                if (task != null)
        //                    UpdateEntityNoCommit(task);
        //            });

        //        }

        //        return true;
        //    });
        //}


    }
}
