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.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SC.PT;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;
using APT.BaseData.Services.DomainServices;
using APT.BaseData.Services.Services.FM;
using APT.BaseData.Domain.IServices;
using APT.MS.Domain.Entities.HM;
using System.Linq.Expressions;
namespace APT.SC.WebApi.Controllers.Api.PTController
{
///
/// 安全生产目标与指标制定表
///
[Route("api/PT/PTIndicatorFormulation")]
public partial class IndicatorFormulationController : AuthorizeApiController
{
IPFCodeRuleService CodeRuleService { get; set; }
IFMNotificationTaskService NotificationTaskService { get; set; }
IFMFlowPermitService MFlowPermitService { get; set; }
IPFApproveCallBackService ApproveCallBackService { get; set; }
public IndicatorFormulationController(IPFCodeRuleService codeRuleService, IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
{
CodeRuleService = codeRuleService;
NotificationTaskService = notificationTaskService;
MFlowPermitService = mFlowPermitService;
ApproveCallBackService = approveCallBackService;
}
///
/// 新增/编辑
///
///
///
[HttpPost, Route("FullUpdate")]
public JsonActionResult FullUpdate([FromBody] T_PT_INDICATOR_FORMULATION entity)
{
return SafeExecute(() =>
{
var goals = entity.Nav_Goals.Where(t => !t.IS_DELETED).ToList();
var files = entity.Nav_Files.Where(t => !t.IS_DELETED).ToList();
var planFiles = entity.Nav_PlanFiles.Where(t => !t.IS_DELETED).ToList();
var formulates = entity.Nav_Formulates.Where(t => !t.IS_DELETED).ToList();
var planResources = entity.Nav_PlanResources.Where(t => !t.IS_DELETED).ToList();
entity.NAME = "安全生产目标与指标制定";
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 => t.ID == loginUserId.Value && t.ENABLE_STATUS == 0)?.DEPARTMENT_ID;
entity.VERSION = (entity.VERSION == null || entity.VERSION == 0) ? 1:entity.VERSION;
entity.DEPARTMENT_ID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID;
entity.SET_TIME = DateTime.Now;
List notices = new List();
T_FM_NOTIFICATION_TASK task = null;
if (goals != null && goals.Any())
{
goals.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
});
}
if (files != null && files.Any())
{
files.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_ImgFile = null;
});
}
if (planFiles != null && planFiles.Any())
{
planFiles.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_ImgFile = null;
});
}
if (formulates != null && formulates.Any())
{
formulates.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_Formulate = null;
});
}
if (planResources != null && planResources.Any())
{
planResources.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_Resources = null;
});
}
if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
{
entity.STATUS = PFStandardStatus.Approving;
entity.Nav_Goals = null;
entity.Nav_Files = null;
entity.Nav_PlanFiles = null;
entity.Nav_Formulates = null;
entity.Nav_PlanResources = 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[] { ',' });
MFlowPermitService.InsertApprove(serialCode[0], "PT017", "", entity.ID, "PT017_SHOWPRINT", entity.TaskID, true, () =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (goals != null && goals.Any())
this.BantchSaveEntityNoCommit(goals);
if (files != null && files.Any())
this.BantchSaveEntityNoCommit(files);
if (planFiles != null && planFiles.Any())
this.BantchSaveEntityNoCommit(planFiles);
if (formulates != null && formulates.Any())
this.BantchSaveEntityNoCommit(formulates);
if (planResources != null && planResources.Any())
this.BantchSaveEntityNoCommit(planResources);
if (task != null)
this.UpdateEntityNoCommit(task);
}, null, null, null, null, null, "PT017_SHOWPRINT",null);
return true;
}
//if (entity.TaskID != Guid.Empty)
//{
// task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
// task.SOURCE_FORMCODE = "PT017_SHOWPRINT";
//}
entity.Nav_Goals = null;
entity.Nav_Files = null;
entity.Nav_PlanFiles = null;
entity.Nav_Formulates = null;
entity.Nav_PlanResources = null;
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (goals != null && goals.Any())
this.BantchSaveEntityNoCommit(goals);
if (files != null && files.Any())
this.BantchSaveEntityNoCommit(files);
if (planFiles != null && planFiles.Any())
this.BantchSaveEntityNoCommit(planFiles);
if (formulates != null && formulates.Any())
this.BantchSaveEntityNoCommit(formulates);
if (planResources != null && planResources.Any())
this.BantchSaveEntityNoCommit(planResources);
if (task != null)
this.UpdateEntityNoCommit(task);
});
return true;
});
}
///
/// 回调函数
///
///
///
[HttpGet, Route("BackUpdate")]
public JsonActionResult BackUpdate(string id)
{
return SafeExecute(() =>
{
return ApproveCallBackService.CallBack("PT/PTIndicatorFormulation/BackUpdate", id);
});
}
///
/// 修订
///
///
///
[HttpPost, Route("FullRevisionUpdate")]
public JsonActionResult FullRevisionUpdate([FromBody] T_PT_INDICATOR_FORMULATION entity)
{
return SafeExecute(() =>
{
var indecator = this.GetEntity(entity.ID.ToString());
if (indecator != null)
{
if (indecator.STATUS != PFStandardStatus.Archived)
this.ThrowError("040003");
}
var goals = entity.Nav_Goals;
var files = entity.Nav_Files;
var planFiles = entity.Nav_PlanFiles;
var formulates = entity.Nav_Formulates;
var planResources = entity.Nav_PlanResources;
entity.ID = Guid.NewGuid();
entity.NAME = "安全生产目标与指标";
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 => t.ID == (Guid)loginUserId && t.ENABLE_STATUS == 0)?.DEPARTMENT_ID;
entity.DEPARTMENT_ID = departmentId;
List notices = new List();
T_FM_NOTIFICATION_TASK task = null;
if (goals != null && goals.Any())
{
goals.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
});
}
if (files != null && files.Any())
{
files.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_ImgFile = null;
});
}
if (planFiles != null && planFiles.Any())
{
planFiles.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_ImgFile = null;
});
}
if (formulates != null && formulates.Any())
{
formulates.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_Formulate = null;
});
}
if (planResources != null && planResources.Any())
{
planResources.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID; t.INDICATOR_FORMULATION_ID = entity.ID;
t.Nav_Resources = null;
});
}
if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
{
entity.STATUS = PFStandardStatus.Approving;
entity.Nav_Goals = null;
entity.Nav_Files = null;
entity.Nav_PlanFiles = null;
entity.Nav_Formulates = null;
entity.Nav_PlanResources = 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[] { ',' });
MFlowPermitService.InsertApprove(serialCode[0], "PT017", "", entity.ID, "PT017_SHOWPRINT", entity.TaskID, true, () =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (goals != null && goals.Any())
this.BantchSaveEntityNoCommit(goals);
if (files != null && files.Any())
this.BantchSaveEntityNoCommit(files);
if (planFiles != null && planFiles.Any())
this.BantchSaveEntityNoCommit(planFiles);
if (formulates != null && formulates.Any())
this.BantchSaveEntityNoCommit(formulates);
if (planResources != null && planResources.Any())
this.BantchSaveEntityNoCommit(planResources);
if (task != null)
this.UpdateEntityNoCommit(task);
}, null, null, null, null, null, "PT017_SHOWPRINT", null);
return true;
}
if (entity.TaskID != Guid.Empty)
{
task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
task.SOURCE_FORMCODE = "PT017_SHOWPRINT";
}
entity.Nav_Goals = null;
entity.Nav_Files = null;
entity.Nav_PlanFiles = null;
entity.Nav_Formulates = null;
entity.Nav_PlanResources = null;
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (goals != null && goals.Any())
this.BantchSaveEntityNoCommit(goals);
if (files != null && files.Any())
this.BantchSaveEntityNoCommit(files);
if (planFiles != null && planFiles.Any())
this.BantchSaveEntityNoCommit(planFiles);
if (formulates != null && formulates.Any())
this.BantchSaveEntityNoCommit(formulates);
if (planResources != null && planResources.Any())
this.BantchSaveEntityNoCommit(planResources);
if (task != null)
this.UpdateEntityNoCommit(task);
});
return true;
});
}
///
/// 获取版本号
///
///
///
[HttpPost, Route("GetVersionEdit")]
public JsonActionResult GetVersionEdit([FromBody] KeywordFilter filter)
{
return SafeExecute(() =>
{
T_PT_INDICATOR_FORMULATION entity = new T_PT_INDICATOR_FORMULATION();
filter.IgnoreDataRule = true;
//取最大时间的数据
var maxInfo = this.GetEntities(t=>!t.IS_DELETED, filter).OrderByDescending(m => m.CREATE_TIME).FirstOrDefault();
if (maxInfo != null)
{
entity = this.GetEntity(maxInfo.ID, new string[] { "Nav_User", "Nav_Department", "Nav_Formulates", "Nav_Formulates.Nav_Formulate", "Nav_PlanResources", "Nav_PlanResources.Nav_Resources", "Nav_Files", "Nav_Files.Nav_ImgFile", "Nav_Files.Nav_ImgFile.Nav_File",
"Nav_PlanFiles", "Nav_PlanFiles.Nav_ImgFile", "Nav_PlanFiles.Nav_ImgFile.Nav_File","Nav_Goals"});
entity.VERSION = entity.VERSION + 1;
}
return entity;
});
}
///
/// 驳回
///
///
///
[HttpGet, Route("RejectUpdate")]
public JsonActionResult RejectUpdate(string id)
{
return SafeExecute(() =>
{
var entity = this.GetEntity(id);
entity.STATUS = PFStandardStatus.Rejected;
//发消息
T_FM_NOTIFICATION_TASK notice = new T_FM_NOTIFICATION_TASK();
var user = this.GetEntity(t => t.ID == entity.CREATER_ID && t.ENABLE_STATUS == 0);
if (user != null)
{
notice = NotificationTaskService.InsertUserNoticeTaskModel("安全生产目标与指标已被驳回", entity.ID, entity.ORG_ID, user.ID, user.NAME, DateTime.Now,
DateTime.Now.AddHours(24), (int)FMNoticeTypeEnum.消息, "PT017");
}
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
if (notice != null)
this.UpdateEntityNoCommit(notice);
});
return true;
//return ApproveCallBackService.CallReject("PT/PTIndicatorFormulation/RejectUpdate", id);
});
}
///
/// 归档
///
///
///
[HttpGet, Route("ArchiveUpdate")]
public JsonActionResult ArchiveUpdate(string id)
{
return SafeExecute(() =>
{
var entity = this.GetEntity(id);
entity.STATUS = PFStandardStatus.Archived;
UnifiedCommit(() =>
{
if (entity != null)
this.UpdateEntityNoCommit(entity);
});
return true;
});
}
}
}