using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
namespace MES.Service.service.BasicData.Material;
///
/// 模具号逻辑处理类
///
public class MesMouldManager : Repository
{
///
/// 单个保存(Type: 0启用,1禁用,2审核/新增或更新,3反审核/删除)
///
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.Code)) return 1;
break;
case "2":
if (InsertOrUpdate(db, entity)) return 1;
break;
default:
throw new ArgumentNullException($"Type参数错误:{mould.Type}");
}
throw new NotImplementedException("操作失败");
}) > 0;
}
///
/// 批量保存(带Type判断)
///
public bool SaveList(List 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()
.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 moulds, string status)
{
var codes = moulds.Select(s => s.Code).ToArray();
var result = db.Updateable()
.SetColumns(s => s.FForbidStatus == status)
.Where(s => codes.Contains(s.Code))
.ExecuteCommand();
return result > 0;
}
///
/// 删除单条记录(按 ErpId)
///
private bool DeleteMould(SqlSugarScope db, string erpId)
{
var result = db.Deleteable().Where(s => s.ErpId == erpId).ExecuteCommand();
return result > 0;
}
///
/// 批量删除(按 ErpId)
///
private bool DeleteMouldBatch(SqlSugarScope db, List moulds)
{
var erpIds = moulds.Select(s => s.ErpId).ToArray();
var result = db.Deleteable().Where(s => erpIds.Contains(s.ErpId)).ExecuteCommand();
return result > 0;
}
private bool InsertOrUpdate(SqlSugarScope db, MesMould entity)
{
//db.Deleteable().Where(s => s.Code == entity.Code).ExecuteCommand();
db.Deleteable().Where(s => s.ErpId == entity.ErpId).ExecuteCommand();
var insert = db.Insertable(entity).ExecuteCommand();
return insert > 0;
}
private bool InsertOrUpdateBatch(SqlSugarScope db, List 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 // 操作类型
};
}