ExtJobService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using Business.Core.Utilities;
  2. using Business.EntityFrameworkCore;
  3. using Bussiness.Model.Ext;
  4. using Bussiness.Model.MES.IC;
  5. using Bussiness.Model.SRM;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Hosting;
  8. using NLog;
  9. using SixLabors.ImageSharp;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Xml.Linq;
  17. using Volo.Abp.Application.Services;
  18. using Volo.Abp.Domain.Repositories;
  19. namespace Business.Quartz
  20. {
  21. //处理Ext数据库表
  22. public class ExtJobService: ApplicationService
  23. {
  24. SnowFlake help = new SnowFlake();
  25. private IRepository<in_kd_material, long> _kd_material;
  26. private IRepository<ic_item, long> _ic_item;
  27. private IRepository<ic_item_inventory, long> _ic_item_inventory;
  28. private IRepository<srm_purchase, long> _srm_purchase;
  29. private IRepository<ic_item_stock, long> _ic_item_stock;
  30. private IRepository<ic_plan, long> _ic_plan;
  31. private IRepository<ic_check, long> _ic_check;
  32. private IRepository<ic_factory_details, long> _ic_factory_details;
  33. private IRepository<in_kd_bom, long> _kd_bom;
  34. private IRepository<ic_bom, long> _ic_bom;
  35. private IRepository<ic_bom_child, long> _ic_bom_child;
  36. private IRepository<ic_substitute, long> _ic_substitute;
  37. private IRepository<ic_substitute_all, long> _ic_substitute_all;
  38. private IRepository<ic_substitute_all_dtl, long> _ic_substitute_all_dtl;
  39. private IRepository<ext_xref, long> _ext_xref;
  40. private readonly BusinessDbContext _businessDbContext;
  41. private readonly BusinessExtDbContext _businessExtDbContext;
  42. public ExtJobService(
  43. IRepository<in_kd_material, long> kd_material,
  44. IRepository<ic_item, long> ic_item,
  45. IRepository<ic_item_inventory, long> ic_item_inventory,
  46. IRepository<srm_purchase, long> srm_purchase,
  47. IRepository<ic_item_stock, long> ic_item_stock,
  48. IRepository<ic_plan, long> ic_plan,
  49. IRepository<ic_check, long> ic_check,
  50. IRepository<ic_factory_details, long> ic_factory_details,
  51. IRepository<in_kd_bom, long> kd_bom,
  52. IRepository<ic_bom, long> ic_bom,
  53. IRepository<ic_bom_child, long> ic_bom_child,
  54. IRepository<ic_substitute, long> ic_substitute,
  55. IRepository<ic_substitute_all, long> ic_substitute_all,
  56. IRepository<ic_substitute_all_dtl, long> ic_substitute_all_dtl,
  57. IRepository<ext_xref, long> ext_xref,
  58. BusinessDbContext businessDbContext,
  59. BusinessExtDbContext businessExtDbContext
  60. )
  61. {
  62. _kd_material= kd_material;
  63. _ic_item= ic_item;
  64. _ic_item_inventory = ic_item_inventory;
  65. _srm_purchase = srm_purchase;
  66. _ic_item_stock = ic_item_stock;
  67. _ic_plan= ic_plan;
  68. _ic_check = ic_check;
  69. _ic_factory_details= ic_factory_details;
  70. _kd_bom = kd_bom;
  71. _ic_bom= ic_bom;
  72. _ic_bom_child= ic_bom_child;
  73. _ic_substitute= ic_substitute;
  74. _ic_substitute_all= ic_substitute_all;
  75. _ic_substitute_all_dtl= ic_substitute_all_dtl;
  76. _ext_xref= ext_xref;
  77. _businessDbContext = businessDbContext;
  78. _businessExtDbContext = businessExtDbContext;
  79. }
  80. /// <summary>
  81. /// 处理物料
  82. /// </summary>
  83. public async void DoExt()
  84. {
  85. await DoMaterial();
  86. //DoBom();
  87. }
  88. /// <summary>
  89. /// 处理物料
  90. /// </summary>
  91. public async Task DoMaterial()
  92. {
  93. var extMaterials=_kd_material.GetListAsync();
  94. var ic_items = _ic_item.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101);
  95. var ic_factory_detailss = _ic_factory_details.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101);
  96. var srm_purchases = _srm_purchase.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101);
  97. var ic_plans = _ic_plan.GetListAsync(p => p.factory_id == 10101 && p.org_id == 101 && p.tenant_id == 101);
  98. List<ic_item> ic_itemListAdd = new List<ic_item>();
  99. List<ic_item> ic_itemListUpdate = new List<ic_item>();
  100. List<ic_factory_details> ic_factory_detailsListAdd = new List<ic_factory_details>();
  101. List<ic_factory_details> ic_factory_detailsListUpdate = new List<ic_factory_details>();
  102. List<srm_purchase> srm_purchaseListAdd = new List<srm_purchase>();
  103. List<srm_purchase> srm_purchaseListUpdate = new List<srm_purchase>();
  104. List<ic_plan> ic_planListAdd = new List<ic_plan>();
  105. List<ic_plan> ic_planListUpdate = new List<ic_plan>();
  106. List<ext_xref> ext_xrefList = new List<ext_xref>();
  107. if (extMaterials != null)
  108. {
  109. extMaterials.Result.ForEach(ext =>
  110. {
  111. if (ic_items != null && ic_items.Result.Exists(a => a.number == ext.FNumber))
  112. {
  113. var ic_item = ic_items.Result.FirstOrDefault(a => a.number == ext.FNumber);
  114. if (ic_item != null)
  115. {
  116. ic_item.number = ext.FNumber;
  117. ic_item.name = ext.FName_2052;
  118. ic_item.model = ext.FSpecification_2052;
  119. ic_item.full_name = ext.FDescription_2052;
  120. ic_item.fms_number = ext.FOldNumber;
  121. //0.配置类 1. 2.委外加工 3.外购 4.虚拟件
  122. 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;
  123. ic_item.source = ext.FDataSource;
  124. ic_item.iskeyitem = ext.FIsKitting == "是" ? 1 : 0;
  125. ic_item.gross_weight = ext.FGROSSWEIGHT;
  126. ic_item.net_weight = ext.FNETWEIGHT;
  127. ic_item.maund = ext.FWEIGHTUNITID_Name;
  128. ic_item.cubic_measure = ext.FVOLUMEUNITID_Name;
  129. ic_item.length = ext.FLENGTH;
  130. ic_item.width = ext.FWIDTH;
  131. ic_item.height = ext.FHEIGHT;
  132. ic_item.size = ext.FVOLUME;
  133. ic_item.allowpur = ext.FIsPurchase == "是" ? 1 : 0;
  134. ic_item.allowsale = ext.FIsSale == "是" ? 1 : 0;
  135. ic_item.allowmanu = ext.FIsProduce == "是" ? 1 : 0;
  136. ic_item.allowout = ext.FIsSubContract == "是" ? 1 : 0;
  137. ic_item.allowbatch = ext.F_SFJLPCH == "是" ? 1 : 0;
  138. ic_itemListUpdate.Add(ic_item);
  139. ext_xrefList.Add(new ext_xref
  140. {
  141. dop_table_name = "ic_item",
  142. dop_id = ic_item.Id,
  143. dop_col1_name = "number",
  144. dop_col1_value = ic_item.number,
  145. other_sys_name = "kd",
  146. other_table_name = "in_kd_material",
  147. other_id = ext.Id,
  148. other_col1_name = "FNumber",
  149. other_col1_value = ext.FNumber
  150. });
  151. var ic_factory_detail = ic_factory_detailss.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id);
  152. if (ic_factory_detail != null)
  153. {
  154. ic_factory_detail.product_line = ext.F_CPX_Name;
  155. ic_factory_detail.minpackqty = ext.FIncreaseQty;
  156. ic_factory_detailsListUpdate.Add(ic_factory_detail);
  157. }
  158. var srm_purchase = srm_purchases.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id);
  159. if (srm_purchases != null)
  160. {
  161. srm_purchase.icitem_name = ic_item.name;
  162. srm_purchase.purchgroup = ext.FPurchaseGroupId_Name;
  163. srm_purchase.purcher = ext.FPurchaserId_Name;
  164. srm_purchase.purchase_unit = ext.FPurchaseUnitId_Name;
  165. srm_purchaseListUpdate.Add(srm_purchase);
  166. }
  167. var ic_plan = ic_plans.Result.FirstOrDefault(a => a.icitem_id == ic_item.Id);
  168. if (ic_plan != null)
  169. {
  170. ic_plan.icitem_name = ic_item.name;
  171. ic_plan.planner_name = ext.FPurchaseGroupId_Name;
  172. ic_plan.fix_leadtime = ext.FFixLeadTime;
  173. ic_plan.plan_trategy = ext.FPlanningStrategy == "MRP" ? 1 : 0;
  174. ic_plan.order_trategy = ext.FOrderPolicy == "LFL(批对批)" ? 1 : 0;
  175. ic_plan.order_inter_val = ext.FOrderIntervalTime;
  176. ic_plan.lead_time = ext.FVarLeadTime;
  177. ic_plan.bat_change_economy = ext.FVarLeadTimeLotSize;
  178. ic_plan.order_point = ext.FReOrderGood;
  179. ic_plan.secinv = ext.FPlanSafeStockQty;
  180. ic_planListUpdate.Add(ic_plan);
  181. }
  182. }
  183. }
  184. else
  185. {
  186. ic_item item = new ic_item(help.NextId());
  187. item.number = ext.FNumber;
  188. item.name = ext.FName_2052;
  189. item.model = ext.FSpecification_2052;
  190. item.full_name = ext.FDescription_2052;
  191. item.fms_number = ext.FOldNumber;
  192. //0.配置类 1. 2.委外加工 3.外购 4.虚拟件
  193. 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;
  194. item.source = ext.FDataSource;
  195. item.iskeyitem = ext.FIsKitting == "是" ? 1 : 0;
  196. item.gross_weight = ext.FGROSSWEIGHT;
  197. item.net_weight = ext.FNETWEIGHT;
  198. item.maund = ext.FWEIGHTUNITID_Name;
  199. item.cubic_measure = ext.FVOLUMEUNITID_Name;
  200. item.length = ext.FLENGTH;
  201. item.width = ext.FWIDTH;
  202. item.height = ext.FHEIGHT;
  203. item.size = ext.FVOLUME;
  204. item.allowpur = ext.FIsPurchase == "是" ? 1 : 0;
  205. item.allowsale = ext.FIsSale == "是" ? 1 : 0;
  206. item.allowmanu = ext.FIsProduce == "是" ? 1 : 0;
  207. item.allowout = ext.FIsSubContract == "是" ? 1 : 0;
  208. item.allowbatch = ext.F_SFJLPCH == "是" ? 1 : 0;
  209. item.tenant_id = 101;//TODO
  210. item.factory_id = 10101;//TODO
  211. item.org_id = 101;//TODO
  212. item.IsDeleted = false;//TODO
  213. item.is_equipment = 0;
  214. item.item_level = 0;
  215. item.picktype = 0;
  216. item.enable_warning = 0;
  217. ic_itemListAdd.Add(item);
  218. ext_xrefList.Add(new ext_xref
  219. {
  220. dop_table_name = "ic_item",
  221. dop_id = item.Id,
  222. dop_col1_name = "number",
  223. dop_col1_value = item.number,
  224. other_sys_name = "kd",
  225. other_table_name = "in_kd_material",
  226. other_id = ext.Id,
  227. other_col1_name = "FNumber",
  228. other_col1_value = ext.FNumber
  229. });
  230. ic_factory_detailsListAdd.Add(new ic_factory_details(help.NextId())
  231. {
  232. icitem_id = item.Id,
  233. icitem_name = ext.FName_2052,
  234. tenant_id = 101,//TODO
  235. factory_id = 10101,////TODO
  236. org_id = 101,//TODO
  237. IsDeleted = false,//TODO
  238. pick_type = 0,
  239. stock_time = 0,
  240. batch_manager = 0,
  241. days_per = 0,
  242. default_chkloc = 0,
  243. default_chksp = 0,
  244. default_loc = 0,
  245. default_sp = 0,
  246. product_line = ext.F_CPX_Name,
  247. production_leadtime = 0,
  248. stock_leadtime = 0,
  249. transportation_leadtime = 0,
  250. order_leadtime = 0,
  251. minpackqty = ext.FIncreaseQty,
  252. minorderqty = 0
  253. });
  254. srm_purchaseListAdd.Add(new srm_purchase(help.NextId())
  255. {
  256. icitem_id = item.Id,
  257. icitem_name = item.name,
  258. currency_type = ext.FCurrencyId_Name == "人民币" ? 1 : 0,//TODO
  259. tenant_id = 101,//TODO
  260. factory_id = 10101,////TODO
  261. org_id = 101,//TODO
  262. IsDeleted = false,//TODO
  263. supplier_id = 0,
  264. purchgroup = ext.FPurchaseGroupId_Name,
  265. purcher = ext.FPurchaserId_Name,
  266. purchase_unit = ext.FPurchaseUnitId_Name
  267. });
  268. ic_planListAdd.Add(new ic_plan(help.NextId())
  269. {
  270. icitem_id = item.Id,
  271. icitem_name = item.name,
  272. planner_name = ext.FPurchaseGroupId_Name,
  273. fix_leadtime = ext.FFixLeadTime,
  274. plan_trategy = ext.FPlanningStrategy == "MRP" ? 1 : 0,
  275. order_trategy = ext.FOrderPolicy == "LFL(批对批)" ? 1 : 0,
  276. order_inter_val = ext.FOrderIntervalTime,
  277. lead_time = ext.FVarLeadTime,
  278. bat_change_economy = ext.FVarLeadTimeLotSize,
  279. order_point = ext.FReOrderGood,
  280. secinv = ext.FPlanSafeStockQty,
  281. tenant_id = 101,//TODO
  282. factory_id = 10101,////TODO
  283. org_id = 101,//TODO
  284. IsDeleted = false,//TODO
  285. });
  286. }
  287. });
  288. if (ic_itemListAdd.Count > 0)
  289. {
  290. //await _ic_item.InsertManyAsync(ic_itemListAdd);
  291. await _businessDbContext.ic_item.BulkInsertAsync(ic_itemListAdd, options =>
  292. {
  293. options.InsertKeepIdentity = true;
  294. options.InsertIfNotExists = true;
  295. options.AutoMapOutputDirection = false;
  296. });
  297. }
  298. if (ic_itemListUpdate.Count > 0)
  299. {
  300. //await _ic_item.UpdateManyAsync(ic_itemListUpdate);
  301. await _businessDbContext.ic_item.BulkUpdateAsync(ic_itemListUpdate, options =>
  302. {
  303. options.InsertKeepIdentity = true;
  304. options.AutoMapOutputDirection = false;
  305. options.ColumnPrimaryKeyExpression = c => c.Id;
  306. });
  307. }
  308. if (ic_factory_detailsListAdd.Count > 0)
  309. {
  310. //await _ic_factory_details.InsertManyAsync(ic_factory_detailsListAdd);
  311. await _businessDbContext.ic_factory_details.BulkInsertAsync(ic_factory_detailsListAdd, options =>
  312. {
  313. options.InsertKeepIdentity = true;
  314. options.InsertIfNotExists = true;
  315. options.AutoMapOutputDirection = false;
  316. });
  317. }
  318. if (ic_factory_detailsListUpdate.Count > 0)
  319. {
  320. //await _ic_factory_details.UpdateManyAsync(ic_factory_detailsListUpdate);
  321. await _businessDbContext.ic_factory_details.BulkUpdateAsync(ic_factory_detailsListUpdate, options =>
  322. {
  323. options.InsertKeepIdentity = true;
  324. options.AutoMapOutputDirection = false;
  325. options.ColumnPrimaryKeyExpression = c => c.Id;
  326. });
  327. }
  328. if (srm_purchaseListAdd.Count > 0)
  329. {
  330. //await _ic_item_pur.InsertManyAsync(ic_item_purListAdd);
  331. await _businessDbContext.srm_purchase.BulkInsertAsync(srm_purchaseListAdd, options =>
  332. {
  333. options.InsertKeepIdentity = true;
  334. options.InsertIfNotExists = true;
  335. options.AutoMapOutputDirection = false;
  336. });
  337. }
  338. if (srm_purchaseListUpdate.Count > 0)
  339. {
  340. //await _ic_item_pur.UpdateManyAsync(ic_item_purListUpdate);
  341. await _businessDbContext.srm_purchase.BulkUpdateAsync(srm_purchaseListUpdate, options =>
  342. {
  343. options.InsertKeepIdentity = true;
  344. options.AutoMapOutputDirection = false;
  345. options.ColumnPrimaryKeyExpression = c => c.Id;
  346. });
  347. }
  348. if (ic_planListAdd.Count > 0)
  349. {
  350. //await _ic_plan.InsertManyAsync(ic_planListAdd);
  351. await _businessDbContext.ic_plan.BulkInsertAsync(ic_planListAdd, options =>
  352. {
  353. options.InsertKeepIdentity = true;
  354. options.InsertIfNotExists = true;
  355. options.AutoMapOutputDirection = false;
  356. });
  357. }
  358. if (ic_planListUpdate.Count > 0)
  359. {
  360. //await _ic_plan.UpdateManyAsync(ic_planListUpdate);
  361. await _businessDbContext.ic_plan.BulkUpdateAsync(ic_planListUpdate, options =>
  362. {
  363. options.InsertKeepIdentity = true;
  364. options.AutoMapOutputDirection = false;
  365. options.ColumnPrimaryKeyExpression = c => c.Id;
  366. });
  367. }
  368. if (ext_xrefList.Count > 0)
  369. {
  370. //await _ext_xref.UpdateManyAsync(ext_xrefList);
  371. await _businessExtDbContext.ext_xref.BulkInsertAsync(ext_xrefList, options =>
  372. {
  373. options.InsertKeepIdentity = true;
  374. options.InsertIfNotExists = true;
  375. options.AutoMapOutputDirection = false;
  376. });
  377. }
  378. }
  379. }
  380. /// <summary>
  381. /// 处理物料清单
  382. /// </summary>
  383. public void DoBom()
  384. {
  385. LogManager.Configuration.Install(new NLog.Config.InstallationContext());
  386. }
  387. }
  388. }