using System; using System.Collections.Generic; using System.Linq; using APT.BaseData.Domain.Entities; using APT.BaseData.Domain.Entities.FM; using APT.BaseData.Domain.Enums; using APT.BaseData.Domain.IServices.FM; using APT.Infrastructure.Core; using APT.MS.Domain.Entities.SE; using APT.MS.Domain.Enums; using APT.Utility; using Microsoft.AspNetCore.Mvc; using NPOI.SS.Formula.Functions; namespace APT.SC.WebApi.Controllers.Api.SE { [Route("api/SE/SETrainSurvey")] public class SETrainSurveyController : AuthorizeApiController { IFMNotificationTaskService NotificationTaskService { get; set; } public SETrainSurveyController(IFMNotificationTaskService notificationTaskService) { NotificationTaskService = notificationTaskService; } /// /// 保存 /// /// /// [HttpPost, Route("FullUpdate")] public JsonActionResult FullUpdate([FromBody] T_SE_TRAIN_SURVEY entity) { return SafeExecute(() => { var userID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID; if (entity.TaskID != null && entity.TaskID != Guid.Empty) { var currTask = GetEntity(t => t.ID == entity.TaskID); if (currTask != null) { userID = currTask.USER_ID; } } if (entity.STATUS != SETrainSurveyStatus.草稿) { throw new Exception("当前状态数据不可编辑"); } if (entity.END_TIME == null) { throw new Exception("请选择截止时间"); } if (entity.LAUNCH_TIME == null) { entity.LAUNCH_TIME = DateTime.Now; } if (entity.LAUNCH_TIME.Value.AddDays(1) > entity.END_TIME.Value) { throw new Exception("截止时间至少比发起时间要大于一天"); } if (entity.LAUNCH_USER_ID == Guid.Empty) { entity.LAUNCH_USER_ID = (Guid)userID; } var Nav_JoinDepartment = entity.Nav_JoinDepartment; var Nav_Points = entity.Nav_Points; if (Nav_JoinDepartment == null) { throw new Exception("参与人员不可为空"); } if (Nav_Points == null) { throw new Exception("调查内容不可为空"); } if (entity.LAUNCH_DEPARTMENT_ID == null) { entity.LAUNCH_DEPARTMENT_ID = GetEntity(entity.LAUNCH_USER_ID.ToString()).DEPARTMENT_ID; } List Nav_Demands = null; List noticeTask = null; if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify")) { Nav_Demands = new List(); entity.STATUS = SETrainSurveyStatus.调查中; var allSendUserIds = GetDepartmentUsers(Nav_JoinDepartment.Where(t => t.IS_DELETED == false).Select(t => t.DEPARTMENT_ID).ToList(), entity.ORG_ID); var currDep = GetEntity(t => t.ID == entity.LAUNCH_DEPARTMENT_ID); if (currDep != null && (currDep.NAME == "安全环保管理部" || currDep.NAME == "安全环保科" || currDep.NAME == "安全环保部" || currDep.NAME == "安环部")) { var manageUsers = GetEntities(t => t.Nav_Department.DEPARTMENT_TYPE == 3 && t.ENABLE_STATUS == 0 && !t.CODE.Contains("admin"), new BaseFilter(entity.ORG_ID)).Select(t => t.ID); allSendUserIds.AddRange(manageUsers); } var allUsers = this.GetEntities(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && allSendUserIds.Contains(t.ID) && !t.CODE.Contains("admin"), new BaseFilter(entity.ORG_ID)); var allUserDepIds = new List(); var allSendUserNames = new List(); var allSendDataIds = new List(); var allSendTitles = new List(); foreach (var user in allSendUserIds) { var current = allUsers.FirstOrDefault(t => t.ID == user); allUserDepIds.Add(current?.DEPARTMENT_ID); allSendUserNames.Add(current?.NAME); } for (int i = 0; i < allSendUserIds.Count; i++) { var newId = Guid.NewGuid(); Nav_Demands.Add(new T_SE_TRAIN_SURVEY_LIST_DEMAND { ID = newId, SURVEY_ID = entity.ID, USER_ID = allSendUserIds[i], DEPARTMENT_ID = allUserDepIds[i], OK = 0, ORG_ID = entity.ORG_ID, }); allSendDataIds.Add(newId); allSendTitles.Add("培训需求调查填报"); } noticeTask = NotificationTaskService.InsertUserNoticeTaskModels(allSendTitles, allSendDataIds, entity.ORG_ID, allSendUserIds, allSendUserNames, DateTime.Now, entity.END_TIME.Value, (int)FMNoticeTypeEnum.消息, "SE007_INPUTPAGE"); } Nav_JoinDepartment.ForEach(p => p.Nav_Department = null ); Nav_Points.ForEach(p => p.Nav_Point = null ); entity.Nav_JoinDepartment = null; entity.Nav_Points = null; entity.Nav_Demands = null; T_FM_NOTIFICATION_TASK finishNotice = null; if (entity.TaskID != Guid.Empty) { finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID, "SE007_SHOWPRINT"); } UnifiedCommit(() => { UpdateEntityNoCommit(entity); if (Nav_JoinDepartment != null && Nav_JoinDepartment.Any()) BantchSaveEntityNoCommit(Nav_JoinDepartment); if (Nav_Points != null && Nav_Points.Any()) BantchSaveEntityNoCommit(Nav_Points); if (Nav_Demands != null && Nav_Demands.Any()) BantchAddEntityNoCommit(Nav_Demands); if (noticeTask != null && noticeTask.Any()) BantchAddEntityNoCommit(noticeTask); if (finishNotice != null) UpdateEntityNoCommit(finishNotice); }); return true; }); } /// /// 提交审阅 /// /// /// [HttpPost, Route("SubmitReport")] public JsonActionResult SubmitReport([FromBody] KeywordFilter filter) { return SafeExecute(() => { var finishId = new Guid(filter.Parameter2); var data = GetEntity(filter.Parameter1); if (data == null || data.STATUS != SETrainSurveyStatus.汇总中) { throw new Exception("当前状记不可提交审阅"); } data.STATUS = SETrainSurveyStatus.完成; var finishNoitice = NotificationTaskService.GetTaskFinishModel(finishId); UnifiedCommit(() => { UpdateEntityNoCommit(data, "STATUS"); if (finishNoitice != null) UpdateEntityNoCommit(finishNoitice); }); return true; }); } /// /// 获得单条实体数据 /// /// /// [HttpPost, Route("GetUserDemand")] public JsonActionResult GetUserDemand([FromBody] KeywordFilter filter) { var uid = APT.Infrastructure.Api.AppContext.CurrentSession.UserID; var data = GetEntity( t => t.USER_ID == uid, filter); var ret = new JsonActionResult(); ret.IsSuccessful = (data != null); ret.TotalCount = data == null ? 0 : 1; ret.Data = data; if (data == null) { ret.ErrorMessage = "未查到相应数据"; } return ret; } /// /// 保存 /// /// /// [HttpPost, Route("SubmitUserDemand")] public JsonActionResult SubmitUserDemand([FromBody] T_SE_TRAIN_SURVEY_LIST_DEMAND entity) { return SafeExecute(() => { if (entity.OK == 1) { throw new Exception("已提交的数据不可更改"); } T_FM_NOTIFICATION_TASK finishNotice = null; var items = entity.Nav_Items; entity.Nav_Items = null; foreach (var item in items) { item.Nav_DEMAND = null; } if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify")) { var survry = GetEntity(t => t.ID == entity.SURVEY_ID); if (survry != null) { if (DateTime.Now > survry.END_TIME) { finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID); UnifiedCommit(() => { UpdateEntityNoCommit(finishNotice); }); throw new Exception("已过截止时间!"); } } var currItemsCounts = items.Count(t => t.IS_DELETED == false); if (currItemsCounts == 0 && entity.OTHER == null) { throw new Exception("请选择需求或填写其他"); } entity.OK = 1; finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID); } UnifiedCommit(() => { UpdateEntityNoCommit(entity); BantchSaveEntityNoCommit(items); if (finishNotice != null) UpdateEntityNoCommit(finishNotice); }); return true; }); } private void GetUsersTree(List users, Guid departID, bool getChild = false) { var us = GetEntities(it => it.DEPARTMENT_ID == departID, new BaseFilter(null), new string[] { "Nav_Department" }).ToList(); if (us != null && us.Count > 0) { users.AddRange(us); } if (getChild) { var dparts = GetEntities(it => it.PARENT_ID == departID, new BaseFilter(null)).ToList(); if (dparts != null && dparts.Count > 0) { dparts.ForEach(d => GetUsersTree(users, d.ID, getChild)); } } } /// /// 查找部门下的所有人 /// /// [HttpPost, Route("GetDepartmentUsers")] public List GetDepartmentUsers(List departmentIDs, Guid? orgId) { var allUsers = new List(); var departments = GetEntities(t => departmentIDs.Contains(t.ID), new BaseFilter(orgId)); var departmentInfos = departments.Where(x => x.DEPARTMENT_TYPE == (int)FMDepartmentType.Company).ToList(); if (departmentInfos != null && departmentInfos.Any()) { var allUsers1 = GetEntities(i => i.ENABLE_STATUS == 0 && !i.CODE.Contains("admin"), new BaseFilter(orgId)).Select(t => t.ID).Distinct().ToList(); allUsers.AddRange(allUsers1); } else { List outputDepartmentIds = new List(); GetDepartmentIds(departmentIDs, orgId, ref outputDepartmentIds); var allUsers1 = GetEntities(i => i.ENABLE_STATUS == 0 && i.DEPARTMENT_ID != null && outputDepartmentIds.Contains((Guid)i.DEPARTMENT_ID) && !i.CODE.Contains("admin"), new BaseFilter(orgId)).Select(t => t.ID).ToList(); allUsers.AddRange(allUsers1); } return allUsers; } /// /// 返回所有部门节点 /// private void GetDepartmentIds(List departmentIdList, Guid? orgId, ref List departmentIds) { departmentIds.AddRange(departmentIdList); var departmentTemps = GetEntities(t => t.PARENT_ID != null && departmentIdList.Contains((Guid)t.PARENT_ID), new BaseFilter(orgId)); if (departmentTemps != null && departmentTemps.Any()) { var temp = departmentTemps.Select(t => t.ID).Distinct().ToList(); GetDepartmentIds(temp, orgId, ref departmentIds); } } /// /// 获得单条实体数据 /// /// 过滤实体 /// [HttpPost, Route("OrderPaged")] public PagedActionResult OrderPaged([FromBody] BasePageFilter filter) { var result = WitOrderPaged(t=>t.TASK_ID!=null, filter); return result; } } }