ExtJobService.cs 20 KB

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