using System; using SqlSugar; using Infrastructure.Attribute; using Infrastructure.Extensions; using DOAN.Model; using DOAN.Model.MES.base_.Dto; using DOAN.Model.MES.base_; using DOAN.Repository; using DOAN.Service.MES.base_.IService; using System.Linq; using System.Threading.Tasks; using Mapster; using DOAN.Service.MES.ERP.IService; using DOAN.Model.MES.ERP; using MimeKit.Tnef; using NPOI.SS.Formula.Functions; using static Aliyun.OSS.Model.LiveChannelConfiguration; using Microsoft.VisualBasic; namespace DOAN.Service.MES.ERP { /// /// 工位Service业务层处理 /// [AppService(ServiceType = typeof(IInteractERPService), ServiceLifetime = LifeTime.Transient)] public class InteractERPService : BaseService, IInteractERPService { /// /// 同步ERP中的客户信息 /// /// public int SynhERPCustom() { int result = 0; List costomer_ERP_List = Context.AsTenant().QueryableWithAttr().ToList(); if (costomer_ERP_List != null && costomer_ERP_List.Count > 0) { List baseCustomList = Context.AsTenant().QueryableWithAttr().ToList(); if (baseCustomList != null && baseCustomList.Count > 0) { foreach (var costomerERP in costomer_ERP_List) { BaseCustom custom = baseCustomList .Where(it => it.CustomNo == costomerERP.CusCode).FirstOrDefault(); if (custom != null) { custom.CustomNo = costomerERP.CusCode; custom.CustomName = costomerERP.CusName; custom.CustomAddName = costomerERP.CusAbbName; custom.UpdatedTime = DateTime.Now; custom.UpdatedBy = "ERP"; result += Context.AsTenant().UpdateableWithAttr(custom).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); } else { BaseCustom custom_insert = new BaseCustom(); custom_insert.CustomNo = costomerERP.CusCode; custom_insert.CustomName = costomerERP.CusName; custom_insert.CustomAddName = costomerERP.CusAbbName; custom_insert.CreatedBy = "ERP"; custom_insert.CreatedTime = DateTime.Now; custom_insert.Status = 1; result += Context.AsTenant().InsertableWithAttr(custom_insert).ExecuteCommand(); } } // 判断删除 foreach (var item in baseCustomList) { bool flag = costomer_ERP_List.Where(it => it.CusCode == item.CustomNo).Any(); if (!flag) { Context.AsTenant().DeleteableWithAttr(item).ExecuteCommand(); } } } else { var childDb = Context.AsTenant().GetConnectionWithAttr();//线程安全用GetConnectionWithAttrScope childDb.Ado.ExecuteCommand("ALTER TABLE base_custom AUTO_INCREMENT = 1"); TypeAdapterConfig.NewConfig() .Map(dest => dest.CustomNo, src => src.CusCode) .Map(dest => dest.CustomName, src => src.CusName) .Map(dest => dest.CustomAddName, src => src.CusAbbName) ; List baseCustoms = costomer_ERP_List.Adapt>(); foreach (var item in baseCustoms) { item.CreatedBy = "ERP"; item.CreatedTime = DateTime.Now; item.Status = 1; } result = Context.AsTenant().InsertableWithAttr(baseCustoms).ExecuteCommand(); } } return result; } /// /// 同步供应商 custDev_mes_Vendor SELECT * from t_custDev_mes_Vendor /// /// public int SynchERPVendor() { int result = 0; List vendor_ERP_List = Context.AsTenant().QueryableWithAttr().ToList(); if (vendor_ERP_List != null && vendor_ERP_List.Count > 0) { List suppliers = Context.AsTenant().QueryableWithAttr().ToList(); if (suppliers != null && suppliers.Count > 0) { foreach (var vendorERP in vendor_ERP_List) { BaseSupplier supplier = suppliers .Where(it => it.SupplierNo == vendorERP.VenCode).FirstOrDefault(); if (supplier != null) { supplier.SupplierNo = vendorERP.VenCode; supplier.SupplierName = vendorERP.VenName; supplier.SupplierAddName = vendorERP.VenAbbName; result += Context.AsTenant().UpdateableWithAttr(supplier).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); } else { BaseSupplier supplier_insert = new BaseSupplier(); supplier_insert.SupplierNo = vendorERP.VenCode; supplier_insert.SupplierName = vendorERP.VenName; supplier_insert.SupplierAddName = vendorERP.VenAbbName; supplier_insert.Status = 1; result += Context.AsTenant().InsertableWithAttr(supplier_insert).ExecuteCommand(); } } // 判断删除 foreach (var item in suppliers) { bool flag = vendor_ERP_List.Where(it => it.VenCode == item.SupplierNo).Any(); if (!flag) { Context.AsTenant().DeleteableWithAttr(item).ExecuteCommand(); } } } else { // ALTER TABLE base_supplier AUTO_INCREMENT = 1 var childDb = Context.AsTenant().GetConnectionWithAttr();//线程安全用GetConnectionWithAttrScope childDb.DbMaintenance.TruncateTable(); childDb.Ado.ExecuteCommand("ALTER TABLE base_supplier AUTO_INCREMENT = 1"); TypeAdapterConfig.NewConfig() .Map(dest => dest.SupplierNo, src => src.VenCode) .Map(dest => dest.SupplierName, src => src.VenName) .Map(dest => dest.SupplierAddName, src => src.VenAbbName) ; List baseSuppliers = vendor_ERP_List.Adapt>(); foreach (var item in baseSuppliers) { item.CreatedBy = "ERP"; item.CreatedTime = DateTime.Now; item.Status = 1; } result = Context.AsTenant().InsertableWithAttr(baseSuppliers).ExecuteCommand(); } } return result; } /// /// 同步物料分类 /// /// public int SynchERPInventoryClass() { //ERP 中 物料分类数据 List ERP_Datas = Context.AsTenant().QueryableWithAttr().ToList(); //清空表 var childDb = Context.AsTenant().GetConnectionWithAttr();//线程安全用GetConnectionWithAttrScope //清空表 childDb.DbMaintenance.TruncateTable(); childDb.Ado.ExecuteCommand("ALTER TABLE base_material_type AUTO_INCREMENT = 1"); // 配置局部映射规则 TypeAdapterConfig.NewConfig() .Map(dest => dest.Code, src => src.InvCCode) .Map(dest => dest.Name, src => src.InvCName) .Map(dest => dest.ParentCode, src => src.SuperiorInvCCode) ; List baseMaterials = ERP_Datas.Adapt>(); foreach (var item in baseMaterials) { item.CreatedBy = "ERP"; item.CreatedTime = DateTime.Now; item.Status = 1; } return Context.AsTenant().InsertableWithAttr(baseMaterials).ExecuteCommand(); } /// /// 同步物料清单 /// /// public int SynchERPInventory() { // ERP中的物料清单 List ERPInventories = Context.AsTenant() .QueryableWithAttr().ToList(); var childDb = Context.AsTenant().GetConnectionWithAttr();//线程安全用GetConnectionWithAttrScope //清空表 childDb.DbMaintenance.TruncateTable(); childDb.Ado.ExecuteCommand("ALTER TABLE base_material_list AUTO_INCREMENT = 1"); // 配置局部映射规则 TypeAdapterConfig.NewConfig() .Map(dest => dest.Code, src => src.InvCode) .Map(dest => dest.Name, src => src.InvName) .Map(dest => dest.Specification, src => src.InvStd) .Map(dest => dest.ADDCode, src => src.InvAddCode) .Map(dest => dest.Unit, src => src.ComUnitName) .Map(dest => dest.FkMaterialTypeCode, src => src.InvCCode) .Map(dest => dest.SupplierCode, src => src.ClnvDefine8) ; List materialLists = ERPInventories.Adapt>(); foreach (BaseMaterialList material in materialLists) { material.Id = XueHua; material.CreatedBy = "ERP"; material.CreatedTime = DateTime.Now; } // return Context.AsTenant().InsertableWithAttr(materialLists).ExecuteCommand(); return childDb.Fastest().PageSize(100000).BulkCopy(materialLists); } /// /// 同步BOM /// /// public int SynchERPBOM() { //ERP中的BOM var childDb2 = Context.AsTenant().GetConnectionWithAttr(); //如何获取同种物料的最高BOM版本 var subQuery=childDb2.Queryable().GroupBy(it => it.McInvCode).Select(it => new { McInvCode = it.McInvCode, BOMVersion = SqlFunc.AggregateMax(it.BOMVersion) }); List ERPBoms = childDb2.Queryable(subQuery) .LeftJoin((sub,cu)=>sub.McInvCode==cu.McInvCode&&sub.BOMVersion==cu.BOMVersion) .Select((sub,cu)=>cu) .Distinct() .ToList(); var childDb = Context.AsTenant().GetConnectionWithAttr(); Task.Run(() => { //清空表 耗时17s childDb.DbMaintenance.TruncateTable(); childDb.Ado.ExecuteCommand("ALTER TABLE base_material_bom AUTO_INCREMENT = 1"); // 配置局部映射规则 TypeAdapterConfig.NewConfig() .Map(dest => dest.InvCode, src => src.McInvCode) .Map(dest => dest.InvName, src => src.McInvName) .Map(dest => dest.SubInvCode, src => src.subcInvCode) .Map(dest => dest.SubInvName, src => src.subcInvName) .Map(dest => dest.Iusequantity, src => src.Iusequantity) .Map(dest => dest.BOMVersion, src => src.BOMVersion); List baseMaterialBoms = ERPBoms.Adapt>(); if (baseMaterialBoms != null && baseMaterialBoms.Count() > 0) { foreach (var bom_item in baseMaterialBoms) { bom_item.Id = XueHua; bom_item.CreatedBy = "ERP"; bom_item.CreatedTime = DateTime.Now; } } // return Context.AsTenant().InsertableWithAttr(baseMaterialBoms).ExecuteCommand(); childDb.Fastest().PageSize(100000).BulkCopy(baseMaterialBoms); }); return 1; } } }