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
{
///
/// 工艺表Service业务层处理
///
[AppService(ServiceType = typeof(IProcessInfoService), ServiceLifetime = LifeTime.Transient)]
public class ProcessInfoService : BaseService, IProcessInfoService
{
private OperationInfoService operationInfoService = new OperationInfoService();
private ProductionLineService productionLineService = new ProductionLineService();
///
/// 查询工艺表列表
///
///
///
public PagedInfo GetList(ProcessInfoQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public ProcessInfo GetInfo(long Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加工艺表
///
///
///
public ProcessInfo AddProcessInfo(ProcessInfo model)
{
return Insertable(model).ExecuteReturnEntity();
}
///
/// 修改工艺表
///
///
///
public int UpdateProcessInfo(ProcessInfo model)
{
return Update(model, true);
}
///
/// 查询导出表达式
///
///
///
private static Expressionable QueryExp(ProcessInfoQueryDto parm)
{
var predicate = Expressionable.Create();
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;
}
///
/// excel表格导入工艺路线
///
///
///
///
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 processInfos = new List();
List operationInfos = new List();
//工艺路线导入模板
List processInfoImports = new List();
List resultList = stream.Query().ToList();
List porocessCodeList = new List();
List lineCodeList = new List();
Dictionary dicLineImport = new Dictionary();
//从1开始循环,排除掉0行的标题行
for (int i = 1;i 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 plineList = productionLineService.Queryable()
.Where(it => lineCodeList.Contains(it.LineCode))
.ToList();
var errorMsgList = new List();
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 GetProcessInfoCondition(ProcessInfoQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage(parm);
return response;
}
public List GetAllProcessInfo()
{
var lineOptions = Queryable()
.OrderBy(it => it.ProcessCode)
.Select(it => new ProcessInfoPullDownDto
{
value = it.ProcessCode,
label = it.ProcessName
}).ToList(); // 执行查询并转换为列表
return lineOptions;
}
}
}