From ab01d75a3fbc8467d81a895cba5d6dde76fbe053 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期二, 16 九月 2025 23:29:30 +0800 Subject: [PATCH] 1111 --- MES.Service/service/BasicData/MesUnitManager.cs | 120 +++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 67 insertions(+), 53 deletions(-) diff --git a/MES.Service/service/BasicData/MesUnitManager.cs b/MES.Service/service/BasicData/MesUnitManager.cs index e966f28..77e637e 100644 --- a/MES.Service/service/BasicData/MesUnitManager.cs +++ b/MES.Service/service/BasicData/MesUnitManager.cs @@ -1,4 +1,5 @@ -锘縰sing MES.Service.DB; +锘縰sing Masuit.Tools; +using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; @@ -43,39 +44,15 @@ public bool SaveList(List<ErpUnit> units) { - var list = units.Select(GetMesUnit).ToList(); - var groupBy = list.GroupBy(s => s.Type) - .ToDictionary(g => g.Key, g => g.ToList()); - - return UseTransaction(db => + if (units == null || !units.Any()) { - foreach (var unitGroup in groupBy) - switch (unitGroup.Key) - { - case "0": - if (!UpdateUnitStatusBatch(db, unitGroup.Value, "A")) - throw new NotImplementedException("鍚敤澶辫触"); - break; - case "1": - if (!UpdateUnitStatusBatch(db, unitGroup.Value, "B")) - throw new NotImplementedException("绂佺敤澶辫触"); - break; - case "3": - if (!DeleteUnitBatch(db, unitGroup.Value)) - throw new NotImplementedException("鍒犻櫎澶辫触"); - break; - case "2": - case "4": - if (!InsertOrUpdateBatch(db, unitGroup.Value)) - throw new NotImplementedException("鍚屾澶辫触"); - break; - default: - throw new ArgumentNullException( - $"type娌℃湁{unitGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�"); - } + Console.WriteLine("璀﹀憡: 浼犲叆鐨勫垪琛ㄤ负绌�"); + return false; + } - return 1; - }) > 0; + // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛� + var result = units.Select(Save).ToList(); + return result.All(b => b); } private bool UpdateUnitStatus(SqlSugarScope db, decimal unitId, @@ -112,21 +89,8 @@ private MesUnit GetMesUnit(ErpUnit unit) { - var fForbidStatus = unit.FForbidStatus; - if (unit.FForbidStatus == "0") - { - fForbidStatus = "A"; - } - else if (unit.FForbidStatus == "1") - { - fForbidStatus = "B"; - } - var entity = new MesUnit { - Id = string.IsNullOrEmpty(unit.Id) - ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - : Convert.ToDecimal(unit.Id), Fnumber = unit.FNumber, Fname = unit.FName, Funitgroupid = unit.FUnitGroupId, @@ -137,7 +101,6 @@ Froundtype = unit.FRoundType, Fprecision = unit.FPrecision, Fdescription = unit.FDescription, - Fforbidstatus = fForbidStatus, CreateDate = DateTime.Now, LastupdateDate = DateTime.Now, Company = "1000", @@ -145,13 +108,31 @@ Type = unit.Type }; - var mesUnit = Db.Queryable<MesUnit>() + if (unit.FForbidStatus.IsNullOrEmpty()) + { + entity.Fforbidstatus = "A"; + } + else + { + //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄� + //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮� + entity.Fforbidstatus = unit.FForbidStatus == "1" ? "B" : "A"; + } + + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽崟浣嶇紪鐮佺殑璁板綍 + var existingUnit = Db.Queryable<MesUnit>() .Where(s => s.Fnumber == entity.Fnumber) .First(); - if (mesUnit != null) + if (existingUnit != null) { - entity.Id = mesUnit.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingUnit.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; @@ -188,12 +169,45 @@ return deleteByIds > 0; } + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesUnit>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesUnit>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; + } + private bool InsertOrUpdate(SqlSugarScope db, MesUnit entity) { - db.Deleteable<MesUnit>() - .Where(s => s.Id == entity.Id).ExecuteCommand(); - var insert = db.Insertable(entity).ExecuteCommand(); - return insert > 0; + if (entity.Id == 0) + { + // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏� + var newId = GenerateNewId(); + entity.Id = newId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } + else + { + // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + var originalId = entity.Id; + + // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級 + db.Deleteable<MesUnit>().Where(s => s.Id == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesUnit> unitList) -- Gitblit v1.9.3