From e159c28b6d090dc875c878e462ceb45a809315d7 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期五, 12 九月 2025 11:29:46 +0800 Subject: [PATCH] 111 --- MES.Service/Modes/SysDepartment.cs | 2 MES.Service/service/BasicData/MesUnitManager.cs | 57 +++ MES.Service/service/BasicData/MesStaffManager.cs | 414 ++++----------------------- MES.Service/Modes/MesUnit.cs | 2 MES.Service/Modes/MesItems.cs | 8 MES.Service/service/BasicData/SysDepartmentManager.cs | 61 +++ MES.Service/service/BasicData/MesCustomerManager.cs | 60 +++ MES.Service/service/BasicData/MesSupplierManager.cs | 61 +++ MES.Service/Modes/MesSupplier.cs | 2 MES.Service/Modes/MesCustomer.cs | 2 MES.Service/Modes/MesStaff.cs | 2 MES.Service/Modes/MesDepots.cs | 2 MES.Service/service/BasicData/MesDepotsManager.cs | 58 +++ MES.Service/service/BasicData/MesItemsManager.cs | 95 +++-- MES.Service/Modes/MesItemType.cs | 2 MES.Service/service/BasicData/MesItemTypeManager.cs | 69 +++- 16 files changed, 421 insertions(+), 476 deletions(-) diff --git a/MES.Service/Modes/MesCustomer.cs b/MES.Service/Modes/MesCustomer.cs index 1fa1188..d7f9cd2 100644 --- a/MES.Service/Modes/MesCustomer.cs +++ b/MES.Service/Modes/MesCustomer.cs @@ -16,7 +16,7 @@ /// <summary> /// ID ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal Id { get; set; } /// <summary> diff --git a/MES.Service/Modes/MesDepots.cs b/MES.Service/Modes/MesDepots.cs index 170f4a3..46cea7c 100644 --- a/MES.Service/Modes/MesDepots.cs +++ b/MES.Service/Modes/MesDepots.cs @@ -17,7 +17,7 @@ /// <summary> /// 浠撳簱ID /// </summary> - [SugarColumn(ColumnName = "depot_id", IsIdentity = true)] + [SugarColumn(ColumnName = "depot_id")] public decimal DepotId { get; set; } /// <summary> diff --git a/MES.Service/Modes/MesItemType.cs b/MES.Service/Modes/MesItemType.cs index 07917c0..e7a0ee3 100644 --- a/MES.Service/Modes/MesItemType.cs +++ b/MES.Service/Modes/MesItemType.cs @@ -18,7 +18,7 @@ /// <summary> /// ID ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal? Id { get; set; } /// <summary> diff --git a/MES.Service/Modes/MesItems.cs b/MES.Service/Modes/MesItems.cs index 5f12ca5..78574d7 100644 --- a/MES.Service/Modes/MesItems.cs +++ b/MES.Service/Modes/MesItems.cs @@ -18,8 +18,14 @@ /// <summary> /// 鐗╂枡ID ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal Id { get; set; } + + /// <summary> + /// 鐗╂枡ID + ///</summary> + [SugarColumn(ColumnName = "ITEM_ID")] + public decimal? ItemId { get; set; } /// <summary> /// 宸ュ巶 diff --git a/MES.Service/Modes/MesStaff.cs b/MES.Service/Modes/MesStaff.cs index 61c6877..17de4b3 100644 --- a/MES.Service/Modes/MesStaff.cs +++ b/MES.Service/Modes/MesStaff.cs @@ -18,7 +18,7 @@ /// <summary> /// ID ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal? Id { get; set; } /// <summary> diff --git a/MES.Service/Modes/MesSupplier.cs b/MES.Service/Modes/MesSupplier.cs index 0a09bd8..6ef30ec 100644 --- a/MES.Service/Modes/MesSupplier.cs +++ b/MES.Service/Modes/MesSupplier.cs @@ -18,7 +18,7 @@ /// <summary> /// ID ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal Id { get; set; } /// <summary> diff --git a/MES.Service/Modes/MesUnit.cs b/MES.Service/Modes/MesUnit.cs index c5ae5b6..9ea8e2a 100644 --- a/MES.Service/Modes/MesUnit.cs +++ b/MES.Service/Modes/MesUnit.cs @@ -18,7 +18,7 @@ /// <summary> /// BASEINFO ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal Id { get; set; } /// <summary> diff --git a/MES.Service/Modes/SysDepartment.cs b/MES.Service/Modes/SysDepartment.cs index 7d90454..f209d1f 100644 --- a/MES.Service/Modes/SysDepartment.cs +++ b/MES.Service/Modes/SysDepartment.cs @@ -125,7 +125,7 @@ /// <summary> /// 閮ㄩ棬id ///</summary> - [SugarColumn(ColumnName = "id", IsIdentity = true)] + [SugarColumn(ColumnName = "id")] public decimal Id { get; set; } /// <summary> diff --git a/MES.Service/service/BasicData/MesCustomerManager.cs b/MES.Service/service/BasicData/MesCustomerManager.cs index 856f146..b05bf0e 100644 --- a/MES.Service/service/BasicData/MesCustomerManager.cs +++ b/MES.Service/service/BasicData/MesCustomerManager.cs @@ -101,10 +101,6 @@ var entity = new MesCustomer { - // Id = string.IsNullOrEmpty(customer.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : Convert.ToDecimal(customer.Id), - CustNo = customer.FNumber, CustSname = customer.FShortName, CustName = customer.FName, @@ -126,16 +122,41 @@ Type = customer.Type, }; - var mesCustomer = Db.Queryable<MesCustomer>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽鎴风紪鐮佺殑璁板綍 + var existingCustomer = Db.Queryable<MesCustomer>() .Where(s => s.CustNo == entity.CustNo) .First(); - if (mesCustomer != null) + if (existingCustomer != null) { - entity.Id = mesCustomer.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingCustomer.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; + } + + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesCustomer>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesCustomer>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; } private bool UpdateCustomerStatusBatch(SqlSugarScope db, @@ -176,12 +197,25 @@ private bool InsertOrUpdate(SqlSugarScope db, MesCustomer entity) { - db.Deleteable<MesCustomer>().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<MesCustomer>().Where(s => s.Id == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } private bool InsertOrUpdateBatch(SqlSugarScope db, diff --git a/MES.Service/service/BasicData/MesDepotsManager.cs b/MES.Service/service/BasicData/MesDepotsManager.cs index eb746d6..16b4919 100644 --- a/MES.Service/service/BasicData/MesDepotsManager.cs +++ b/MES.Service/service/BasicData/MesDepotsManager.cs @@ -76,14 +76,46 @@ throw new NotImplementedException("鍙嶅鏍稿け璐�"); } + /// <summary> + /// 鐢熸垚鏂扮殑DepotId锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesDepots>().Max(x => (decimal?)x.DepotId) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨凞epotId涓嶅瓨鍦� + while (Db.Queryable<MesDepots>().Where(x => x.DepotId == newId).Any()) + { + newId++; + } + + return newId; + } + // 鎻掑叆鎴栨洿鏂颁粨搴撶殑鏂规硶 private bool InsertOrUpdate(SqlSugarScope db, MesDepots entity) { - db.Deleteable<MesDepots>() - .Where(s => s.DepotId == entity.DepotId) - .ExecuteCommand(); - var insert = db.Insertable(entity).ExecuteCommand(); - return insert > 0; + if (entity.DepotId == 0) + { + // 鏂板鎯呭喌锛氱敓鎴愭柊DepotId骞舵彃鍏� + var newId = GenerateNewId(); + entity.DepotId = newId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } + else + { + // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塂epotId + var originalId = entity.DepotId; + + // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級 + db.Deleteable<MesDepots>().Where(s => s.DepotId == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塂epotId + entity.DepotId = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } // 灏� ErpDepots 瀵硅薄杞崲涓� MesDepots 瀵硅薄鐨勬柟娉� @@ -93,9 +125,6 @@ { DepotCode = depots.FNumber, DepotName = depots.FName, - // DepotId = string.IsNullOrEmpty(depots.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : Convert.ToInt32(depots.Id), IsFkc = depots.FAllowMinusQty, CreateBy = depots.FPrincipal, Depottype = depots.FStockProperty, @@ -115,13 +144,20 @@ Factory = "1000" }; - var mesDepots = Db.Queryable<MesDepots>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屼粨搴撶紪鐮佺殑璁板綍 + var existingDepot = Db.Queryable<MesDepots>() .Where(s => s.DepotCode == entity.DepotCode) .First(); - if (mesDepots != null) + if (existingDepot != null) { - entity.DepotId = mesDepots.DepotId; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑DepotId锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.DepotId = existingDepot.DepotId; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊DepotId + entity.DepotId = 0; } return entity; diff --git a/MES.Service/service/BasicData/MesItemTypeManager.cs b/MES.Service/service/BasicData/MesItemTypeManager.cs index 6736c56..351d87d 100644 --- a/MES.Service/service/BasicData/MesItemTypeManager.cs +++ b/MES.Service/service/BasicData/MesItemTypeManager.cs @@ -31,24 +31,53 @@ } } + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesItemType>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesItemType>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; + } + private bool InsertItemType(MesItemType entity) { - // 鍏堟牴鎹甀D鍒犻櫎鐜版湁璁板綍 - try + if (entity.Id == 0) { - Db.Deleteable<MesItemType>() - .Where(it => it.Id == entity.Id).ExecuteCommand(); + // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏� + var newId = GenerateNewId(); + entity.Id = newId; + return base.Insert(entity); } - catch (Exception) + else { - // 鍒犻櫎澶辫触鍙兘鏄洜涓鸿褰曚笉瀛樺湪锛岀户缁墽琛屾彃鍏ユ搷浣� + // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + var originalId = entity.Id; + + // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級 + try + { + Db.Deleteable<MesItemType>() + .Where(it => it.Id == originalId).ExecuteCommand(); + } + catch (Exception) + { + // 鍒犻櫎澶辫触鍙兘鏄洜涓鸿褰曚笉瀛樺湪锛岀户缁墽琛屾彃鍏ユ搷浣� + } + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + return base.Insert(entity); } - - var insert = base.Insert(entity); - if (insert) - return true; - - throw new NotImplementedException("鎻掑叆澶辫触"); } private bool DeleteItemType(decimal? id) @@ -65,9 +94,6 @@ { var entity = new MesItemType { - // Id = string.IsNullOrEmpty(department.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : long.Parse(department.Id), Pgroup = department.FParentId, Tcode = department.FNumber, Tname = department.FName, @@ -78,13 +104,20 @@ Factory = "1000" }; - var mesItemType = Db.Queryable<MesItemType>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岀被鍨嬬紪鐮佺殑璁板綍 + var existingItemType = Db.Queryable<MesItemType>() .Where(it => it.Tcode == entity.Tcode) .First(); - if (mesItemType != null) + if (existingItemType != null) { - entity.Id = mesItemType.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingItemType.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; diff --git a/MES.Service/service/BasicData/MesItemsManager.cs b/MES.Service/service/BasicData/MesItemsManager.cs index 7ba11c5..6ab9610 100644 --- a/MES.Service/service/BasicData/MesItemsManager.cs +++ b/MES.Service/service/BasicData/MesItemsManager.cs @@ -49,7 +49,7 @@ Console.WriteLine("璀﹀憡: 浼犲叆鐨勭墿鏂欏垪琛ㄤ负绌�"); return false; } - + // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛� var result = items.Select(Save).ToList(); return result.All(b => b); @@ -89,37 +89,18 @@ 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, @@ -129,33 +110,54 @@ 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锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊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; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesItems>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; } private bool UpdateItemStatusBatch(SqlSugarScope db, @@ -194,11 +196,30 @@ 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涓嶪d淇濇寔涓�鑷� + 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 + { + // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + var originalId = entity.Id; + + // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級 + db.Deleteable<MesItems>().Where(s => s.Id == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + entity.ItemId = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesItems> itemList) diff --git a/MES.Service/service/BasicData/MesStaffManager.cs b/MES.Service/service/BasicData/MesStaffManager.cs index fc1f882..af5f8fb 100644 --- a/MES.Service/service/BasicData/MesStaffManager.cs +++ b/MES.Service/service/BasicData/MesStaffManager.cs @@ -1,334 +1,4 @@ -锘�/* -namespace MES.Service.service.BasicData; - -using MES.Service.DB; -using MES.Service.Dto.webApi; -using MES.Service.Modes; -using SqlSugar; - -public class MesStaffManager : Repository<MesStaff> -{ - // Save 鏂规硶鐢ㄤ簬淇濆瓨鍗曚釜鍛樺伐璁板綍锛屾牴鎹被鍨嬫墽琛屼笉鍚岀殑鎿嶄綔 - public bool Save(ErpStaff unit) - { - var entity = GetMesStaff(unit); // 灏� ErpStaff 杞崲涓� MesStaff - var sysUser = GetUser(unit); // 鑾峰彇 SysUser 瀹炰緥 - var mesStaffPositionLink = GetMesStaffPositionLink(unit); // 鑾峰彇 MesStaffPositionLink 瀹炰緥 - return UseTransaction(db => - { - switch (unit.Type) - { - case "0": - if (UpdateStaffStatus(db, entity.Id, "A")) // 鍚敤鍛樺伐 - return 1; - break; - case "1": - if (UpdateStaffStatus(db, entity.Id, "B")) // 绂佺敤鍛樺伐 - return 1; - break; - case "2": - case "4": - if (InsertOrUpdateStaff(db, sysUser, entity,mesStaffPositionLink)) // 鎻掑叆鎴栨洿鏂板憳宸� - return 1; - break; - case "3": - if (DeleteStaff(db, sysUser, entity)) // 鍒犻櫎鍛樺伐 - return 1; - break; - default: - throw new ArgumentNullException( - $"type娌℃湁{unit.Type}杩欎釜绫诲瀷鐨勫弬鏁�"); - } - - throw new NotImplementedException("鎿嶄綔澶辫触"); - }) > 0; - } - - // 鏇存柊鍛樺伐鐘舵�佺殑鏂规硶 - private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId, - string status) - { - var result = db.Updateable<MesStaff>() - .SetColumns(s => s.FforbidStatus == status) - .Where(s => s.Id == staffId).ExecuteCommand(); - - if (result > 0) - return true; - - throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触"); - } - - // 鎻掑叆鎴栨洿鏂板憳宸ョ殑鏂规硶 - private bool InsertOrUpdateStaff(SqlSugarScope db, SysUser sysUser, - MesStaff entity, List<MesStaffPositionLink> mesStaffPositionLink) - { - var exists = db.Queryable<SysUser>().Any(u => u.Sid == sysUser.Sid); - if (!exists) - { - var insertUser = db.Insertable(sysUser).ExecuteCommand(); - } - db.Deleteable<MesStaff>() - .Where(s => s.Id == entity.Id) - .ExecuteCommand(); - - var insertStaff = - db.Insertable(entity).IgnoreColumns(true).ExecuteCommand(); - - var insertPosition = 1; - if (mesStaffPositionLink != null && mesStaffPositionLink.Count > 0) - { - //鍏堝垹闄わ紝鍐嶆柊澧� - db.Deleteable<MesStaffPositionLink>() - .Where(s => - mesStaffPositionLink.Any(m => m.StaffId == s.StaffId)) - .ExecuteCommand(); - - //鎵归噺鎻掑叆鏃讹紝蹇界暐绌哄瓧娈� - insertPosition = db.Insertable(mesStaffPositionLink).PageSize(1) - .IgnoreColumnsNull().ExecuteCommand(); - } - - - return insertStaff > 0 && insertPosition > 0; - } - - // 鍒犻櫎鍛樺伐鐨勬柟娉� - - - private bool DeleteStaff(SqlSugarScope db, SysUser sysUser, MesStaff entity) - { - var deleteUser = db.Deleteable<SysUser>() - .Where(s => s.Sid == sysUser.Sid).ExecuteCommand(); - if (deleteUser > 0) - { - var deleteStaff = - db.Deleteable<MesStaff>() - .Where(s => - s.Id == entity.Id && - s.PositionCode == entity.PositionCode) - .ExecuteCommand(); - - //鍒犻櫎瀵瑰簲鍛樺伐宀椾綅淇℃伅鍏宠仈琛� 姹犲崡楠� 2025-01-02 - var insertPosition = db.Deleteable<MesStaffPositionLink>() - .Where(s => s.StaffId == entity.Id) - .ExecuteCommand(); - - if (deleteStaff > 0) - return true; - } - - throw new NotImplementedException("鍙嶅鏍稿け璐�"); - } - // 灏� ErpStaff 瀵硅薄杞崲涓� MesStaff 瀵硅薄鐨勬柟娉� - private MesStaff GetMesStaff(ErpStaff staff) - { - var entity = new MesStaff - { - Id = Convert.ToDecimal(staff.Id), - StaffNo = staff.FStaffNumber, - StaffName = staff.FName, - DepartmentName = staff.FPostDept, - PositionCode = staff.FPostId, - PhoneNumber = staff.FMobile, - Remark = staff.FDescription, - FforbidStatus = staff.FForbidStatus, - FSubsidiary = staff.FUseOrgId, - Fumbrella = staff.FCreateOrgId, - CreateDate = DateTime.Now, - LastupdateDate = DateTime.Now, - Type = staff.Type - }; - - if (staff.FStaffStartDate != null) - entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate, - "yyyy-MM-dd HH:mm:ss", null); - - return entity; - } - - // 灏� ErpStaff 瀵硅薄杞崲涓� SysUser 瀵硅薄鐨勬柟娉� - private SysUser GetUser(ErpStaff staff) - { - return new SysUser - { - IsStatus = true, - Account = staff.FStaffNumber, - UserName = staff.FName, - Password = "E1ADC3949BA59ABBE56E057F2F883E", // 鍒濆瀵嗙爜 - DepartNo = staff.FPostDept, - Type = staff.Type, - CreateTime = DateTime.Now - }; - } - - /// <summary> - /// 鎺ュ彛瀛楁璋冩暣锛氭牴鎹甹son鏍煎紡锛屽湪MesStaffPositionLink<List>涓瓨鍏ユ暟鎹�� - /// </summary> - /// <remarks> - /// 淇敼浜猴細姹犲崡楠� - /// 淇敼鏃ユ湡锛�2025-01-02 鈥斺�斻�� 2025-01-05 浜屾淇敼 - /// 淇敼璇存槑锛� - /// - 鏂板浠g爜 - /// </remarks> - private List<MesStaffPositionLink> GetMesStaffPositionLink(ErpStaff staff) - { - // 鍒濆鍖栬繑鍥炵殑鑱屼綅鍏宠仈鍒楄〃 - List<MesStaffPositionLink> staffDetails = new List<MesStaffPositionLink>(); - - // 妫�鏌rpStaff鏄惁鍖呭惈鑱屼綅璇︽儏鏁版嵁 - if (staff.ErpStaffDetails != null && staff.ErpStaffDetails.Count > 0) - { - // 浣跨敤LINQ灏咵rpStaffDetails杞崲涓篗esStaffPositionLink瀵硅薄 - staffDetails = staff.ErpStaffDetails.Select(staffDetail => - new MesStaffPositionLink - { - // 鍛樺伐ID锛氫粠ErpStaff鐨処d灞炴�ц浆鎹负decimal绫诲瀷 - StaffId = Convert.ToDecimal(staff.Id), - - // 鑱屼綅ID锛� - // 1. 妫�鏌PostId鏄惁涓虹┖鎴杗ull - // 2. 闈炵┖鏃惰浆鎹负decimal锛屽惁鍒欒涓簄ull - PositionId = string.IsNullOrEmpty(staffDetail.fPostId.ToString()) - ? null - : Convert.ToDecimal(staffDetail.fPostId), - - // 鑱屼綅閮ㄩ棬ID锛氶�昏緫涓庤亴浣岻D绫讳技 - FPostDeptId = string.IsNullOrEmpty(staffDetail.fPostDeptid.ToString()) - ? null - : Convert.ToDecimal(staffDetail.fPostDeptid), - - // 鍛樺伐鍏ヨ亴鏃ユ湡锛� - // 1. 妫�鏌StaffStartDate鏄惁涓虹┖鎴杗ull - // 2. 闈炵┖鏃舵寜"yyyy-MM-dd HH:mm:ss"鏍煎紡瑙f瀽涓篋ateTime锛屽惁鍒欒涓簄ull - FStaffStartDate = string.IsNullOrEmpty(staffDetail.fStaffStartDate.ToString()) - ? null - : DateTime.ParseExact(staffDetail.fStaffStartDate, "yyyy-MM-dd HH:mm:ss", null) - }).ToList(); - } - // 杩斿洖杞崲鍚庣殑鑱屼綅鍏宠仈鍒楄〃 - return staffDetails; - } - // SaveList 鏂规硶鐢ㄤ簬淇濆瓨澶氫釜鍛樺伐璁板綍锛屾牴鎹被鍨嬫壒閲忔墽琛屼笉鍚岀殑鎿嶄綔 - public bool SaveList(List<ErpStaff> departments) - { - var list = new List<MesStaff>(); - var userList = new List<SysUser>(); - var mesStaffPositionLinkList = new List<MesStaffPositionLink>(); - departments.ForEach(s => - { - var entity = GetMesStaff(s); - entity.Type = s.Type; - list.Add(entity); - - var sysUser = GetUser(s); - sysUser.Type = s.Type; - userList.Add(sysUser); - var mesStaffPositionLink = - GetMesStaffPositionLink(s); // 鑾峰彇 SysUser 瀹炰緥 - mesStaffPositionLinkList.AddRange(mesStaffPositionLink); - }); - - var groupBy = list.GroupBy(s => s.Type) - .ToDictionary(g => g.Key, g => g.ToList()); - return UseTransaction(db => - { - foreach (var staffGroup in groupBy) - switch (staffGroup.Key) - { - case "0": - if (!UpdateStaffStatusBatch(db, staffGroup.Value, - "A")) // 鎵归噺鍚敤鍛樺伐 - throw new NotImplementedException("鍚敤澶辫触"); - break; - case "1": - if (!UpdateStaffStatusBatch(db, staffGroup.Value, - "B")) // 鎵归噺绂佺敤鍛樺伐 - throw new NotImplementedException("绂佺敤澶辫触"); - break; - case "3": - if (!DeleteStaffBatch(db, userList, - staffGroup.Value)) // 鎵归噺鍒犻櫎鍛樺伐 - throw new NotImplementedException("鍒犻櫎澶辫触"); - break; - case "2": - case "4": - if (!InsertOrUpdateBatch(db, userList, - staffGroup.Value,mesStaffPositionLinkList)) // 鎵归噺鎻掑叆鎴栨洿鏂板憳宸� - throw new NotImplementedException("鍚屾澶辫触"); - break; - default: - throw new ArgumentNullException( - $"type娌℃湁{staffGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�"); - } - - return 1; - }) > 0; - } - - // 鎵归噺鏇存柊鍛樺伐鐘舵�佺殑鏂规硶 - private bool UpdateStaffStatusBatch(SqlSugarScope db, - List<MesStaff> staffList, string status) - { - var ids = staffList.Select(it => it.Id).ToArray(); - var result = db.Updateable<MesStaff>() - .SetColumns(s => s.FforbidStatus == status) - .Where(s => ids.Contains(s.Id)).ExecuteCommand(); - - if (result > 0) - return true; - - throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触"); - } - - // 鎵归噺鎻掑叆鍛樺伐鐨勬柟娉� - private bool InsertStaffBatch(SqlSugarScope db, List<SysUser> userList, - List<MesStaff> staffList) - { - var userInsert = userList.FindAll(s => s.Type == "2"); - var executeCommand = db.Insertable(userInsert).ExecuteCommand(); - if (executeCommand <= 0) throw new ArgumentNullException("瀹℃牳澶辫触"); - if (db.Insertable(staffList).ExecuteCommand() > 0) - return true; - - throw new ArgumentNullException("瀹℃牳澶辫触"); - } - - // 鎵归噺鍒犻櫎鍛樺伐鐨勬柟娉� - private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> userList, - List<MesStaff> staffList) - { - var sid = userList.FindAll(s => s.Type == "3").Select(s => s.Account) - .ToArray(); - var result = db.Deleteable<SysUser>() - .Where(s => sid.Contains(s.Account)).ExecuteCommand(); - - if (result <= 0) throw new ArgumentNullException("鍙嶅鏍稿け璐�"); - var ids = staffList.Select(it => it.Id).ToArray(); - if (db.Deleteable<MesStaff>() - .Where(s => ids.Contains(s.Id)).ExecuteCommand() > 0) - return true; - - throw new ArgumentNullException("鍙嶅鏍稿け璐�"); - } - - // 鎵归噺鎻掑叆鎴栨洿鏂板憳宸ョ殑鏂规硶 - private bool InsertOrUpdateBatch(SqlSugarScope db, List<SysUser> userList, - List<MesStaff> staffList, - List<MesStaffPositionLink> mesStaffPositionLink) - { - foreach (var sysUser in userList) - { - var entity = staffList.First(s => s.Id == sysUser.Sid); - if (!InsertOrUpdateStaff(db, sysUser, entity, mesStaffPositionLink)) - return false; - } - - return true; - } -} - */ - -namespace MES.Service.service.BasicData; +锘縩amespace MES.Service.service.BasicData; using MES.Service.DB; using MES.Service.Dto.webApi; @@ -374,24 +44,62 @@ }) > 0; } + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesStaff>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesStaff>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; + } + private bool InsertUser(SqlSugarScope db, MesStaff entity) { - db.Deleteable<MesStaff>() - .Where(s => s.Id == entity.Id) - .ExecuteCommand(); - - db.Deleteable<SysUser>() - .Where(s => s.StaffId == entity.Id.ToString()) - .ExecuteCommand(); - - var sysUser = GetUser(entity); - if (entity.Id == null) + if (entity.Id == 0) { - var id = db.Insertable<MesStaff>(entity).ExecuteReturnIdentity(); - sysUser.StaffId = id.ToString(); + // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏� + var newId = GenerateNewId(); + entity.Id = newId; + + var sysUser = GetUser(entity); + var staffInsertId = db.Insertable(entity).ExecuteReturnIdentity(); + + if (staffInsertId > 0) + { + sysUser.StaffId = staffInsertId.ToString(); + return db.Insertable(sysUser).ExecuteCommand() > 0; + } + return false; } - - return db.Insertable(sysUser).ExecuteCommand() > 0; + else + { + // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + var originalId = entity.Id; + + // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級 + db.Deleteable<MesStaff>().Where(s => s.Id == originalId).ExecuteCommand(); + db.Deleteable<SysUser>().Where(s => s.StaffId == originalId.ToString()).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + var sysUser = GetUser(entity); + + var staffInsert = db.Insertable(entity).ExecuteCommand(); + if (staffInsert > 0) + { + return db.Insertable(sysUser).ExecuteCommand() > 0; + } + return false; + } } private bool DeleteStaff(SqlSugarScope db, MesStaff entity) @@ -611,9 +319,6 @@ { var entity = new MesStaff { - // Id = string.IsNullOrEmpty(staff.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : Convert.ToDecimal(staff.Id), Guid = Guid.NewGuid(), StaffNo = staff.FStaffNumber, StaffName = staff.FName, @@ -639,13 +344,20 @@ "yyyy-MM-dd HH:mm:ss", null); } - var mesStaff = Db.Queryable<MesStaff>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽憳宸ョ紪鐮佺殑璁板綍 + var existingStaff = Db.Queryable<MesStaff>() .Where(s => s.StaffNo == entity.StaffNo) .First(); - if (mesStaff != null) + if (existingStaff != null) { - entity.Id = mesStaff.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingStaff.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertUser鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; diff --git a/MES.Service/service/BasicData/MesSupplierManager.cs b/MES.Service/service/BasicData/MesSupplierManager.cs index 6a1aa6a..ac1cbd1 100644 --- a/MES.Service/service/BasicData/MesSupplierManager.cs +++ b/MES.Service/service/BasicData/MesSupplierManager.cs @@ -90,9 +90,6 @@ { var entity = new MesSupplier { - // Id = string.IsNullOrEmpty(supplier.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : Convert.ToDecimal(supplier.Id), Type = supplier.Type, SuppNo = supplier.FNumber, SuppSname = supplier.FShortName, @@ -114,13 +111,20 @@ Factory = "1000", }; - var mesSupplier = Db.Queryable<MesSupplier>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屼緵搴斿晢缂栫爜鐨勮褰� + var existingSupplier = Db.Queryable<MesSupplier>() .Where(s => s.SuppNo == entity.SuppNo) .First(); - if (mesSupplier != null) + if (existingSupplier != null) { - entity.Id = mesSupplier.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingSupplier.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; @@ -162,16 +166,45 @@ throw new NotImplementedException("鍒犻櫎澶辫触"); } + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<MesSupplier>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<MesSupplier>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; + } + private bool InsertOrUpdate(SqlSugarScope db, MesSupplier entity) { - db.Deleteable<MesSupplier>().Where(s => s.Id == entity.Id) - .ExecuteCommand(); - - var insert = db.Insertable(entity).ExecuteCommand(); - if (insert > 0) - return true; - - return false; + 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<MesSupplier>().Where(s => s.Id == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } private bool InsertOrUpdateBatch(SqlSugarScope db, diff --git a/MES.Service/service/BasicData/MesUnitManager.cs b/MES.Service/service/BasicData/MesUnitManager.cs index 7f6058d..ba637e9 100644 --- a/MES.Service/service/BasicData/MesUnitManager.cs +++ b/MES.Service/service/BasicData/MesUnitManager.cs @@ -100,9 +100,6 @@ 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, @@ -121,13 +118,20 @@ Type = unit.Type }; - var mesUnit = Db.Queryable<MesUnit>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽崟浣嶇紪鐮佺殑璁板綍 + 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; @@ -164,12 +168,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) diff --git a/MES.Service/service/BasicData/SysDepartmentManager.cs b/MES.Service/service/BasicData/SysDepartmentManager.cs index 91c9b8b..1e91613 100644 --- a/MES.Service/service/BasicData/SysDepartmentManager.cs +++ b/MES.Service/service/BasicData/SysDepartmentManager.cs @@ -80,24 +80,52 @@ throw new NotImplementedException("鍒犻櫎澶辫触"); } + /// <summary> + /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅 + /// </summary> + private decimal GenerateNewId() + { + // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮� + var maxId = Db.Queryable<SysDepartment>().Max(x => (decimal?)x.Id) ?? 0; + var newId = maxId + 1; + + // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦� + while (Db.Queryable<SysDepartment>().Where(x => x.Id == newId).Any()) + { + newId++; + } + + return newId; + } + // 鎻掑叆鎴栨洿鏂伴儴闂ㄧ殑鏂规硶 private bool InsertOrUpdateDepartment(SqlSugarScope db, SysDepartment entity) { - db.Deleteable<SysDepartment>() - .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<SysDepartment>().Where(s => s.Id == originalId).ExecuteCommand(); + + // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D + entity.Id = originalId; + return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; + } } // 灏� ErpDepartment 瀵硅薄杞崲涓� SysDepartment 瀵硅薄鐨勬柟娉� private SysDepartment GetSysDepartment(ErpDepartment department) { - // var unixTimeSeconds = string.IsNullOrEmpty(department.Id) - // ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() - // : Convert.ToDecimal(department.Id); - var fForbidStatus = department.FForbidStatus; if (department.FForbidStatus == "0") { @@ -112,8 +140,6 @@ { Departmentcode = department.FNumber, Departmentname = department.FName, - // Departmentid = unixTimeSeconds, - // Id = unixTimeSeconds, Depextr1 = department.FDeptProperty, Depextr2 = department.FGroup, Depextr3 = department.FWIPStockID, @@ -131,13 +157,20 @@ Factory = "1000" }; - var sysDepartment = Db.Queryable<SysDepartment>() + // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岄儴闂ㄧ紪鐮佺殑璁板綍 + var existingDepartment = Db.Queryable<SysDepartment>() .Where(s => s.Departmentcode == entity.Departmentcode) .First(); - if (sysDepartment != null) + if (existingDepartment != null) { - entity.Id = sysDepartment.Id; + // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏� + entity.Id = existingDepartment.Id; + } + else + { + // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID + entity.Id = 0; } return entity; -- Gitblit v1.9.3