386 lines
18 KiB
C#
386 lines
18 KiB
C#
using APT.BaseData.Domain.ApiModel;
|
||
using APT.BaseData.Domain.Entities;
|
||
using APT.BaseData.Domain.Entities.FM;
|
||
using APT.BaseData.Domain.Enums;
|
||
using APT.BaseData.Services.Services.FM;
|
||
using APT.Infrastructure.Core;
|
||
using APT.MS.Domain.Entities.BS;
|
||
using APT.MS.Domain.Entities.HM;
|
||
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.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace APT.HM.WebApi.Controllers.Api
|
||
{
|
||
/// <summary>
|
||
/// 作业任务库
|
||
/// </summary>
|
||
[Route("api/HM/HMOperationLink")]
|
||
public partial class OperationLinkController : AuthorizeApiController<T_HM_OPERATION_LINK>
|
||
{
|
||
/// <summary>
|
||
/// 更新或新增数据
|
||
/// </summary>
|
||
/// <param name="entity">对象实体</param>
|
||
/// <returns></returns>
|
||
[HttpPost, Route("FullUpdate")]
|
||
public JsonActionResult<bool> FullUpdate([FromBody] T_HM_OPERATION_LINK entity)
|
||
{
|
||
return SafeExecute(() =>
|
||
{
|
||
var history = this.GetEntity<T_HM_OPERATION_LINK>(t => t.OPERATION_STEP_ID == entity.OPERATION_STEP_ID && t.ID != entity.ID, "Nav_OperationStep");
|
||
if (history != null)
|
||
throw new Exception("已存在名为"+ history.Nav_OperationStep.NAME+"的数据,请修改作业名称");
|
||
var details = entity.Nav_LinkPost;
|
||
entity.Nav_OperationLinkEvaluateRisk = null;
|
||
entity.Nav_LinkPost = null;
|
||
var areas = entity.Nav_LinkArea;
|
||
entity.Nav_LinkArea = null;
|
||
if (areas != null && areas.Any())
|
||
{
|
||
areas.ForEach(t =>
|
||
{
|
||
t.ORG_ID = entity.ORG_ID;
|
||
t.OPERATION_LINK_ID = entity.ID;
|
||
t.Nav_Area = null;
|
||
});
|
||
}
|
||
if (details != null && details.Any())
|
||
{
|
||
details.ForEach(t =>
|
||
{
|
||
t.ORG_ID = entity.ORG_ID;
|
||
t.OPERATION_LINK_ID = entity.ID;
|
||
t.Nav_Post = null;
|
||
});
|
||
var postName = details.Where(x => x.Nav_Post != null).Select(m => m.Nav_Post.NAME).Distinct().ToList();
|
||
entity.POST = string.Join(",", postName);
|
||
}
|
||
var safeConfirms = entity.Nav_SafeConfirms;
|
||
entity.Nav_SafeConfirms = null;
|
||
if (safeConfirms != null && safeConfirms.Any())
|
||
{
|
||
safeConfirms.ForEach(t =>
|
||
{
|
||
t.ORG_ID = entity.ORG_ID;
|
||
t.OPERATION_LINK_ID = entity.ID;
|
||
});
|
||
}
|
||
var safeMesasures = entity.Nav_SafeMeasures;
|
||
entity.Nav_SafeMeasures = null;
|
||
if (safeMesasures != null && safeMesasures.Any())
|
||
{
|
||
safeMesasures.ForEach(t =>
|
||
{
|
||
t.ORG_ID = entity.ORG_ID;
|
||
t.OPERATION_LINK_ID = entity.ID;
|
||
});
|
||
}
|
||
var safeDeal = entity.Nav_DealMeasures;
|
||
entity.Nav_DealMeasures = null;
|
||
if (safeDeal != null && safeDeal.Any())
|
||
{
|
||
safeDeal.ForEach(t =>
|
||
{
|
||
t.ORG_ID = entity.ORG_ID;
|
||
t.OPERATION_LINK_ID = entity.ID;
|
||
});
|
||
}
|
||
UnifiedCommit(() =>
|
||
{
|
||
if (entity != null)
|
||
this.UpdateEntityNoCommit(entity);
|
||
if (details != null && details.Any())
|
||
this.BantchSaveEntityNoCommit(details);
|
||
if (areas != null && areas.Any())
|
||
this.BantchSaveEntityNoCommit(areas);
|
||
if (safeConfirms != null && safeConfirms.Any())
|
||
this.BantchSaveEntityNoCommit(safeConfirms);
|
||
if (safeMesasures != null && safeMesasures.Any())
|
||
this.BantchSaveEntityNoCommit(safeMesasures);
|
||
if (safeDeal != null && safeDeal.Any())
|
||
this.BantchSaveEntityNoCommit(safeDeal);
|
||
});
|
||
return true;
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 获取
|
||
/// </summary>
|
||
/// <param name="filter"></param>
|
||
/// <returns></returns>
|
||
[HttpPost, Route("GetEdit")]
|
||
public JsonActionResult<T_HM_OPERATION_LINK> GetEdit([FromBody] KeywordFilter filter)
|
||
{
|
||
return SafeExecute(() => {
|
||
var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
|
||
if (string.IsNullOrEmpty(id))
|
||
this.ThrowError("030017");
|
||
var result = this.GetEntity<T_HM_OPERATION_LINK>(id, new string[] { "Nav_OperationStep", "Nav_WorkPermitType","Nav_LinkPost","Nav_LinkPost.Nav_Post",
|
||
"Nav_SafeConfirms", "Nav_SafeMeasures", "Nav_DealMeasures", "Nav_Files","Nav_Unit" });
|
||
if (result.Nav_LinkPost != null && result.Nav_LinkPost.Any())
|
||
{
|
||
var postName = result.Nav_LinkPost.Where(t=>t.Nav_Post!=null).Select(m => m.Nav_Post.NAME).Distinct().ToList();
|
||
result.POST = string.Join(",", postName);
|
||
}
|
||
if (result.Nav_SafeConfirms != null && result.Nav_SafeConfirms.Any())
|
||
{
|
||
result.Nav_SafeConfirms.OrderBy(t => t.NUM);
|
||
}
|
||
if (result.Nav_SafeMeasures != null && result.Nav_SafeMeasures.Any())
|
||
{
|
||
result.Nav_SafeMeasures.OrderBy(t => t.NUM);
|
||
}
|
||
if (result.Nav_DealMeasures != null && result.Nav_DealMeasures.Any())
|
||
{
|
||
result.Nav_DealMeasures.OrderBy(t => t.NUM);
|
||
}
|
||
return result;
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 排序分页查询数据
|
||
/// </summary>
|
||
/// <param name="pageFilter">分页过滤实体</param>
|
||
/// <returns></returns>
|
||
[HttpPost, Route("FullOrderPaged")]
|
||
public PagedActionResult<T_HM_OPERATION_LINK> FullOrderPaged([FromBody] KeywordPageFilter pageFilter)
|
||
{
|
||
KeywordPageFilter keywordPageFilter = new KeywordPageFilter();
|
||
keywordPageFilter = pageFilter;
|
||
var rule = keywordPageFilter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "POST");
|
||
if (rule != null)
|
||
keywordPageFilter.FilterGroup.Rules.Remove(rule);
|
||
keywordPageFilter.IgnoreDataRule = true;
|
||
//pageFilter.Include.Add("Nav_LinkPost");
|
||
//pageFilter.Include.Add("Nav_LinkPost.Nav_Post");
|
||
Expression<Func<T_HM_OPERATION_LINK, bool>> expression = e => !e.IS_DELETED;
|
||
//var mineType = APT.Infrastructure.Api.AppContext.CurrentSession.MineType;
|
||
//if (!string.IsNullOrEmpty(mineType))
|
||
//{
|
||
// var tempList = mineType.Split(",").ToList();
|
||
// expression = expression.And(t => tempList.Contains(t.MineType.ToString()));
|
||
//}
|
||
var result = this.GetOrderPageEntities<T_HM_OPERATION_LINK>(expression, keywordPageFilter, null);
|
||
if (result.TotalCount > 0)
|
||
{
|
||
result.Data.ForEach(t =>
|
||
{
|
||
if (t.Nav_LinkPost != null && t.Nav_LinkPost.Any())
|
||
{
|
||
var postName = t.Nav_LinkPost.Where(x => x.Nav_Post != null).Select(m => m.Nav_Post.NAME).Distinct().ToList();
|
||
t.POST = string.Join(",", postName);
|
||
}
|
||
if (t.Nav_SafeConfirms != null && t.Nav_SafeConfirms.Any())
|
||
{
|
||
foreach (var safe in t.Nav_SafeConfirms)
|
||
{
|
||
var safeTemp = safe.NUM + safe.NAME + ";";
|
||
t.SafeConfirmsStr = t.SafeConfirmsStr + safeTemp;
|
||
}
|
||
}
|
||
if (t.Nav_SafeMeasures != null && t.Nav_SafeMeasures.Any())
|
||
{
|
||
foreach (var safe in t.Nav_SafeMeasures)
|
||
{
|
||
var safeTemp = safe.NUM + safe.NAME + ";";
|
||
t.SafeMeasuresStr = t.SafeMeasuresStr + safeTemp;
|
||
}
|
||
}
|
||
if (t.Nav_DealMeasures != null && t.Nav_DealMeasures.Any())
|
||
{
|
||
foreach (var safe in t.Nav_DealMeasures)
|
||
{
|
||
var safeTemp = safe.NUM + safe.NAME + ";";
|
||
t.DealMeasuresStr = t.DealMeasuresStr + safeTemp;
|
||
}
|
||
}
|
||
});
|
||
if (rule != null)
|
||
{
|
||
result.Data = result.Data.Where(t =>t.POST!=null && t.POST.Contains(rule.Value.ToString()));
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 获取导入数据
|
||
/// 参考 [Route("api/PF/Import")]
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost, Route("GetImportData")]
|
||
public JsonActionResult<ImportDataModel> GetImportData()
|
||
{
|
||
return SafeExecute<ImportDataModel>(() =>
|
||
{
|
||
var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
|
||
var httpRequest = this.HttpContext.Request;
|
||
string orgIdStr = httpRequest.Form["OrgId"];// 获取 组织
|
||
ImportDataModel result = new ImportDataModel();
|
||
var dic = Path.Combine(System.AppContext.BaseDirectory, "tempImportFiles");
|
||
if (!Directory.Exists(dic))
|
||
Directory.CreateDirectory(dic);
|
||
foreach (var key in httpRequest.Form.Files) // 文件键
|
||
{
|
||
var postedFile = key; // 获取文件键对应的文件对象
|
||
string filePath = Path.Combine(dic, DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_" + postedFile.FileName);
|
||
Byte[] fileData = new Byte[postedFile.Length];
|
||
Stream sr = postedFile.OpenReadStream();//创建数据流对象
|
||
sr.Read(fileData, 0, (int)postedFile.Length);
|
||
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
|
||
{
|
||
fs.Write(fileData, 0, fileData.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
}
|
||
|
||
//获取数据
|
||
Dictionary<int, int> startRowIndexs = new Dictionary<int, int>();
|
||
startRowIndexs.Add(0, 2);//根据Excel格式数据赋值
|
||
var dataTables = FileUtils.ReadExcelByOledb(filePath, startRowIndexs);
|
||
string Msg = string.Empty;
|
||
|
||
bool isOK = InsertModel(dataTables.Tables[0], orgId, ref Msg);
|
||
|
||
try
|
||
{
|
||
System.IO.File.Delete(filePath);
|
||
}
|
||
catch { }
|
||
|
||
result.Data = Msg;
|
||
result.MessageList = new List<string> { Msg };
|
||
}
|
||
|
||
return result;
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数据插入
|
||
/// </summary>
|
||
/// <param name="dtSource"></param>
|
||
/// <param name="OrgID"></param>
|
||
/// <param name="Msg"></param>
|
||
/// <param name="rowIndex"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
public bool InsertModel(DataTable dtSource, Guid? orgId, ref string Msg, int rowIndex = 1)
|
||
{
|
||
var userId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
|
||
if (dtSource == null || dtSource.Rows.Count < rowIndex)
|
||
{
|
||
Msg = "未获取到导入数据";
|
||
throw new Exception(Msg);
|
||
}
|
||
int rowAll = dtSource.Rows.Count;
|
||
|
||
//作业任务库
|
||
var links = this.GetEntities<T_HM_OPERATION_LINK>(t => !t.IS_DELETED, new BaseFilter(orgId));
|
||
//作业环节
|
||
var steps = this.GetEntities<T_HM_OPERATION_STEP>(t => !t.IS_DELETED, new BaseFilter(orgId));
|
||
//作业许可类别
|
||
var workTypes = this.GetEntities<T_HM_WORK_PERMIT_TYPE>(t => !t.IS_DELETED, new BaseFilter(orgId));
|
||
//生产单元
|
||
var proUnits = this.GetEntities<T_FM_USER_PRODUCTION_UNIT_SET>(t => !t.IS_DELETED, new BaseFilter(orgId));
|
||
//人员岗位
|
||
var posts = this.GetEntities<T_FM_USER_POST>(t => !t.IS_DELETED, new BaseFilter(orgId));
|
||
List<T_HM_OPERATION_LINK> linkList = new List<T_HM_OPERATION_LINK>();
|
||
List<T_HM_OPERATION_LINK_POST> linkPostList = new List<T_HM_OPERATION_LINK_POST>();
|
||
List<T_HM_OPERATION_TASK_SAFE_CONFIRM> linkConList = new List<T_HM_OPERATION_TASK_SAFE_CONFIRM>();
|
||
List<T_HM_OPERATION_TASK_SAFE_MEASURE> linkMeasList = new List<T_HM_OPERATION_TASK_SAFE_MEASURE>();
|
||
List<T_HM_OPERATION_TASK_DEAL_MEASURE> linkDealList = new List<T_HM_OPERATION_TASK_DEAL_MEASURE>();
|
||
for (int i = 0; i < rowAll; i++)
|
||
{
|
||
if (string.IsNullOrEmpty(dtSource.Rows[i][0].ToString().Trim()) || string.IsNullOrEmpty(dtSource.Rows[i][1].ToString().Trim())
|
||
|| string.IsNullOrEmpty(dtSource.Rows[i][2].ToString().Trim()) || string.IsNullOrEmpty(dtSource.Rows[i][3].ToString().Trim())
|
||
|| string.IsNullOrEmpty(dtSource.Rows[i][4].ToString().Trim()) || string.IsNullOrEmpty(dtSource.Rows[i][5].ToString().Trim())
|
||
|| string.IsNullOrEmpty(dtSource.Rows[i][6].ToString().Trim()) || string.IsNullOrEmpty(dtSource.Rows[i][7].ToString().Trim()))
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行生产单元,作业环节,任务类型,是否关键作业,作业周期,作业许可审批层级,作业许可类别,作业等级存在空数据,请先检查所有字段内容;";
|
||
}
|
||
}
|
||
//先做一次非空判断
|
||
if (!string.IsNullOrEmpty(Msg))
|
||
{
|
||
throw new Exception(Msg);
|
||
}
|
||
for (int i = 0; i < rowAll; i++)
|
||
{
|
||
var proUnit = dtSource.Rows[i][0].ToString().Trim();
|
||
var proUnitFirst = proUnits.FirstOrDefault(t => t.NAME == proUnit);
|
||
if (proUnitFirst == null)
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行生产单元"+ proUnit + "不存在,请先到系统管理维护;";
|
||
}
|
||
var step = dtSource.Rows[i][1].ToString().Trim();
|
||
var stepFirst = steps.FirstOrDefault(t => t.NAME == step);
|
||
if (stepFirst == null)
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行作业环节" + step + "不存在,请先到系统管理维护;";
|
||
}
|
||
else
|
||
{
|
||
var linkFirst = links.FirstOrDefault(t => t.OPERATION_STEP_ID == stepFirst.ID);
|
||
if (linkFirst != null)
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行作业环节" + step + "已存在库中,请勿重复导入;";
|
||
}
|
||
}
|
||
var workType = dtSource.Rows[i][6].ToString().Trim();
|
||
var workTypeFirst = workTypes.FirstOrDefault(t => t.NAME == workType);
|
||
if (workTypeFirst == null)
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行作业许可类别"+ workType + "不存在,请先到系统管理维护;";
|
||
}
|
||
if (!string.IsNullOrEmpty(dtSource.Rows[i][9].ToString().Trim()))
|
||
{
|
||
var lists = dtSource.Rows[i][9].ToString().Trim().Split(",").ToList();
|
||
if (lists!=null && lists.Any())
|
||
{
|
||
foreach (var item in lists)
|
||
{
|
||
if (!string.IsNullOrEmpty(item))
|
||
{
|
||
var postFirst = posts.FirstOrDefault(t => t.NAME == item);
|
||
if (postFirst == null)
|
||
{
|
||
Msg = Msg + "第" + (i + 2) + "行岗位"+ item + "不存在,请先到系统管理维护;";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
if (!string.IsNullOrEmpty(Msg))
|
||
{
|
||
throw new Exception(Msg);
|
||
}
|
||
UnifiedCommit(() =>
|
||
{
|
||
if (linkList != null && linkList.Any())
|
||
BantchAddEntityNoCommit(linkList);
|
||
if (linkPostList != null && linkPostList.Any())
|
||
BantchAddEntityNoCommit(linkPostList);
|
||
if (linkConList != null && linkConList.Any())
|
||
BantchAddEntityNoCommit(linkConList);
|
||
if (linkMeasList != null && linkMeasList.Any())
|
||
BantchAddEntityNoCommit(linkMeasList);
|
||
if (linkDealList != null && linkDealList.Any())
|
||
BantchAddEntityNoCommit(linkDealList);
|
||
});
|
||
Msg = "导入成功!";
|
||
return true;
|
||
}
|
||
}
|
||
}
|