using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using MES.Service.util; using SqlSugar; namespace MES.Service.service.BasicData; public class MesSupplierManager : Repository { public bool Save(ErpSupplier unit) { var entity = GetMesSupplier(unit); return UseTransaction(db => { switch (unit.Type) { case "0": if (UpdateSupplierStatus(db, entity, "A")) return 1; break; case "1": if (UpdateSupplierStatus(db, entity, "B")) return 1; break; case "3": if (DeleteSupplier(entity)) return 1; break; case "2": case "4": if (InsertOrUpdate(db, entity)) return 1; break; default: throw new ArgumentNullException( $"type没有{unit.Type}这个类型的参数"); } throw new NotImplementedException("操作失败"); }) > 0; } public bool SaveList(List suppliers) { var result = suppliers.Select(Save).ToList(); return result.All(b => b); } private bool UpdateSupplierStatus(SqlSugarScope db, List supplierId, string status) { var ids = supplierId.Select(it => it.Id).ToArray(); var result = db.Updateable() .SetColumns(s => s.Fforbidstatus == status) .Where(s => ids.Contains(s.Id)).ExecuteCommand(); if (result > 0) return true; throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败"); } private bool DeleteSupplier(List supplierId) { // 提取所有需要的 MesId 组合 var ids = supplierId.Select(customer => customer.Id.ToString()) .ToList(); // 使用这些 MesId 去查询 MesLinkU9 表,找到对应的 U9Id var u9Ids = Db.Queryable() .Where(u => ids.Contains(u.MesId) && u.TableType == "MES_SUPPLIER") .Select(u => u.U9Id) .Distinct() // 应用 Distinct 去重 .ToList(); // 根据 U9Id 找到所有对应的 MesId,进行删除 // 注意:这里假设您确实想要删除这些记录。如果不确定,请先查询以验证结果。 var idsToDelete = Db.Queryable() .Where(u => u9Ids.Contains(u.U9Id) && u.TableType == "MES_SUPPLIER") .Select(u => u.MesId) .Distinct() // 应用 Distinct 去重 .ToList(); //以上方法可以考虑优化,将U9id找个新建字段存在MES_CUSTOMER中,就可以少一步查询了。 var deleteByIds = Db.Deleteable().In(idsToDelete) .ExecuteCommand(); // if (deleteByIds > 0) // return true; return true; //throw new NotImplementedException("删除失败"); } private List GetMesSupplier(ErpSupplier supplier) { List list = new(); foreach (var se in supplier.FUseOrgId) { var exists = Db.Queryable().Any(u => u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId && u.TableType == "MES_SUPPLIER"); decimal mesId = 0; if (exists) { //获取mesid mesId = Convert.ToDecimal(Db.Queryable() .Where(u => u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId && u.TableType == "MES_SUPPLIER") .Select(u => u.MesId) // 选择 MesId 字段 .First()); } else { mesId = GetSeqUtil.GetSeq("SEQ_BASEINFO_ID"); var entity = new MesLinkU9 { CreateDate = DateTime.Now, MesId = mesId.ToString(), U9Id = supplier.Id, OrgId = se.FUseOrgId, TableType = "MES_SUPPLIER" }; Db.Insertable(entity).ExecuteCommand(); } var mesSupplier = new MesSupplier { Id = mesId, SuppNo = supplier.FNumber, SuppSname = supplier.FShortName, SuppName = supplier.FName, Lxr = supplier.FContact, Telf1 = supplier.FTel, Fstaffid = supplier.FStaffId, /// /// 接口字段调整:标准版接口新增 ERP单据状态 字段。 /// /// /// 修改人:<池南骏> /// 修改日期:<2024-12-28> /// 修改说明: /// - 原代码:空 /// - 修改后: ///FDOCUMENTSTATUS = supplier.FDocumentStatus, /// DocumentStatus = supplier.FDocumentStatus, Fforbidstatus = supplier.FForbidStatus, CreateDate = DateTime.Now, //CreateOrg= Convert.ToDecimal(supplier.FCreateOrgId), UseOrg = se.FUseOrgId, //Remark=supplier.fremarks, PrivateDescSeg = supplier.PrivateDescSeg, Company = "1000", Factory = "1000" }; list.Add(mesSupplier); } return list; } /// /// "插入或更新的方法"调整:修改为先删除再插入。 /// /// /// 修改人:池南骏 /// 修改日期:2024-12-30 /// 修改说明:` /// //// - 原代码: //private bool InsertOrUpdate(SqlSugarScope db, MesSupplier entity) //{ // var exists = db.Queryable().Any(e => e.Id == entity.Id); // if (exists) // { // var update = db.Updateable(entity).ExecuteCommand(); // if (update > 0) // return true; // } // else // { // var insert = db.Insertable(entity).ExecuteCommand(); // if (insert > 0) // return true; // } // return false; //} ///新代码 private bool InsertOrUpdate(SqlSugarScope db, List entity) { DeleteSupplier(entity); var insert = db.Insertable(entity).ExecuteCommand(); return insert > 0; } }