using APT.BaseData.Domain.Entities.FM; using APT.BaseData.Domain.Enums; using APT.BaseData.Domain.IServices; using APT.BaseData.Domain.IServices.FM; using APT.BaseData.Services.DomainServices; using APT.BaseData.Services.Services.FM; using APT.Infrastructure.Core; using APT.MS.Domain.Entities.HM; using APT.MS.Domain.Entities.OG; using APT.MS.Domain.Entities.SC; using APT.MS.Domain.Entities.SC.SC; using APT.MS.Domain.Entities.SE; using APT.MS.Domain.Enums; using APT.Utility; using Microsoft.AspNetCore.Mvc; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace APT.SC.WebApi.Controllers.Api.SC { [Route("api/SC/Post")] public class SCPostController : AuthorizeApiController { IPFCodeRuleService CodeRuleService { get; set; } public SCPostController(IPFCodeRuleService codeRuleService, IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService) { CodeRuleService = codeRuleService; } /// /// 更新 /// /// /// [HttpPost, Route("FullUpdate")] public JsonActionResult FullUpdate([FromBody] T_SC_POST entity) { return SafeExecute(() => { //if (entity.CODE == null) //{ // SystemCodeFilter questionFilter = new SystemCodeFilter // { // CodeType = (int)PFCodeRuleType.责任制岗位, // Count = 1, // OrgId = entity.ORG_ID, // }; // entity.CODE = CodeRuleService.NewGenSerial(questionFilter); //} List postList = new List(); var deposts = entity.Nav_Depts; entity.Nav_Depts = null; entity.CODE = DateTime.Now.Year.ToString().PadLeft(4, '0') + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + new Random().Next().ToString(); var deleteIdDs = this.GetEntities(t => t.POST_ID == entity.ID, new BaseFilter(entity.ORG_ID)).Select(t => t.ID).ToList(); var deleteIds = this.GetEntities(t => deleteIdDs.Contains(t.DEPT_ID), new BaseFilter(entity.ORG_ID)).Select(t => t.ID).ToList(); if (deposts != null && deposts.Any()) { deposts.Where(m => !m.IS_DELETED).ForEach(t => { t.ORG_ID = entity.ORG_ID; t.POST_ID = entity.ID; t.Nav_Department = null; t.Nav_Post = null; if (t.Nav_Posts != null && t.Nav_Posts.Any()) { var posts = t.Nav_Posts.ToList(); posts.ForEach(x => { x.DEPT_ID = t.ID; x.ORG_ID = t.ORG_ID; x.Nav_Dept = null; x.Nav_Depost = null; postList.Add(x); }); } t.Nav_Posts = null; }); } //var template_files = entity.Nav_Files; //if (template_files != null) { // template_files.ForEach(file => // { // file.POST_ID = entity.ID; // file.ORG_ID = entity.ORG_ID; // }); //} var modelCheck = GetCount(e => e.ID != entity.ID && !e.IS_DELETED && e.NAME == entity.NAME, null); if (modelCheck > 0) { throw new Exception("责任制岗位名称不能重复,以避免关联操作误选!"); } UnifiedCommit(() => { UpdateEntity(entity); if (deleteIds != null && deleteIds.Any()) BantchDeleteEntity(deleteIds); if (deleteIdDs != null && deleteIdDs.Any()) BantchDeleteEntity(deleteIdDs); if (deposts != null && deposts.Any()) BantchSaveEntity(deposts); if (postList != null && postList.Any()) BantchSaveEntity(postList); //if (template_files != null) //{ // BantchSaveEntity(template_files); //} }); //UpdateBySCPostID(entity.ID); return true; }); } /// /// 获得单条实体数据 /// /// /// [HttpPost, Route("UpdateAll")] public JsonActionResult UpdateAll([FromBody] KeywordFilter filter) { return SafeExecute(() => { var entities = GetEntities(null, new BaseFilter(filter.GetOrgId())).ToList(); if (entities != null) { entities.ForEach(it => { //UpdateBySCPostID(it.ID); }); } return true; }); } /// /// 人员岗位取值 /// /// /// [HttpPost, Route("GetPersonPost")] public PagedActionResult GetPersonPost([FromBody] KeywordPageFilter pageFilter) { var result = new PagedActionResult();// this.GetOrderPageEntities(null, pageFilter); var filter = pageFilter.FilterGroup.Groups.Select(t => t.Rules.FirstOrDefault(m => m.Field == "DEPARTMENT_ID")).FirstOrDefault(); if (filter == null) { filter = pageFilter.FilterGroup.Rules.FirstOrDefault(m => m.Field == "DEPARTMENT_ID"); } if (filter != null) { //部门下的人员岗位 var ogRelated = this.GetEntities(t => t.DEPARTMENT_ID == Guid.Parse(filter.Value.ToString()) && t.ENABLE_STATUS == 0, new BaseFilter(pageFilter.OrgId), new string[] { "Nav_Person.Nav_Post" }); if (ogRelated != null && ogRelated.Any()) { var postList = ogRelated.Where(t => t.Nav_Person != null && t.Nav_Person.POST_ID != null).Select(m => m.Nav_Person.Nav_Post).Distinct().ToList(); var userList = new List(); if (postList != null && postList.Any()) { var postIds = postList.Select(t => t.ID).Distinct().ToList(); foreach (var postId in postIds) { var t = postList.FirstOrDefault(t => t.ID == postId); T_FM_USER_POST user = new T_FM_USER_POST(); user.ID = t.ID; user.CODE = t.CODE; user.NAME = t.NAME; userList.Add(user); } result.Data = userList; } else { result.Data = this.GetOrderPageEntities(null, new BasePageFilter(pageFilter.PageIndex, 10)).Data; } } else { result.Data = this.GetOrderPageEntities(null, new BasePageFilter(pageFilter.PageIndex, 10)).Data; } } else { result = this.GetOrderPageEntities(null, new BasePageFilter(pageFilter.PageIndex, 10)); } return result; } ///// ///// 按责任制岗位ID更新用户责任制岗位 ///// //public void UpdateBySCPostID(Guid SCPostId) //{ // var entity = GetEntity(t => t.ID == SCPostId, new BaseFilter(null, FilterOrgTypeEnum.忽略组织)); // if (entity == null) // { // return; // } // var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId; // //查询当前责任制岗位所有的部门岗位 // var deposts = GetEntities( // t => t.POST_ID == SCPostId, // new BaseFilter(orgId), // new string[] { "Nav_Depost" } // ).ToList(); // //生成查出来的所有部门岗位的所有人员 // var depostids = deposts.Select(t => t.DEPOST_ID).Distinct().ToList(); // var allUids = GetEntities(t => t.POSITION_ID != null && depostids.Contains(t.POSITION_ID.Value), new BaseFilter(null, Core.Data.FilterOrgTypeEnum.忽略组织)).Select(t => t.ID).ToList(); // //兼职安全员 // if (entity.AS_SAFE_USER) // { // //获取当前责任制关联部门中职务为安全员的人 // var departIds = deposts.Select(t => t.Nav_Depost.DEPARTMENT_ID).Distinct().ToList(); // var safeUids = GetEntities( // t => t.Nav_DepartmentPost.Nav_Workduty.NAME == "安全员" && // departIds.Contains(t.Nav_DepartmentPost.DEPARTMENT_ID), // new BaseFilter(null, Core.Data.FilterOrgTypeEnum.忽略组织), // new string[] { "Nav_DepartmentPost", "Nav_DepartmentPost.Nav_Workduty" } // ).ToList().Select(u => u.USER_ID).ToList(); // //关联的所有人员与关联的所有部门所有安全员的交集 // allUids = allUids.Intersect(safeUids).Distinct().ToList(); // } // var datas = GetEntities( // t => t.RESPON_POST_ID == SCPostId, // new BaseFilter(orgId)).ToList(); // datas.ForEach(t => // { // if (!allUids.Contains(t.USER_ID)) // { // t.IS_DELETED = true; // } // }); // var existIds = datas.Select(t => t.USER_ID).Distinct().ToList(); // var newIds = allUids.Except(existIds).ToList(); // newIds.ForEach(uid => // { // datas.Add(new T_FM_USER_RESPON_POST // { // ID = Guid.NewGuid(), // ENTITY_ORG_TPYE = 2, // ORG_ID = entity.ORG_ID, // USER_ID = uid, // RESPON_POST_ID = SCPostId, // }); // }); // BantchSaveEntity(datas); //} ///// ///// 按用户ID更新用户责任制岗位 ///// //public void UpdateByUserId(Guid UserId) //{ // var user = GetEntity(t => t.ID == UserId, new BaseFilter(null, FilterOrgTypeEnum.忽略组织)); // if (user == null) // return; // var currentUserSCPosts = GetEntities( // t => t.USER_ID == UserId, // new BaseFilter(null, Core.Data.FilterOrgTypeEnum.忽略组织)).ToList(); // var SCPOSTIDS = new List(); // if (currentUserSCPosts != null) // { // SCPOSTIDS = currentUserSCPosts. // Where(it => it.RESPON_POST_ID != null). // Distinct(it => it.RESPON_POST_ID). // Select(it => it.RESPON_POST_ID.Value).ToList(); // } // if (user.POSITION_ID != null) // { // var currentPostDeposts = GetEntities(it => // it.DEPOST_ID == user.POSITION_ID.Value, // new BaseFilter(null, FilterOrgTypeEnum.忽略组织) // ).ToList(); // if (currentPostDeposts != null) // { // currentPostDeposts.ForEach(it => // { // if (!SCPOSTIDS.Contains(it.POST_ID)) // { // SCPOSTIDS.Add(it.POST_ID); // } // }); // } // } // if (SCPOSTIDS.Count == 0) // return; // SCPOSTIDS.ForEach(id => // { // try // { // UpdateBySCPostID(id); // } // catch { } // }); //} } }