| | |
| | | |
| | | public bool SaveList(List<ErpItems> items) |
| | | { |
| | | var list = items.Select(GetMesItems).ToList(); |
| | | var groupBy = list.GroupBy(s => s.Type) |
| | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | | |
| | | return UseTransaction(db => |
| | | if (items == null || !items.Any()) |
| | | { |
| | | foreach (var itemGroup in groupBy) |
| | | switch (itemGroup.Key) |
| | | { |
| | | case "0": |
| | | if (!UpdateItemStatusBatch(db, itemGroup.Value, "A")) |
| | | throw new NotImplementedException("启用失败"); |
| | | break; |
| | | case "1": |
| | | if (!UpdateItemStatusBatch(db, itemGroup.Value, "B")) |
| | | throw new NotImplementedException("禁用失败"); |
| | | break; |
| | | case "3": |
| | | if (!DeleteItemBatch(db, itemGroup.Value)) |
| | | throw new NotImplementedException("删除失败"); |
| | | break; |
| | | case "2": |
| | | case "4": |
| | | if (!InsertOrUpdateBatch(db, itemGroup.Value)) |
| | | throw new NotImplementedException("同步失败"); |
| | | break; |
| | | default: |
| | | throw new ArgumentNullException( |
| | | $"type没有{itemGroup.Key}这个类型的参数"); |
| | | } |
| | | Console.WriteLine("警告: 传入的物料列表为空"); |
| | | return false; |
| | | } |
| | | |
| | | return 1; |
| | | }) > 0; |
| | | // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑) |
| | | var result = items.Select(Save).ToList(); |
| | | return result.All(b => b); |
| | | } |
| | | |
| | | private bool UpdateItemStatus(SqlSugarScope db, decimal itemId, |
| | |
| | | |
| | | private MesItems GetMesItems(ErpItems item) |
| | | { |
| | | return new MesItems |
| | | var entity = new MesItems |
| | | { |
| | | Id = Convert.ToDecimal(item.Id), |
| | | Guid = Guid.NewGuid(), |
| | | Type = item.Type, |
| | | ItemNo = item.FNumber, |
| | | FOldNumber = item.FOldNumber, |
| | | ItemName = item.FName, |
| | | ItemModel = item.FSpecification, |
| | | ItemUnit = item.FBaseUnitId, |
| | | Lowlimit = Convert.ToDouble(item.FSafeStock), |
| | | Highlimit = Convert.ToDouble(item.FMaxStock), |
| | | FMinStock = item.FMinStock, |
| | | PrdPack = Convert.ToDouble(item.FMinPackCount), |
| | | DepotCode = item.FStockId, |
| | | Fmaterialgroup = item.FMaterialGroup, |
| | | Remarks = item.FDescription, |
| | | Ffinishreceiptoverrate = Convert.ToDecimal(item.FFinishReceiptOverRate), |
| | | FFinishReceiptShortRate = item.FFinishReceiptShortRate, |
| | | Fissuetype = item.FIssueType, |
| | | Fisbatchmanage = Convert.ToInt32(item.FIsBatchManage), |
| | | Fpurchaserid = item.FPurchaserId, |
| | | FDefaultVendor = item.FDefaultVendor, |
| | | Fpurchaseunitid = Convert.ToDecimal(item.FPurchaseUnitId), |
| | | Fpurchaseunitid = item.FPurchaseUnitId, |
| | | Storeunit = item.FStoreUnitID, |
| | | Saleunit = item.FSaleUnitId, |
| | | FDocumentStatus = item.FDocumentStatus, |
| | | Fforbidstatus = item.FForbidStatus, |
| | | MaterialProperti = item.FErpClsID, |
| | | FMfgPolicyId = item.FMfgPolicyId, |
| | | ProductionWorkshop = item.FWorkShopId, |
| | | FBOMUnitId = item.FBOMUnitId, |
| | | ProduceUnit = item.FPRODUCEUNITID, |
| | | FIsKitting = item.FIsKitting, |
| | | FIsCoby = item.FIsCoby, |
| | | FOverControlMode = item.FOverControlMode, |
| | | SubconUnit = item.FSUBCONUNITID, |
| | | Fumbrella = item.FCreateOrgId, |
| | | FSubsidiary = item.FUseOrgId, |
| | | FLOSSPERCENT = item.FLOSSPERCENT, |
| | | FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId) ? "1" : item.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId) ? "1" : item.FCreateOrgId, |
| | | LossPercent = item.FLOSSPERCENT, |
| | | MnemonicCode = item.FMnemonicCode, |
| | | FExpPeriod = item.FExpPeriod, |
| | | EItemId = long.Parse(item.Id), |
| | | ItemId = long.Parse(item.Id), |
| | | ExpPeriod = item.FExpPeriod, |
| | | LastupdateDate = DateTime.Now, |
| | | CreateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000" |
| | | }; |
| | | |
| | | // 查找是否已存在相同物料编码的记录 |
| | | var existingItem = Db.Queryable<MesItems>() |
| | | .Where(s => s.ItemNo == entity.ItemNo) |
| | | .First(); |
| | | |
| | | if (existingItem != null) |
| | | { |
| | | // 如果存在,使用现有的ID,后续将删除后重新插入 |
| | | entity.Id = existingItem.Id; |
| | | entity.ItemId = existingItem.Id; |
| | | } |
| | | else |
| | | { |
| | | // 如果不存在,设为0,InsertOrUpdate方法将生成新ID |
| | | entity.Id = 0; |
| | | entity.ItemId = 0; |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成新的ID,确保不重复 |
| | | /// </summary> |
| | | private decimal GenerateNewId() |
| | | { |
| | | // 处理空表的情况,从1开始 |
| | | var maxId = Db.Queryable<MesItems>().Max(x => (decimal?)x.Id) ?? 0; |
| | | var newId = maxId + 1; |
| | | |
| | | // 双重检查,确保生成的ID不存在 |
| | | while (Db.Queryable<MesItems>().Where(x => x.Id == newId).Any()) |
| | | { |
| | | newId++; |
| | | } |
| | | |
| | | return newId; |
| | | } |
| | | |
| | | private bool UpdateItemStatusBatch(SqlSugarScope db, |
| | |
| | | |
| | | private bool InsertOrUpdate(SqlSugarScope db, MesItems entity) |
| | | { |
| | | db.Deleteable<MesItems>().Where(s => s.Id == entity.Id) |
| | | .ExecuteCommand(); |
| | | |
| | | var insert = db.Insertable(entity).ExecuteCommand(); |
| | | return insert > 0; |
| | | // 确保ItemId与Id保持一致 |
| | | entity.ItemId = entity.Id; |
| | | |
| | | if (entity.Id == 0) |
| | | { |
| | | // 新增情况:生成新ID并插入 |
| | | var newId = GenerateNewId(); |
| | | entity.Id = newId; |
| | | entity.ItemId = newId; |
| | | return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; |
| | | } |
| | | else |
| | | { |
| | | // 更新情况:删除后重新插入,保持原有ID |
| | | var originalId = entity.Id; |
| | | |
| | | // 先删除原记录(如果存在) |
| | | db.Deleteable<MesItems>().Where(s => s.Id == originalId).ExecuteCommand(); |
| | | |
| | | // 重新插入,保持原有ID |
| | | entity.Id = originalId; |
| | | entity.ItemId = originalId; |
| | | return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; |
| | | } |
| | | } |
| | | |
| | | private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesItems> itemList) |