using MES.Service.DB;
|
using MES.Service.Dto.webApi;
|
using MES.Service.Modes;
|
using SqlSugar;
|
|
namespace MES.Service.service.BasicData.Material;
|
|
/// <summary>
|
/// 模具号逻辑处理类
|
/// </summary>
|
public class MesMouldManager : Repository<MesMould>
|
{
|
/// <summary>
|
/// 单个保存(Type: 0启用,1禁用,2审核/新增或更新,3反审核/删除)
|
/// </summary>
|
public bool Save(ErpMould mould)
|
{
|
var entity = ConvertToEntity(mould);
|
|
return UseTransaction(db =>
|
{
|
switch (mould.Type)
|
{
|
case "0":
|
if (UpdateStatus(db, entity.Code, "A")) return 1;
|
break;
|
case "1":
|
if (UpdateStatus(db, entity.Code, "B")) return 1;
|
break;
|
case "3":
|
if (DeleteMould(db, entity.ErpId)) return 1;
|
break;
|
case "2":
|
if (InsertOrUpdate(db, entity)) return 1;
|
break;
|
default:
|
throw new ArgumentNullException($"Type参数错误:{mould.Type}");
|
}
|
throw new NotImplementedException("操作失败");
|
}) > 0;
|
}
|
|
/// <summary>
|
/// 批量保存(带Type判断)
|
/// </summary>
|
public bool SaveList(List<ErpMould> moulds)
|
{
|
var list = moulds.Select(ConvertToEntity).ToList();
|
var groupByType = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList());
|
|
return UseTransaction(db =>
|
{
|
foreach (var group in groupByType)
|
{
|
switch (group.Key)
|
{
|
case "0":
|
if (!UpdateStatusBatch(db, group.Value, "A"))
|
throw new NotImplementedException("启用失败");
|
break;
|
case "1":
|
if (!UpdateStatusBatch(db, group.Value, "B"))
|
throw new NotImplementedException("禁用失败");
|
break;
|
case "3":
|
if (!DeleteMouldBatch(db, group.Value))
|
throw new NotImplementedException("删除失败");
|
break;
|
case "2":
|
if (!InsertOrUpdateBatch(db, group.Value))
|
throw new NotImplementedException("同步失败");
|
break;
|
default:
|
throw new ArgumentNullException($"Type参数错误:{group.Key}");
|
}
|
}
|
return 1;
|
}) > 0;
|
}
|
|
private bool UpdateStatus(SqlSugarScope db, string code, string status)
|
{
|
var result = db.Updateable<MesMould>()
|
.SetColumns(s => s.FForbidStatus == status)
|
.Where(s => s.Code == code)
|
.ExecuteCommand();
|
if (result > 0) return true;
|
throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
|
}
|
|
private bool UpdateStatusBatch(SqlSugarScope db, List<MesMould> moulds, string status)
|
{
|
var codes = moulds.Select(s => s.Code).ToArray();
|
var result = db.Updateable<MesMould>()
|
.SetColumns(s => s.FForbidStatus == status)
|
.Where(s => codes.Contains(s.Code))
|
.ExecuteCommand();
|
return result > 0;
|
}
|
|
/// <summary>
|
/// 删除单条记录(按 ErpId)
|
/// </summary>
|
private bool DeleteMould(SqlSugarScope db, string erpId)
|
{
|
var result = db.Deleteable<MesMould>().Where(s => s.ErpId == erpId).ExecuteCommand();
|
return true;
|
}
|
|
/// <summary>
|
/// 批量删除(按 ErpId)
|
/// </summary>
|
private bool DeleteMouldBatch(SqlSugarScope db, List<MesMould> moulds)
|
{
|
var erpIds = moulds.Select(s => s.ErpId).ToArray();
|
var result = db.Deleteable<MesMould>().Where(s => erpIds.Contains(s.ErpId)).ExecuteCommand();
|
return result > 0;
|
}
|
|
|
private bool InsertOrUpdate(SqlSugarScope db, MesMould entity)
|
{
|
//db.Deleteable<MesMould>().Where(s => s.Code == entity.Code).ExecuteCommand();
|
db.Deleteable<MesMould>().Where(s => s.ErpId == entity.ErpId).ExecuteCommand();
|
var insert = db.Insertable(entity).ExecuteCommand();
|
return insert > 0;
|
}
|
|
private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesMould> moulds)
|
{
|
foreach (var entity in moulds)
|
if (!InsertOrUpdate(db, entity))
|
return false;
|
return true;
|
}
|
|
private MesMould ConvertToEntity(ErpMould m) => new MesMould
|
{
|
Code = m.Code, // 编码
|
Name = m.Name, // 名称
|
DataStatus = m.DataStatus, // 数据状态
|
OldMouldNo = m.OldMouldNo, // 旧模具号
|
Cavity = m.Cavity, // 穴数
|
Cycle = m.Cycle, // 周期
|
Capacity = m.Capacity, // 产能
|
MachineType = m.MachineType, // 机型
|
FForbidStatus = m.FForbidStatus, // 禁用状态
|
ProductWeight = m.ProductWeight, // 产品单重
|
MaterialHead = m.MaterialHead, // 料头
|
StandardLabor = m.StandardLabor, // 标准人力
|
LaborHoursPerThousand = m.LaborHoursPerThousand, // 千片人力工时
|
Remark = m.Remark, // 备注
|
CreateTime = DateTime.Now, // 时间
|
ErpId = m.ErpId, // ERP编号
|
Type = m.Type // 操作类型
|
};
|
}
|