using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Services.DomainServices;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SC.PT;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Domain.IServices;
using APT.MS.Domain.Enums;
using APT.MS.Domain.Entities.SC;
using APT.MS.Domain.Entities.HM;
using Microsoft.AspNetCore.Identity;
using MySqlX.XDevAPI.Common;
using Microsoft.CodeAnalysis.Differencing;
using APT.BaseData.Domain.ApiModel;

namespace APT.SC.WebApi.Controllers.Api.PTController
{
    /// <summary>
    ///  部门、车间、班组安全生产目标与指标制定表
    /// </summary>
    [Route("api/PT/PTIndicatorFormulationDepartment")]
    public partial class IndicatorFormulationDepartmentController : AuthorizeApiController<T_PT_INDICATOR_FORMULATION_DEPARTMENT>
    {
        IPFCodeRuleService CodeRuleService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        IFMDepartmentService DepartmentService { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="notificationTaskService"></param>
        public IndicatorFormulationDepartmentController(IPFCodeRuleService codeRuleService, IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService, IFMDepartmentService departmentService)
        {
            CodeRuleService = codeRuleService;
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
            DepartmentService = departmentService;
        }
        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PT_INDICATOR_FORMULATION_DEPARTMENT entity)
        {
            return SafeExecute(() =>
            {
                var departments = entity.Nav_DepartmentDetail;
                if (departments != null && departments.Any())
                {
                    departments = departments.Where(t => !t.IS_DELETED).ToList();
                }
                var teams = entity.Nav_TeamDetail;
                if (teams != null && teams.Any())
                {
                    teams = teams.Where(t => !t.IS_DELETED).ToList();
                }
                var files = entity.Nav_Files;
                if (files != null && files.Any())
                {
                    files = files.Where(t => !t.IS_DELETED).ToList();
                }
                entity.STATUS = PFStandardStatus.Draft;
                entity.YEAR = DateTime.Now.Year.ToString();
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.TaskID != Guid.Empty)
                {
                    var currTask = GetEntity<T_FM_NOTIFICATION_TASK>(t => t.ID == entity.TaskID);
                    if (currTask != null)
                    {
                        loginUserId = currTask.USER_ID;
                    }
                }
                entity.USER_ID = loginUserId;
                //var department = this.GetEntity<T_FM_USER>(t => t.ID == loginUserId.Value && t.ENABLE_STATUS == 0, "Nav_Department");
                entity.DEPARTMENT_ID = this.GetEntity<T_FM_USER>(t => t.ENABLE_STATUS == 0 && t.ID == (Guid)loginUserId)?.DEPARTMENT_ID;
                //entity.DEPARTMENT_TYPE = entity.DEPARTMENT_TYPE!=0? entity.DEPARTMENT_TYPE : FMDepartmentType.部门;
                List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT> contents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT> teamContents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT>();
                T_FM_NOTIFICATION_TASK task = null;
                if (departments != null && departments.Any())
                {
                    departments.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_DEPARTMENT_ID = entity.ID;
                        if (t.Nav_DetailContent != null && t.Nav_DetailContent.Any())
                        {
                            t.Nav_DetailContent.ForEach(t1 =>
                            {
                                t1.ORG_ID = entity.ORG_ID;
                                t1.INDICATOR_FORMULATION_DEPARTMENT_DETAIL_ID = t.ID;
                                contents.Add(t1);
                            });
                        }
                        else
                            throw new Exception("目标与指标未全部填写");
                        t.Nav_DetailContent = null;
                    });
                }
                if (teams != null && teams.Any())
                {
                    teams.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_DEPARTMENT_ID = entity.ID;
                        if (t.Nav_DetailContent != null && t.Nav_DetailContent.Any())
                        {
                            t.Nav_DetailContent.ForEach(t1 =>
                            {
                                t1.ORG_ID = entity.ORG_ID;
                                t1.INDICATOR_FORMULATION_DEPARTMENT_TEAM_ID = t.ID;
                                teamContents.Add(t1);
                            });
                        }
                        else
                            throw new Exception("目标与指标未全部填写");
                        t.Nav_DetailContent = null;
                    });
                }
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    entity.STATUS = PFStandardStatus.Approving;
                    entity.Nav_DepartmentDetail = null;
                    entity.Nav_TeamDetail = null;
                    //取审批流水码
                    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), entity.DEPARTMENT_TYPE);
                    MFlowPermitService.InsertApprove(serialCode[0], "PT020", param, entity.ID, "PT020_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        if (entity != null)
                            this.UpdateEntityNoCommit(entity);
                        if (departments != null && departments.Any())
                            this.BantchSaveEntityNoCommit(departments);
                        if (contents != null && contents.Any())
                            this.BantchSaveEntityNoCommit(contents);
                        if (teams != null && teams.Any())
                            this.BantchSaveEntityNoCommit(teams);
                        if (teamContents != null && teamContents.Any())
                            this.BantchSaveEntityNoCommit(teamContents);
                        if (task != null)
                            this.UpdateEntityNoCommit(task);
                        if (notices != null && notices.Any())
                            this.BantchSaveEntityNoCommit(notices);
                    }, null, null, null, null, null, "PT020_SHOWPRINT", null);
                    return true;
                    //if (entity.TaskID != Guid.Empty)
                    //{
                    //    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    //}
                }
                entity.Nav_DepartmentDetail = null;
                entity.Nav_TeamDetail = null;
                entity.Nav_Files = null;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (departments != null && departments.Any())
                        this.BantchSaveEntityNoCommit(departments);
                    if (contents != null && contents.Any())
                        this.BantchSaveEntityNoCommit(contents);
                    if (teams != null && teams.Any())
                        this.BantchSaveEntityNoCommit(teams);
                    if (teamContents != null && teamContents.Any())
                        this.BantchSaveEntityNoCommit(teamContents);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                    if (notices != null && notices.Any())
                        this.BantchSaveEntityNoCommit(notices);
                    //保存新的
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                });
                return true;
            });
        }

        /// <summary>
        /// 已阅
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("IdentityUpdate")]
        public JsonActionResult<bool> IdentityUpdate([FromBody] T_PT_INDICATOR_FORMULATION_DEPARTMENT entity)
        {
            return SafeExecute(() =>
            {
                var formulationDepartment = this.GetEntity<T_PT_INDICATOR_FORMULATION_DEPARTMENT>(entity.ID);
                //发起车间、班组、个人目标与指标
                T_PT_INDICATOR_FORMULATION_DEPARTMENT department = null;
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.TaskID != Guid.Empty)
                {
                    var currTask = GetEntity<T_FM_NOTIFICATION_TASK>(t => t.ID == entity.TaskID);
                    if (currTask != null)
                    {
                        loginUserId = currTask.USER_ID;
                    }
                }
                var loginUser = this.GetEntity<T_FM_USER>(t => t.ID == loginUserId.Value && t.ENABLE_STATUS == 0, "Nav_Department", "Nav_ApproveRole");
                var name = ""; var type = 0; var entityDepartment = FMDepartmentType.Company;
                switch (formulationDepartment.DEPARTMENT_TYPE)
                {
                    case FMDepartmentType.Company:
                        entityDepartment = FMDepartmentType.Department;
                        name = "车间"; type = 1;
                        break;
                    case FMDepartmentType.Department:
                        entityDepartment = FMDepartmentType.Workshop;
                        name = "班组"; type = 2;
                        break;
                    case FMDepartmentType.Workshop:
                        entityDepartment = FMDepartmentType.Class;
                        name = "个人";
                        break;
                    case FMDepartmentType.Class:
                        break;
                    default:
                        name = "";
                        break;
                }
                var departmentName = Enum.GetName(typeof(FMDepartmentType), entityDepartment);
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL> detailList = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM> teamDetailList = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT> departmentContents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT> teamContents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT>();
                //触发考核方案审核表
                T_PT_ASSESSMENT_PLAN_AUDIT audit = null;
                List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL> auditDetails = new List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL>();
                List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT> contents = new List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT>();
                List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                if (((int)formulationDepartment.DEPARTMENT_TYPE != loginUser.Nav_Department.DEPARTMENT_TYPE && loginUser.Nav_ApproveRole != null && loginUser.Nav_ApproveRole.NAME.Contains("负责人")) || ((int)formulationDepartment.DEPARTMENT_TYPE == loginUser.Nav_Department.DEPARTMENT_TYPE && formulationDepartment.DEPARTMENT_TYPE == FMDepartmentType.Class))
                {
                    department = new T_PT_INDICATOR_FORMULATION_DEPARTMENT();
                    department.STATUS = PFStandardStatus.Draft;
                    department.ORG_ID = formulationDepartment.ORG_ID;
                    department.YEAR = formulationDepartment.YEAR;
                    department.SET_TIME = DateTime.Now;
                    department.USER_ID = loginUserId;
                    department.DEPARTMENT_ID = loginUser?.DEPARTMENT_ID;
                    department.DEPARTMENT_TYPE = entityDepartment;
                    audit = new T_PT_ASSESSMENT_PLAN_AUDIT();
                    audit.ORG_ID = formulationDepartment.ORG_ID;
                    audit.DEPARTMENT_TYPE = entityDepartment;
                    audit.STATUS = PFStandardStatus.Draft;
                    audit.YEAR = formulationDepartment.YEAR;
                    audit.USER_ID = loginUserId;
                    audit.DEPARTMENT_ID = loginUser?.DEPARTMENT_ID;
                    audit.SET_TIME = DateTime.Now;
                    if (name == "")
                    {
                        department = null;
                        audit = null;
                        formulationDepartment.STATUS = PFStandardStatus.Archived;
                    }
                    else if (name == "个人")
                    {
                        var userIdLists = this.GetEntities<T_FM_USER>(t => t.DEPARTMENT_ID == loginUser.DEPARTMENT_ID && (t.Nav_ApproveRole == null || (t.Nav_ApproveRole != null && !t.Nav_ApproveRole.NAME.Contains("副总") && !t.Nav_ApproveRole.NAME.Contains("总经理") && !t.Nav_ApproveRole.NAME.Contains("负责人"))) && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && !t.NAME.Contains("管理员"), new BaseFilter(formulationDepartment.ORG_ID)).ToList();
                        if (userIdLists != null && userIdLists.Any())
                        {
                            userIdLists.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.USER_ID = t.ID;
                                teamDetailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t.DEPARTMENT_ID;
                                auditDetail.USER_ID = t.ID;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var departIds = userIdLists.Select(t => t.DEPARTMENT_ID).ToList();
                        var userIds = userIdLists.Select(t => t.ID).ToList();
                        var tempDetails = this.GetEntities<T_PT_ASSESSMENT_PLAN_AUDIT_TEMP_DETAIL>(t => departIds.Contains(t.DEPARTMENT_ID.Value) || userIds.Contains(t.USER_ID.Value), new BaseFilter(entity.ORG_ID), new string[] { "Nav_Contents" }).ToList();
                        if (teamDetailList != null && teamDetailList.Any())
                        {
                            foreach (var auditDs in teamDetailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_TEAM_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            teamContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (auditDetails != null && auditDetails.Any())
                        {
                            foreach (var auditDs in auditDetails)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    auditDs.ASSESSMENT_CYCLE = tempCurrent.ASSESSMENT_CYCLE;
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT content = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.PLAN_AUDIT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.SCORE = t.SCORE;
                                            content.METHOD = t.METHOD;
                                            contents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (departIds.Count() == 0 && userIds.Count() == 0)
                        { department = null; audit = null; }
                        else
                        {
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(name + "目标与指标分解", department.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT025"));//userIds, userNames,
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(departmentName + "考核方案审核", audit.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT024"));//userIds, userNames,
                        }
                    }
                    else
                    {
                        var departInfos = this.GetEntities<T_FM_DEPARTMENT>(t => t.DEPARTMENT_TYPE == type && t.PARENT_ID == loginUser.DEPARTMENT_ID && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, new BaseFilter(formulationDepartment.ORG_ID));
                        var departIds = departInfos.Select(m => m.ID).ToList();
                        if (departIds != null && departIds.Any())
                        {
                            departIds.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.DEPARTMENT_ID = t;
                                detailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var chargeIds = departInfos.Select(t => t.USER_ID).Distinct().ToList();
                        var userIdLists = this.GetEntities<T_FM_USER>(t => (t.DEPARTMENT_ID == loginUser.DEPARTMENT_ID && (t.Nav_ApproveRole == null || (t.Nav_ApproveRole != null && !t.Nav_ApproveRole.NAME.Contains("副总") && !t.Nav_ApproveRole.NAME.Contains("总经理") && !t.Nav_ApproveRole.NAME.Contains("负责人"))) && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && !t.NAME.Contains("管理员")) || chargeIds.Contains(t.ID), new BaseFilter(formulationDepartment.ORG_ID)).ToList();
                        if (userIdLists != null && userIdLists.Any())
                        {
                            userIdLists.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.USER_ID = t.ID;
                                teamDetailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t.DEPARTMENT_ID;
                                auditDetail.USER_ID = t.ID;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var userIds = userIdLists.Select(t => t.ID).ToList();
                        var tempDetails = this.GetEntities<T_PT_ASSESSMENT_PLAN_AUDIT_TEMP_DETAIL>(t => departIds.Contains(t.DEPARTMENT_ID.Value) || userIds.Contains(t.USER_ID.Value), new BaseFilter(entity.ORG_ID), new string[] { "Nav_Contents" }).ToList();
                        if (detailList != null && detailList.Any())
                        {
                            foreach (var auditDs in detailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == null);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            departmentContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (teamDetailList != null && teamDetailList.Any())
                        {
                            foreach (var auditDs in teamDetailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_TEAM_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            teamContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (auditDetails != null && auditDetails.Any())
                        {
                            foreach (var auditDs in auditDetails)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    auditDs.ASSESSMENT_CYCLE = tempCurrent.ASSESSMENT_CYCLE;
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT content = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.PLAN_AUDIT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.SCORE = t.SCORE;
                                            content.METHOD = t.METHOD;
                                            contents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (departIds.Count() == 0 && userIds.Count() == 0)
                        { department = null; audit = null; }
                        else
                        {
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(name + "目标与指标分解", department.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                         DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT020"));//userIds, userNames,
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(departmentName + "考核方案审核", audit.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT024"));//userIds, userNames,
                        }
                    }
                }

                //发消息
                //var userIds = new List<Guid>();
                //userIds.Add(loginUser.ID);
                //var userNames = new List<string>();
                //var user = this.GetEntity<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIds.Contains(t.ID));
                //userNames.Add(user?.NAME);

                T_FM_NOTIFICATION_TASK task = null;
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    task.SOURCE_FORMCODE = "PT020_SHOWPRINT";
                }
                UnifiedCommit(() =>
                {
                    if (formulationDepartment != null)
                        this.UpdateEntityNoCommit(formulationDepartment);
                    if (department != null)
                        this.UpdateEntityNoCommit(department);
                    if (detailList != null && detailList.Any())
                        this.BantchSaveEntityNoCommit(detailList);
                    if (teamDetailList != null && teamDetailList.Any())
                        this.BantchSaveEntityNoCommit(teamDetailList);
                    if (departmentContents != null && departmentContents.Any())
                        this.BantchSaveEntityNoCommit(departmentContents);
                    if (teamContents != null && teamContents.Any())
                        this.BantchSaveEntityNoCommit(teamContents);
                    if (audit != null)
                        this.UpdateEntityNoCommit(audit);
                    if (auditDetails != null && auditDetails.Any())
                        this.BantchSaveEntityNoCommit(auditDetails);
                    if (contents != null && contents.Any())
                        this.BantchSaveEntityNoCommit(contents);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                    if (notices != null && notices.Any())
                        this.BantchSaveEntityNoCommit(notices);
                });
                return true;
            });
        }

        /// <summary>
        /// 已阅(豫鹭)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("IdentityUpdateYL")]
        public JsonActionResult<bool> IdentityUpdateYL([FromBody] T_PT_INDICATOR_FORMULATION_DEPARTMENT entity)
        {
            return SafeExecute(() =>
            {
                var formulationDepartment = this.GetEntity<T_PT_INDICATOR_FORMULATION_DEPARTMENT>(entity.ID);
                //发起车间、班组、个人目标与指标
                T_PT_INDICATOR_FORMULATION_DEPARTMENT department = null;
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.TaskID != Guid.Empty)
                {
                    var currTask = GetEntity<T_FM_NOTIFICATION_TASK>(t => t.ID == entity.TaskID);
                    if (currTask != null)
                    {
                        loginUserId = currTask.USER_ID;
                    }
                }
                var loginUser = this.GetEntity<T_FM_USER>(t => t.ID == loginUserId.Value && t.ENABLE_STATUS == 0, "Nav_Department", "Nav_ApproveRole");
                var name = ""; var type = 0; var entityDepartment = FMDepartmentType.Company;
                switch (formulationDepartment.DEPARTMENT_TYPE)
                {
                    case FMDepartmentType.Company:
                        entityDepartment = FMDepartmentType.Department;
                        name = "班组"; type = 2;
                        break;
                    case FMDepartmentType.Department:
                        entityDepartment = FMDepartmentType.Class;
                        name = "个人";
                        break;
                    case FMDepartmentType.Class:
                        break;
                    default:
                        name = "";
                        break;
                }
                var departmentName = Enum.GetName(typeof(FMDepartmentType), entityDepartment);
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL> detailList = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM> teamDetailList = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT> departmentContents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT>();
                List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT> teamContents = new List<T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT>();
                //触发考核方案审核表
                T_PT_ASSESSMENT_PLAN_AUDIT audit = null;
                List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL> auditDetails = new List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL>();
                List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT> contents = new List<T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT>();
                List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
                if (((int)formulationDepartment.DEPARTMENT_TYPE != loginUser.Nav_Department.DEPARTMENT_TYPE && loginUser.Nav_ApproveRole != null && loginUser.Nav_ApproveRole.NAME.Contains("负责人")) || ((int)formulationDepartment.DEPARTMENT_TYPE == loginUser.Nav_Department.DEPARTMENT_TYPE && formulationDepartment.DEPARTMENT_TYPE == FMDepartmentType.Class))
                {
                    department = new T_PT_INDICATOR_FORMULATION_DEPARTMENT();
                    department.STATUS = PFStandardStatus.Draft;
                    department.ORG_ID = formulationDepartment.ORG_ID;
                    department.YEAR = formulationDepartment.YEAR;
                    department.SET_TIME = DateTime.Now;
                    department.USER_ID = loginUserId;
                    department.DEPARTMENT_ID = loginUser?.DEPARTMENT_ID;
                    department.DEPARTMENT_TYPE = entityDepartment;
                    audit = new T_PT_ASSESSMENT_PLAN_AUDIT();
                    audit.ORG_ID = formulationDepartment.ORG_ID;
                    audit.DEPARTMENT_TYPE = entityDepartment;
                    audit.STATUS = PFStandardStatus.Draft;
                    audit.YEAR = formulationDepartment.YEAR;
                    audit.USER_ID = loginUserId;
                    audit.DEPARTMENT_ID = loginUser?.DEPARTMENT_ID;
                    audit.SET_TIME = DateTime.Now;
                    if (name == "")
                    {
                        department = null;
                        audit = null;
                        formulationDepartment.STATUS = PFStandardStatus.Archived;
                    }
                    else if (name == "个人")
                    {
                        var userIdLists = this.GetEntities<T_FM_USER>(t => t.DEPARTMENT_ID == loginUser.DEPARTMENT_ID && (t.Nav_ApproveRole == null || (t.Nav_ApproveRole != null && !t.Nav_ApproveRole.NAME.Contains("副总") && !t.Nav_ApproveRole.NAME.Contains("总经理") && !t.Nav_ApproveRole.NAME.Contains("负责人"))) && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && !t.NAME.Contains("管理员"), new BaseFilter(formulationDepartment.ORG_ID)).ToList();
                        if (userIdLists != null && userIdLists.Any())
                        {
                            userIdLists.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.USER_ID = t.ID;
                                teamDetailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t.DEPARTMENT_ID;
                                auditDetail.USER_ID = t.ID;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var departIds = userIdLists.Select(t => t.DEPARTMENT_ID).ToList();
                        var userIds = userIdLists.Select(t => t.ID).ToList();
                        var tempDetails = this.GetEntities<T_PT_ASSESSMENT_PLAN_AUDIT_TEMP_DETAIL>(t => departIds.Contains(t.DEPARTMENT_ID.Value) || userIds.Contains(t.USER_ID.Value), new BaseFilter(entity.ORG_ID), new string[] { "Nav_Contents" }).ToList();
                        if (teamDetailList != null && teamDetailList.Any())
                        {
                            foreach (var auditDs in teamDetailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_TEAM_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            teamContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (auditDetails != null && auditDetails.Any())
                        {
                            foreach (var auditDs in auditDetails)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    auditDs.ASSESSMENT_CYCLE = tempCurrent.ASSESSMENT_CYCLE;
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT content = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.PLAN_AUDIT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.SCORE = t.SCORE;
                                            content.METHOD = t.METHOD;
                                            contents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (departIds.Count() == 0 && userIds.Count() == 0)
                        { department = null; audit = null; }
                        else
                        {
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(name + "目标与指标分解", department.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT025"));//userIds, userNames,
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(departmentName + "考核方案审核", audit.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT024"));//userIds, userNames,
                        }
                    }
                    else
                    {
                        //取部门下的所有车间
                        List<Guid> departmentId = new List<Guid>() { loginUser.DEPARTMENT_ID.Value };
                        List<Guid> departmentIds = new List<Guid>();
                        DepartmentService.GetDepartmentIds(formulationDepartment.ORG_ID.Value, departmentId, ref departmentIds);
                        var departInfos = this.GetEntities<T_FM_DEPARTMENT>(t => t.DEPARTMENT_TYPE == type && departmentIds.Contains(t.PARENT_ID.Value) && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, new BaseFilter(formulationDepartment.ORG_ID));
                        var departIds = departInfos.Select(m => m.ID).ToList();
                        if (departIds != null && departIds.Any())
                        {
                            departIds.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.DEPARTMENT_ID = t;
                                detailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var chargeIds = departInfos.Select(t => t.USER_ID).Distinct().ToList();
                        var userIdLists = this.GetEntities<T_FM_USER>(t => (t.DEPARTMENT_ID == loginUser.DEPARTMENT_ID && (t.Nav_ApproveRole == null || (t.Nav_ApproveRole != null && !t.Nav_ApproveRole.NAME.Contains("副总") && !t.Nav_ApproveRole.NAME.Contains("总经理") && !t.Nav_ApproveRole.NAME.Contains("负责人"))) && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && !t.NAME.Contains("管理员")) || chargeIds.Contains(t.ID), new BaseFilter(formulationDepartment.ORG_ID)).ToList();
                        if (userIdLists != null && userIdLists.Any())
                        {
                            userIdLists.ForEach(t =>
                            {
                                T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM detail = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM();
                                detail.ORG_ID = formulationDepartment.ORG_ID;
                                detail.INDICATOR_FORMULATION_DEPARTMENT_ID = department.ID;
                                detail.USER_ID = t.ID;
                                teamDetailList.Add(detail);
                                //细表
                                T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL auditDetail = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL();
                                auditDetail.ORG_ID = audit.ORG_ID;
                                auditDetail.DEPARTMENT_ID = t.DEPARTMENT_ID;
                                auditDetail.USER_ID = t.ID;
                                auditDetail.ASSESSMENT_PLAN_ID = audit.ID;
                                auditDetail.YEAR = formulationDepartment.YEAR;
                                auditDetails.Add(auditDetail);
                            });
                        }
                        var userIds = userIdLists.Select(t => t.ID).ToList();
                        var tempDetails = this.GetEntities<T_PT_ASSESSMENT_PLAN_AUDIT_TEMP_DETAIL>(t => departIds.Contains(t.DEPARTMENT_ID.Value) || userIds.Contains(t.USER_ID.Value), new BaseFilter(entity.ORG_ID), new string[] { "Nav_Contents" }).ToList();
                        if (detailList != null && detailList.Any())
                        {
                            foreach (var auditDs in detailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == null);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            departmentContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (teamDetailList != null && teamDetailList.Any())
                        {
                            foreach (var auditDs in teamDetailList)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT content = new T_PT_INDICATOR_FORMULATION_DEPARTMENT_TEAM_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.INDICATOR_FORMULATION_DEPARTMENT_TEAM_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.ROW_NO = t.ROW_NO;
                                            teamContents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (auditDetails != null && auditDetails.Any())
                        {
                            foreach (var auditDs in auditDetails)
                            {
                                var tempCurrent = tempDetails.FirstOrDefault(t => t.DEPARTMENT_ID == auditDs.DEPARTMENT_ID && t.USER_ID == auditDs.USER_ID);
                                if (tempCurrent != null)
                                {
                                    auditDs.ASSESSMENT_CYCLE = tempCurrent.ASSESSMENT_CYCLE;
                                    if (tempCurrent.Nav_Contents != null && tempCurrent.Nav_Contents.Any())
                                    {
                                        tempCurrent.Nav_Contents.ForEach(t =>
                                        {
                                            T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT content = new T_PT_ASSESSMENT_PLAN_AUDIT_DETAIL_CONTENT();
                                            content.ORG_ID = t.ORG_ID;
                                            content.PLAN_AUDIT_DETAIL_ID = auditDs.ID;
                                            content.NAME = t.NAME;
                                            content.SCORE = t.SCORE;
                                            content.METHOD = t.METHOD;
                                            contents.Add(content);
                                        });
                                    }
                                }
                            }
                        }
                        if (departIds.Count() == 0 && userIds.Count() == 0)
                        { department = null; audit = null; }
                        else
                        {
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(name + "目标与指标分解", department.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                         DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT020"));//userIds, userNames,
                            notices.Add(NotificationTaskService.InsertUserNoticeTaskModel(departmentName + "考核方案审核", audit.ID, formulationDepartment.ORG_ID, loginUser.ID, loginUser.NAME, DateTime.Now,
                        DateTime.Now.AddDays(5), (int)FMNoticeTypeEnum.消息, "PT024"));//userIds, userNames,
                        }
                    }
                }

                //发消息
                //var userIds = new List<Guid>();
                //userIds.Add(loginUser.ID);
                //var userNames = new List<string>();
                //var user = this.GetEntity<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && userIds.Contains(t.ID));
                //userNames.Add(user?.NAME);

                T_FM_NOTIFICATION_TASK task = null;
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    task.SOURCE_FORMCODE = "PT020_SHOWPRINT";
                }
                UnifiedCommit(() =>
                {
                    if (formulationDepartment != null)
                        this.UpdateEntityNoCommit(formulationDepartment);
                    if (department != null)
                        this.UpdateEntityNoCommit(department);
                    if (detailList != null && detailList.Any())
                        this.BantchSaveEntityNoCommit(detailList);
                    if (teamDetailList != null && teamDetailList.Any())
                        this.BantchSaveEntityNoCommit(teamDetailList);
                    if (departmentContents != null && departmentContents.Any())
                        this.BantchSaveEntityNoCommit(departmentContents);
                    if (teamContents != null && teamContents.Any())
                        this.BantchSaveEntityNoCommit(teamContents);
                    if (audit != null)
                        this.UpdateEntityNoCommit(audit);
                    if (auditDetails != null && auditDetails.Any())
                        this.BantchSaveEntityNoCommit(auditDetails);
                    if (contents != null && contents.Any())
                        this.BantchSaveEntityNoCommit(contents);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                    if (notices != null && notices.Any())
                        this.BantchSaveEntityNoCommit(notices);
                });
                return true;
            });
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetEdit")]
        public JsonActionResult<T_PT_INDICATOR_FORMULATION_DEPARTMENT> GetEdit([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
                if (string.IsNullOrEmpty(id))
                    this.ThrowError("030017");
                var result = this.GetEntity<T_PT_INDICATOR_FORMULATION_DEPARTMENT>(id, new string[] { "Nav_User", "Nav_Department","Nav_Department.Nav_User","Nav_Files","Nav_Files.Nav_ImgFile",
            "Nav_DepartmentDetail","Nav_DepartmentDetail.Nav_Department","Nav_DepartmentDetail.Nav_Department.Nav_User","Nav_DepartmentDetail.Nav_DetailContent","Nav_TeamDetail","Nav_TeamDetail.Nav_User",
                    "Nav_TeamDetail.Nav_User.Nav_Person","Nav_TeamDetail.Nav_User.Nav_Person.Nav_Post","Nav_TeamDetail.Nav_DetailContent"});
                return result;
            });
        }

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

        /// <summary>
        /// 驳回
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("RejectUpdate")]
        public JsonActionResult<bool> RejectUpdate(string id)
        {
            return SafeExecute(() =>
            {
                var entity = this.GetEntity<T_PT_INDICATOR_FORMULATION_DEPARTMENT>(id);
                entity.STATUS = PFStandardStatus.Rejected;
                var param = entity.DEPARTMENT_TYPE.GetDescription();
                //发消息
                T_FM_NOTIFICATION_TASK notice = new T_FM_NOTIFICATION_TASK();
                var user = this.GetEntity<T_FM_USER>(t => t.ID == entity.USER_ID && t.ENABLE_STATUS == 0);
                if (user != null)
                {
                    notice = NotificationTaskService.InsertUserNoticeTaskModel(param + "目标与指标已被驳回", entity.ID, entity.ORG_ID, user.ID, user.NAME, DateTime.Now,
                    DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "PT020");
                }
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (notice != null)
                        this.UpdateEntityNoCommit(notice);
                });
                return true;
                //return ApproveCallBackService.CallReject("PT/PTIndicatorFormulation/RejectUpdate", id);
            });
        }
    }
}
