| | |
| | | Console.WriteLine("警告: 传入的物料列表为空"); |
| | | return false; |
| | | } |
| | | |
| | | |
| | | // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑) |
| | | var result = items.Select(Save).ToList(); |
| | | return result.All(b => b); |
| | |
| | | |
| | | private MesItems GetMesItems(ErpItems item) |
| | | { |
| | | var id = string.IsNullOrEmpty(item.Id) |
| | | ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() |
| | | : long.Parse(item.Id); |
| | | var entity = new MesItems |
| | | { |
| | | Id = id, |
| | | Guid = Guid.NewGuid(), |
| | | Type = item.Type, |
| | | ItemNo = item.FNumber, |
| | | ItemName = item.FName, |
| | | ItemModel = item.FSpecification, |
| | | ItemUnit = item.FBaseUnitId, |
| | | // Lowlimit = !string.IsNullOrEmpty(item.FSafeStock) |
| | | // ? Convert.ToDecimal(item.FSafeStock) |
| | | // : null, |
| | | // Highlimit = !string.IsNullOrEmpty(item.FMaxStock) |
| | | // ? Convert.ToDecimal(item.FMaxStock) |
| | | // : null, |
| | | // PrdPack = !string.IsNullOrEmpty(item.FMinPackCount) |
| | | // ? Convert.ToDecimal(item.FMinPackCount) |
| | | // : null, |
| | | DepotCode = item.FStockId, |
| | | Fmaterialgroup = item.FMaterialGroup, |
| | | Remarks = item.FDescription, |
| | | // Ffinishreceiptoverrate = |
| | | // !string.IsNullOrEmpty(item.FFinishReceiptOverRate) |
| | | // ? Convert.ToInt32(item.FFinishReceiptOverRate) |
| | | // : null, |
| | | Fissuetype = item.FIssueType, |
| | | // Fisbatchmanage = !string.IsNullOrEmpty(item.FIsBatchManage) |
| | | // ? Convert.ToInt32(item.FIsBatchManage) |
| | | // : null, |
| | | Fpurchaserid = item.FPurchaserId, |
| | | Fpurchaseunitid = item.FPurchaseUnitId, |
| | | Storeunit = item.FStoreUnitID, |
| | |
| | | ProductionWorkshop = item.FWorkShopId, |
| | | ProduceUnit = item.FPRODUCEUNITID, |
| | | SubconUnit = item.FSUBCONUNITID, |
| | | FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId) |
| | | ? "1" |
| | | : item.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId) |
| | | ? "1" |
| | | : item.FCreateOrgId, |
| | | FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId) ? "1" : item.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId) ? "1" : item.FCreateOrgId, |
| | | LossPercent = item.FLOSSPERCENT, |
| | | MnemonicCode = item.FMnemonicCode, |
| | | ExpPeriod = item.FExpPeriod, |
| | | EItemId = id, |
| | | ItemId = id, |
| | | LastupdateDate = DateTime.Now, |
| | | CreateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000" |
| | | }; |
| | | |
| | | var mesItems = Db.Queryable<MesItems>() |
| | | // 查找是否已存在相同物料编码的记录 |
| | | var existingItem = Db.Queryable<MesItems>() |
| | | .Where(s => s.ItemNo == entity.ItemNo) |
| | | .First(); |
| | | |
| | | if (mesItems != null) |
| | | if (existingItem != null) |
| | | { |
| | | entity.Id = mesItems.Id; |
| | | // 如果存在,使用现有的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) |