298 lines
12 KiB
C#
298 lines
12 KiB
C#
using Infrastructure.Attribute;
|
||
using Infrastructure.Extensions;
|
||
using Infrastructure.Model;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using MiniExcelLibs;
|
||
using RIZO.Model.Mes.Dto.MasterData;
|
||
using RIZO.Model.Mes.Dto.Process;
|
||
using RIZO.Model.Mes.MasterData;
|
||
using RIZO.Model.Mes.Process;
|
||
using RIZO.Model.System;
|
||
using RIZO.Repository;
|
||
using RIZO.Service.Mes.IMesService.Process;
|
||
using RIZO.Service.Mes.MasterData;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using static MiniExcelLib.Core.MiniExcel;
|
||
|
||
namespace RIZO.Service.Mes.Process
|
||
{
|
||
/// <summary>
|
||
/// 工艺表Service业务层处理
|
||
/// </summary>
|
||
[AppService(ServiceType = typeof(IProcessInfoService), ServiceLifetime = LifeTime.Transient)]
|
||
public class ProcessInfoService : BaseService<ProcessInfo>, IProcessInfoService
|
||
{
|
||
private OperationInfoService operationInfoService = new OperationInfoService();
|
||
private ProductionLineService productionLineService = new ProductionLineService();
|
||
/// <summary>
|
||
/// 查询工艺表列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public PagedInfo<ProcessInfoDto> GetList(ProcessInfoQueryDto parm)
|
||
{
|
||
var predicate = QueryExp(parm);
|
||
|
||
var response = Queryable()
|
||
.Where(predicate.ToExpression())
|
||
.ToPage<ProcessInfo, ProcessInfoDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
public ProcessInfo GetInfo(long Id)
|
||
{
|
||
var response = Queryable()
|
||
.Where(x => x.Id == Id)
|
||
.First();
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加工艺表
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public ProcessInfo AddProcessInfo(ProcessInfo model)
|
||
{
|
||
return Insertable(model).ExecuteReturnEntity();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改工艺表
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public int UpdateProcessInfo(ProcessInfo model)
|
||
{
|
||
return Update(model, true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询导出表达式
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
private static Expressionable<ProcessInfo> QueryExp(ProcessInfoQueryDto parm)
|
||
{
|
||
var predicate = Expressionable.Create<ProcessInfo>();
|
||
if (!string.IsNullOrWhiteSpace(parm.ProcessCode))
|
||
{
|
||
predicate.And(it => it.ProcessCode.Contains(parm.ProcessCode));
|
||
}
|
||
if (!string.IsNullOrWhiteSpace(parm.ProcessName))
|
||
{
|
||
predicate.And(it => it.ProcessName.Contains(parm.ProcessName));
|
||
}
|
||
return predicate;
|
||
}
|
||
|
||
/// <summary>
|
||
/// excel表格导入工艺路线
|
||
/// </summary>
|
||
/// <param name="stream"></param>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
public ApiResult ImportProcessInfo(Stream stream, string userId,string userName)
|
||
{
|
||
int code = 200;
|
||
string strMessage = "";
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(userId))
|
||
{
|
||
code = 400;
|
||
strMessage = "创建人不可为空!";
|
||
return new ApiResult(code, strMessage);
|
||
}
|
||
if (stream == null || stream.Length == 0)
|
||
{
|
||
code = 400;
|
||
strMessage = "导入的Excel流不能为空!";
|
||
return new ApiResult(code, strMessage);
|
||
}
|
||
if (stream.CanSeek)
|
||
{
|
||
stream.Position = 0;
|
||
}
|
||
|
||
List<ProcessInfo> processInfos = new List<ProcessInfo>();
|
||
List<OperationInfo> operationInfos = new List<OperationInfo>();
|
||
//工艺路线导入模板
|
||
List<ProcessInfoImport> processInfoImports = new List<ProcessInfoImport>();
|
||
List<ProcessInfoImport> resultList = stream.Query<ProcessInfoImport>().ToList();
|
||
List<string> porocessCodeList = new List<string>();
|
||
List<string> lineCodeList = new List<string>();
|
||
Dictionary<string, string> dicLineImport = new Dictionary<string, string>();
|
||
//从1开始循环,排除掉0行的标题行
|
||
for (int i = 1;i<resultList.Count; i++)
|
||
{
|
||
ProcessInfoImport processInfoImport = resultList[i];
|
||
if (!lineCodeList.Contains(processInfoImport.LineCode))
|
||
{
|
||
lineCodeList.Add(processInfoImport.LineCode);
|
||
}
|
||
if (!dicLineImport.ContainsKey(processInfoImport.LineCode))
|
||
{
|
||
dicLineImport.Add(processInfoImport.LineCode, processInfoImport.LineName);
|
||
}
|
||
if (!processInfos.Any(p => p.ProcessCode == processInfoImport.ProcessCode))
|
||
{
|
||
ProcessInfo processInfo = new ProcessInfo();
|
||
processInfo.ProcessCode = processInfoImport.ProcessCode;
|
||
processInfo.ProcessName = processInfoImport.ProcessName;
|
||
processInfo.LineCode = processInfoImport.LineCode;
|
||
processInfo.LineName = processInfoImport.LineName;
|
||
processInfo.Delflag = 0;
|
||
processInfo.CreateBy = userId;
|
||
processInfo.CreateTime = DateTime.Now;
|
||
processInfo.UpdateBy = userId;
|
||
processInfo.UpdateTime = DateTime.Now;
|
||
processInfo.CreateName = userName;
|
||
processInfo.UpdateName = userName;
|
||
processInfos.Add(processInfo);
|
||
porocessCodeList.Add(processInfoImport.ProcessCode);
|
||
}
|
||
OperationInfo operationInfo = new OperationInfo();
|
||
operationInfo.OperationCode = processInfoImport.OperationCode;
|
||
operationInfo.OperationName = processInfoImport.OperationName;
|
||
operationInfo.ProcessCode = processInfoImport.ProcessCode;
|
||
operationInfo.Delflag = 0;
|
||
operationInfo.CreateBy = userId;
|
||
operationInfo.CreateTime = DateTime.Now;
|
||
operationInfo.UpdateBy = userId;
|
||
operationInfo.UpdateTime = DateTime.Now;
|
||
operationInfo.CreateName = userName;
|
||
operationInfo.UpdateName = userName;
|
||
operationInfos.Add(operationInfo);
|
||
}
|
||
// 判断产线是否存在:检查导入的产线编码中是否有不存在于系统中的
|
||
if (lineCodeList != null && lineCodeList.Any())
|
||
{
|
||
var existingLineCodes = productionLineService.Queryable()
|
||
.Where(it => lineCodeList.Contains(it.LineCode))
|
||
.Select(it => it.LineCode) // 只取存在的编码
|
||
.ToList();
|
||
|
||
var notExistLineCodes = lineCodeList
|
||
.Except(existingLineCodes) // 取差集:导入有但系统没有的
|
||
.ToList();
|
||
if (notExistLineCodes.Any())
|
||
{
|
||
string errorMsg = $"导入失败:以下产线编码不存在,请检查后重试:{string.Join("、", notExistLineCodes)}";
|
||
code = 404;
|
||
strMessage = errorMsg;
|
||
return new ApiResult(code, strMessage);
|
||
}
|
||
List<ProductionLine> plineList = productionLineService.Queryable()
|
||
.Where(it => lineCodeList.Contains(it.LineCode))
|
||
.ToList();
|
||
var errorMsgList = new List<string>();
|
||
foreach (ProductionLine item in plineList)
|
||
{
|
||
string dbLineCode = item.LineCode?.Trim();
|
||
string dbLineName = item.LineName?.Trim();
|
||
|
||
// 从导入字典中获取对应编码的导入名称
|
||
if (dicLineImport.TryGetValue(dbLineCode, out string importLineName))
|
||
{
|
||
string trimImportName = importLineName?.Trim();
|
||
if (!trimImportName.Equals(dbLineName, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
errorMsgList.Add($"产线编码[{dbLineCode}]:导入名称「{trimImportName}」与系统名称「{dbLineName}」不一致");
|
||
}
|
||
}
|
||
}
|
||
// 存在不一致则中止导入,返回错误
|
||
if (errorMsgList.Any())
|
||
{
|
||
return new ApiResult(404, $"导入失败:{string.Join(";", errorMsgList)}");
|
||
}
|
||
}
|
||
if (porocessCodeList.Any())
|
||
{
|
||
var delCountP = Deleteable().Where(it => porocessCodeList.Contains(it.ProcessCode))
|
||
.ExecuteCommand();
|
||
var delCountO = operationInfoService.Deleteable().Where(it => porocessCodeList.Contains(it.ProcessCode))
|
||
.ExecuteCommand();
|
||
}
|
||
int iResult = Insert(processInfos);
|
||
if (iResult > 0)
|
||
{
|
||
code = 200;
|
||
strMessage = "导入成功!";
|
||
int iResult2 = operationInfoService.Insert(operationInfos);
|
||
}
|
||
else
|
||
{
|
||
code = 400;
|
||
strMessage = "导入失败!";
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
code = 500;
|
||
strMessage = "发生错误!"+ex.ToString();
|
||
}
|
||
return new ApiResult(code, strMessage);
|
||
}
|
||
|
||
public int DeleteByProcessCode(string[] processCodeArr)
|
||
{
|
||
int iResult = 0;
|
||
try
|
||
{
|
||
if(processCodeArr != null && processCodeArr.Any())
|
||
{
|
||
int delCountP = Deleteable().Where(it => processCodeArr.Contains(it.ProcessCode))
|
||
.ExecuteCommand();
|
||
int delCountO = operationInfoService.Deleteable().Where(it => processCodeArr.Contains(it.ProcessCode))
|
||
.ExecuteCommand();
|
||
if (delCountP > 0)
|
||
{
|
||
iResult = delCountP;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
iResult = 0;
|
||
}
|
||
return iResult;
|
||
}
|
||
|
||
public PagedInfo<ProcessInfoDto> GetProcessInfoCondition(ProcessInfoQueryDto parm)
|
||
{
|
||
var predicate = QueryExp(parm);
|
||
var response = Queryable()
|
||
.Where(predicate.ToExpression())
|
||
.ToPage<ProcessInfo, ProcessInfoDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
public List<ProcessInfoPullDownDto> GetAllProcessInfo()
|
||
{
|
||
var lineOptions = Queryable()
|
||
.OrderBy(it => it.ProcessCode)
|
||
.Select(it => new ProcessInfoPullDownDto
|
||
{
|
||
value = it.ProcessCode,
|
||
label = it.ProcessName
|
||
}).ToList(); // 执行查询并转换为列表
|
||
return lineOptions;
|
||
}
|
||
}
|
||
} |