ExtJobService.cs 23 KB

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