using AngleSharp.Dom;
|
using MES.Service.DB;
|
using MES.Service.Dto.webApi;
|
using MES.Service.Modes;
|
using SqlSugar;
|
using System.Data;
|
using System.Globalization;
|
using DbType = System.Data.DbType;
|
|
namespace MES.Service.service;
|
|
public class WomcaaManager : Repository<Womcaa>
|
{
|
//当前类已经继承了 Repository 增、删、查、改的方法
|
|
//这里面写的代码不会给覆盖,如果要重新生成请删除 WomcaaManager.cs
|
|
private readonly WomcabManager _womcabManager = new();
|
|
public bool SaveList(List<ErpWOM> rohIns)
|
{
|
var result = rohIns.Select(Save).ToList();
|
return result.All(b => b);
|
}
|
|
public bool Save(ErpWOM wom)
|
{
|
var womErpCaa = wom. ErpCaa;
|
var mesWomcaa = MapErpCAAtoWomcaa(womErpCaa);
|
var mesWomcabs = MapErpCABtoWomcab(wom.ErpCabs);
|
|
return UseTransaction(db =>
|
{
|
switch (womErpCaa.Type)
|
{
|
case "3":
|
return UpdateData(db, mesWomcaa, mesWomcabs) ? 1 : 0;
|
case "2":
|
case "4":
|
return SaveOrUpdateData(db, mesWomcaa, mesWomcabs)
|
? 1
|
: 0;
|
default:
|
throw new NotImplementedException(
|
$"type没有{womErpCaa.Type}这个类型");
|
}
|
}) > 0;
|
}
|
|
private bool SaveOrUpdateData(SqlSugarScope db, Womcaa mesWomcaa,
|
List<Womcab> mesWomcabs)
|
{
|
|
|
if (mesWomcaa.Caa001 != null && mesWomcaa.SrcBillType != null)
|
{
|
|
db.Deleteable<Womcaa>()
|
.Where(it => it.Caa001 == mesWomcaa.Caa001 &&
|
it.SrcBillType == mesWomcaa.SrcBillType)
|
.ExecuteCommand();
|
|
};
|
|
|
if (mesWomcabs.Count > 0)
|
{
|
|
// var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002, CAB003 = s.Cab003 }).ToList();
|
var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002}).ToList();
|
|
|
/*db.Deleteable<Womcab>()
|
.Where(it => mesWomcab
|
.Any(p => p.CAB001 == it.Cab001
|
&& p.CAB002 == it.Cab002
|
&& p.CAB003 == it.Cab003))
|
.ExecuteCommand();*/
|
db.Deleteable<Womcab>()
|
.Where(it => mesWomcab
|
.Any(p => p.CAB001 == it.Cab001
|
&& p.CAB002 == it.Cab002))
|
.ExecuteCommand();
|
|
};
|
|
|
var orUpdate = db.Insertable(mesWomcaa).ExecuteCommand();
|
var baOrUpdate = db.Insertable(mesWomcabs).ExecuteCommand();
|
|
|
//定义输入参数
|
var inputParam1 = new SugarParameter("P_WORK_NO", mesWomcaa.Caa001);
|
var inputParam2 = new SugarParameter("P_WORK_TYPE", mesWomcaa.SrcBillType);
|
// 定义输出参数
|
var outParam1 = new SugarParameter("c_Result",null,true);
|
var outParam2 = new SugarParameter("C_MSG",null,true);
|
|
|
|
// 使用 SqlSugar 执行存储过程
|
Db.Ado.ExecuteCommand("BEGIN PRC_UPDATE_DAA(:P_WORK_NO,:P_WORK_TYPE,:c_Result,:C_MSG); END;", inputParam1, inputParam2, outParam1, outParam2);
|
|
|
|
// 获取输出参数的值
|
|
int result = int.Parse((string)outParam1.Value);
|
string message = outParam2.Value == DBNull.Value ? string.Empty : (string)outParam2.Value;
|
|
|
if (result == 1)
|
{
|
//存储过程失败则事务进行回滚
|
db.Ado.RollbackTran();
|
throw new Exception(message);
|
|
}
|
|
// 提交事务
|
db.Ado.CommitTran();
|
return true;
|
|
|
throw new NotImplementedException("插入或更新失败");
|
|
|
}
|
|
private bool UpdateData(SqlSugarScope db, Womcaa mesWomcaa,
|
List<Womcab> mesWomcabs)
|
{
|
//根据单别和单号进行删除
|
var update = db.Deleteable<Womcaa>()
|
.Where(it => it.Caa001 == mesWomcaa.Caa001 &&
|
it.SrcBillType == mesWomcaa.SrcBillType)
|
.ExecuteCommand() > 0;
|
|
|
//var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002, CAB003 = s.Cab003 }).ToList();
|
var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002}).ToList();
|
|
|
|
//var insertOrUpdate = db.Deleteable<Womcab>().Where(it => mesWomcab.Any(p => p.CAB001 == it.Cab001 && p.CAB002 == it.Cab002 && p.CAB003 == it.Cab003)).ExecuteCommand() > 0;
|
var insertOrUpdate = db.Deleteable<Womcab>().Where(it => mesWomcab.Any(p => p.CAB001 == it.Cab001 && p.CAB002 == it.Cab002)).ExecuteCommand() > 0;
|
|
|
if (update && insertOrUpdate) return true;
|
throw new NotImplementedException("更新失败");
|
}
|
|
private Womcaa MapErpCAAtoWomcaa(ErpCAA dto)
|
{
|
|
|
string DemandDate = !string.IsNullOrEmpty(dto.DemandDate) ? DateTime.ParseExact(dto.DemandDate, "yyyymmdd", CultureInfo.InvariantCulture).ToString("yyyy-mm-dd") : null;
|
|
var entity = new Womcaa
|
{
|
SrcBillType = dto.FSrcBillType,
|
Caa001 = dto.FBillNo,
|
Caa021 = dto.FWorkShopID0,
|
|
DepotCode = dto.FStockId,
|
CreateDate = dto.FDate,
|
Caa004 = dto.FBillType,
|
|
Caa006 = dto.FMaterialId,
|
Caa009 = dto.FUnitId,
|
Caa012 = !string.IsNullOrEmpty(dto.FQty)
|
? Convert.ToDecimal(dto.FQty)
|
: null,
|
|
Caa010 = dto.FPlanStartDate,
|
Caa011 = dto.FPlanFinishDate,
|
Caa005 = dto.FConveyDate,
|
|
Lot = dto.FLot,
|
|
Caa015 = dto.F_UNW_Text_xsddh,
|
CAA015_Head = dto.xsddh_type,
|
|
|
Caa016 = dto.FMemoItem,
|
Caa023 = dto.FStatus,
|
Suppno = dto.FSuppno,
|
Cust_no = dto.FCustno,
|
Cust_item_no = dto.FCustitemno,
|
DemandDate = DemandDate,
|
CustNumber = dto.CustNumber,
|
ModelType = dto.ModelType
|
|
|
|
};
|
|
|
|
return entity;
|
}
|
|
private List<Womcab> MapErpCABtoWomcab(List<ErpCAB> dtoList)
|
{
|
var womcabList = new List<Womcab>();
|
|
foreach (var dto in dtoList)
|
{
|
var womcab = new Womcab
|
{
|
|
Cab001 = dto.FBillNo,
|
Cab002 = dto.FBillNoType,
|
Cab003 = dto.FMaterialID2,
|
Cab006 = !string.IsNullOrEmpty(dto.FNeedQty2)
|
? Convert.ToDecimal(dto.FNeedQty2)
|
: null,
|
Cab007 = !string.IsNullOrEmpty(dto.FPickedQty)
|
? Convert.ToDecimal(dto.FPickedQty)
|
: null,
|
PositionNo = dto.FPositionNO,
|
DepotCode = dto.FStockID.Trim(),
|
IssueType = dto.FIssueType,
|
Cab008 = dto.F_UNW_Text_tpgy,
|
Cab009 = dto.FUnitID,
|
SupplyType = dto.FSupplyType,
|
Cab014 = dto.FIsKeyItem,
|
Numerator = dto.FNumerator,
|
Denominator = dto.FDenominator
|
|
};
|
|
|
|
womcabList.Add(womcab);
|
}
|
|
return womcabList;
|
}
|
|
|
public bool Delete(YFDelete data)
|
{
|
if (data == null)
|
throw new ArgumentNullException(nameof(data));
|
|
if (string.IsNullOrWhiteSpace(data.FBillNo))
|
throw new ArgumentException("FBillNo 不能为空", nameof(data.FBillNo));
|
|
if (string.IsNullOrWhiteSpace(data.FBillTypeID))
|
throw new ArgumentException("FBillTypeID 不能为空", nameof(data.FBillTypeID));
|
|
return UseTransaction(db =>
|
{
|
// 删除主表数据
|
var deleteMain = db.Deleteable<Womcaa>()
|
.Where(it => it.Caa001 == data.FBillNo && it.SrcBillType == data.FBillTypeID)
|
.ExecuteCommand() > 0;
|
|
// 删除子表数据
|
var deleteDetail = db.Deleteable<Womcab>()
|
.Where(it => it.Cab001 == data.FBillNo && it.Cab002 == data.FBillTypeID)
|
.ExecuteCommand() > 0;
|
|
if (!deleteMain || !deleteDetail)
|
throw new Exception("删除失败:主表或子表记录不存在");
|
|
// 调用存储过程进行后续处理
|
var inputParam1 = new SugarParameter("P_WORK_NO", data.FBillNo);
|
var inputParam2 = new SugarParameter("P_WORK_TYPE", data.FBillTypeID);
|
var outParam1 = new SugarParameter("C_RESULT", null, true); // 输出参数
|
var outParam2 = new SugarParameter("C_MSG", null, true); // 输出参数
|
|
db.Ado.ExecuteCommand(
|
"BEGIN PRC_DELETE_DAA(:P_WORK_NO, :P_WORK_TYPE, :C_RESULT, :C_MSG); END;",
|
inputParam1, inputParam2, outParam1, outParam2);
|
|
int result = int.Parse((string)outParam1.Value);
|
string message = outParam2.Value == DBNull.Value ? string.Empty : (string)outParam2.Value;
|
|
if (result == 1)
|
{
|
//存储过程失败则事务进行回滚
|
db.Ado.RollbackTran();
|
throw new Exception(message);
|
|
}
|
|
return 1;
|
}) > 0;
|
}
|
|
|
}
|