using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using System;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System.Linq;
using APT.MS.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using System.Linq.Expressions;
using Newtonsoft.Json;
using APT.BaseData.Domain.IServices.BS;
using APT.MS.Domain.Entities.BS;


namespace APT.BaseData.Services.DomainServices
{
    /// <summary>
    /// 安全检查
    /// </summary>
    public class BSOperateLogService : CommonService, IBSOperateLogService
    {
        public BSOperateLogService(IRepository repository) : base(repository)
        {
        }

        /// <summary>  
        /// 直接添加多个操作
        /// 或者 一个操作 引起多个后续操作人
        /// </summary>
        /// <param name="OPERATEPOINT">操作的节点</param>
        /// <param name="DATA_ID">SafeCheckID RiskSubmitID</param>
        /// <param name="UserIDLogin">登入人ID</param>
        /// <param name="ListDATA_ID">可为null与 DATA_ID 只有一个</param>
        /// <param name="ListUserID">可为null 需要添加下一个节点的人员ID</param>
        /// <param name="ListUserName">可为null需要添加下一个节点的人员姓名</param>
        /// <param name="ORG_ID"></param>
        /// <param name="DATA_ID_SUB"></param>
        /// <param name="ListUserIDDone">默认操作通过的</param>
        /// <param name="isEnd"></param>
        /// <returns></returns>
        public List<T_BS_OPERATE_LOG> GetListOperateLog(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserIDLogin, List<Guid> ListDATA_ID, List<Guid> ListUserID, List<string> ListUserName, Guid? ORG_ID, Guid? DATA_ID_SUB = null, List<Guid> ListUserIDDone = null, bool isEnd = false, DateTime? TASK_ENDDT = null)
        {
            List<T_BS_OPERATE_LOG> listResult = new List<T_BS_OPERATE_LOG>();
            T_BS_OPERATE_LOG modelLogUpdate = null;
            string UserName = string.Empty;
            switch (OPERATEPOINT)
            {
                case OPERATEPOINT_Enums.GotCheck:
                    //系统触发 只是新增
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, ListDATA_ID[i], ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.Cancel:
                    //系统触发 只是新增
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.GotCheck, DATA_ID, UserIDLogin, null, TASK_ENDDT);
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckForm:
                    //BS032保存并发送 到审批流
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.GotCheck, DATA_ID, UserIDLogin, null, TASK_ENDDT);
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的待办日志
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)//最后一个审批人 添加 通知
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckerCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckerCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    break;
                case OPERATEPOINT_Enums.CheckRegisterRun:
                    //检查登记
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++) //发起检查登记
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckRegister, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckRegister:
                    //检查登记
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++) //发起检查结果确认
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckAgree, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckAgree:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++) //检查结果确认完 直接到审批
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckResultAudit, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.CheckResultAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (isEnd) //最后一个审批就归档
                    {
                        var logEnd = AddModel(OPERATEPOINT_Enums.CheckEnd, DATA_ID, UserIDLogin, APT.Infrastructure.Api.AppContext.CurrentSession.UserName, ORG_ID);
                        logEnd.OPERTETIME = DateTime.Now;
                        if (modelLogUpdate != null)
                            logEnd.ISINTTIME = modelLogUpdate.ISINTTIME;
                        listResult.Add(logEnd);
                    }
                    else if (DATA_ID_SUB != null)
                    {
                        if (ListUserID != null && ListUserName != null && ListUserID.Any())
                            for (int i = 0; i < ListUserID.Count; i++)
                                listResult.Add(AddModel(OPERATEPOINT_Enums.RiskGet, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    }
                    break;
                case OPERATEPOINT_Enums.CheckEnd:
                    //此处没操作
                    break;
                case OPERATEPOINT_Enums.RiskNoticeSD:
                    break;
                case OPERATEPOINT_Enums.RiskUpSD:
                    //有用
                    UserName = APT.Infrastructure.Api.AppContext.CurrentSession.UserName;
                    if (string.IsNullOrEmpty(UserName) && ListUserID != null && ListUserID.Any() && ListUserID.Count == 1 && ListUserID[0] == UserIDLogin)
                    {
                        UserName = ListUserName[0];
                    }
                    listResult.Add(AddModel(OPERATEPOINT_Enums.RiskUpSD, DATA_ID, UserIDLogin, UserName, ORG_ID, 1, true, DATA_ID_SUB));
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));//RiskInfoCompleteSD  新流程 做修改  RiskInfoCompleteSDNew
                    break;
                case OPERATEPOINT_Enums.RiskInfoCompleteSD:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                    {
                        listResult.Add(modelLogUpdate);
                    }
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskInfoCompleteSDNew, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskUpSDNew://新问题
                    //listResult.Add(AddModel(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, UserIDLogin, APT.Infrastructure.Api.AppContext.CurrentSession.UserName, ORG_ID, 1, true, DATA_ID_SUB));
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    T_BS_OPERATE_LOG modelAddExt = null;
                    if (modelLogUpdate != null)
                    {
                        modelAddExt = (T_BS_OPERATE_LOG)modelLogUpdate.Clone();
                    }
                    else
                    {
                        modelAddExt = new T_BS_OPERATE_LOG();
                        modelAddExt.ORG_ID = ORG_ID;
                    }

                    modelAddExt.OPERATEPOINT = OPERATEPOINT_Enums.RiskUpSDNew;//操作节点名称修改 为新问题
                    modelAddExt.ID = Guid.NewGuid();
                    modelAddExt.CREATE_TIME = DateTime.Now;
                    modelAddExt.OPERTETIME = DateTime.Now;
                    modelAddExt.MODIFY_TIME = DateTime.Now;
                    modelAddExt.CREATER_ID = UserIDLogin;
                    listResult.Add(modelAddExt);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskInfoCompleteSDNew, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskInfoCompleteSDNew://新问题处理
                    //listResult.Add(AddModel(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, UserIDLogin, APT.Infrastructure.Api.AppContext.CurrentSession.UserName, ORG_ID, 1, true, DATA_ID_SUB));
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.RiskInfoCompleteSD, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);

                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskGet, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskGet:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskSendAudit, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                //case OPERATEPOINT_Enums.RiskSend:
                //    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                //    if (modelLogUpdate != null)
                //        listResult.Add(modelLogUpdate);
                //    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                //        for (int i = 0; i < ListUserID.Count; i++)
                //            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskSendAudit, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                //    break;
                case OPERATEPOINT_Enums.RiskSendAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, null, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskAssignActure, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskAssignActure:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskNoticeCheckerCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskNoticeCheckerCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskNoticeActureCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskNoticeActureCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDeal, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDelayApplyCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    break;
                case OPERATEPOINT_Enums.RiskDeal:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDealDealCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDealDealCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDealCheckerCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDealDealCheckReject://case OPERATEPOINT_Enums.RiskDealDealCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.RiskDealDealCheck, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                    {
                        modelLogUpdate.OPERATEPOINT = OPERATEPOINT_Enums.RiskDealDealCheckReject;////状态修改为拒绝
                        listResult.Add(modelLogUpdate);
                    }
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDeal, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDealCheckerCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDealAudit, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDealCheckerCheckReject:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.RiskDealCheckerCheck, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                    {
                        modelLogUpdate.OPERATEPOINT = OPERATEPOINT_Enums.RiskDealCheckerCheckReject;//状态修改为拒绝
                        listResult.Add(modelLogUpdate);
                    }
                    if (ListUserID != null && ListUserName != null && ListUserID.Any())
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT_Enums.RiskDeal, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i, false, DATA_ID_SUB));
                    break;
                case OPERATEPOINT_Enums.RiskDealAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin, DATA_ID_SUB, TASK_ENDDT);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);

                    var logEndRisk = AddModel(OPERATEPOINT_Enums.RiskEnd, DATA_ID, UserIDLogin, APT.Infrastructure.Api.AppContext.CurrentSession.UserName, ORG_ID);
                    logEndRisk.OPERTETIME = DateTime.Now;
                    if (modelLogUpdate != null)
                        logEndRisk.ISINTTIME = modelLogUpdate.ISINTTIME;
                    listResult.Add(logEndRisk);

                    break;
                case OPERATEPOINT_Enums.RiskEnd:
                    break;
                default:
                    break;
            }
            if (ListUserIDDone != null && ListUserIDDone.Count > 0)
            {
                //除了登陆者 需要默认操作的
                listResult.ForEach(e =>
                {
                    if (ListUserIDDone.Contains(e.USER_ID))
                        e.OPERTETIME = DateTime.Now;
                });
            }
            return listResult;
        }



        /// <summary>
        /// 获取新记录
        /// </summary>
        /// <param name="OPERATEPOINT"></param>
        /// <param name="DATA_ID"></param>
        /// <param name="UserID"></param>
        /// <param name="UserName"></param>
        /// <param name="ORG_ID"></param>
        /// <param name="ORDERNO"></param>
        /// <param name="isAddLog">是否添加记录 比如没找到记录 直接添加记录</param>
        /// <returns></returns>
        private T_BS_OPERATE_LOG AddModel(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserID, string UserName, Guid? ORG_ID, int ORDERNO = 0, bool isAddLog = false, Guid? DATA_ID_SUB = null)
        {
            if (!string.IsNullOrEmpty(UserName) && UserName.Contains("=>"))
            {
                try
                {
                    UserName = UserName.Split(" ")[2];
                }
                catch { }
            }
            T_BS_OPERATE_LOG ModelAdd = new T_BS_OPERATE_LOG()
            {
                ID = Guid.NewGuid(),
                ORG_ID = ORG_ID,
                LOGTYPE = ((int)OPERATEPOINT) > 80 ? LOGTYPE_Enums.RiskSubmit : LOGTYPE_Enums.SafeCheck,
                DATA_ID = DATA_ID,
                DATA_ID_SUB = DATA_ID_SUB,
                OPERATEPOINT = OPERATEPOINT,
                USER_ID = UserID,
                USRTNAME = UserName,
                ORDERNO = ORDERNO,
                //OPERTETIME = null,
                //ISINTTIME = false,
                CREATE_TIME = DateTime.Now,
                CREATER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID
            };
            if (isAddLog || (APT.Infrastructure.Api.AppContext.CurrentSession.UserID.HasValue && APT.Infrastructure.Api.AppContext.CurrentSession.UserID.Value == UserID))
            {
                ModelAdd.OPERTETIME = DateTime.Now;//默认通过
                ModelAdd.ISINTTIME = true;
            }
            return ModelAdd;
        }

        /// <summary>
        /// 获取需要修改的Model
        /// </summary>
        /// <param name="OPERATEPOINT">操作节点</param>
        /// <param name="DATA_ID"></param>
        /// <param name="UserID"></param>
        /// <param name="DATA_ID_SUB"></param>
        /// <param name="IsAutoFilter">自动处理转办</param>
        /// <returns></returns>
        private T_BS_OPERATE_LOG GetUpdateDeal(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserID, Guid? DATA_ID_SUB = null, DateTime? TASK_ENDDT = null, bool IsAutoFilter = true)
        {
            if (UserID == Guid.Empty)
            {
                return null;
            }
            Expression<Func<T_BS_OPERATE_LOG, bool>> expression = e => e.OPERATEPOINT == OPERATEPOINT && e.USER_ID == UserID;
            if (DATA_ID != Guid.Empty)
                expression = expression.And(e => e.DATA_ID == DATA_ID);

            if (DATA_ID_SUB.HasValue)
                expression = expression.And(e => e.DATA_ID_SUB.HasValue && e.DATA_ID_SUB == DATA_ID_SUB);

            //var model = GetEntity(expression);
            var listModel = GetEntities(expression);
            T_BS_OPERATE_LOG model = null;
            if (listModel != null && listModel.Any())
            {
                if (listModel.Count() == 1)
                    model = listModel.ToList()[0];
                else
                    model = listModel.OrderByDescending(e => e.CREATE_TIME).ToList()[0];
            }
            if (model == null && IsAutoFilter)
            {
                //转办
                expression = e => e.DATA_ID == DATA_ID && e.OPERATEPOINT == OPERATEPOINT;
                if (DATA_ID_SUB.HasValue)
                    expression = expression.And(e => e.DATA_ID_SUB.HasValue && e.DATA_ID_SUB == DATA_ID_SUB);
                listModel = GetEntities(expression);
                if (listModel != null && listModel.Count() == 1)
                {
                    model = listModel.ToList()[0];
                    var UserOP = GetEntity<T_FM_USER>(UserID);
                    model.MODIFIER_ID = UserOP.ID;
                    model.USRTNAME += (" => " + UserOP.NAME);
                }
            }
            if (model != null)
            {
                model.OPERTETIME = DateTime.Now;
                if (TASK_ENDDT != null)
                    model.ISINTTIME = (model.OPERTETIME > TASK_ENDDT.Value ? false : true);
            }
            return model;
        }

    }
}