using Business.Core.Utilities; using Business.EntityFrameworkCore; using Business.Model.Ext; using Business.Model.MES.IC; using Business.Model.SRM; using NLog; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Business.Quartz { //处理Ext数据库表 public class ExtJobService: ApplicationService { SnowFlake help = new SnowFlake(); private IRepository _kd_material; private IRepository _ic_item; private IRepository _ic_item_inventory; private IRepository _srm_purchase; private IRepository _ic_item_stock; private IRepository _ic_plan; private IRepository _ic_check; private IRepository _ic_factory_details; private IRepository _kd_bom; private IRepository _ic_bom; private IRepository _ic_bom_child; private IRepository _ic_substitute; private IRepository _ic_substitute_group; private IRepository _ic_substitute_group_detail; private IRepository _ext_xref; private readonly BusinessDbContext _businessDbContext; private readonly BusinessExtDbContext _businessExtDbContext; public ExtJobService( IRepository kd_material, IRepository ic_item, IRepository ic_item_inventory, IRepository srm_purchase, IRepository ic_item_stock, IRepository ic_plan, IRepository ic_check, IRepository ic_factory_details, IRepository kd_bom, IRepository ic_bom, IRepository ic_bom_child, IRepository ic_substitute, IRepository ic_substitute_group, IRepository ic_substitute_group_detail, IRepository ext_xref, BusinessDbContext businessDbContext, BusinessExtDbContext businessExtDbContext ) { _kd_material= kd_material; _ic_item= ic_item; _ic_item_inventory = ic_item_inventory; _srm_purchase = srm_purchase; _ic_item_stock = ic_item_stock; _ic_plan= ic_plan; _ic_check = ic_check; _ic_factory_details= ic_factory_details; _kd_bom = kd_bom; _ic_bom= ic_bom; _ic_bom_child= ic_bom_child; _ic_substitute= ic_substitute; _ic_substitute_group= ic_substitute_group; _ic_substitute_group_detail= ic_substitute_group_detail; _ext_xref= ext_xref; _businessDbContext = businessDbContext; _businessExtDbContext = businessExtDbContext; } /// /// 处理物料 /// public async void DoExt() { await DoMaterial(); //DoBom(); } /// /// 处理物料 /// public async Task DoMaterial() { var extMaterials=_kd_material.GetListAsync(); var ic_items = _ic_item.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101); var ic_factory_detailss = _ic_factory_details.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101); var srm_purchases = _srm_purchase.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101); var ic_plans = _ic_plan.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101); List ic_itemListAdd = new List(); List ic_itemListUpdate = new List(); List ic_factory_detailsListAdd = new List(); List ic_factory_detailsListUpdate = new List(); List srm_purchaseListAdd = new List(); List srm_purchaseListUpdate = new List(); List ic_planListAdd = new List(); List ic_planListUpdate = new List(); List ext_xrefList = new List(); if (extMaterials != null) { extMaterials.Result.ForEach(ext => { if (ic_items != null && ic_items.Result.Exists(a => a.number == ext.FNumber)) { var ic_item = ic_items.Result.FirstOrDefault(a => a.number == ext.FNumber); if (ic_item != null) { ic_item.number = ext.FNumber; ic_item.name = ext.FName_2052; ic_item.model = ext.FSpecification_2052; ic_item.full_name = ext.FDescription_2052; ic_item.fms_number = ext.FOldNumber; //0.配置类 1. 2.委外加工 3.外购 4.虚拟件 ic_item.erp_cls = ext.FErpClsID == "配置类" ? 0 : ext.FErpClsID == "自制" ? 1 : ext.FErpClsID == "委外" ? 2 : ext.FErpClsID == "外购" ? 3 : ext.FErpClsID == "虚拟" ? 4 : ext.FErpClsID == "费用" ? 5 : ext.FErpClsID == "服务" ? 6 : -1; ic_item.source = ext.FDataSource; ic_item.iskeyitem = ext.FIsKitting == "是" ? 1 : 0; ic_item.gross_weight = ext.FGROSSWEIGHT; ic_item.net_weight = ext.FNETWEIGHT; ic_item.maund = ext.FWEIGHTUNITID_Name; ic_item.cubic_measure = ext.FVOLUMEUNITID_Name; ic_item.length = ext.FLENGTH; ic_item.width = ext.FWIDTH; ic_item.height = ext.FHEIGHT; ic_item.size = ext.FVOLUME; ic_item.allowpur = ext.FIsPurchase == "是" ? 1 : 0; ic_item.allowsale = ext.FIsSale == "是" ? 1 : 0; ic_item.allowmanu = ext.FIsProduce == "是" ? 1 : 0; ic_item.allowout = ext.FIsSubContract == "是" ? 1 : 0; ic_item.allowbatch = ext.F_SFJLPCH == "是" ? 1 : 0; ic_itemListUpdate.Add(ic_item); ext_xrefList.Add(new ext_xref { dop_table_name = "ic_item", dop_id = ic_item.Id, dop_col1_name = "number", dop_col1_value = ic_item.number, other_sys_name = "kd", other_table_name = "in_kd_material", other_id = ext.Id, other_col1_name = "FNumber", other_col1_value = ext.FNumber }); var ic_factory_detail = ic_factory_detailss.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id); if (ic_factory_detail != null) { ic_factory_detail.product_line = ext.F_CPX_Name; ic_factory_detail.minpackqty = ext.FIncreaseQty; ic_factory_detailsListUpdate.Add(ic_factory_detail); } var srm_purchase = srm_purchases.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id); if (srm_purchases != null) { srm_purchase.icitem_name = ic_item.name; srm_purchase.purchgroup = ext.FPurchaseGroupId_Name; srm_purchase.purcher = ext.FPurchaserId_Name; srm_purchase.purchase_unit = ext.FPurchaseUnitId_Name; srm_purchaseListUpdate.Add(srm_purchase); } var ic_plan = ic_plans.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id); if (ic_plan != null) { ic_plan.icitem_name = ic_item.name; ic_plan.planner_name = ext.FPurchaseGroupId_Name; ic_plan.fix_leadtime = ext.FFixLeadTime; ic_plan.plan_trategy = ext.FPlanningStrategy == "MRP" ? 1 : 0; ic_plan.order_trategy = ext.FOrderPolicy == "LFL(批对批)" ? 1 : 0; ic_plan.order_inter_val = ext.FOrderIntervalTime; ic_plan.lead_time = ext.FVarLeadTime; ic_plan.bat_change_economy = ext.FVarLeadTimeLotSize; ic_plan.order_point = ext.FReOrderGood; ic_plan.secinv = ext.FPlanSafeStockQty; ic_planListUpdate.Add(ic_plan); } } } else { ic_item item = new ic_item(help.NextId()); item.number = ext.FNumber; item.name = ext.FName_2052; item.model = ext.FSpecification_2052; item.full_name = ext.FDescription_2052; item.fms_number = ext.FOldNumber; //0.配置类 1. 2.委外加工 3.外购 4.虚拟件 item.erp_cls = ext.FErpClsID == "配置类" ? 0 : ext.FErpClsID == "自制" ? 1 : ext.FErpClsID == "委外" ? 2 : ext.FErpClsID == "外购" ? 3 : ext.FErpClsID == "虚拟" ? 4 : ext.FErpClsID == "费用" ? 5 : ext.FErpClsID == "服务" ? 6 : -1; item.source = ext.FDataSource; item.iskeyitem = ext.FIsKitting == "是" ? 1 : 0; item.gross_weight = ext.FGROSSWEIGHT; item.net_weight = ext.FNETWEIGHT; item.maund = ext.FWEIGHTUNITID_Name; item.cubic_measure = ext.FVOLUMEUNITID_Name; item.length = ext.FLENGTH; item.width = ext.FWIDTH; item.height = ext.FHEIGHT; item.size = ext.FVOLUME; item.allowpur = ext.FIsPurchase == "是" ? 1 : 0; item.allowsale = ext.FIsSale == "是" ? 1 : 0; item.allowmanu = ext.FIsProduce == "是" ? 1 : 0; item.allowout = ext.FIsSubContract == "是" ? 1 : 0; item.allowbatch = ext.F_SFJLPCH == "是" ? 1 : 0; item.tenant_id = 101;//TODO item.factory_id = 10101;//TODO item.org_id = 101;//TODO item.IsDeleted = false;//TODO item.is_equipment = 0; item.item_level = 0; item.picktype = 0; item.enable_warning = 0; ic_itemListAdd.Add(item); ext_xrefList.Add(new ext_xref { dop_table_name = "ic_item", dop_id = item.Id, dop_col1_name = "number", dop_col1_value = item.number, other_sys_name = "kd", other_table_name = "in_kd_material", other_id = ext.Id, other_col1_name = "FNumber", other_col1_value = ext.FNumber }); ic_factory_detailsListAdd.Add(new ic_factory_details(help.NextId()) { icitem_id = item.Id, icitem_name = ext.FName_2052, tenant_id = 101,//TODO factory_id = 10101,////TODO org_id = 101,//TODO IsDeleted = false,//TODO pick_type = 0, stock_time = 0, batch_manager = 0, days_per = 0, default_chkloc = 0, default_chksp = 0, default_loc = 0, default_sp = 0, product_line = ext.F_CPX_Name, production_leadtime = 0, stock_leadtime = 0, transportation_leadtime = 0, order_leadtime = 0, minpackqty = ext.FIncreaseQty, minorderqty = 0 }); srm_purchaseListAdd.Add(new srm_purchase(help.NextId()) { icitem_id = item.Id, icitem_name = item.name, currency_type = ext.FCurrencyId_Name == "人民币" ? 1 : 0,//TODO tenant_id = 101,//TODO factory_id = 10101,////TODO org_id = 101,//TODO IsDeleted = false,//TODO supplier_id = 0, purchgroup = ext.FPurchaseGroupId_Name, purcher = ext.FPurchaserId_Name, purchase_unit = ext.FPurchaseUnitId_Name }); ic_planListAdd.Add(new ic_plan(help.NextId()) { icitem_id = item.Id, icitem_name = item.name, planner_name = ext.FPurchaseGroupId_Name, fix_leadtime = ext.FFixLeadTime, plan_trategy = ext.FPlanningStrategy == "MRP" ? 1 : 0, order_trategy = ext.FOrderPolicy == "LFL(批对批)" ? 1 : 0, order_inter_val = ext.FOrderIntervalTime, lead_time = ext.FVarLeadTime, bat_change_economy = ext.FVarLeadTimeLotSize, order_point = ext.FReOrderGood, secinv = ext.FPlanSafeStockQty, tenant_id = 101,//TODO factory_id = 10101,////TODO org_id = 101,//TODO IsDeleted = false,//TODO }); } }); if (ic_itemListAdd.Count > 0) { //await _ic_item.InsertManyAsync(ic_itemListAdd); await _businessDbContext.ic_item.BulkInsertAsync(ic_itemListAdd, options => { options.InsertKeepIdentity = true; options.InsertIfNotExists = true; options.AutoMapOutputDirection = false; }); } if (ic_itemListUpdate.Count > 0) { //await _ic_item.UpdateManyAsync(ic_itemListUpdate); await _businessDbContext.ic_item.BulkUpdateAsync(ic_itemListUpdate, options => { options.InsertKeepIdentity = true; options.AutoMapOutputDirection = false; options.ColumnPrimaryKeyExpression = c => c.Id; }); } if (ic_factory_detailsListAdd.Count > 0) { //await _ic_factory_details.InsertManyAsync(ic_factory_detailsListAdd); await _businessDbContext.ic_factory_details.BulkInsertAsync(ic_factory_detailsListAdd, options => { options.InsertKeepIdentity = true; options.InsertIfNotExists = true; options.AutoMapOutputDirection = false; }); } if (ic_factory_detailsListUpdate.Count > 0) { //await _ic_factory_details.UpdateManyAsync(ic_factory_detailsListUpdate); await _businessDbContext.ic_factory_details.BulkUpdateAsync(ic_factory_detailsListUpdate, options => { options.InsertKeepIdentity = true; options.AutoMapOutputDirection = false; options.ColumnPrimaryKeyExpression = c => c.Id; }); } if (srm_purchaseListAdd.Count > 0) { //await _ic_item_pur.InsertManyAsync(ic_item_purListAdd); await _businessDbContext.srm_purchase.BulkInsertAsync(srm_purchaseListAdd, options => { options.InsertKeepIdentity = true; options.InsertIfNotExists = true; options.AutoMapOutputDirection = false; }); } if (srm_purchaseListUpdate.Count > 0) { //await _ic_item_pur.UpdateManyAsync(ic_item_purListUpdate); await _businessDbContext.srm_purchase.BulkUpdateAsync(srm_purchaseListUpdate, options => { options.InsertKeepIdentity = true; options.AutoMapOutputDirection = false; options.ColumnPrimaryKeyExpression = c => c.Id; }); } if (ic_planListAdd.Count > 0) { //await _ic_plan.InsertManyAsync(ic_planListAdd); await _businessDbContext.ic_plan.BulkInsertAsync(ic_planListAdd, options => { options.InsertKeepIdentity = true; options.InsertIfNotExists = true; options.AutoMapOutputDirection = false; }); } if (ic_planListUpdate.Count > 0) { //await _ic_plan.UpdateManyAsync(ic_planListUpdate); await _businessDbContext.ic_plan.BulkUpdateAsync(ic_planListUpdate, options => { options.InsertKeepIdentity = true; options.AutoMapOutputDirection = false; options.ColumnPrimaryKeyExpression = c => c.Id; }); } if (ext_xrefList.Count > 0) { //await _ext_xref.UpdateManyAsync(ext_xrefList); await _businessExtDbContext.ext_xref.BulkInsertAsync(ext_xrefList, options => { options.InsertKeepIdentity = true; options.InsertIfNotExists = true; options.AutoMapOutputDirection = false; }); } } } /// /// 处理物料清单 /// public void DoBom() { LogManager.Configuration.Install(new NLog.Config.InstallationContext()); } } }