using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; using System.Globalization; using System.Security.Cryptography; namespace MES.Service.service.BasicData; public class TransferRequestManager : Repository { //当前类已经继承了 Repository 增、删、查、改的方法 //这里面写的代码不会给覆盖,如果要重新生成请删除 TransferRequestManager.cs //当前类已经继承了 Repository 增、删、查、改的方法 private readonly TransferRequestDetailManager _TransferRequestDetailManager = new(); //private Guid Tid = Guid.Empty; //ErpTransferRequest public bool Save(TransferRequest TransferRequest) { var erpTransferRequestDto = TransferRequest.ErpTransferRequest; var mesTransferRequest = ConvertErpToTransferRequest(TransferRequest.ErpTransferRequest); var mesTransferRequestDatas = ConvertErpToTransferRequestDetail(mesTransferRequest,TransferRequest.ErpTransferRequestDetail); return UseTransaction(db => { switch (erpTransferRequestDto.Type) { // case "2": // return InsertData(db, mesTransferRequest, mesTransferRequestDatas, // rohInErpRohIn.FBILLTYPE) // ? 1 // : 0; case "3": return UpdateData(db, mesTransferRequest, mesTransferRequestDatas) ? 1 : 0; case "2": case "4": return SaveOrUpdateData(db, mesTransferRequest, mesTransferRequestDatas, erpTransferRequestDto.Type) ? 1 : 0; default: throw new NotImplementedException( $"type没有{erpTransferRequestDto.Type}这个类型"); } }) > 0; } private bool UpdateData(SqlSugarScope db, MesDbck mesTransferRequest, List mesTransferRequestDatas) { int update = 0; int insertOrUpdate = 0; if (mesTransferRequest.Id != null) { update = db.Deleteable() .Where(s => s.ErpID == mesTransferRequest.ErpID) .ExecuteCommand(); } if (mesTransferRequestDatas.Count > 0 && mesTransferRequest.ErpID != null) { insertOrUpdate = db.Deleteable() .Where(s => s.Eid == mesTransferRequest.ErpID) .ExecuteCommand(); } // 修正逻辑判断,将 int 类型与 bool 类型比较改为实际值判断 if (update > 0 && insertOrUpdate > 0) { return true; } throw new NotImplementedException("更新失败"); } // 插入或更新数据的方法 private bool SaveOrUpdateData(SqlSugarScope db, MesDbck mesTransferRequest, List mesTransferRequestDatas, string type) { //if (mesTransferRequest.Id != null) base.DeleteById(mesTransferRequest.Id); if (mesTransferRequest.ErpID != null) db.Deleteable() .Where(s => s.ErpID == mesTransferRequest.ErpID).ExecuteCommand(); if (mesTransferRequestDatas.Count > 0) db.Deleteable() .Where(s => s.Eid == mesTransferRequest.ErpID).ExecuteCommand(); //var orUpdate = base.Insert(mesTransferRequest); //var baOrUpdate = _TransferRequestDetailManager.InsertRange(mesTransferRequestDatas); var orUpdate = db.Insertable(mesTransferRequest) .IgnoreColumns(true).ExecuteCommand() > 0; var baOrUpdate = db.Insertable(mesTransferRequestDatas).PageSize(1) .IgnoreColumnsNull() .ExecuteCommand() > 0; if (orUpdate && baOrUpdate) return true; throw new NotImplementedException("插入或更新失败"); } // 批量保存记录的方法 public bool SaveList(List TransferRequest) { var result = TransferRequest.Select(Save).ToList(); return result.All(b => b); } private MesDbck ConvertErpToTransferRequest( ErpTransferRequest erpDto) { DateTime parsedDate; // 时间格式转换函数,ERP时间格式为 "yyyy-MM-dd HH:mm:ss" DateTime? ParseDateTime(string dateStr) { if (DateTime.TryParseExact(dateStr, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate)) return parsedDate; return null; // 如果转换失败,返回null } var mesDbck = new MesDbck { Id = Guid.NewGuid(), ErpID = Convert.ToDecimal(erpDto.id), FBillNo = erpDto.FBillNo, // 单据编号 FDocumentStatus = erpDto.FDocumentStatus, // 单据状态 FDate = !String.IsNullOrEmpty(erpDto.FDate) ? DateTime.ParseExact(erpDto.FDate, "yyyy-MM-dd HH:mm:ss", null) : null, // 申请日期 FAppOrgId = erpDto.FAPPORGID, // 申请组织 FBusinessType = erpDto.FBusinessType, // 业务类型 //FTransType = erpDto.FTRANSTYPE, // 调拨类型 //FTransferDirect = erpDto.FTransferDirect, // 调拨方向 //FOwnerTypeIdHead = erpDto.FOwnerTypeIdHead, // 调出货主类型 //FOwnerTypeInIdHead = erpDto.FOwnerTypeInIdHead, // 调入货主类型 FRemarks = erpDto.FRemarks, // 备注 FCreatorId = erpDto.FCreatorId, // 创建人 FCreateDate = !String.IsNullOrEmpty(erpDto.FCreateDate) ? DateTime.ParseExact(erpDto.FCreateDate, "yyyy-MM-dd HH:mm:ss", null) : null, // 创建日期 FModifierId = erpDto.FModifierId, // 最后修改人 FModifyDate = !String.IsNullOrEmpty(erpDto.FModifyDate) ? DateTime.ParseExact(erpDto.FModifyDate, "yyyy-MM-dd HH:mm:ss", null) : null, // 最后修改日期 FApproveDate = DateTime.Now,//审核状态 FCloseStatus = erpDto.FCloseStatus, // 关闭状态 FCloserId = erpDto.FCloserId, // 关闭人 //FCloseDate = !String.IsNullOrEmpty(erpDto.FCloseDate) ? DateTime.ParseExact(erpDto.FCloseDate, "yyyy-MM-dd HH:mm:ss", null) : null, // 关闭日期 FUnwBaseBomBb = erpDto.F_UNW_Base_BOMBB, // BOM版本 FUnwBaseFxwlBm = erpDto.F_UNW_Base_FXWLBM, // 父项物料编号 Source = "ERP",//单据来源 FHasLink = true, FApproveStatus = 1,//审核状态 }; if (string.IsNullOrEmpty(erpDto.FCloserId.ToString())) mesDbck.FCloseDate = !String.IsNullOrEmpty(erpDto.FCloseDate) ? DateTime.ParseExact(erpDto.FCloseDate, "yyyy-MM-dd HH:mm:ss", null) : null; // 关闭日期 if (erpDto.FBillTypeID == "DBSQD01_SYS") { mesDbck.FBillTypeId = "标准调拨申请单(DBSQD01_SYS)"; mesDbck.DJTYPE = "标准直接调拨单(ZJDB01_SYS)"; } else if (erpDto.FBillTypeID == "DBSQD04_SYS") { mesDbck.FBillTypeId = "委外调拨申请单(DBSQD04_SYS)"; mesDbck.DJTYPE = "委外直接调拨单(ZJDB03_SYS)"; } // 根据 FTRANSTYPE 设置调拨类型 if (erpDto.FTRANSTYPE == "InnerOrgTransfer") { mesDbck.FTransType = "组织内调拨(InnerOrgTransfer)"; } else if (erpDto.FTRANSTYPE == "OverOrgTransfer") { mesDbck.FTransType = "跨组织调拨(OverOrgTransfer)"; } // 根据 FTransferDirect 设置调拨方向 if (erpDto.FTransferDirect == "GENERAL") { mesDbck.FTransferDirect = "普通(GENERAL)"; } else if (erpDto.FTransferDirect == "RETURN") { mesDbck.FTransferDirect = "退货(RETURN)"; } // 根据 FOwnerTypeIdHead 设置调出货主类型 if (erpDto.FOwnerTypeIdHead == "BD_OwnerOrg") { mesDbck.FOwnerTypeIdHead = "组织(BD_OwnerOrg)"; } else if (erpDto.FOwnerTypeIdHead == "BD_Supplier") { mesDbck.FOwnerTypeIdHead = "供应商(BD_Supplier)"; } else if (erpDto.FOwnerTypeIdHead == "BD_Customer") { mesDbck.FOwnerTypeIdHead = "客户(BD_Customer)"; } // 根据 FOwnerTypeInIdHead 设置调入货主类型 if (erpDto.FOwnerTypeInIdHead == "BD_OwnerOrg") { mesDbck.FOwnerTypeInIdHead = "组织(BD_OwnerOrg)"; } else if (erpDto.FOwnerTypeInIdHead == "BD_Supplier") { mesDbck.FOwnerTypeInIdHead = "供应商(BD_Supplier)"; } else if (erpDto.FOwnerTypeInIdHead == "BD_Customer") { mesDbck.FOwnerTypeInIdHead = "客户(BD_Customer)"; } var single = base.GetSingle(it => it.ErpID == Convert.ToDecimal(erpDto.id)); if (single != null) mesDbck.Id = single.Id; return mesDbck; } private List ConvertErpToTransferRequestDetail( MesDbck TransferRequest, List erpDtoList) { var MesDbckDetailList = new List(); foreach (var erpDto in erpDtoList) { var mesDbckDetail = new MesDbckDetail { Id = Guid.NewGuid(), Pid = TransferRequest.Id, ErpID = Convert.ToDecimal(erpDto.ID), Eid = Convert.ToDecimal(erpDto.EID), // ID FSeq = Convert.ToInt32(erpDto.FSEQ), // 序号 FMaterialId = erpDto.FMATERIALID, // 物料编码 FQty = Convert.ToDecimal(erpDto.FQty), // 申请数量 FUnitId = erpDto.FUNITID, // 单位 FBusinessClose = erpDto.FBusinessClose, // 业务关闭 FLot = erpDto.FLot, // 调出批号 FProduceDate = !String.IsNullOrEmpty(erpDto.FProduceDate) ? DateTime.ParseExact(erpDto.FProduceDate, "yyyy-MM-dd HH:mm:ss", null) : null, // 生产日期 FStockOrgId = erpDto.FStockOrgId, // 调出组织 FStockId = erpDto.FStockId, // 调出仓库 FStockOrgInId = erpDto.FStockOrgInId, // 调入组织 FStockInId = erpDto.FStockInId, // 调入仓库 FMtoNo = erpDto.FMtoNo, // 计划跟踪号 FOwnerId = erpDto.FOwnerId, // 调出货主 FOwnerInId = erpDto.FOwnerInId, // 调入货主 FStockStatusId = erpDto.FStockStatusId, // 调出库存状态 FStockStatusInId = erpDto.FStockStatusInId, // 调入库存状态 FNote = erpDto.FNote // 备注 }; var single = _TransferRequestDetailManager.GetSingle(it => it.ErpID == Convert.ToDecimal(mesDbckDetail.ErpID)); if (single != null) mesDbckDetail.Id = single.Id; MesDbckDetailList.Add(mesDbckDetail); } return MesDbckDetailList; } #region 教学方法 /// /// 仓储方法满足不了复杂业务需求,业务代码请在这里面定义方法 /// public void Study() { /*********查询*********/ var data1 = base.GetById(1); //根据ID查询 var data2 = base.GetList(); //查询所有 var data3 = base.GetList(it => 1 == 1); //根据条件查询 //var data4 = base.GetSingle(it => 1 == 1);//根据条件查询一条,如果超过一条会报错 var p = new PageModel { PageIndex = 1, PageSize = 2 }; // 分页查询 var data5 = base.GetPageList(it => 1 == 1, p); Console.Write(p.TotalCount); //返回总数 var data6 = base.GetPageList(it => 1 == 1, p, it => SqlFunc.GetRandom()); // 分页查询加排序 Console.Write(p.TotalCount); //返回总数 var conModels = new List(); //组装条件查询作为条件实现 分页查询加排序 conModels.Add(new ConditionalModel { FieldName = typeof(MesDbck).GetProperties()[0].Name, ConditionalType = ConditionalType.Equal, FieldValue = "1" }); //id=1 var data7 = base.GetPageList(conModels, p, it => SqlFunc.GetRandom()); AsQueryable().Where(x => 1 == 1) .ToList(); //支持了转换成queryable,我们可以用queryable实现复杂功能 /*********插入*********/ var insertData = new MesDbck(); //测试参数 var insertArray = new[] { insertData }; base.Insert(insertData); //插入 base.InsertRange(insertArray); //批量插入 var id = base.InsertReturnIdentity(insertData); //插入返回自增列 AsInsertable(insertData).ExecuteCommand(); //我们可以转成 Insertable实现复杂插入 /*********更新*********/ var updateData = new MesDbck(); //测试参数 var updateArray = new[] { updateData }; //测试参数 base.Update(updateData); //根据实体更新 base.UpdateRange(updateArray); //批量更新 //base.Update(it => new () { ClassName = "a", CreateTime = DateTime.Now }, it => it.id==1);// 只更新ClassName列和CreateTime列,其它列不更新,条件id=1 AsUpdateable(updateData).ExecuteCommand(); //转成Updateable可以实现复杂的插入 /*********删除*********/ var deldata = new MesDbck(); //测试参数 base.Delete(deldata); //根据实体删除 base.DeleteById(1); //根据主键删除 base.DeleteById(new[] { 1, 2 }); //根据主键数组删除 base.Delete(it => 1 == 2); //根据条件删除 AsDeleteable().Where(it => 1 == 2) .ExecuteCommand(); //转成Deleteable实现复杂的操作 } #endregion }