using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.FO;
using APT.MS.Domain.Enums;
using APT.Utility;
using Google.Protobuf.Collections;
using InfluxData.Net.InfluxDb.Models.Responses;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace APT.FO.WebApi.Controllers
{
    /// <summary>
    ///  领导带班下井记录
    /// </summary>
	[Route("api/FO/FOLeaderWellRecord")]
    public partial class LeaderWellRecordController : AuthorizeApiController<T_FO_LEADER_WELL_RECORD>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }
        public LeaderWellRecordController(IFMNotificationTaskService notificationTaskService)
        {
            NotificationTaskService = notificationTaskService;
        }
        [HttpPost, Route("GetEdit")]
        public JsonActionResult<T_FO_LEADER_WELL_RECORD> GetEdit([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
                if (string.IsNullOrEmpty(id))
                    this.ThrowError("060010");
                var entity = this.GetEntity<T_FO_LEADER_WELL_RECORD>(id, "Nav_ApplyUser", "Nav_Company", "Nav_ApplyDepartment", "Nav_ProductionUnit", "Nav_Class", "Nav_Users.Nav_User.Nav_UserSignFiles.Nav_ImgFile",
                    "Nav_Files.Nav_ImgFile", "Nav_PreUsers.Nav_PreUser.Nav_UserSignFiles.Nav_ImgFile", "Nav_AfterUsers.Nav_AfterUser.Nav_UserSignFiles.Nav_ImgFile");
                if (entity != null)
                {
                    var details = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == entity.ID, new BaseFilter(entity.ORG_ID), "Nav_DetailFiles.Nav_ImgFile", "Nav_User.Nav_UserSignFiles.Nav_ImgFile", "Nav_DetailAreas.Nav_Area").ToList();
                    entity.Nav_Details = details.OrderByDescending(t => t.START_DATE).ToList();
                }
                return entity;
            });
        }
        /// <summary>
        /// 新增修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_FO_LEADER_WELL_RECORD entity)
        {
            return SafeExecute<bool>(() =>
            {
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                var departmentId = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
                entity.APPLY_USER_ID = loginUserId;
                entity.APPLY_DEPARTMENT_ID = departmentId;
                if (entity.JOB_START_DATE == null)
                {
                    throw new Exception("值班开始时间不能为空");
                }
                if (entity.JOB_END_DATE == null)
                {
                    throw new Exception("值班结束时间不能为空");
                }
                if (entity.COMPANY_ID == null)
                {
                    entity.COMPANY_ID = this.GetEntity<T_FM_DEPARTMENT>(t => t.PARENT_ID == null)?.ID;
                }
                if (entity.PRODUCTION_UNIT_ID == null)
                {
                    entity.PRODUCTION_UNIT_ID = this.GetEntity<T_FM_DEPARTMENT>(t => t.ID == entity.APPLY_DEPARTMENT_ID)?.PRODUCTION_UNIT_ID;
                }
                var userIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_USER>(t => t.LEADER_WELL_RECORD_ID == entity.ID, new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var fileIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_FILE>(t => t.LEADER_WELL_RECORD_ID == entity.ID, new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var afterUserIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_AFTERUSER>(t => t.LEADER_WELL_RECORD_ID == entity.ID, new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var detailIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == entity.ID, new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var detailFileIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL_FILE>(t => detailIds.Contains(t.LEADER_WELL_RECORD_DETAIL_ID), new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var detailAreaIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL_AREA>(t => detailIds.Contains(t.LEADER_WELL_RECORD_DETAIL_ID), new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                //var detailPreUserIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL_PREUSER>(t => detailIds.Contains(t.LEADER_WELL_RECORD_DETAIL_ID), new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                //var detailAfterUserIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL_AFTERUSER>(t => detailIds.Contains(t.LEADER_WELL_RECORD_DETAIL_ID), new BaseFilter(entity.ORG_ID)).Select(m => m.ID).ToList();
                var userList = new List<T_FO_LEADER_WELL_RECORD_USER>();
                var fileList = new List<T_FO_LEADER_WELL_RECORD_FILE>();
                var afterUserList = new List<T_FO_LEADER_WELL_RECORD_AFTERUSER>();
                var detailList = new List<T_FO_LEADER_WELL_RECORD_DETAIL>();
                var detailFileList = new List<T_FO_LEADER_WELL_RECORD_DETAIL_FILE>();
                var detailAreaList = new List<T_FO_LEADER_WELL_RECORD_DETAIL_AREA>();
                //var detailPreUserList = new List<T_FO_LEADER_WELL_RECORD_DETAIL_PREUSER>();
                //var detailAfterUserList = new List<T_FO_LEADER_WELL_RECORD_DETAIL_AFTERUSER>();
                var users = entity.Nav_Users;
                if (users != null && users.Any())
                {
                    users = users.Where(t => !t.IS_DELETED).ToList();
                }
                else
                {
                    users = new List<T_FO_LEADER_WELL_RECORD_USER>();
                    var currUserRecord = new T_FO_LEADER_WELL_RECORD_USER()
                    {
                        LEADER_WELL_RECORD_ID = entity.ID,
                        USER_ID = (Guid)loginUserId,
                        ORG_ID = entity.ORG_ID,
                        DEAL_STATUS = 0
                    };
                    users.Add(currUserRecord);
                }
                entity.Nav_Users = null;
                if (users != null && users.Any())
                {
                    foreach (var use in users.Where(t=>!t.IS_DELETED))
                    {
                        T_FO_LEADER_WELL_RECORD_USER item = new T_FO_LEADER_WELL_RECORD_USER();
                        item.ORG_ID = entity.ORG_ID;
                        item.LEADER_WELL_RECORD_ID = entity.ID;
                        item.USER_ID = use.USER_ID;
                        item.Nav_User = null;
                        if (item.USER_ID == loginUserId)
                        {
                            item.DEAL_STATUS = MS.Domain.Enums.FOUserShiftStatusEnum.已处理;
                        }
                        userList.Add(item);
                    }
                }
                var afterUsers = entity.Nav_AfterUsers;
                entity.Nav_AfterUsers = null;
                if (afterUsers != null && afterUsers.Any())
                {
                    afterUsers = afterUsers.Where(t => !t.IS_DELETED).ToList();
                }
                if (afterUsers != null && afterUsers.Any())
                {
                    foreach (var after in afterUsers)
                    {
                        T_FO_LEADER_WELL_RECORD_AFTERUSER item = new T_FO_LEADER_WELL_RECORD_AFTERUSER();
                        item.ORG_ID = entity.ORG_ID;
                        item.LEADER_WELL_RECORD_ID = entity.ID;
                        item.AFTER_USER_ID = after.AFTER_USER_ID;
                        item.Nav_AfterUser = null;
                        afterUserList.Add(item);
                    }
                }
                var details = entity.Nav_Details;
                if (details != null && details.Any())
                {
                    details = details.Where(t => !t.IS_DELETED).ToList();
                }
                entity.Nav_Details = null;
                if (details != null && details.Any())
                {
                    foreach (var item in details)
                    {
                        T_FO_LEADER_WELL_RECORD_DETAIL de = new T_FO_LEADER_WELL_RECORD_DETAIL();
                        de.ORG_ID = entity.ORG_ID;
                        de.LEADER_WELL_RECORD_ID = entity.ID;
                        de.START_DATE = item.START_DATE;
                        de.END_DATE = item.END_DATE;
                        de.USER_ID = item.USER_ID;
                        de.DESCRIPTION = item.DESCRIPTION;
                        de.QUESTION = item.QUESTION;
                        de.MEASURE = item.MEASURE;
                        de.ITEM = item.ITEM;
                        de.PLACE = item.PLACE;
                        if (de.USER_ID == loginUserId)
                        {
                            de.USER_DEAL_STATUS = MS.Domain.Enums.FOUserShiftStatusEnum.已处理;
                        }
                        detailList.Add(de);
                        if (item.Nav_DetailFiles != null && item.Nav_DetailFiles.Any())
                        {
                            foreach (var file in item.Nav_DetailFiles.Where(t=>!t.IS_DELETED))
                            {
                                T_FO_LEADER_WELL_RECORD_DETAIL_FILE item2 = new T_FO_LEADER_WELL_RECORD_DETAIL_FILE();
                                item2.ORG_ID = entity.ORG_ID;
                                item2.LEADER_WELL_RECORD_DETAIL_ID = de.ID;
                                item2.Nav_ImgFile = null;
                                item2.IMG_FILE_ID = file.IMG_FILE_ID;
                                detailFileList.Add(item2);
                            }
                        }
                        if (item.Nav_DetailAreas != null && item.Nav_DetailAreas.Any())
                        {
                            foreach (var area in item.Nav_DetailAreas.Where(t=>!t.IS_DELETED))
                            {
                                T_FO_LEADER_WELL_RECORD_DETAIL_AREA item2 = new T_FO_LEADER_WELL_RECORD_DETAIL_AREA();
                                item2.ORG_ID = entity.ORG_ID;
                                item2.LEADER_WELL_RECORD_DETAIL_ID = de.ID;
                                item2.AREA_ID = area.AREA_ID;
                                item2.Nav_Area = null;
                                detailAreaList.Add(item2);
                            }
                        }
                        //if (item.Nav_PreUsers != null && item.Nav_PreUsers.Any())
                        //{
                        //    foreach (var item2 in item.Nav_PreUsers)
                        //    {
                        //        item2.ORG_ID = entity.ORG_ID;
                        //        item2.LEADER_WELL_RECORD_DETAIL_ID = item.ID;
                        //        item2.Nav_PreUser = null;
                        //        if (item2.PRE_USER_ID == loginUserId)
                        //        {
                        //            item2.PRE_USER_DEAL_STATUS = MS.Domain.Enums.FOUserShiftStatusEnum.已处理;
                        //        }
                        //        detailPreUserList.Add(item2);
                        //    }
                        //}
                        //else
                        //{
                        //    foreach(var user in users)
                        //    {
                        //        T_FO_LEADER_WELL_RECORD_DETAIL_PREUSER currUserRecord = new T_FO_LEADER_WELL_RECORD_DETAIL_PREUSER();
                        //        currUserRecord.ORG_ID = entity.ORG_ID;
                        //        currUserRecord.LEADER_WELL_RECORD_DETAIL_ID = item.ID;
                        //        currUserRecord.PRE_USER_ID = user.USER_ID;
                        //        currUserRecord.PRE_USER_DEAL_STATUS = user.DEAL_STATUS;
                        //        currUserRecord.Nav_PreUser = null;
                        //        if (currUserRecord.PRE_USER_ID == loginUserId)
                        //        {
                        //            currUserRecord.PRE_USER_DEAL_STATUS = MS.Domain.Enums.FOUserShiftStatusEnum.已处理;
                        //        }
                        //        detailPreUserList.Add(currUserRecord);
                        //    }
                        //}
                        //if (item.Nav_AfterUsers != null && item.Nav_AfterUsers.Any())
                        //{
                        //    foreach (var item2 in item.Nav_AfterUsers)
                        //    {
                        //        item2.ORG_ID = entity.ORG_ID;
                        //        item2.LEADER_WELL_RECORD_DETAIL_ID = item.ID;
                        //        item2.Nav_AfterUser = null;
                        //        if (item2.AFTER_USER_ID == loginUserId)
                        //        {
                        //            item2.AFTER_USER_DEAL_STATUS = MS.Domain.Enums.FOUserShiftStatusEnum.已处理;
                        //        }
                        //        detailAfterUserList.Add(item2);
                        //    }
                        //}
                       
                    }
                }
                var files = entity.Nav_Files;
                if (files != null && files.Any())
                {
                    files = files.Where(t => !t.IS_DELETED).ToList();
                }
                entity.Nav_Files = null;
                if (files != null && files.Any())
                {
                    foreach (var file in files)
                    {
                        T_FO_LEADER_WELL_RECORD_FILE item = new T_FO_LEADER_WELL_RECORD_FILE();
                        item.ORG_ID = entity.ORG_ID;
                        item.LEADER_WELL_RECORD_ID = entity.ID;
                        item.IMG_FILE_ID = file.IMG_FILE_ID;
                        item.Nav_ImgFile = null;
                        fileList.Add(item);
                    }
                }
                entity.STATUS = FOStatusEnum.Draft;
                T_FM_NOTIFICATION_TASK task = null;
                var sendNotices = new List<T_FM_NOTIFICATION_TASK>();
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    if (!userList.Any())
                    {
                        throw new Exception("值班领导不能为空");
                    }
                    if (!afterUserList.Any())
                    {
                        throw new Exception("接班人不能为空");
                    }
                    if (!detailList.Any())
                    {
                        throw new Exception("明细行至少填写一行");
                    }
                    var allUsers = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == 0 && !t.CODE.Contains("admin"), new BaseFilter(entity.ORG_ID));
                    var sendUserIds = userList.Where(t => t.USER_ID != loginUserId && t.USER_ID != null).Select(m => (Guid)m.USER_ID).Distinct().ToList();
                    if (sendUserIds != null && sendUserIds.Any())
                    {
                        //当班人员不等于自己的，触发签字确认单
                        var sendUserNames = allUsers.Where(t => sendUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                        sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-交班人签字确认", entity.ID, entity.ORG_ID, sendUserIds, sendUserNames, DateTime.Now, entity.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                        entity.STATUS = FOStatusEnum.PreSign;
                    }
                    else
                    {
                        //触发给现场责任人签字确认
                        var detailUserIds = details.Where(t => t.USER_ID != loginUserId && t.USER_ID != null).Select(m => (Guid)m.USER_ID).Distinct().ToList();
                        if (detailUserIds != null && detailUserIds.Any())
                        {
                            var sendUserNames = allUsers.Where(t => detailUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                            sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-现场责任人签字确认", entity.ID, entity.ORG_ID, detailUserIds, sendUserNames, DateTime.Now, entity.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                            entity.STATUS = FOStatusEnum.Sign;
                        }
                        else
                        {
                            var sendAfterUserIds = afterUserList.Where(t =>  t.AFTER_USER_ID != null).Select(m => (Guid)m.AFTER_USER_ID).Distinct().ToList();
                            if (sendAfterUserIds != null && sendAfterUserIds.Any())
                            {
                                //触发给接班人签字确认
                                var sendUserNames = allUsers.Where(t => sendAfterUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                                sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-接班人签字确认", entity.ID, entity.ORG_ID, sendAfterUserIds, sendUserNames, DateTime.Now, entity.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                                entity.STATUS = FOStatusEnum.SignAccept;
                            }
                            else
                            {
                                entity.STATUS = FOStatusEnum.Archived;
                            }
                        }
                    }
                    
                    if (entity.TaskID != Guid.Empty)
                    {
                        task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                        task.SOURCE_FORMCODE = "FO041_SHOWPRINT";
                    }
                }
                this.UnifiedCommit(() =>
                {
                    if (afterUserIds != null && afterUserIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_AFTERUSER>(afterUserIds);
                    //if (detailAfterUserIds != null && detailAfterUserIds.Any())
                    //    this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_AFTERUSER>(detailAfterUserIds);
                    if (detailFileIds != null && detailFileIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_DETAIL_FILE>(detailFileIds);
                    if (detailAreaIds != null && detailAreaIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_DETAIL_AREA>(detailAreaIds);
                    if (detailIds != null && detailIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_DETAIL>(detailIds);
                    if (fileIds != null && fileIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_FILE>(fileIds);
                    if (userIds != null && userIds.Any())
                        this.BantchDeleteEntityNoCommit<T_FO_LEADER_WELL_RECORD_USER>(userIds);
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (detailList != null && detailList.Any())
                        this.BantchSaveEntityNoCommit(detailList);
                    if (detailFileList != null && detailFileList.Any())
                        this.BantchSaveEntityNoCommit(detailFileList);
                    if (detailAreaList != null && detailAreaList.Any())
                        this.BantchSaveEntityNoCommit(detailAreaList);
                    //if (detailPreUserList != null && detailPreUserList.Any())
                    //    this.BantchSaveEntityNoCommit(detailPreUserList);
                    if (afterUserList != null && afterUserList.Any())
                        this.BantchSaveEntityNoCommit(afterUserList);
                    if (userList != null && userList.Any())
                        this.BantchSaveEntityNoCommit(userList);
                    if (fileList != null && fileList.Any())
                        this.BantchSaveEntityNoCommit(fileList);
                    if (sendNotices != null && sendNotices.Any())
                        this.BantchSaveEntityNoCommit(sendNotices);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }
        /// <summary>
        /// 签到同意
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("PersonalAgree")]
        public JsonActionResult<bool> PersonalAgree([FromBody] T_FO_LEADER_WELL_RECORD entity)
        {
            return SafeExecute<bool>(() =>
            {
                var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
                var userID = 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)
                    {
                        userID = currTask.USER_ID;
                    }
                }
                var allUsers = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == 0 && !t.CODE.Contains("admin"), new BaseFilter(orgId));
                var sendNotices = new List<T_FM_NOTIFICATION_TASK>();
                var detailUsers = new List<T_FO_LEADER_WELL_RECORD_DETAIL>();
                var preUsers = new List<T_FO_LEADER_WELL_RECORD_USER>();
                var afterUsers = new List<T_FO_LEADER_WELL_RECORD_AFTERUSER>();
                var record = this.GetEntity<T_FO_LEADER_WELL_RECORD>(entity.ID);
                if (record.STATUS == FOStatusEnum.PreSign)
                {
                    var details = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == record.ID, new BaseFilter(orgId));
                    preUsers = this.GetEntities<T_FO_LEADER_WELL_RECORD_USER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.USER_ID == userID, new BaseFilter(orgId)).ToList();
                    if (preUsers != null && preUsers.Any())
                    {
                        preUsers.ForEach(t => t.DEAL_STATUS = FOUserShiftStatusEnum.已处理);
                    }
                    var todoCount = this.GetCount<T_FO_LEADER_WELL_RECORD_USER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.USER_ID != userID && t.DEAL_STATUS == 0 && t.USER_ID != null, new BaseFilter(orgId));
                    if (todoCount == 0)
                    {
                        //触发给现场责任人签字确认
                        var detailUserIds = details.Where(t => t.USER_ID != userID && t.USER_ID != null).Select(m => (Guid)m.USER_ID).Distinct().ToList();
                        if (detailUserIds != null && detailUserIds.Any())
                        {
                            var sendUserNames = allUsers.Where(t => detailUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                            sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-现场责任人签字确认", entity.ID, record.ORG_ID, detailUserIds, sendUserNames, DateTime.Now, record.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                            record.STATUS = FOStatusEnum.Sign;
                        }
                        else
                        {
                            var afterUserIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_AFTERUSER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.AFTER_USER_ID != null, new BaseFilter(orgId)).Select(m => (Guid)m.AFTER_USER_ID).Distinct().ToList();
                            if (afterUserIds != null && afterUserIds.Any())
                            {
                                //触发给接班人签字确认
                                var sendUserNames = allUsers.Where(t => afterUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                                sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-接班人签字确认", entity.ID, record.ORG_ID, afterUserIds, sendUserNames, DateTime.Now, record.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                                record.STATUS = FOStatusEnum.SignAccept;
                            }
                            else
                            {
                                record.STATUS = FOStatusEnum.Archived;
                            }
                        }
                    }
                }
                else if (record.STATUS == FOStatusEnum.Sign)
                {
                    var details = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == record.ID, new BaseFilter(orgId));
                    detailUsers = details.Where(t => t.USER_ID == userID).ToList();
                    if (detailUsers != null && detailUsers.Any())
                    {
                        detailUsers.ForEach(t => t.USER_DEAL_STATUS = FOUserShiftStatusEnum.已处理);
                    }
                    var todoCount = this.GetCount<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.USER_ID != userID && t.USER_DEAL_STATUS == 0 && t.USER_ID != null, new BaseFilter(orgId));
                    if (todoCount == 0)
                    {
                        var detailIds = details.Select(m => m.ID).ToList();
                        var afterUserIds = this.GetEntities<T_FO_LEADER_WELL_RECORD_AFTERUSER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.AFTER_USER_ID != null, new BaseFilter(orgId)).Select(m => (Guid)m.AFTER_USER_ID).Distinct().ToList();
                        if (afterUserIds != null && afterUserIds.Any())
                        {
                            //触发给接班人签字确认
                            var sendUserNames = allUsers.Where(t => afterUserIds.Contains(t.ID)).Select(m => m.NAME).ToList();
                            sendNotices = NotificationTaskService.InsertUserNoticeTaskModels("领导带班下井记录-接班人签字确认", entity.ID, record.ORG_ID, afterUserIds, sendUserNames, DateTime.Now, record.JOB_END_DATE.Value, 1, "FO041_SHOWPRINT");
                            record.STATUS = FOStatusEnum.SignAccept;
                        }
                        else
                        {
                            record.STATUS = FOStatusEnum.Archived;
                        }
                    }
                }
                else if (record.STATUS == FOStatusEnum.SignAccept)
                {
                    var details = this.GetEntities<T_FO_LEADER_WELL_RECORD_DETAIL>(t => t.LEADER_WELL_RECORD_ID == record.ID, new BaseFilter(orgId));
                    var detailIds = details.Select(m => m.ID).ToList();
                    afterUsers = this.GetEntities<T_FO_LEADER_WELL_RECORD_AFTERUSER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.AFTER_USER_ID == userID, new BaseFilter(orgId)).ToList();
                    if (afterUsers != null && afterUsers.Any())
                    {
                        afterUsers.ForEach(t => t.AFTER_USER_DEAL_STATUS = FOUserShiftStatusEnum.已处理);
                    }
                    var todoCount = this.GetCount<T_FO_LEADER_WELL_RECORD_AFTERUSER>(t => t.LEADER_WELL_RECORD_ID == record.ID && t.AFTER_USER_ID != userID && t.AFTER_USER_DEAL_STATUS == 0 && t.AFTER_USER_ID != null, new BaseFilter(orgId));
                    if (todoCount == 0)
                    {
                        record.STATUS = FOStatusEnum.Archived;
                    }
                }
                else
                {
                    record.STATUS = FOStatusEnum.Archived;
                }
                T_FM_NOTIFICATION_TASK task = null;
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    task.SOURCE_FORMCODE = "FO041_SHOWPRINT";
                }
                this.UnifiedCommit(() =>
                {
                    if (record != null)
                        UpdateEntityNoCommit(record);
                    if (preUsers != null && preUsers.Any())
                        this.BantchSaveEntityNoCommit(preUsers);
                    if (detailUsers != null && detailUsers.Any())
                        this.BantchSaveEntityNoCommit(detailUsers);
                    if (afterUsers != null && afterUsers.Any())
                        this.BantchSaveEntityNoCommit(afterUsers);
                    if (sendNotices != null && sendNotices.Any())
                        this.BantchSaveEntityNoCommit(sendNotices);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }
    }
}
