zhuangpei-mesbackend/DOAN.Service/MES/ERP/InteractERPService.cs
2025-08-04 11:05:21 +08:00

330 lines
14 KiB
C#

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
{
/// <summary>
/// 工位Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IInteractERPService), ServiceLifetime = LifeTime.Transient)]
public class InteractERPService : BaseService<CustDevMesCustomer>, IInteractERPService
{
/// <summary>
/// 同步ERP中的客户信息
/// </summary>
/// <returns></returns>
public int SynhERPCustom()
{
int result = 0;
List<CustDevMesCustomer> costomer_ERP_List = Context.AsTenant().QueryableWithAttr<CustDevMesCustomer>().ToList();
if (costomer_ERP_List != null && costomer_ERP_List.Count > 0)
{
List<BaseCustom> baseCustomList = Context.AsTenant().QueryableWithAttr<BaseCustom>().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<BaseCustom>();//线程安全用GetConnectionWithAttrScope
childDb.Ado.ExecuteCommand("ALTER TABLE base_custom AUTO_INCREMENT = 1");
TypeAdapterConfig<CustDevMesCustomer, BaseCustom>.NewConfig()
.Map(dest => dest.CustomNo, src => src.CusCode)
.Map(dest => dest.CustomName, src => src.CusName)
.Map(dest => dest.CustomAddName, src => src.CusAbbName)
;
List<BaseCustom> baseCustoms = costomer_ERP_List.Adapt<List<BaseCustom>>();
foreach (var item in baseCustoms)
{
item.CreatedBy = "ERP";
item.CreatedTime = DateTime.Now;
item.Status = 1;
}
result = Context.AsTenant().InsertableWithAttr(baseCustoms).ExecuteCommand();
}
}
return result;
}
/// <summary>
/// 同步供应商 custDev_mes_Vendor SELECT * from t_custDev_mes_Vendor
/// </summary>
/// <returns></returns>
public int SynchERPVendor()
{
int result = 0;
List<CustDevMesVendor> vendor_ERP_List = Context.AsTenant().QueryableWithAttr<CustDevMesVendor>().ToList();
if (vendor_ERP_List != null && vendor_ERP_List.Count > 0)
{
List<BaseSupplier> suppliers = Context.AsTenant().QueryableWithAttr<BaseSupplier>().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<BaseSupplier>();//线程安全用GetConnectionWithAttrScope
childDb.DbMaintenance.TruncateTable<BaseSupplier>();
childDb.Ado.ExecuteCommand("ALTER TABLE base_supplier AUTO_INCREMENT = 1");
TypeAdapterConfig<CustDevMesVendor, BaseSupplier>.NewConfig()
.Map(dest => dest.SupplierNo, src => src.VenCode)
.Map(dest => dest.SupplierName, src => src.VenName)
.Map(dest => dest.SupplierAddName, src => src.VenAbbName)
;
List<BaseSupplier> baseSuppliers = vendor_ERP_List.Adapt<List<BaseSupplier>>();
foreach (var item in baseSuppliers)
{
item.CreatedBy = "ERP";
item.CreatedTime = DateTime.Now;
item.Status = 1;
}
result = Context.AsTenant().InsertableWithAttr(baseSuppliers).ExecuteCommand();
}
}
return result;
}
/// <summary>
/// 同步物料分类
/// </summary>
/// <returns></returns>
public int SynchERPInventoryClass()
{
//ERP 中 物料分类数据
List<CustDevMesInventoryClass> ERP_Datas = Context.AsTenant().QueryableWithAttr<CustDevMesInventoryClass>().ToList();
//清空表
var childDb = Context.AsTenant().GetConnectionWithAttr<BaseMaterialType>();//线程安全用GetConnectionWithAttrScope
//清空表
childDb.DbMaintenance.TruncateTable<BaseMaterialType>();
childDb.Ado.ExecuteCommand("ALTER TABLE base_material_type AUTO_INCREMENT = 1");
// 配置局部映射规则
TypeAdapterConfig<CustDevMesInventoryClass, BaseMaterialType>.NewConfig()
.Map(dest => dest.Code, src => src.InvCCode)
.Map(dest => dest.Name, src => src.InvCName)
.Map(dest => dest.ParentCode, src => src.SuperiorInvCCode)
;
List<BaseMaterialType> baseMaterials = ERP_Datas.Adapt<List<BaseMaterialType>>();
foreach (var item in baseMaterials)
{
item.CreatedBy = "ERP";
item.CreatedTime = DateTime.Now;
item.Status = 1;
}
return Context.AsTenant().InsertableWithAttr(baseMaterials).ExecuteCommand();
}
/// <summary>
/// 同步物料清单
/// </summary>
/// <returns></returns>
public int SynchERPInventory()
{
// ERP中的物料清单
List<CustDevMesInventory> ERPInventories = Context.AsTenant()
.QueryableWithAttr<CustDevMesInventory>().ToList();
var childDb = Context.AsTenant().GetConnectionWithAttr<BaseMaterialList>();//线程安全用GetConnectionWithAttrScope
//清空表
childDb.DbMaintenance.TruncateTable<BaseMaterialList>();
childDb.Ado.ExecuteCommand("ALTER TABLE base_material_list AUTO_INCREMENT = 1");
// 配置局部映射规则
TypeAdapterConfig<CustDevMesInventory, BaseMaterialList>.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<BaseMaterialList> materialLists = ERPInventories.Adapt<List<BaseMaterialList>>();
foreach (BaseMaterialList material in materialLists)
{
material.Id = XueHua;
material.CreatedBy = "ERP";
material.CreatedTime = DateTime.Now;
}
// return Context.AsTenant().InsertableWithAttr(materialLists).ExecuteCommand();
return childDb.Fastest<BaseMaterialList>().PageSize(100000).BulkCopy(materialLists);
}
/// <summary>
/// 同步BOM
/// </summary>
/// <returns></returns>
public int SynchERPBOM()
{
//ERP中的BOM
var childDb2 = Context.AsTenant().GetConnectionWithAttr<CustDevMesBom>();
//如何获取同种物料的最高BOM版本
var subQuery=childDb2.Queryable<CustDevMesBom>().GroupBy(it => it.McInvCode).Select(it => new
{
McInvCode = it.McInvCode,
BOMVersion = SqlFunc.AggregateMax(it.BOMVersion)
});
List<CustDevMesBom> ERPBoms = childDb2.Queryable(subQuery)
.LeftJoin<CustDevMesBom>((sub,cu)=>sub.McInvCode==cu.McInvCode&&sub.BOMVersion==cu.BOMVersion)
.Select((sub,cu)=>cu)
.Distinct()
.ToList();
var childDb = Context.AsTenant().GetConnectionWithAttr<BaseMaterialBom>();
Task.Run(() =>
{
//清空表 耗时17s
childDb.DbMaintenance.TruncateTable<BaseMaterialBom>();
childDb.Ado.ExecuteCommand("ALTER TABLE base_material_bom AUTO_INCREMENT = 1");
// 配置局部映射规则
TypeAdapterConfig<CustDevMesBom, BaseMaterialBom>.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<BaseMaterialBom> baseMaterialBoms = ERPBoms.Adapt<List<BaseMaterialBom>>();
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<BaseMaterialBom>().PageSize(100000).BulkCopy(baseMaterialBoms);
});
return 1;
}
}
}