| | |
| | | using MES.Service.Dto.service; |
| | | using MES.Service.Dto.webApi; |
| | | using MES.Service.Modes; |
| | | using MES.Service.util; |
| | | using SqlSugar; |
| | | |
| | | namespace MES.Service.service.BasicData; |
| | | |
| | | public class MesItemsManager : Repository<MesItems> |
| | | { |
| | | public MesItems GetItemQcPrint(WarehouseQuery query) |
| | | { |
| | | return Db.Queryable<MesItems>() |
| | | .Where(c => c.Factory == query.Factory |
| | | && c.Company == query.Company |
| | | && c.ItemNo == query.ItemNo).First(); |
| | | } |
| | | |
| | | |
| | | public bool Save(ErpItems item) |
| | | { |
| | | var entity = GetMesItems(item); |
| | |
| | | { |
| | | switch (item.Type) |
| | | { |
| | | case "0": |
| | | if (UpdateItemStatus(db, entity.Id, "A")) |
| | | return 1; |
| | | break; |
| | | case "1": |
| | | if (UpdateItemStatus(db, entity.Id, "B")) |
| | | return 1; |
| | | break; |
| | | //case "2": |
| | | // if (InsertItem(db, entity)) |
| | | // return 1; |
| | | // break; |
| | | case "3": |
| | | if (DeleteItem(db, entity.Id)) |
| | | if (DeleteItem(db, entity)) |
| | | return 1; |
| | | break; |
| | | case "2": |
| | |
| | | |
| | | 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 => |
| | | { |
| | | 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 "2": |
| | | // if (!InsertItemBatch(db, itemGroup.Value)) |
| | | // throw new NotImplementedException("插入失败"); |
| | | // break; |
| | | case "3": |
| | | if (!DeleteItemBatch(db, itemGroup.Value)) |
| | | throw new NotImplementedException("删除失败"); |
| | | break; |
| | | case "2": |
| | | case "4": |
| | | case "5": |
| | | if (!InsertOrUpdateBatch(db, itemGroup.Value)) |
| | | throw new NotImplementedException("同步失败"); |
| | | break; |
| | | default: |
| | | throw new ArgumentNullException( |
| | | $"type没有{itemGroup.Key}这个类型的参数"); |
| | | } |
| | | |
| | | return 1; |
| | | }) > 0; |
| | | var result = items.Select(Save).ToList(); |
| | | return result.All(b => b); |
| | | } |
| | | |
| | | private bool UpdateItemStatus(SqlSugarScope db, decimal itemId, |
| | | string status) |
| | | private bool DeleteItem(SqlSugarScope db, List<MesItems> listItem) |
| | | { |
| | | var result = db.Updateable<MesItems>() |
| | | .SetColumns(s => s.Fforbidstatus == status) |
| | | .Where(s => s.Id == itemId).ExecuteCommand(); |
| | | // 提取所有需要的 MesId 组合 |
| | | var ids = listItem.Select(customer => customer.Id.ToString()) |
| | | .ToList(); |
| | | |
| | | if (result > 0) |
| | | return true; |
| | | // 使用这些 MesId 去查询 MesLinkU9 表,找到对应的 U9Id |
| | | var u9Ids = Db.Queryable<MesLinkU9>() |
| | | .Where(u => |
| | | ids.Contains(u.MesId) && u.TableType == "MES_ITEMS") |
| | | .Select(u => u.U9Id) |
| | | .Distinct() // 应用 Distinct 去重 |
| | | .ToList(); |
| | | |
| | | throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败"); |
| | | } |
| | | // 根据 U9Id 找到所有对应的 MesId,进行删除 |
| | | // 注意:这里假设您确实想要删除这些记录。如果不确定,请先查询以验证结果。 |
| | | var idsToDelete = Db.Queryable<MesLinkU9>() |
| | | .Where(u => u9Ids.Contains(u.U9Id) && u.TableType == "MES_ITEMS") |
| | | .Select(u => u.MesId) |
| | | .Distinct() // 应用 Distinct 去重 |
| | | .ToList(); |
| | | |
| | | private bool InsertItem(SqlSugarScope db, MesItems entity) |
| | | { |
| | | var insert = db.Insertable(entity).ExecuteCommand(); |
| | | if (insert > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException("插入失败"); |
| | | } |
| | | |
| | | private bool DeleteItem(SqlSugarScope db, decimal itemId) |
| | | { |
| | | var deleteById = db.Deleteable<MesItems>().In(itemId).ExecuteCommand(); |
| | | if (deleteById > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException("删除失败"); |
| | | } |
| | | |
| | | private MesItems GetMesItems(ErpItems item) |
| | | { |
| | | return new MesItems |
| | | { |
| | | Id = Convert.ToDecimal(item.Id), |
| | | ItemId = Convert.ToDecimal(item.Id), |
| | | ItemNo = item.FNumber, |
| | | ItemName = item.FName, |
| | | ItemModel = item.FSpecification, |
| | | ItemUnit = item.FBaseUnitId, |
| | | Lowlimit = item.FSafeStock, |
| | | Highlimit = item.FMaxStock, |
| | | PrdPack = item.FMinPackCount, |
| | | DepotCode = item.FStockId, |
| | | /// <summary> |
| | | /// 接口字段调整:已删除或不再使用PPBOM00019124字段。 |
| | | /// </summary> |
| | | /// <remarks> |
| | | /// 修改人:池南骏 |
| | | /// 修改日期:2024-12-25 |
| | | /// 说明:此字段已被删除或不再使用于当前接口中。 |
| | | /// 保留被注释的代码以记录修改历史。 |
| | | /// </remarks> |
| | | //Ppbom00019124 = item.PPBOM00019124, |
| | | Fmaterialgroup = item.FMaterialGroup, |
| | | Remarks = item.FDescription, |
| | | Ffinishreceiptoverrate = item.FFinishReceiptOverRate, |
| | | Fissuetype = item.FIssueType, |
| | | Fisbatchmanage = item.FIsBatchManage, |
| | | Fpurchaserid = item.FPurchaserId, |
| | | Fpurchaseunitid = item.FPurchaseUnitId, |
| | | Storeunit = item.FStoreUnitID, |
| | | Saleunit = item.FSaleUnitId, |
| | | Fforbidstatus = item.FForbidStatus, |
| | | MaterialProperti = item.FErpClsID, |
| | | ProductionWorkshop = item.FWorkShopId, |
| | | ProduceUnit = item.FPRODUCEUNITID, |
| | | SubconUnit = item.FSUBCONUNITID, |
| | | CreateOrg = Convert.ToDecimal(item.FCreateOrgId), |
| | | UseOrg = Convert.ToDecimal(item.FUseOrgId), |
| | | LossPercent = item.FLOSSPERCENT, |
| | | MnemonicCode = item.FMnemonicCode, |
| | | ExpPeriod = item.FExpPeriod, |
| | | //ColorName = item.FColor, |
| | | CreateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000", |
| | | /// <summary> |
| | | /// 接口字段调整:标准版接口新增字段。 |
| | | /// </summary> |
| | | /// <remarks> |
| | | /// 修改人:<池南骏> |
| | | /// 修改日期:<2024-12-31> |
| | | /// 修改说明: |
| | | /// - 原代码:空 |
| | | /// - 修改后: |
| | | /// </remarks> |
| | | FOLDNUMBER = item.FOldNumber, |
| | | FMINSTOCK = item.FMinStock, |
| | | FFINISHRECEIPTSHORTRATE = item.FFinishReceiptShortRate, |
| | | FDEFAULTVENDOR = item.FDefaultVendor, |
| | | FDOCUMENTSTATUS = item.FDocumentStatus, |
| | | FMFGPOLICYID = item.FMfgPolicyId, |
| | | FBOMUNITID = item.FBOMUnitId, |
| | | FISKITTING = item.FIsKitting, |
| | | FISCOBY = item.FIsCoby, |
| | | FOVERCONTROLMODE = item.FOverControlMode, |
| | | F_MJBH = item.F_MJBH |
| | | }; |
| | | } |
| | | |
| | | private bool UpdateItemStatusBatch(SqlSugarScope db, |
| | | List<MesItems> itemList, string status) |
| | | { |
| | | var ids = itemList.Select(it => it.Id).ToArray(); |
| | | var result = db.Updateable<MesItems>() |
| | | .SetColumns(s => s.Fforbidstatus == status) |
| | | .Where(s => ids.Contains(s.Id)).ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败"); |
| | | } |
| | | |
| | | private bool InsertItemBatch(SqlSugarScope db, List<MesItems> itemList) |
| | | { |
| | | var insertRange = db.Insertable(itemList).ExecuteCommand(); |
| | | if (insertRange > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException("插入失败"); |
| | | } |
| | | |
| | | private bool DeleteItemBatch(SqlSugarScope db, List<MesItems> itemList) |
| | | { |
| | | var ids = itemList.Select(it => it.Id).ToArray(); |
| | | var deleteByIds = db.Deleteable<MesItems>().In(ids).ExecuteCommand(); |
| | | if (deleteByIds > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException("删除失败"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// "插入或更新的方法"调整:修改为先删除再插入。 |
| | | /// </summary> |
| | | /// <remarks> |
| | | /// 修改人:池南骏 |
| | | /// 修改日期:2024-12-31 |
| | | /// 修改说明:` |
| | | /// </remarks> |
| | | /// 原代码: |
| | | //private bool InsertOrUpdate(SqlSugarScope db, MesItems entity) |
| | | //{ |
| | | // var exists = db.Queryable<MesItems>().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, MesItems entity) |
| | | { |
| | | db.Deleteable<MesItems>() |
| | | .Where(s => s.Id == entity.Id) |
| | | var deleteByIds = Db.Deleteable<MesItems>().In(idsToDelete) |
| | | .ExecuteCommand(); |
| | | |
| | | var insert = db.Insertable(entity).ExecuteCommand(); |
| | | return insert > 0; |
| | | // if (deleteByIds > 0) |
| | | // return true; |
| | | return true; |
| | | //throw new NotImplementedException("删除失败"); |
| | | } |
| | | |
| | | private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesItems> itemList) |
| | | private List<MesItems> GetMesItems(ErpItems item) |
| | | { |
| | | foreach (var entity in itemList) |
| | | if (!InsertOrUpdate(db, entity)) |
| | | return false; |
| | | List<MesItems> list = new(); |
| | | |
| | | return true; |
| | | foreach (var erpDetail in item.FUseOrgId) |
| | | { |
| | | var exists = Db.Queryable<MesLinkU9>().Any(u => |
| | | u.U9Id == item.Id && u.OrgId == erpDetail.FUseOrgId && |
| | | u.TableType == "MES_ITEMS"); |
| | | |
| | | decimal mesId = 0; |
| | | if (exists) |
| | | { |
| | | //获取mesid |
| | | mesId = Convert.ToDecimal(Db.Queryable<MesLinkU9>() |
| | | .Where(u => |
| | | u.U9Id == item.Id && u.OrgId == erpDetail.FUseOrgId && |
| | | u.TableType == "MES_ITEMS") |
| | | .Select(u => u.MesId) // 选择 MesId 字段 |
| | | .First()); |
| | | } |
| | | else |
| | | { |
| | | mesId = GetSeqUtil.GetSeq("SEQ_ITEMS_ID"); |
| | | var entity = new MesLinkU9 |
| | | { |
| | | CreateDate = DateTime.Now, |
| | | MesId = mesId.ToString(), |
| | | U9Id = item.Id, |
| | | OrgId = erpDetail.FUseOrgId, |
| | | TableType = "MES_ITEMS" |
| | | }; |
| | | Db.Insertable(entity).ExecuteCommand(); |
| | | } |
| | | |
| | | var items = new MesItems |
| | | { |
| | | Id = mesId, |
| | | ItemId = mesId, |
| | | |
| | | Fmaterialgroup = item.FMaterialGroup, |
| | | ItemNo = item.FNumber, |
| | | OldItemNo = item.FOldNumber, |
| | | ItemName = item.FName, |
| | | CatalogNo = item.CatalogNo, |
| | | ItemModel = item.FSpecification, |
| | | SafeStock = item.FSafeStock, |
| | | Lowlimit = item.FMinStock, |
| | | Highlimit = item.FMaxStock, |
| | | DepotCode = item.FStockId, |
| | | Fisbatchmanage = item.FIsBatchManage, |
| | | MaterialProperti = item.FErpClsID, |
| | | Storeunit = item.FStoreUnitID, |
| | | ItemUnit = item.FBaseUnitId, |
| | | Fpurchaseunitid = item.FPurchaseUnitId, |
| | | Saleunit = item.FSaleUnitId, |
| | | ProduceUnit = item.FPRODUCEUNITID, |
| | | MaterialOutUomid = item.MaterialOutUomid, |
| | | CreateBy = item.CreateBy, |
| | | PubDescSeg38 = item.PubDescSeg38, |
| | | Description = item.Description, |
| | | PrivateDescSeg4 = item.PrivateDescSeg4, |
| | | PrivateDescSeg9 = item.PrivateDescSeg9, |
| | | IsDualQuantity = item.IsDualQuantity, |
| | | IsDualUom = item.IsDualUom, |
| | | IsMultyUom = item.IsMultyUom, |
| | | IsVarRatio = item.IsVarRatio, |
| | | ConverRatioRule = item.ConverRatioRule, |
| | | PrivateDescSeg11 = item.PrivateDescSeg11, |
| | | PrivateDescSeg7 = item.PrivateDescSeg7, |
| | | PrivateDescSeg2 = item.PrivateDescSeg2, |
| | | DocumentStatus = item.FDocumentStatus, |
| | | ForbidStatus = item.FForbidStatus, |
| | | UseOrg = erpDetail.FUseOrgId, |
| | | CreateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000", |
| | | }; |
| | | list.Add(items); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private bool InsertOrUpdate(SqlSugarScope db, List<MesItems> listItem) |
| | | { |
| | | DeleteItem(db, listItem); |
| | | |
| | | var insert = db.Insertable(listItem).IgnoreColumns(true) |
| | | .ExecuteCommand(); |
| | | return insert > 0; |
| | | } |
| | | } |