using APT.BaseData.Domain.ApiModel.PF;
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.FO;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Enums;
using APT.Utility;
using Castle.Core.Internal;
using log4net.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace APT.HM.WebApi.Controllers.Api
{
    [Route("api/HM/HMJobtaskIdentify")]
    public partial class JobtaskIdentifyController : AuthorizeApiController<T_HM_JOBTASK_IDENTIFY>
    {
        IPFCodeRuleService CodeRuleService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        IFMDepartmentService DepartmentService { get; set; }
        /// <summary>
        /// 作业任务识别
        /// </summary>
        public JobtaskIdentifyController(IPFCodeRuleService codeRuleService, IFMFlowPermitService mFlowPermitService, IFMNotificationTaskService notificationTaskService, IPFApproveCallBackService approveCallBackService, IFMDepartmentService departmentService)
        {
            CodeRuleService = codeRuleService;
            MFlowPermitService = mFlowPermitService;
            NotificationTaskService = notificationTaskService;
            ApproveCallBackService = approveCallBackService;
            DepartmentService = departmentService;
        }
        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("FullGet")]
        public JsonActionResult<T_HM_JOBTASK_IDENTIFY> FullGet([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                T_HM_JOBTASK_IDENTIFY iDENTIFY = new T_HM_JOBTASK_IDENTIFY();
                List<T_HM_JOBTASK_IDENTIFY_DETAIL> detailList = new List<T_HM_JOBTASK_IDENTIFY_DETAIL>();
                if (string.IsNullOrEmpty(filter.Keyword))
                    filter.Keyword = APT.Infrastructure.Api.AppContext.CurrentSession.UserID.ToString();
                //取人员部门对应的部门层级：公司级、部门级、车间级、班组级
                var user = GetEntity<T_FM_USER>(t => t.ID == Guid.Parse(filter.Keyword) && t.ENABLE_STATUS == 0, false, "Nav_Department");
                var type = user.Nav_Department == null? 3 : user.Nav_Department?.DEPARTMENT_TYPE;
                //主表部门自动赋值
                iDENTIFY.ORG_ID = user.ORG_ID;
                iDENTIFY.DEPARTMENT_ID = user.DEPARTMENT_ID;
                List<Guid> departmentIds = new List<Guid>() { user.DEPARTMENT_ID.Value };
                DepartmentService.GetDepartmentIds(user.ORG_ID.Value, new List<Guid>() { user.DEPARTMENT_ID.Value }, ref departmentIds);
                //根据辨识区域和部门层级到风险库获取作业任务、岗位、风险信息typeList.Contains((int)t.LEVEL) && areaIds.Contains((Guid)t.AREA_ID)
                var stepIds = GetEntities<T_HM_EVALUATE_RISK>(t => departmentIds.Contains(t.DEPARTMENT_LIABLE_ID.Value) && t.OPERATION_STEP_ID != null && t.STATUS == (int)HMOperationStepEnum.有效 && (int)t.LEVEL == type, new BaseFilter(user.ORG_ID)).Select(t => t.OPERATION_STEP_ID).Distinct().ToList();
                var links = GetEntities<T_HM_OPERATION_LINK>(t => stepIds.Contains(t.OPERATION_STEP_ID), new BaseFilter(user.ORG_ID), new string[] { "Nav_LinkPost", "Nav_LinkPost.Nav_Post", "Nav_OperationStep" });
                if (links != null && links.Any())
                {
                    links.ForEach(t =>
                    {
                        List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST> postList = new List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST>();
                        List<string> detailPost = new List<string>(); var postName = "";
                        T_HM_JOBTASK_IDENTIFY_DETAIL detail = new T_HM_JOBTASK_IDENTIFY_DETAIL();
                        //List<T_HM_JOBTASK_IDENTIFY_DETAIL_RISK> riskList = new List<T_HM_JOBTASK_IDENTIFY_DETAIL_RISK>();
                        detail.ORG_ID = user.ORG_ID;
                        detail.JOBTASK_IDENTIFY_ID = iDENTIFY.ID;
                        //detail.AREA_ID = t.AREA_ID;
                        //detail.Nav_Area = t.Nav_Area;t
                        if (t.Nav_LinkPost != null && t.Nav_LinkPost.Any())
                        {
                            t.Nav_LinkPost.ForEach(x =>
                            {
                                T_HM_JOBTASK_IDENTIFY_DETAIL_POST post = new T_HM_JOBTASK_IDENTIFY_DETAIL_POST();
                                post.ORG_ID = t.ORG_ID;
                                post.JOBTASK_IDENTIFY_DETAIL_ID = detail.ID;
                                post.POST_ID = x.POST_ID;
                                post.Nav_Post = x.Nav_Post;
                                postName = x.Nav_Post?.NAME;
                                detailPost.Add(postName);
                                postList.Add(post);
                            });
                        }
                        //风险表
                        //var risks = links.Where(m => m.OPERATION_STEP_ID == t.OPERATION_STEP_ID).ToList();
                        //if (risks != null && risks.Any())
                        //{
                        //    risks.ForEach(x =>
                        //    {
                        //        T_HM_JOBTASK_IDENTIFY_DETAIL_RISK risk = new T_HM_JOBTASK_IDENTIFY_DETAIL_RISK();
                        //        risk.ORG_ID = t.ORG_ID;
                        //        risk.JOBTASK_IDENTIFY_DETAIL_ID = detail.ID;
                        //        risk.EVALUATE_RISK_ID = x.ID;
                        //        risk.Nav_EvaluateRisk = x;
                        //        riskList.Add(risk);
                        //    });
                        //}
                        //detail.Nav_DetailRisk = riskList.Distinct(m => m.EVALUATE_RISK_ID).ToList();
                        detail.OPERATION_STEP_ID = t.OPERATION_STEP_ID;
                        detail.Nav_OperationStep = t.Nav_OperationStep;
                        detail.Nav_DetailPost = postList.Distinct(m => m.POST_ID).ToList();
                        detail.DetailPost = string.Join(",", detailPost);
                        detail.OperationStepName = t.Nav_OperationStep?.NAME;
                        detail.CYCLE_TYPE = t.CYCLE_TYPE;
                        detailList.Add(detail);
                    });
                }
                iDENTIFY.Nav_Details = detailList.OrderBy(t => t.Nav_OperationStep.NAME).ToList();
                return iDENTIFY;
            });
        }

        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_HM_JOBTASK_IDENTIFY entity)
        {
            return SafeExecute(() =>
            {
                //识别明细
                var details = entity.Nav_Details;
                //if (details == null || !details.Any())
                //    this.ThrowError("030015");
                //识别人
                var identityUsers = entity.Nav_IdentifyUser;
                //附件
                var files = entity.Nav_Files;
                //主表
                entity.Nav_Details = null;
                entity.Nav_IdentifyUser = null;
                entity.Nav_ApproveDetails = null;
                entity.Nav_Files = null;
                entity.COMPLETE_DATE = null;
                entity.IS_PUBLISH = (int)FOPreMeetingStatusEnum.草稿;
                entity.Nav_CreateUser = null;
                if (entity.Nav_CreateUser != null && entity.Nav_CreateUser.Nav_Department != null)
                    entity.DEPARTMENT_ID = entity.Nav_CreateUser.Nav_Department.ID;
                else
                    entity.DEPARTMENT_ID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID.Value;
                if (string.IsNullOrEmpty(entity.CODE))
                {
                    //取审批流水码
                    var sysFilter = new SystemCodeFilter();
                    sysFilter.CodeType = (int)PFCodeRuleType.作业任务识别表编号;
                    sysFilter.Count = 1;
                    sysFilter.OrgId = entity.ORG_ID;
                    var codes = CodeRuleService.NewGenSerial(sysFilter);
                    var codeList = codes.Split(new char[] { ',' });
                    //主表
                    entity.CODE = codeList[0];
                }
                List<T_HM_JOBTASK_IDENTIFY_DETAIL_RISK> riskList = new List<T_HM_JOBTASK_IDENTIFY_DETAIL_RISK>();
                List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST> postList = new List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST>();
                //任务明细表
                if (details != null && details.Any())
                {
                    details.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID;
                        //岗位表
                        if (t.Nav_DetailPost != null && t.Nav_DetailPost.Any())
                        {
                            var posts = t.Nav_DetailPost.ToList();
                            posts.ForEach(x =>
                            {
                                x.JOBTASK_IDENTIFY_DETAIL_ID = t.ID;
                                x.ORG_ID = t.ORG_ID;
                                x.Nav_Post = null;
                                postList.Add(x);
                            });
                        }
                        //风险表
                        if (t.Nav_DetailRisk != null && t.Nav_DetailRisk.Any())
                        {
                            var risks = t.Nav_DetailRisk.ToList();
                            risks.ForEach(x =>
                            {
                                x.JOBTASK_IDENTIFY_DETAIL_ID = t.ID;
                                x.ORG_ID = t.ORG_ID;
                                riskList.Add(x);
                            });
                        }
                        t.Nav_DetailPost = null;
                        t.JOBTASK_IDENTIFY_ID = entity.ID;
                        t.Nav_DetailRisk = null;
                        t.Nav_OperationStep = null;
                    });
                }
                //附件保存
                if (files != null && files.Any())
                {
                    files.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.JOBTASK_IDENTIFY_ID = entity.ID;
                    });
                }
                //取部门负责人
                //var chargeUserId = GetChargeUserId();
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
                List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                //T_HM_OTHER_APPROVE_LOG log = null;
                T_FM_NOTIFICATION_TASK task = null;
                //识别人
                if (identityUsers != null && identityUsers.Any())
                {
                    identityUsers.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.JOBTASK_IDENTIFY_ID = entity.ID;
                        if (t.USER_ID == loginUserId)
                            t.DEAL_STATUS = FOUserShiftStatusEnum.已处理;
                        t.Nav_User = null;
                    });
                    if (identityUsers.FirstOrDefault(t => t.IS_FIRST == true) == null)
                        identityUsers.Where(i => i == identityUsers.First()).ForEach(i => i.IS_FIRST = true);
                    var userIdTemp = identityUsers.Select(t => t.USER_ID).Distinct().ToList();
                    var userInfo = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIdTemp.Contains(t.ID),new BaseFilter(orgId), "Nav_Department");
                    var loginInfo = userInfo.FirstOrDefault(t => t.ID == loginUserId);
                    DateTime dtEnd = NotificationTaskService.GetTaskEndTime(FMTASKTYPE.Default, entity.ORG_ID.Value, DateTime.Now, null, null);
                    if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                    {
                        entity.IS_PUBLISH = FOPreMeetingStatusEnum.分析中;
                        //发给分析人中第一个
                        var userIds = new List<Guid>();
                        var userTemp = identityUsers.FirstOrDefault(t => t.IS_FIRST == true && t.USER_ID != loginUserId);
                        if (userTemp != null)
                        {
                            userIds.Add((Guid)userTemp.USER_ID);
                            var UserNames = new List<string>();
                            var user = userInfo.FirstOrDefault(t=>t.ID == userTemp.USER_ID);
                            UserNames.Add(user?.NAME);
                            //发消息
                            notices = NotificationTaskService.InsertUserNoticeTaskModels("作业任务识别表", entity.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                            dtEnd, (int)FMNoticeTypeEnum.消息, "HM105");
                        }
                        else
                        {
                            userIds = identityUsers.Where(t => t.USER_ID !=null && t.USER_ID != loginUserId).Select(m=>(Guid)m.USER_ID).ToList();
                            if (userIds.Any())
                            {
                                entity.IS_PUBLISH = FOPreMeetingStatusEnum.签到中;
                                var UserNames = new List<string>();
                                foreach (var user in userIds)
                                {
                                    var current = userInfo.FirstOrDefault(t => t.ID == user);
                                    UserNames.Add(current?.NAME);
                                }
                                //发消息
                                notices = NotificationTaskService.InsertUserNoticeTaskModels("作业任务识别表", entity.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                                dtEnd, (int)FMNoticeTypeEnum.消息, "HM104_SHOWPRINT");
                            }
                            else
                            {
                                entity.IS_PUBLISH = FOPreMeetingStatusEnum.审核中;
                                //取审批流水码
                                var sysFilter = new SystemCodeFilter();
                                sysFilter.CodeType = (int)PFCodeRuleType.审批流编码;
                                sysFilter.Count = 1;
                                sysFilter.OrgId = entity.ORG_ID;
                                var codes = CodeRuleService.NewGenSerial(sysFilter);
                                var serialCode = codes.Split(new char[] { ',' });
                                var param = Enum.GetName(typeof(FMDepartmentType), loginInfo?.Nav_Department?.DEPARTMENT_TYPE);
                                if (string.IsNullOrEmpty(param))
                                    param = "部门";
                                MFlowPermitService.InsertApprove(serialCode[0], "HM104", param, entity.ID, "HM104_SHOWPRINT", entity.TaskID, true, () =>
                                {
                                    if (entity != null)
                                        this.UpdateEntityNoCommit(entity);
                                    if (details != null && details.Any())
                                        this.BantchSaveEntityNoCommit(details);
                                    if (postList != null && postList.Any())
                                        this.BantchSaveEntityNoCommit(postList);
                                    if (riskList != null && riskList.Any())
                                        this.BantchSaveEntityNoCommit(riskList);
                                    if (identityUsers != null && identityUsers.Any())
                                        this.BantchSaveEntityNoCommit(identityUsers);
                                    if (files != null && files.Any())
                                        this.BantchSaveEntityNoCommit(files);
                                    if (notices != null && notices.Any())
                                        this.BantchSaveEntityNoCommit(notices);
                                    if (task != null)
                                        this.UpdateEntityNoCommit(task);
                                    //if (log != null)
                                    //    this.AddEntityNoCommit(log);
                                }, null, null, null, null, null, "HM104_SHOWPRINT", null,"", FMTASKTYPE.Default);
                                return true;
                            }
                        }
                        ////发消息
                        //notices.AddRange(NotificationTaskService.InsertUserNoticeTaskModels("请xx年xx月xx日，需要进行xx事情，特此提醒！", entity.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                        //DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.今日提醒, "PF135"));
                        if (entity.TaskID != Guid.Empty)
                        {
                            task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                            task.SOURCE_FORMCODE = "HM104_SHOWPRINT";
                        }
                    }
                    if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotifyAnother"))
                    {
                        //记录第一个识别人log
                        //log = new T_HM_OTHER_APPROVE_LOG();
                        //log.USER_ID = loginUserId;
                        //log.MAIN_FORM_ID = entity.ID;
                        //log.ORG_ID = entity.ORG_ID;
                        //发给其他识别人确认
                        var userIds = identityUsers.Where(t => t.USER_ID != loginUserId).Select(t => (Guid)t.USER_ID).Distinct().ToList();
                        identityUsers.Where(i => i.USER_ID == loginUserId).ForEach(i => i.DEAL_STATUS = FOUserShiftStatusEnum.已处理);
                        if (userIds != null && userIds.Any())
                        {
                            entity.IS_PUBLISH = FOPreMeetingStatusEnum.签到中;
                            var UserNames = new List<string>();
                            var users = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIds.Contains(t.ID), new BaseFilter(entity.ORG_ID));
                            foreach (var user in userIds)
                            {
                                var current = users.FirstOrDefault(t => t.ID == user);
                                UserNames.Add(current?.NAME);
                            }
                            //发消息
                            notices = NotificationTaskService.InsertUserNoticeTaskModels("作业任务识别表", entity.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                            dtEnd, (int)FMNoticeTypeEnum.消息, "HM104_SHOWPRINT");
                            if (entity.TaskID != Guid.Empty)
                            {
                                task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                                task.SOURCE_FORMCODE = "HM104_SHOWPRINT";
                            }
                        }
                        else
                        {
                            entity.IS_PUBLISH = FOPreMeetingStatusEnum.审核中;
                            //取审批流水码
                            var sysFilter = new SystemCodeFilter();
                            sysFilter.CodeType = (int)PFCodeRuleType.审批流编码;
                            sysFilter.Count = 1;
                            sysFilter.OrgId = entity.ORG_ID;
                            var codes = CodeRuleService.NewGenSerial(sysFilter);
                            var serialCode = codes.Split(new char[] { ',' });
                            var param = Enum.GetName(typeof(FMDepartmentType), loginInfo?.Nav_Department?.DEPARTMENT_TYPE);
                            if (string.IsNullOrEmpty(param))
                                param = "部门";
                            MFlowPermitService.InsertApprove(serialCode[0], "HM104", param, entity.ID, "HM104_SHOWPRINT", entity.TaskID, true, () =>
                            {
                                if (entity != null)
                                    this.UpdateEntityNoCommit(entity);
                                if (details != null && details.Any())
                                    this.BantchSaveEntityNoCommit(details);
                                if (postList != null && postList.Any())
                                    this.BantchSaveEntityNoCommit(postList);
                                if (riskList != null && riskList.Any())
                                    this.BantchSaveEntityNoCommit(riskList);
                                if (identityUsers != null && identityUsers.Any())
                                    this.BantchSaveEntityNoCommit(identityUsers);
                                if (files != null && files.Any())
                                    this.BantchSaveEntityNoCommit(files);
                                if (notices != null && notices.Any())
                                    this.BantchSaveEntityNoCommit(notices);
                                if (task != null)
                                    this.UpdateEntityNoCommit(task);
                                //if (log != null)
                                //    this.AddEntityNoCommit(log);
                            }, null, null, null, null, null, "HM104_SHOWPRINT", null,"", FMTASKTYPE.Default);
                            return true;
                        }
                    }
                }
                else
                    this.ThrowError("030018");
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (details != null && details.Any())
                        this.BantchSaveEntityNoCommit(details);
                    if (postList != null && postList.Any())
                        this.BantchSaveEntityNoCommit(postList);
                    if (riskList != null && riskList.Any())
                        this.BantchSaveEntityNoCommit(riskList);
                    if (identityUsers != null && identityUsers.Any())
                        this.BantchSaveEntityNoCommit(identityUsers);
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                    if (notices != null && notices.Any())
                        this.BantchSaveEntityNoCommit(notices);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                    //if (log != null)
                    //    this.AddEntityNoCommit(log);
                });
                return true;
            });
        }

        /// <summary>
        /// 识别人确认
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("IdentityUpdate")]
        public JsonActionResult<bool> IdentityUpdate([FromBody] T_HM_JOBTASK_IDENTIFY entity)
        {
            return SafeExecute(() =>
            {
                var identity = this.GetEntity<T_HM_JOBTASK_IDENTIFY>(entity.ID, "Nav_IdentifyUser");
                var userId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                var user = this.GetEntity<T_HM_JOBTASK_IDENTIFY_USER>(t => t.JOBTASK_IDENTIFY_ID == identity.ID && t.USER_ID == userId, new BaseFilter(identity.ORG_ID));
                user.DEAL_STATUS = FOUserShiftStatusEnum.已处理;
                //T_HM_OTHER_APPROVE_LOG log = new T_HM_OTHER_APPROVE_LOG();
                //log.USER_ID = userId;
                //log.MAIN_FORM_ID = identity.ID;
                //log.ORG_ID = identity.ORG_ID;
                identity.COMPLETE_DATE = null;
                //查询消息表
                //var task = this.GetEntity<T_FM_NOTIFICATION_TASK>(i => i.SOURCE_DATA_ID == identity.ID && i.USER_ID == userId
                //&& (i.NOTICE_STATUS == FMNoticeStatusEnum.未处理.GetInt() || i.NOTICE_STATUS == FMNoticeStatusEnum.超期办理.GetInt()), false);
                //if (task != null)
                //{
                //    task.NOTICE_STATUS = FMNoticeStatusEnum.正常已办.GetInt();
                //    task.TASK_DT = DateTime.Now;
                //}
                //var userCount = identity != null ? identity.Nav_IdentifyUser.Count() : 0;
                //var userLogCount = this.GetEntities<T_HM_OTHER_APPROVE_LOG>(t => t.MAIN_FORM_ID == identity.ID, new BaseFilter(entity.ORG_ID)).Count();
                //如果识别人数，等于已确认人数+当前确认人数，发消息给第一个审核人
                T_FM_NOTIFICATION_TASK task = null;
                var todoCount = this.GetCount<T_HM_JOBTASK_IDENTIFY_USER>(t => t.JOBTASK_IDENTIFY_ID == identity.ID && t.DEAL_STATUS == 0, new BaseFilter(identity.ORG_ID));
                if (todoCount == 0 || todoCount == 1)
                {
                    identity.IS_PUBLISH = FOPreMeetingStatusEnum.审核中;
                    //创建人部门层级
                    var department = this.GetEntity<T_FM_USER>(t=>t.ID == identity.CREATER_ID && t.ENABLE_STATUS == 0, "Nav_Department");
                    //默认为班组
                    int departmentType = (int)FMDepartmentType.班组;
                    if (department != null && department.Nav_Department != null)
                    {
                        departmentType = department.Nav_Department.DEPARTMENT_TYPE;
                    }
                    var param = Enum.GetName(typeof(FMDepartmentType), departmentType);
                    //取审批流水码
                    var sysFilter = new SystemCodeFilter();
                    sysFilter.CodeType = (int)PFCodeRuleType.审批流编码;
                    sysFilter.Count = 1;
                    sysFilter.OrgId = identity.ORG_ID;
                    var codes = CodeRuleService.NewGenSerial(sysFilter);
                    var serialCode = codes.Split(new char[] { ',' });
                    MFlowPermitService.InsertApprove(serialCode[0], "HM104", param, entity.ID, "HM104_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        if (identity != null)
                            this.UpdateEntityNoCommit(identity);
                        if (user != null)
                            this.UpdateEntityNoCommit(user);
                        //if (log != null)
                        //    this.AddEntityNoCommit(log);
                        if (task != null)
                            this.UpdateEntityNoCommit(task);
                    }, null, null, null, null, null, "HM104_SHOWPRINT", null,"", FMTASKTYPE.Default);
                    return true;
                }
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    task.SOURCE_FORMCODE = "HM104_SHOWPRINT";
                }
                UnifiedCommit(() =>
                {
                    if (identity != null)
                        this.UpdateEntityNoCommit(identity);
                    if (user != null)
                        this.UpdateEntityNoCommit(user);
                    //if (log != null)
                    //    this.AddEntityNoCommit(log);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }

        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("BackUpdate")]
        public JsonActionResult<bool> BackUpdate(string id)
        {
            return SafeExecute(() =>
            {
                return ApproveCallBackService.CallBack("HM/HMJobtaskIdentify/BackUpdate", id);

                //var entity = this.GetEntity<T_HM_JOBTASK_IDENTIFY>(id, false, "Nav_Details", "Nav_Details.Nav_OperationStep", "Nav_Details.Nav_DetailRisk");
                //entity.IS_PUBLISH = FOPreMeetingStatusEnum.归档;
                //entity.COMPLETE_DATE = DateTime.Now;
                ////自动生成作业任务分析表
                //T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE operation = new T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE();
                ////取审批流水码
                //var sysFilter = new SystemCodeFilter();
                //sysFilter.CodeType = (int)PFCodeRuleType.作业任务分析表编号;
                //sysFilter.Count = 1;
                //sysFilter.OrgId = entity.ORG_ID;
                //var codes = CodeRuleService.NewGenSerial(sysFilter);
                //var codeList = codes.Split(new char[] { ',' });
                ////主表
                //operation.CODE = codeList[0];
                //operation.COMPLETION_TIME = DateTime.Now;
                //operation.DEPARTMENT_ID = entity.DEPARTMENT_ID;
                //operation.JOBTASK_IDENTIFY_ID = entity.ID;
                //operation.ORG_ID = entity.ORG_ID;
                //operation.Nav_AnalyzeDetails = null;
                //operation.Nav_AnalyzeFiles = null;
                //operation.Nav_Users = null;
                //operation.IS_AUTO = ISImportantEnum.是;
                //operation.CREATER_ID = entity.CREATER_ID;
                ////明细表
                //List<T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_DETAIL> analyzeDetails = new List<T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_DETAIL>();
                //List<T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_RISK> riskList = new List<T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_RISK>();
                //var details = entity.Nav_Details;
                //if (details != null && details.Any())
                //{
                //    details.ForEach(t =>
                //    {
                //        T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_DETAIL detail = new T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_DETAIL();
                //        detail.AREA_ID = t.AREA_ID;
                //        detail.OPERATION_STEP_ID = t.OPERATION_STEP_ID;
                //        detail.OPERATION_TASK_DISTINGUISH_ANALYZE_ID = operation.ID;
                //        detail.ORG_ID = t.ORG_ID;
                //        detail.CREATER_ID = t.CREATER_ID;
                //        if (t.Nav_DetailRisk != null && t.Nav_DetailRisk.Any())
                //        {
                //            t.Nav_DetailRisk.ForEach(x => {
                //                T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_RISK risk = new T_HM_OPERATION_TASK_DISTINGUISH_ANALYZE_RISK();
                //                risk.EVALUATE_RISK_ID = x.EVALUATE_RISK_ID;
                //                risk.OPERATION_TASK_DISTINGUISH_ANALYZE_DETAIL_ID = detail.ID;
                //                risk.ORG_ID = t.ORG_ID;
                //                riskList.Add(risk);
                //            });
                //        }
                //        detail.Nav_DetailRisk = null;// riskList.Distinct(m=>m.EVALUATE_RISK_ID).ToList();
                //        analyzeDetails.Add(detail);
                //    });
                //}
                ////
                //KeywordFilter filter = new KeywordFilter();
                //filter.Keyword = entity.CREATER_ID.ToString();
                //var detailTemp = FullGet(filter);
                //int count = 0;
                //if (detailTemp.IsSuccessful && detailTemp.Data != null && detailTemp.Data.Nav_Details != null)
                //{
                //    count = detailTemp.Data.Nav_Details.Count();
                //}
                ////发消息通知部门负责人
                //List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                //var chargeUserId = GetChargeUserId(entity.CREATER_ID);
                //if (chargeUserId != null)
                //{
                //    var userIds = new List<Guid>();
                //    userIds.Add((Guid)chargeUserId);
                //    var UserNames = new List<string>();
                //    var users = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIds.Contains(t.ID), new BaseFilter(entity.ORG_ID));
                //    foreach (var user in userIds)
                //    {
                //        var current = users.FirstOrDefault(t => t.ID == user);
                //        UserNames.Add(current?.NAME);
                //    }
                //    //发消息
                //    notices = NotificationTaskService.InsertUserNoticeTaskModels("作业任务分析表", operation.ID, entity.ORG_ID, userIds, UserNames,DateTime.Now,
                //    DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "HM088");
                //    if (count != 0 && details.Count() > count)
                //    {
                //        //如果识别细表有新增数据，则触发风险辨识任务制定表
                //        notices.AddRange(NotificationTaskService.InsertUserNoticeTaskModels("危险源/风险辨识任务", operation.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                //        DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "HM062"));
                //    }
                //}
                //else
                //    this.ThrowError("030016");
                //UnifiedCommit(() =>
                //{
                //    if (entity != null)
                //        this.UpdateEntityNoCommit(entity);
                //    if (operation != null)
                //        this.UpdateEntityNoCommit(operation);
                //    if (analyzeDetails != null && analyzeDetails.Any())
                //        this.BantchSaveEntityNoCommit(analyzeDetails);
                //    if (riskList != null && riskList.Any())
                //        this.BantchSaveEntityNoCommit(riskList);
                //    if (notices != null && notices.Any())
                //        this.BantchSaveEntityNoCommit(notices);
                //});
                //return true;
            });
        }
        /// <summary>
        /// 获取创建人部门负责人ID
        /// <returns></returns>
        private Guid? GetChargeUserId(Guid? id)
        {
            var chargeId = id;
            //var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
            var user = GetEntity<T_FM_USER>(t => t.ID == id && t.ENABLE_STATUS == 0, false, "Nav_Department");
            if (user.Nav_Department.DEPARTMENT_TYPE == (int)FMDepartmentType.部门)
            {
                chargeId = user.Nav_Department.USER_ID;
            }
            else
            {
                chargeId = GetDepartmentId(user.DEPARTMENT_ID);
            }
            return chargeId;
        }

        /// <summary>
        /// 发布给识别人确认
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("PublishToIdentify")]
        public JsonActionResult<bool> PublishToIdentify(Guid id)
        {
            return SafeExecute<bool>(() =>
            {
                T_HM_JOBTASK_IDENTIFY entity = GetEntity<T_HM_JOBTASK_IDENTIFY>(t => t.ID == id, false, "Nav_IdentifyUser");
                List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                //发布给识别人确认
                if (entity.Nav_IdentifyUser != null && entity.Nav_IdentifyUser.Any())
                {
                    //取部门负责人
                    var chargeUserId = GetChargeUserId(entity.CREATER_ID);
                    var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                    //如果当前登录人等于部门负责人，直接发起审批，无需识别人确认
                    if (chargeUserId == loginUserId)
                    {
                        entity.IS_PUBLISH = FOPreMeetingStatusEnum.审核中;
                        //取审批流水码
                        var sysFilter = new SystemCodeFilter();
                        sysFilter.CodeType = (int)PFCodeRuleType.审批流编码;
                        sysFilter.Count = 1;
                        sysFilter.OrgId = entity.ORG_ID;
                        var codes = CodeRuleService.NewGenSerial(sysFilter);
                        var serialCode = codes.Split(new char[] { ',' });
                        MFlowPermitService.InsertApprove(serialCode[0], "HM104", "部门", entity.ID, "HM104_SHOWPRINT", null, true, () =>
                        {
                            if (entity != null)
                                this.UpdateEntityNoCommit(entity);
                            if (notices.Any())
                                this.BantchAddEntityNoCommit(notices);
                        });
                        return true;
                    }
                    else
                    {
                        entity.IS_PUBLISH = FOPreMeetingStatusEnum.签到中;
                        var userIds = entity.Nav_IdentifyUser.Where(x => x.USER_ID != null).Select(t => (Guid)t.USER_ID).Distinct().ToList();
                        var UserNames = new List<string>();
                        var users = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIds.Contains(t.ID), new BaseFilter(entity.ORG_ID));
                        foreach (var user in userIds)
                        {
                            var current = users.FirstOrDefault(t => t.ID == user);
                            UserNames.Add(current?.NAME);
                        }
                        //发消息
                        notices = NotificationTaskService.InsertUserNoticeTaskModels("表单【" + entity.CODE + "】作业风险识别待确认", entity.ID, entity.ORG_ID, userIds, UserNames, DateTime.Now,
                        DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "HM104ShowPrint");
                    }
                }
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (notices.Any())
                        this.BantchAddEntityNoCommit(notices);
                });
                return true;
            });
        }

        /// <summary>
        /// 单条删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("FullDelete")]
        public JsonActionResult<bool> FullDelete(string id)
        {
            return SafeExecute<bool>(() =>
            {
                T_HM_JOBTASK_IDENTIFY entity = GetEntity<T_HM_JOBTASK_IDENTIFY>(t => t.ID.ToString() == id, false, "Nav_IdentifyUser", "Nav_Details", "Nav_Details.Nav_DetailPost", "Nav_Files", "Nav_Details.Nav_DetailRisk");
                List<Guid> userIds = new List<Guid>();
                List<Guid> fileIds = new List<Guid>();
                List<Guid> postIds = new List<Guid>();
                List<Guid> riskIds = new List<Guid>();
                List<Guid> detailIds = new List<Guid>();
                if (entity.Nav_IdentifyUser != null && entity.Nav_IdentifyUser.Any())
                {
                    var userIdList = entity.Nav_IdentifyUser.Select(t => t.ID).ToList();
                    userIds.AddRange(userIdList);
                }
                if (entity.Nav_Files != null && entity.Nav_Files.Any())
                {
                    var fileIdList = entity.Nav_Files.Select(t => t.ID).ToList();
                    fileIds.AddRange(fileIdList);
                }
                if (entity.Nav_Details != null && entity.Nav_Details.Any())
                {
                    var detailIdList = entity.Nav_Details.Select(t => t.ID).ToList();
                    detailIds.AddRange(detailIdList);
                    entity.Nav_Details.ForEach(t =>
                    {
                        if (t.Nav_DetailPost != null && t.Nav_DetailPost.Any())
                        {
                            var postIdList = t.Nav_DetailPost.Select(t => t.ID).ToList();
                            postIds.AddRange(postIdList);
                        }
                        if (t.Nav_DetailRisk != null && t.Nav_DetailRisk.Any())
                        {
                            var riskIdList = t.Nav_DetailRisk.Select(t => t.ID).ToList();
                            riskIds.AddRange(riskIdList);
                        }
                    });
                }
                UnifiedCommit(() =>
                {
                    if (userIds.Any())
                        this.BantchDeleteEntityNoCommit<T_HM_JOBTASK_IDENTIFY_USER>(userIds);
                    if (fileIds.Any())
                        this.BantchDeleteEntityNoCommit<T_HM_JOBTASK_IDENTIFY_FILE>(fileIds);
                    if (postIds.Any())
                        this.BantchDeleteEntityNoCommit<T_HM_JOBTASK_IDENTIFY_DETAIL_POST>(postIds);
                    if (riskIds.Any())
                        this.BantchDeleteEntityNoCommit<T_HM_JOBTASK_IDENTIFY_DETAIL_RISK>(riskIds);
                    if (detailIds.Any())
                        this.BantchDeleteEntityNoCommit<T_HM_JOBTASK_IDENTIFY_DETAIL>(detailIds);
                    if (entity != null)
                        this.DeleteEntityNoCommit(entity);
                });
                return true;
            });
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetEdit")]
        public JsonActionResult<T_HM_JOBTASK_IDENTIFY> GetEdit([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() => {
                //filter.Include.Clear();
                //filter.Include.Add("Nav_CreateUser");
                //filter.Include.Add("Nav_CreateUser.Nav_Department");
                //filter.Include.Add("Nav_Department");
                //filter.Include.Add("Nav_IdentifyUser");
                //filter.Include.Add("Nav_IdentifyUser.Nav_User");
                //filter.Include.Add("Nav_IdentifyUser.Nav_User.Nav_UserSignFiles");
                //filter.Include.Add("Nav_IdentifyUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File");
                //filter.Include.Add("Nav_Files");
                //filter.Include.Add("Nav_Files.Nav_ImgFile");
                //filter.Include.Add("Nav_Files.Nav_ImgFile.Nav_File");
                //filter.Include.Add("Nav_Details");
                //filter.Include.Add("Nav_Details.Nav_Area");
                //filter.Include.Add("Nav_Details.Nav_OperationStep");
                //filter.Include.Add("Nav_Details.Nav_DetailPost");
                //filter.Include.Add("Nav_Details.Nav_DetailPost.Nav_Post");
                //filter.Include.Add("Nav_Details.Nav_DetailRisk");
                //filter.Include.Add("Nav_Details.Nav_DetailRisk.Nav_EvaluateRisk");
                //var result = WitEntity(null, filter);
                var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
                if (string.IsNullOrEmpty(id))
                    this.ThrowError("030017");
                var result = this.GetEntity<T_HM_JOBTASK_IDENTIFY>(id, new string[] { "Nav_CreateUser", "Nav_CreateUser.Nav_Department",
            "Nav_IdentifyUser","Nav_IdentifyUser.Nav_User","Nav_IdentifyUser.Nav_User.Nav_UserSignFiles",
                "Nav_IdentifyUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File","Nav_Files","Nav_Files.Nav_ImgFile","Nav_Files.Nav_ImgFile.Nav_File",
            "Nav_Details","Nav_Details.Nav_Area","Nav_Details.Nav_OperationStep","Nav_Details.Nav_DetailPost","Nav_Details.Nav_DetailPost.Nav_Post"
                });//"Nav_Details.Nav_DetailRisk","Nav_Details.Nav_DetailRisk.Nav_EvaluateRisk"
                if (result != null && result.Nav_Details.Any())
                {
                    result.Nav_Details.ForEach(t =>
                    {
                        if (t.Nav_DetailPost != null && t.Nav_DetailPost.Any())
                        {
                            var nameList = t.Nav_DetailPost.Select(x => x.Nav_Post.NAME).ToList();
                            t.DetailPost = string.Join(",", nameList);
                            t.Nav_DetailPost = t.Nav_DetailPost.OrderBy(m => m.Nav_Post?.NAME).ToList();
                        }
                        t.OperationStepName = t.Nav_OperationStep?.NAME;
                    });
                    result.Nav_Details = result.Nav_Details.OrderBy(t => t.OperationStepName).ToList();
                    result.Nav_IdentifyUser = result.Nav_IdentifyUser?.OrderBy(t => t.MODIFY_TIME).ThenByDescending(m => m.DEAL_STATUS).ToList();
                    if (result.IS_PUBLISH == FOPreMeetingStatusEnum.审批拒绝)
                    {
                        result.CONTEXT = ApproveCallBackService.RejectContent(result.ID);
                    }
                }
                return result;
            });
        }
        private Guid? GetDepartmentId(Guid? departmentId)
        {
            var result = new Guid?();
            var department = GetEntity<T_FM_DEPARTMENT>(t => t.ID == departmentId);
            if (department.DEPARTMENT_TYPE != (int)FMDepartmentType.部门)
            {
                GetDepartmentId(department.PARENT_ID);
            }
            else
            {
                result = department.USER_ID;
            }
            return result;
        }

        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullOrderPaged")]
        public PagedActionResult<T_HM_JOBTASK_IDENTIFY> FullOrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            var result = new PagedActionResult<T_HM_JOBTASK_IDENTIFY>();
            var loginDepartmentId = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
            var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
            var loginUserCode = APT.Infrastructure.Api.AppContext.CurrentSession.UserCode;
            pageFilter.IgnoreDataRule = true;
            //安环部负责人departmentID
            var manageDepartId = this.GetEntity<T_FM_USER>(t=>t.Nav_ApproveRole!=null && t.Nav_ApproveRole.NAME == "安环部负责人" && t.ENABLE_STATUS == 0)?.DEPARTMENT_ID;
            if (loginUserCode == "admin" || loginDepartmentId == manageDepartId)
            {
                result = this.GetOrderPageEntities<T_HM_JOBTASK_IDENTIFY>(null,pageFilter);
            }
            else
            {
                List<Guid> departmentId = new List<Guid>() { loginDepartmentId.Value };
                List<Guid> departmentIds = new List<Guid>() { loginDepartmentId.Value };
                DepartmentService.GetDepartmentIds(pageFilter.OrgId.Value,departmentId, ref departmentIds);
                if (departmentIds != null && departmentIds.Any())
                {
                    result = this.GetOrderPageEntities<T_HM_JOBTASK_IDENTIFY>(t => (t.DEPARTMENT_ID != null && departmentIds.Contains(t.DEPARTMENT_ID.Value)), pageFilter);//|| dataIds.Contains(t.ID)
                    if (result.TotalCount > 0)
                    {
                        result.Data.ForEach(t =>
                        {
                            if (t.Nav_CreateUser.ID == loginUserId && t.IS_PUBLISH == 0)
                            {
                                t.PUBLISH = "true";
                            }
                        });
                    }
                }
                else
                    result.Data = null;
            }
            return result;
        }
        /// <summary>
        /// </summary>
        /// 返回所有下级部门节点
        /// <returns></returns>
        private void GetDepartmentId(List<Guid> departmentId, ref List<Guid> departmentIds)
        {
            var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
            departmentIds.AddRange(departmentId);
            var department = GetEntities<T_FM_DEPARTMENT>(t => t.PARENT_ID != null && departmentId.Contains((Guid)t.PARENT_ID), new BaseFilter(orgId));
            if (department != null && department.Any())
            {
                var ids = department.Select(t => t.ID).ToList();
                GetDepartmentId(ids, ref departmentIds);
            }
        }
        /// <summary>
        /// 驳回
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("RejectUpdate")]
        public JsonActionResult<bool> RejectUpdate([FromBody] T_PF_APPROVE model)
        {
            return SafeExecute(() =>
            {
                //公共 获取审批流信息
                T_PF_APPROVE modelApp = null;
                List<T_PF_APPROVE_DETAIL> listAppDetail = null;
                T_FM_NOTIFICATION_TASK taskFinish = null;
                string Msg = string.Empty;
                bool ResultGetInfo = ApproveCallBackService.GetApproject(model, ref modelApp, ref listAppDetail, ref taskFinish, ref Msg);
                if (!ResultGetInfo)
                    throw new Exception("驳回失败！");

                if (modelApp == null || listAppDetail == null)
                    throw new Exception("获取驳回信息失败！");
                var entity = this.GetEntity<T_HM_JOBTASK_IDENTIFY>(model.DATA_ID, new string[] { "Nav_IdentifyUser.Nav_User", "Nav_CreateUser" });
                entity.IS_PUBLISH = FOPreMeetingStatusEnum.审批拒绝;
                T_FM_NOTIFICATION_TASK notice = new T_FM_NOTIFICATION_TASK();
                if (entity.Nav_IdentifyUser.Any())
                {
                    //第一个识别人
                    var user = entity.Nav_IdentifyUser.FirstOrDefault(t => t.IS_FIRST == true);
                    if (user == null)//忘记录第一个时，随机取一个
                        user = entity.Nav_IdentifyUser.OrderBy(t => t.CREATE_TIME).FirstOrDefault();
                    //发消息
                    notice = NotificationTaskService.InsertUserNoticeTaskModel("作业任务识别表已被驳回", entity.ID, entity.ORG_ID, (Guid)user.USER_ID, user.Nav_User.NAME, DateTime.Now,
                    DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "HM104");
                }
                else
                {
                    //没有识别人，驳回给创建人
                    notice = NotificationTaskService.InsertUserNoticeTaskModel("作业任务识别表已被驳回", entity.ID, entity.ORG_ID, (Guid)entity.CREATER_ID, entity.Nav_CreateUser.NAME, DateTime.Now,
                    DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "HM104");
                }
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (notice != null)
                        this.UpdateEntityNoCommit(notice);
                    if (modelApp != null)
                        UpdateEntityNoCommit(modelApp);
                    if (listAppDetail != null && listAppDetail.Count > 0)
                        BantchUpdateEntityNoCommit(listAppDetail);
                    if (taskFinish != null)
                        UpdateEntityNoCommit(taskFinish);
                });
                return true;
                //return ApproveCallBackService.CallReject("HM/HMJobtaskIdentify/RejectUpdate", id);
            });
        }
        /// <summary>
        /// 归档
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("ArchiveUpdate")]
        public JsonActionResult<bool> ArchiveUpdate(string id)
        {
            return SafeExecute(() =>
            {
                var entity = this.GetEntity<T_HM_JOBTASK_IDENTIFY>(id);
                entity.IS_PUBLISH = FOPreMeetingStatusEnum.归档;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                });
                return true;
            });
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetRiskPost")]
        public JsonActionResult<T_HM_JOBTASK_IDENTIFY_DETAIL> GetRiskPost([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() => {
                var result = new T_HM_JOBTASK_IDENTIFY_DETAIL();
                //var departmentId = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentId;&& !string.IsNullOrEmpty(filter.Parameter1) && !string.IsNullOrEmpty(departmentId)
                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    var link = this.GetEntity<T_HM_OPERATION_LINK>(t => t.OPERATION_STEP_ID == Guid.Parse(filter.Keyword),"Nav_LinkPost");
                    if (link != null)
                    {
                        if (link.Nav_LinkPost != null && link.Nav_LinkPost.Any())
                        {
                            List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST> detailPosts = new List<T_HM_JOBTASK_IDENTIFY_DETAIL_POST>();
                            foreach (var post in link.Nav_LinkPost)
                            {
                                T_HM_JOBTASK_IDENTIFY_DETAIL_POST detailPost = new T_HM_JOBTASK_IDENTIFY_DETAIL_POST();
                                detailPost.POST_ID = post.POST_ID;
                                detailPost.Nav_Post = post.Nav_Post;
                                detailPosts.Add(detailPost);
                            }
                            result.Nav_DetailPost = detailPosts.Distinct(t => t.POST_ID).ToList();
                        }
                        result.CYCLE_TYPE = link.CYCLE_TYPE;
                    }
                }
                return result;
            });
        }
    }
}
