mh_sms/APT.MicroApi/APT.SC.WebApi/Controllers/Api/PTController/SafetyPolicySurveyController.cs
2024-01-22 09:17:01 +08:00

412 lines
22 KiB
C#

using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Services.DomainServices;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.ApiModel.SC.PT;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.SC;
using APT.MS.Domain.Entities.SC.PT;
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.Threading.Tasks;
namespace APT.SC.WebApi.Controllers.Api.PTController
{
/// <summary>
/// 安全方针调查表
/// </summary>
[Route("api/PT/PTSafetyPolicySurvey")]
public partial class SafetyPolicySurveyController : AuthorizeApiController<T_PT_SAFETY_POLICY_SURVEY>
{
IPFCodeRuleService CodeRuleService { get; set; }
IFMNotificationTaskService NotificationTaskService { get; set; }
public SafetyPolicySurveyController(IFMNotificationTaskService notificationTaskService, IPFCodeRuleService codeRuleService)
{
NotificationTaskService = notificationTaskService;
CodeRuleService = codeRuleService;
}
/// <summary>
/// 新增/编辑
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost, Route("FullUpdate")]
public JsonActionResult<bool> FullUpdate([FromBody] T_PT_SAFETY_POLICY_SURVEY entity)
{
return SafeExecute(() =>
{
var ranges = entity.Nav_Ranges.Where(t => !t.IS_DELETED).ToList();
//if (ranges == null || !ranges.Any())
// this.ThrowError("040001");
entity.Nav_Feedbacks = null;
var collects = entity.Nav_Collects.Where(t => !t.IS_DELETED).ToList();
entity.STATUS = PFStandardStatus.Draft;
entity.YEAR = DateTime.Now.Year.ToString();
var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
entity.USER_ID = loginUserId;
//var departmentId = this.GetEntity<T_FM_USER>(t => t.ID == loginUserId.Value && t.ENABLE_STATUS == 0)?.DEPARTMENT_ID;
entity.DEPARTMENT_ID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
List<T_FM_NOTIFICATION_TASK> notices = new List<T_FM_NOTIFICATION_TASK>();
T_FM_NOTIFICATION_TASK task = null;
if (ranges != null && ranges.Any())
{
ranges.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.SAFETY_POLICY_ID = entity.ID;
t.Nav_Department = null;
});
}
if (collects != null && collects.Any())
{
collects.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.SAFETY_POLICY_ID = entity.ID;
t.Nav_Collect = null;
});
}
if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
{
entity.STATUS = PFStandardStatus.Sign;
//发给选中范围内的所有人
//var userIds = GetDepartmentUsers(entity);
var userNames = new List<string>();
//默认发给所有人
var user = this.GetEntities<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum. && t.CODE!="admin", new BaseFilter(entity.ORG_ID));
var userIds = user.Select(t => t.ID).ToList();
foreach (var u in userIds)
{
var current = user.FirstOrDefault(t => t.ID == u);
userNames.Add(current?.NAME);
}
//发消息
notices = NotificationTaskService.InsertUserNoticeTaskModels("安全生产方针调查表", entity.ID, entity.ORG_ID, userIds, userNames, DateTime.Now,
DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum., "PT015");
if (entity.TaskID != Guid.Empty)
{
task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
task.SOURCE_FORMCODE = "PT014_SHOWPRINT";
}
}
entity.Nav_Ranges = null;
entity.Nav_Collects = null;
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (ranges != null && ranges.Any())
this.BantchSaveEntityNoCommit(ranges);
if (collects != null && collects.Any())
this.BantchSaveEntityNoCommit(collects);
if (notices != null && notices.Any())
this.BantchSaveEntityNoCommit(notices);
if (task != null)
this.UpdateEntityNoCommit(task);
});
return true;
});
}
/// <summary>
/// 查找部门下的所有人
/// </summary>
/// <param name="entity"></param>
[HttpPost, Route("GetDepartmentUsers")]
public List<Guid> GetDepartmentUsers(T_PT_SAFETY_POLICY_SURVEY entity)
{
var allUsers = new List<Guid>();
if (entity != null && entity.Nav_Ranges != null && entity.Nav_Ranges.Any())
{
var departmentIds = entity.Nav_Ranges.Where(t => t.DEPARTMENT_ID != null).Select(x => (Guid)x.DEPARTMENT_ID).Distinct().ToList();
var departments = this.GetEntities<T_FM_DEPARTMENT>(t => departmentIds.Contains(t.ID), new BaseFilter(entity.ORG_ID));
//先查询选中部门有没有公司级
var departmentInfos = departments.Where(x => x.DEPARTMENT_TYPE == (int)FMDepartmentType.).ToList();
//发给所有人
if (departmentInfos != null && departmentInfos.Any())
{
var allUsers1 = GetEntities<T_FM_USER>(i => i.ENABLE_STATUS == 0, new BaseFilter(entity.ORG_ID)).Select(t => t.ID).Distinct().ToList();
allUsers.AddRange(allUsers1);
}
else
{
List<Guid> outputDepartmentIds = new List<Guid>();
GetDepartmentIds(departmentIds, ref outputDepartmentIds);
//选中的部门人员
var allUsers1 = GetEntities<T_FM_USER>(i => i.ENABLE_STATUS == 0 && i.DEPARTMENT_ID != null && outputDepartmentIds.Contains((Guid)i.DEPARTMENT_ID), new BaseFilter(entity.ORG_ID)).Select(t => t.ID).Distinct().ToList();
allUsers.AddRange(allUsers1);
}
}
return allUsers;
}
/// <summary>
/// 返回所有部门节点
/// <returns></returns>
/// </summary>
private void GetDepartmentIds(List<Guid> departmentIdList, ref List<Guid> departmentIds)
{
var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
departmentIds.AddRange(departmentIdList);
var departmentTemps = GetEntities<T_FM_DEPARTMENT>(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, ref departmentIds);
}
}
/// <summary>
/// 调查表保存
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost, Route("SingleUpdate")]
public JsonActionResult<bool> SingleUpdate([FromBody] T_PT_SAFETY_POLICY_SURVEY entity)
{
return SafeExecute(() =>
{
if(entity.COMPLETE_DATE < DateTime.Now)
this.ThrowError("040002");
var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
var feedBack = this.GetEntity<T_PT_SAFETY_POLICY_SURVEY_FEEDBACK>(t=>t.SAFETY_POLICY_ID == entity.ID && t.USER_ID == loginUserId);
if (feedBack == null)
{
feedBack = new T_PT_SAFETY_POLICY_SURVEY_FEEDBACK();
feedBack.ORG_ID = entity.ORG_ID;
feedBack.SAFETY_POLICY_ID = entity.ID;
feedBack.GUIDELINES_ID = entity.GUIDELINES_ID;
feedBack.IS_AGREE = entity.IS_AGREE;
feedBack.OTHER_SUGGESTIONS = entity.OTHER_SUGGESTIONS;
feedBack.USER_ID = loginUserId;
var departmentId = this.GetEntity<T_FM_USER>(t => t.ID == (Guid)loginUserId && t.ENABLE_STATUS == 0)?.DEPARTMENT_ID;
feedBack.DEPARTMENT_ID = departmentId;
feedBack.STATUS = PFStandardStatus.Draft;
}
else
{
feedBack.IS_AGREE = entity.IS_AGREE;
feedBack.OTHER_SUGGESTIONS = entity.OTHER_SUGGESTIONS;
}
T_FM_NOTIFICATION_TASK task = null;
if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
{
feedBack.STATUS = PFStandardStatus.Archived;
if (entity.TaskID != Guid.Empty)
{
task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
task.SOURCE_FORMCODE = "PT014_SHOWPRINT";
}
}
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (feedBack != null)
this.UpdateEntityNoCommit(feedBack);
if (task != null)
this.UpdateEntityNoCommit(task);
});
return true;
});
}
/// <summary>
/// 获取
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
[HttpPost, Route("GetSingleEdit")]
public JsonActionResult<T_PT_SAFETY_POLICY_SURVEY> GetSingleEdit([FromBody] KeywordFilter filter)
{
return SafeExecute(() => {
var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
T_PT_SAFETY_POLICY_SURVEY entity = new T_PT_SAFETY_POLICY_SURVEY();
var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
if (!string.IsNullOrEmpty(id))
{
entity = this.GetEntity<T_PT_SAFETY_POLICY_SURVEY>(id, new string[] { "Nav_Department", "Nav_User", "Nav_Guidelines", "Nav_Collects", "Nav_Collects.Nav_Collect", "Nav_Feedbacks", "Nav_Feedbacks.Nav_Guidelines" });
if (entity.Nav_Feedbacks != null && entity.Nav_Feedbacks.Any())
{
var feedBack = entity.Nav_Feedbacks.FirstOrDefault(t => t.USER_ID == loginUserId);
if (feedBack != null)
{
entity.GUIDELINES_ID = feedBack.GUIDELINES_ID;
entity.Nav_Guidelines = feedBack.Nav_Guidelines;
entity.OTHER_SUGGESTIONS = feedBack.OTHER_SUGGESTIONS;
entity.IS_AGREE = feedBack.IS_AGREE;
}
else
entity.IS_AGREE = true;
}
else
entity.IS_AGREE = true;
}
return entity;
});
}
[HttpPost, Route("GetNew")]
public JsonActionResult<T_PT_SAFETY_POLICY_SURVEY> GetNew([FromBody] KeywordFilter filter)
{
return SafeExecute(() => {
var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
T_PT_SAFETY_POLICY_SURVEY entity = new T_PT_SAFETY_POLICY_SURVEY();
var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
if (!string.IsNullOrEmpty(id))
{
entity = this.GetEntity<T_PT_SAFETY_POLICY_SURVEY>(id, new string[] { "Nav_Department", "Nav_User", "Nav_Collects", "Nav_Collects.Nav_Collect", "Nav_Guidelines", "Nav_Ranges" });
}
return entity;
});
}
/// <summary>
/// 征集表获取
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
[HttpPost, Route("GetCollectEdit")]
public JsonActionResult<T_PT_SAFETY_POLICY_SURVEY> GetCollectEdit([FromBody] KeywordFilter filter)
{
return SafeExecute(() => {
T_PT_SAFETY_POLICY_SURVEY entity = new T_PT_SAFETY_POLICY_SURVEY();
var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
if (!string.IsNullOrEmpty(id))
{
var guidId = Guid.Parse(id);
entity = this.GetEntity<T_PT_SAFETY_POLICY_SURVEY>(t=>t.ID == guidId, new string[] { "Nav_Department", "Nav_User","Nav_Guidelines", "Nav_Collects.Nav_Collect" });//, "Nav_Feedbacks.Nav_User", "Nav_Feedbacks.Nav_Department","Nav_Feedbacks.Nav_Guidelines", "Nav_Ranges", "Nav_Ranges.Nav_Department"
entity.RATE = "0";
entity.ALL_USER_COUNT = this.GetCount<T_FM_USER>(t => t.ENABLE_STATUS == (int)FMEnableStatusEnum. && t.CODE != "admin", new BaseFilter(entity.ORG_ID));
//entity.IntentionalGuids = new List<IntentionalGuidModel>();
var feedBack = this.GetEntities<T_PT_SAFETY_POLICY_SURVEY_FEEDBACK>(t => t.SAFETY_POLICY_ID == entity.ID && t.STATUS == PFStandardStatus.Archived, new BaseFilter(filter.OrgId),new string[] { "Nav_User", "Nav_Department" }).ToList();
//var feedBack = entity.Nav_Feedbacks.Where(m => m.STATUS == PFStandardStatus.Archived).ToList();
if (feedBack != null && feedBack.Any())
{
entity.USER_COUNT = feedBack.Count();
decimal count = feedBack.Where(m => m.IS_AGREE == true).Count();
entity.RATE = String.Format("{0:F}", (count / entity.USER_COUNT * 100));
//var guidLine = feedBack.GroupBy(t => t.GUIDELINES_ID).ToList();
//var otherSuggetion = feedBack.Where(m=>!string.IsNullOrEmpty(m.OTHER_SUGGESTIONS)).GroupBy(t => t.OTHER_SUGGESTIONS).ToList();
//if (guidLine != null && guidLine.Any())
//{
// guidLine.ForEach(t =>
// {
// var guidName = feedBack.FirstOrDefault(m => m.GUIDELINES_ID == t.Key.Value).Nav_Guidelines.NAME;
// decimal count = feedBack.Where(m => m.GUIDELINES_ID == t.Key.Value).Count();
// decimal totalCount = feedBack.Count() + otherSuggetion.Count();
// var rateString = String.Format("{0:F}", (count / totalCount * 100));
// entity.IntentionalGuids.Add(new IntentionalGuidModel
// {
// GUIDELINES_ID = t.Key.Value,
// GUIDELINES_NAME = guidName,
// RATE = rateString,
// }) ;
// });
//}
//if (otherSuggetion != null && otherSuggetion.Any())
//{
// otherSuggetion.ForEach(t =>
// {
// decimal count = feedBack.Where(m => m.OTHER_SUGGESTIONS == t.Key).Count();
// decimal totalCount = feedBack.Count() + otherSuggetion.Count();
// var rateString = String.Format("{0:F}", (count / totalCount * 100));
// entity.IntentionalGuids.Add(new IntentionalGuidModel
// {
// GUIDELINES_NAME = t.Key,
// RATE = rateString,
// });
// });
//}
entity.Nav_Feedbacks= feedBack.OrderBy(t => t.IS_AGREE).ToList();
}
entity.USER_RATE = entity.ALL_USER_COUNT ==0?"0": String.Format("{0:F}", ((decimal)entity.USER_COUNT / entity.ALL_USER_COUNT * 100));
}
//if (!string.IsNullOrEmpty(filter.Keyword))
//{
// entity.Nav_Feedbacks = entity.Nav_Feedbacks.Where(t => t.GUIDELINES_ID == Guid.Parse(filter.Keyword)).ToList();
//}
//if (string.IsNullOrEmpty(filter.Keyword) && !string.IsNullOrEmpty(filter.Parameter1))
//{
// entity.Nav_Feedbacks = entity.Nav_Feedbacks.Where(t => t.OTHER_SUGGESTIONS == filter.Parameter1).ToList();
//}
return entity;
});
}
/// <summary>
/// 征集表已阅
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
[HttpPost, Route("IdentityUpdate")]
public JsonActionResult<bool> IdentityUpdate([FromBody] KeywordFilter filter)
{
return SafeExecute(() => {
T_PT_SAFETY_POLICY_SURVEY entity = null;
var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
var taskId = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "TASK_ID").Value.ToString();
T_FM_NOTIFICATION_TASK task = null;
T_SC_MT_MEETING met = null;
T_SC_MT_MEETING_CONTENT meetContent = null;
T_FM_NOTIFICATION_TASK sendNotice = null;
if (!string.IsNullOrEmpty(taskId) && !string.IsNullOrEmpty(id))
{
entity = this.GetEntity<T_PT_SAFETY_POLICY_SURVEY>(id);
entity.STATUS = PFStandardStatus.Archived;
task = NotificationTaskService.GetTaskFinishModel(Guid.Parse(taskId));
var userInfo = this.GetEntity<T_FM_USER>(t => t.Nav_ApproveRole != null && t.Nav_ApproveRole.NAME == "安环部负责人" && t.ENABLE_STATUS == 0);
if (userInfo == null && entity.ORG_ID.ToString() == "3efd5276-632b-e379-9ff3-7a7546591fca")
{
userInfo = this.GetEntity<T_FM_USER>(t => t.Nav_ApproveRole != null && t.Nav_ApproveRole.NAME == "安环部安全员" && t.ENABLE_STATUS == 0);
}
if (userInfo != null)
{
//发起普通会议通知
met = new T_SC_MT_MEETING();
//取审批流水码
var sysFilter = new SystemCodeFilter();
sysFilter.CodeType = (int)PFCodeRuleType.;
sysFilter.Count = 1;
sysFilter.OrgId = userInfo.ORG_ID;
var codes = CodeRuleService.NewGenSerial(sysFilter);
var serialCode = codes.Split(new char[] { ',' });
met.CODE = serialCode[0];
met.ORG_ID = filter.OrgId;
met.NAME = "安全生产方针征集表会议通知";
met.MEETINGTYPE = SCMEETINGTYPE.Ordinary;
met.DEPARTMENT_ID = (Guid)userInfo.DEPARTMENT_ID;
met.USER_ID_ORIGINATOR = userInfo.ID;
var metContent = this.GetEntity<T_SC_MT_CONTENT_NAME>(t => t.NAME == "安全生产方针讨论" && t.ENABLE_STATUS == FMEnableStatusEnum.);
if (metContent != null)
{
meetContent = new T_SC_MT_MEETING_CONTENT();
meetContent.ORG_ID = met.ORG_ID;
meetContent.MEETING_ID = met.ID;
meetContent.CONTENTNAME_ID = metContent.ID;
}
sendNotice = NotificationTaskService.InsertUserNoticeTaskModel("安全生产方针征集表会议通知", met.ID, filter.OrgId, userInfo.ID, userInfo.NAME, DateTime.Now, DateTime.Now.AddDays(30), (int)FMNoticeTypeEnum., "SC032");
}
}
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (task != null)
this.UpdateEntityNoCommit(task);
if (met != null)
UpdateEntityNoCommit(met);
if (meetContent != null)
UpdateEntityNoCommit(meetContent);
if (sendNotice != null)
UpdateEntityNoCommit(sendNotice);
});
return true;
});
}
}
}