SyncWMSDataAppService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using Business.Core.Utilities;
  2. using Business.EntityFrameworkCore;
  3. using Business.EntityFrameworkCore.SqlRepositories;
  4. using Business.Model.MES.IC;
  5. using Business.Model.Sale;
  6. using Business.Model.SRM;
  7. using EFCore.BulkExtensions;
  8. using Microsoft.EntityFrameworkCore;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel.DataAnnotations;
  13. using System.Linq;
  14. using Volo.Abp.Application.Services;
  15. using Volo.Abp.Domain.Repositories;
  16. namespace Business.SyncDataManagement
  17. {
  18. /// <summary>
  19. /// 同步mysql数据到mongodb
  20. /// </summary>
  21. public class SyncWMSDataAppService : ApplicationService
  22. {
  23. #region 服务
  24. /// <summary>
  25. /// 物料bom
  26. /// </summary>
  27. private IRepository<ic_bom, long> _mysql_ic_bom;
  28. /// <summary>
  29. /// 物料bom子表
  30. /// </summary>
  31. private IRepository<ic_bom_child, long> _mysql_ic_bom_child;
  32. /// <summary>
  33. /// 物料
  34. /// </summary>
  35. private IRepository<ic_item, long> _mysql_ic_item;
  36. /// <summary>
  37. /// 供应商
  38. /// </summary>
  39. private IRepository<srm_supplier, long> _mysql_srm_supplier;
  40. /// <summary>
  41. /// 物料采购计划表
  42. /// </summary>
  43. private IRepository<ic_plan, long> _mysql_ic_plan;
  44. /// <summary>
  45. /// 物料工厂明细表
  46. /// </summary>
  47. private IRepository<ic_factory_details, long> _mysql_ic_factory_details;
  48. /// <summary>
  49. /// 物料采购报价单
  50. /// </summary>
  51. private IRepository<srm_purchase, long> _mysql_srm_purchase;
  52. /// <summary>
  53. /// 物料采购报价单
  54. /// </summary>
  55. private IRepository<crm_customer, long> _mysql_crm_customer;
  56. /// <summary>
  57. /// 销售订单
  58. /// </summary>
  59. private IRepository<crm_seorder, long> _mysql_crm_seorder;
  60. /// <summary>
  61. /// 销售订单明细
  62. /// </summary>
  63. private IRepository<crm_seorderentry, long> _mysql_crm_seorderentry;
  64. /// <summary>
  65. /// 雪花算法
  66. /// </summary>
  67. SnowFlake help = new SnowFlake();
  68. private readonly BusinessDbContext _businessDbContext;
  69. private readonly ISqlRepository<CustMaster> _custMaster;
  70. private readonly ISqlRepository<SuppMaster> _suppMaster;
  71. private readonly ISqlRepository<ConsigneeAddressMaster> _consigneeAddressMaster;
  72. private readonly ISqlRepository<ItemMaster> _itemMaster;
  73. private readonly ISqlRepository<ProductStructureMaster> _productStructureMaster;
  74. #endregion
  75. /// <summary>
  76. /// 构造函数
  77. /// </summary>
  78. public SyncWMSDataAppService(
  79. IRepository<ic_bom, long> mysql_ic_bom,
  80. IRepository<ic_bom_child, long> mysql_ic_bom_child,
  81. IRepository<ic_check, long> mysql_ic_check,
  82. IRepository<ic_item, long> mysql_ic_item,
  83. IRepository<srm_supplier, long> mysql_srm_supplier,
  84. IRepository<ic_factory_details, long> mysql_ic_factory_details,
  85. IRepository<srm_purchase, long> mysql_srm_purchase,
  86. IRepository<ic_plan, long> mysql_ic_plan,
  87. IRepository<crm_customer, long> mysql_crm_customer,
  88. IRepository<crm_seorder, long> mysql_crm_seorder,
  89. IRepository<crm_seorderentry, long> mysql_crm_seorderentry,
  90. ISqlRepository<CustMaster> custMaster,
  91. ISqlRepository<SuppMaster> suppMaster,
  92. ISqlRepository<ConsigneeAddressMaster> consigneeAddressMaster,
  93. ISqlRepository<ItemMaster> itemMaster,
  94. ISqlRepository<ProductStructureMaster> productStructureMaster,
  95. BusinessDbContext businessDbContext
  96. )
  97. {
  98. _mysql_ic_bom = mysql_ic_bom;
  99. _mysql_ic_bom_child = mysql_ic_bom_child;
  100. _mysql_ic_item = mysql_ic_item;
  101. _mysql_srm_supplier = mysql_srm_supplier;
  102. _mysql_ic_factory_details = mysql_ic_factory_details;
  103. _mysql_srm_purchase = mysql_srm_purchase;
  104. _mysql_ic_plan = mysql_ic_plan;
  105. _mysql_crm_customer = mysql_crm_customer;
  106. _mysql_crm_seorder = mysql_crm_seorder;
  107. _mysql_crm_seorderentry = mysql_crm_seorderentry;
  108. _custMaster = custMaster;
  109. _suppMaster = suppMaster;
  110. _consigneeAddressMaster = consigneeAddressMaster;
  111. _itemMaster = itemMaster;
  112. _productStructureMaster = productStructureMaster;
  113. _businessDbContext = businessDbContext;
  114. }
  115. /// <summary>
  116. /// 同步WMS基础数据到MySQL
  117. /// </summary>
  118. /// <returns></returns>
  119. /// <exception cref="NotImplementedException"></exception>
  120. public void SyncWMSDataToMySQL()
  121. {
  122. //SyncCustMaster();
  123. //SyncSuppMaster();
  124. //SyncSalesOrdMaster();
  125. //SyncSalesOrdMasterEntry();
  126. //SyncItemMaster();
  127. SyncBom();
  128. }
  129. //同步客户
  130. public void SyncCustMaster()
  131. {
  132. var customerList = _mysql_crm_customer.GetListAsync().Result;
  133. List<CustMaster> custList = _custMaster.Select(a => a.Domain == "1001" && a.IsActive);
  134. var addressList = _consigneeAddressMaster.Select(a => a.Domain == "1001" && a.Typed == "Cust" && a.IsActive);
  135. List<crm_customer> ItemsAdd = new List<crm_customer>();
  136. if (custList != null && custList.Count > 0)
  137. {
  138. for (int i = 0; i < custList.Count; i++)
  139. {
  140. long domain = Convert.ToInt64(custList[i].Domain);
  141. long factory_id = domain % 1000;
  142. long tenant_id = domain - factory_id;
  143. var cust = customerList.Find(x => x.customer_no == custList[i].Cust && x.tenant_id == 1000 && x.factory_id == 1001);
  144. if (cust is null)
  145. {
  146. ConsigneeAddressMaster address = addressList.Find(a => a.Address == custList[i].Cust);
  147. var customer = new crm_customer
  148. {
  149. customer_no = custList[i].Cust,
  150. customer_name = address is null ? address.Name : "",
  151. telephone = address is null ? address.Telephone : "",
  152. email = address is null ? address.AttentionEmail : "",
  153. contact = address is null ? address.Attention1 : "",
  154. post_code = address is null ? address.Post : "",
  155. country = address is null ? address.Country : "",
  156. province = address is null ? address.State : "",
  157. city = address is null ? address.City : "",
  158. region = address is null ? address.State : "",
  159. address = address is null ? address.Address1 : "",
  160. short_name = custList[i].SortName,
  161. carrying_aos = Convert.ToInt32(custList[i].CustShippingLT),
  162. currency = custList[i].Curr,
  163. employee_name = custList[i].Salesperson1,
  164. factory_id = 1001,
  165. tenant_id = 1000
  166. };
  167. customer.GenerateNewId();
  168. ItemsAdd.Add(customer);
  169. }
  170. }
  171. _businessDbContext.BulkInsert(ItemsAdd);
  172. }
  173. }
  174. //同步供应商
  175. public void SyncSuppMaster()
  176. {
  177. var suppList = _mysql_srm_supplier.GetListAsync().Result;
  178. List<SuppMaster> custList = _suppMaster.Select(a => a.Domain == "1001" && a.IsActive);
  179. var addressList = _consigneeAddressMaster.Select(a => a.Domain == "1001" && a.Typed == "Supp" && a.IsActive);
  180. List<srm_supplier> ItemsAdd = new List<srm_supplier>();
  181. if (custList != null && custList.Count > 0)
  182. {
  183. for (int i = 0; i < custList.Count; i++)
  184. {
  185. long domain = Convert.ToInt64(custList[i].Domain.ToString());
  186. long factory_id = domain % 1000;
  187. long tenant_id = domain - factory_id;
  188. var supp = suppList.Find(x => x.supplier_no == custList[i].Supp.ToString() && x.tenant_id == factory_id && x.factory_id == tenant_id);
  189. if (supp is null)
  190. {
  191. ConsigneeAddressMaster address = addressList.Find(a => a.Address == custList[i].Supp);
  192. var supplier = new srm_supplier
  193. {
  194. supplier_no = custList[i].Supp,
  195. supplier_name = address is null ? address.Name : "",
  196. supplier_short_name = custList[i].SortName,
  197. country = address is null ? address.Country : "",
  198. state = address is null ? address.State : "",
  199. region = address is null ? address.City : "",
  200. contact = address is null ? address.Attention1 : "",
  201. telephone = address is null ? address.Telephone : "",
  202. post_code = address is null ? address.Post : "",
  203. email = address is null ? address.AttentionEmail : "",
  204. currency = custList[i].Curr,
  205. supply_type = custList[i].Type,
  206. settlement = custList[i].CrTerms,
  207. factory_id = factory_id,
  208. tenant_id = tenant_id
  209. };
  210. supplier.GenerateNewId();
  211. ItemsAdd.Add(supplier);
  212. }
  213. }
  214. _businessDbContext.BulkInsert(ItemsAdd);
  215. }
  216. }
  217. ////同步销售订单
  218. //public void SyncSalesOrdMaster()
  219. //{
  220. // var customerList = _mysql_crm_customer.GetListAsync().Result;
  221. // var saleOrderList = _mysql_crm_seorder.GetListAsync().Result;
  222. // var ItemMasterDS = _repository.SelectDataBaseBySql("SELECT A.SalesOrd,C.SortName AS CustomName,A.SoldTo AS Cust,A.OrdDate,A.RequiredDate,A.Sequence,A.Curr,A.ExchRate,A.Salesperson1,A.Project,B.Descr AS ProjectName,A.[Domain] from SalesOrdMaster A INNER JOIN CustMaster C ON A.SoldTo=C.Cust and A.[Domain]=C.[Domain]\r\nLEFT JOIN ProjectMaster B On A.Project=B.Project AND A.[Domain]=B.[Domain] ", "SalesOrdMaster");
  223. // List<crm_seorder> ItemsAdd = new List<crm_seorder>();
  224. // List<crm_seorder> ItemsUpdate = new List<crm_seorder>();
  225. // if (ItemMasterDS != null && ItemMasterDS.Tables.Count > 0 && ItemMasterDS.Tables[0].Rows.Count > 0)
  226. // {
  227. // for (int i = 0; i < ItemMasterDS.Tables[0].Rows.Count; i++)
  228. // {
  229. // long domain = Convert.ToInt64(ItemMasterDS.Tables[0].Rows[i]["Domain"].ToString());
  230. // long factory_id = domain % 1000;
  231. // long tenant_id = domain - factory_id;
  232. // var custno = ItemMasterDS.Tables[0].Rows[i]["Cust"].ToString();
  233. // var cust = customerList.Find(a => a.customer_no == custno && a.factory_id == factory_id && a.tenant_id == tenant_id);
  234. // long? custom_id = cust is null ? cust.Id : null;
  235. // var saleorder = saleOrderList.Find(x => x.bill_no == ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString() && x.tenant_id == factory_id && x.factory_id == tenant_id);
  236. // if (saleorder is null)
  237. // {
  238. // var order = new crm_seorder
  239. // {
  240. // bill_no = ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString(),
  241. // custom_id = custom_id,
  242. // custom_name = ItemMasterDS.Tables[0].Rows[i]["CustomName"].ToString(),
  243. // custom_no = ItemMasterDS.Tables[0].Rows[i]["Cust"].ToString(),
  244. // //custom_level = ItemMasterDS.Tables[0].Rows[i]["PurMfg"].ToString() == "P" ? "外购" : "自制",
  245. // date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  246. // rdate = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["RequiredDate"],
  247. // //urgent = "",
  248. // currency = ItemMasterDS.Tables[0].Rows[i]["Curr"].ToString() == "RMB" ? 1 : 0,
  249. // exchange_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  250. // //biller = ItemMasterDS.Tables[0].Rows[i]["NetWeightUM"].ToString(),
  251. // //emp_no = (decimal)ItemMasterDS.Tables[0].Rows[i]["Length"],
  252. // emp_name = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  253. // //auditor = (decimal)ItemMasterDS.Tables[0].Rows[i]["Height"],
  254. // //audit_date = ItemMasterDS.Tables[0].Rows[i]["PurMfg"].ToString() == "P" ? 1 : 0,
  255. // //status = 1,
  256. // //closed = "",
  257. // //op_time = 1,
  258. // bill_from = "",
  259. // //project_name = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["LotSerialControl"]) == true ? 1 : 0,
  260. // //project_code = "",
  261. // out_stock_type = 1,//TODO
  262. // //sale_dept_id = "",
  263. // //sale_dept_name = 1,
  264. // //sale_dept_code = 1,
  265. // //create_dept = 1
  266. // factory_id = factory_id,
  267. // tenant_id = tenant_id
  268. // };
  269. // order.GenerateNewId();
  270. // ItemsAdd.Add(order);
  271. // }
  272. // else
  273. // {
  274. // saleorder.bill_no = ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString();
  275. // saleorder.custom_id = custom_id;
  276. // saleorder.custom_name = ItemMasterDS.Tables[0].Rows[i]["CustomName"].ToString();
  277. // saleorder.custom_no = ItemMasterDS.Tables[0].Rows[i]["Cust"].ToString();
  278. // //custom_level = ItemMasterDS.Tables[0].Rows[i]["PurMfg"].ToString() == "P" ? "外购" : "自制",
  279. // saleorder.date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"];
  280. // saleorder.rdate = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["RequiredDate"];
  281. // saleorder.urgent = (int?)ItemMasterDS.Tables[0].Rows[i]["Sequence"];
  282. // saleorder.currency = ItemMasterDS.Tables[0].Rows[i]["Curr"].ToString() == "RMB" ? 1 : 0;
  283. // saleorder.exchange_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"];
  284. // //biller = ItemMasterDS.Tables[0].Rows[i]["NetWeightUM"].ToString(),
  285. // //emp_no = (decimal)ItemMasterDS.Tables[0].Rows[i]["Length"],
  286. // saleorder.emp_name = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString();
  287. // //auditor = (decimal)ItemMasterDS.Tables[0].Rows[i]["Height"],
  288. // //audit_date = ItemMasterDS.Tables[0].Rows[i]["PurMfg"].ToString() == "P" ? 1 : 0,
  289. // //status = 1,
  290. // //closed = "",
  291. // //op_time = 1,
  292. // saleorder.bill_from = "";
  293. // saleorder.project_name = ItemMasterDS.Tables[0].Rows[i]["ProjectName"].ToString();
  294. // saleorder.project_code = ItemMasterDS.Tables[0].Rows[i]["Project"].ToString();
  295. // saleorder.out_stock_type = 1;//TODO
  296. // //sale_dept_id = "",
  297. // //sale_dept_name = 1,
  298. // //sale_dept_code = 1,
  299. // //create_dept = 1
  300. // ItemsUpdate.Add(saleorder);
  301. // }
  302. // }
  303. // //_businessDbContext.crm_seorder.Delete();
  304. // _businessDbContext.crm_seorder.BulkInsert(ItemsAdd);
  305. // _businessDbContext.crm_seorder.BulkUpdate(ItemsUpdate);
  306. // }
  307. //}
  308. ////同步销售订单明细
  309. //public void SyncSalesOrdMasterEntry()
  310. //{
  311. // var saleOrderEntryList = _mysql_crm_seorderentry.GetListAsync().Result;
  312. // var saleOrderList = _mysql_crm_seorder.GetListAsync().Result;
  313. // var ItemMasterDS = _repository.SelectDataBaseBySql("SELECT A.SalesOrd,A.Line,C.Descr,C.Descr1,A.ItemNum,A.UM,A.QtyOrded,A.Price,A.Disc,A.RequiredDate,A.Remark,B.CustPO,A.CustItem,A.Status,A.QtyShipped,A.QtyReturned,B.Contract,A.Domain from SalesOrdDetail A INNER JOIN SalesOrdMaster B On A.SalesOrd=B.SalesOrd and A.[Domain]=B.[Domain] INNER JOIN ItemMaster C on A.ItemNum=C.ItemNum and A.[Domain]=C.[Domain]", "SalesOrdDetail");
  314. // List<crm_seorderentry> ItemsAdd = new List<crm_seorderentry>();
  315. // List<crm_seorderentry> ItemsUpdate = new List<crm_seorderentry>();
  316. // if (ItemMasterDS != null && ItemMasterDS.Tables.Count > 0 && ItemMasterDS.Tables[0].Rows.Count > 0)
  317. // {
  318. // for (int i = 0; i < ItemMasterDS.Tables[0].Rows.Count; i++)
  319. // {
  320. // long domain = Convert.ToInt64(ItemMasterDS.Tables[0].Rows[i]["Domain"].ToString());
  321. // long factory_id = domain % 1000;
  322. // long tenant_id = domain - factory_id;
  323. // var bill_no = ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString();
  324. // var seorder = saleOrderList.Find(a => a.bill_no == bill_no && a.factory_id == factory_id && a.tenant_id == tenant_id);
  325. // long? seorder_id = seorder is null ? seorder.Id : null;
  326. // var saleorderentry = saleOrderEntryList.Find(x => x.bill_no == bill_no && x.tenant_id == factory_id && x.factory_id == tenant_id);
  327. // if (saleorderentry is null)
  328. // {
  329. // var entry = new crm_seorderentry
  330. // {
  331. // seorder_id = seorder_id,
  332. // bill_no = ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString(),
  333. // entry_seq = (int?)ItemMasterDS.Tables[0].Rows[i]["Line"],
  334. // item_number = ItemMasterDS.Tables[0].Rows[i]["ItemNum"].ToString(),
  335. // item_name = ItemMasterDS.Tables[0].Rows[i]["Descr"].ToString(),
  336. // specification = ItemMasterDS.Tables[0].Rows[i]["Descr1"].ToString(),
  337. // //urgent = (int?)ItemMasterDS.Tables[0].Rows[i]["PLPriority"],
  338. // bom_number = ItemMasterDS.Tables[0].Rows[i]["ItemNum"].ToString(),
  339. // unit = ItemMasterDS.Tables[0].Rows[i]["UM"].ToString(),
  340. // qty = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyOrded"],
  341. // //price = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Price"],
  342. // //tax_price = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  343. // //tax_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  344. // amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Price"],
  345. // //discount_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  346. // discount_amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Disc"],
  347. // //total_amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  348. // plan_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["RequiredDate"],
  349. // //date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  350. // //planner_name = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  351. // //planner_no = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  352. // remark = ItemMasterDS.Tables[0].Rows[i]["Remark"].ToString(),
  353. // //soure_bill_no = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  354. // custom_order_bill_no = ItemMasterDS.Tables[0].Rows[i]["CustPO"].ToString(),
  355. // //custom_order_entryid = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  356. // //sys_capacity_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  357. // //adjust_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  358. // //mrp_closed = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  359. // custom_order_itemno = ItemMasterDS.Tables[0].Rows[i]["CustItem"].ToString(),
  360. // state = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["Status"]),//TODO
  361. // deliver_count = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyShipped"],
  362. // returned_count = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyReturned"],
  363. // //se_reject_reason = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  364. // //out_stock_type = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  365. // //is_checked = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  366. // //sys_material_date = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  367. // contract_no = ItemMasterDS.Tables[0].Rows[i]["Contract"].ToString(),
  368. // //create_dept = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"])
  369. // factory_id = factory_id,
  370. // tenant_id = tenant_id
  371. // };
  372. // entry.GenerateNewId();
  373. // ItemsAdd.Add(entry);
  374. // }
  375. // else
  376. // {
  377. // saleorderentry.seorder_id = seorder_id;
  378. // saleorderentry.bill_no = ItemMasterDS.Tables[0].Rows[i]["SalesOrd"].ToString();
  379. // saleorderentry.entry_seq = (int?)ItemMasterDS.Tables[0].Rows[i]["Line"];
  380. // saleorderentry.item_number = ItemMasterDS.Tables[0].Rows[i]["ItemNum"].ToString();
  381. // saleorderentry.item_name = ItemMasterDS.Tables[0].Rows[i]["Descr"].ToString();
  382. // saleorderentry.specification = ItemMasterDS.Tables[0].Rows[i]["Descr1"].ToString();
  383. // //urgent = (int?)ItemMasterDS.Tables[0].Rows[i]["PLPriority"],
  384. // saleorderentry.bom_number = ItemMasterDS.Tables[0].Rows[i]["ItemNum"].ToString();
  385. // saleorderentry.unit = ItemMasterDS.Tables[0].Rows[i]["UM"].ToString();
  386. // saleorderentry.qty = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyOrded"];
  387. // //price = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Price"],
  388. // //tax_price = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  389. // //tax_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  390. // saleorderentry.amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Price"];
  391. // //discount_rate = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  392. // saleorderentry.discount_amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["Disc"];
  393. // //total_amount = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  394. // saleorderentry.plan_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["RequiredDate"];
  395. // //date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  396. // //planner_name = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  397. // //planner_no = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  398. // saleorderentry.remark = ItemMasterDS.Tables[0].Rows[i]["Remark"].ToString();
  399. // //soure_bill_no = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  400. // saleorderentry.custom_order_bill_no = ItemMasterDS.Tables[0].Rows[i]["CustPO"].ToString();
  401. // //custom_order_entryid = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  402. // //sys_capacity_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  403. // //adjust_date = (DateTime?)ItemMasterDS.Tables[0].Rows[i]["OrdDate"],
  404. // //mrp_closed = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  405. // saleorderentry.custom_order_itemno = ItemMasterDS.Tables[0].Rows[i]["CustItem"].ToString();
  406. // saleorderentry.state = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["Status"]);//TODO
  407. // //rnumber = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  408. // //deliver_notice_count = (decimal?)ItemMasterDS.Tables[0].Rows[i]["ExchRate"],
  409. // saleorderentry.deliver_count = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyShipped"];
  410. // saleorderentry.returned_count = (decimal?)ItemMasterDS.Tables[0].Rows[i]["QtyReturned"];
  411. // //se_reject_reason = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  412. // //out_stock_type = ItemMasterDS.Tables[0].Rows[i]["Salesperson1"].ToString(),
  413. // //is_checked = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  414. // //sys_material_date = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"]),
  415. // saleorderentry.contract_no = ItemMasterDS.Tables[0].Rows[i]["Contract"].ToString();
  416. // //create_dept = Convert.ToBoolean(ItemMasterDS.Tables[0].Rows[i]["OrdDate"])
  417. // ItemsUpdate.Add(saleorderentry);
  418. // }
  419. // }
  420. // //_businessDbContext.crm_seorder.Delete();
  421. // _businessDbContext.crm_seorderentry.BulkInsert(ItemsAdd);
  422. // _businessDbContext.crm_seorderentry.BulkUpdate(ItemsUpdate);
  423. // }
  424. //}
  425. //同步物料
  426. public void SyncItemMaster()
  427. {
  428. var ic_itemList = _mysql_ic_item.GetListAsync(a=>a.tenant_id==1000 && a.factory_id==1001).Result;
  429. var custList = _itemMaster.Select(a => a.IsActive);
  430. List<ic_item> ItemsAdd = new List<ic_item>();
  431. if (custList != null && custList.Count > 0)
  432. {
  433. for (int i = 0; i < custList.Count; i++)
  434. {
  435. var ic_item = ic_itemList.Find(x => x.number == custList[i].ItemNum && x.tenant_id == 1000 && x.factory_id == 1001);
  436. if (ic_item == null)
  437. {
  438. ItemsAdd.Add(new ic_item(help.NextId())
  439. {
  440. number = custList[i].ItemNum, //物料编码
  441. name = custList[i].Descr, //物料名称
  442. model = custList[i].Descr1,//规格型号
  443. fversion = "",//版本号
  444. //ext.FErpClsID == "配置类" ? 0 : ext.FErpClsID == "自制" ? 1 : ext.FErpClsID == "委外" ? 2 : ext.FErpClsID == "外购" ? 3 : ext.FErpClsID == "虚拟" ? 4 : ext.FErpClsID == "费用" ? 5 : ext.FErpClsID == "服务" ? 6 : -1;
  445. //erp_cls = custList[i].PurMfg == "P" ? 3 : 1,//物料属性
  446. erp_cls = custList[i].ItemNum == "1.SD1.D.0056-F" || custList[i].ItemNum == "1.ZC1.D.0001" || custList[i].ItemNum == "1.BW1.D.0030" ? 1 : 3,
  447. //erp_cls_name = custList[i].PurMfg == "P" ? "外购" : "自制",//物料属性名称
  448. erp_cls_name = custList[i].ItemNum == "1.SD1.D.0056-F" || custList[i].ItemNum == "1.ZC1.D.0001" || custList[i].ItemNum == "1.BW1.D.0030" ? "自制" : "外购",
  449. unit = custList[i].UM,//单位
  450. item_level = 0,//物料等级
  451. source = "",//来源
  452. iskeyitem = custList[i].IsMainMas ? 1 : 0,//是否关键件
  453. net_weight = (decimal)custList[i].NetWeight,//净重
  454. maund = custList[i].NetWeightUM,//重量单位
  455. length = (decimal)custList[i].Length,//长度
  456. width = (decimal)custList[i].Width,//宽度
  457. height = (decimal)custList[i].Height,//高度
  458. allowpur = custList[i].PurMfg == "P" ? 1 : 0,//允许采购
  459. allowsale = 1,//允许销售
  460. allowmanu = custList[i].PurMfg == "L" ? 1 : 0,//允许生产
  461. allowout = 1,//允许委外
  462. allowbatch = custList[i].LotSerialControl ? 1 : 0,//批号管理
  463. allowserial = custList[i].LotSerialControl ? 1 : 0,//序列号管理
  464. photo = "",//图片
  465. enable_warning = 1,//启用预警
  466. factory_id = 1001,
  467. tenant_id = 1000,
  468. IsDeleted = false
  469. });
  470. }
  471. }
  472. }
  473. _businessDbContext.BulkInsert(ItemsAdd);
  474. }
  475. public void SyncBom()
  476. {
  477. var ic_bomList = _mysql_ic_bom.GetListAsync(a=>a.tenant_id==1000 && a.factory_id==1001).Result;
  478. var ic_itemList = _mysql_ic_item.GetListAsync(a => a.tenant_id == 1000 && a.factory_id == 1001).Result;
  479. List<ProductStructureMaster> wmsBomList =_productStructureMaster.Select(a=>a.Domain=="1001" && (a.ParentItem== "1.SD1.D.0056-F" || a.ParentItem == "1.ZC1.D.0001" || a.ParentItem == "1.BW1.D.0030") && a.IsActive);
  480. List<ic_bom> ItemsAdd = new List<ic_bom>();
  481. List<ic_bom_child> childItemsAdd = new List<ic_bom_child>();
  482. if (wmsBomList != null && wmsBomList.Count > 0)
  483. {
  484. var ItemMasterDS = wmsBomList.Select(a => a.ParentItem).Distinct().ToList();
  485. foreach(var c in ItemMasterDS)
  486. {
  487. var item = ic_itemList.Find(a => a.number == c);
  488. var ItemList = wmsBomList.Where(a=>a.ParentItem ==c);
  489. long domain = Convert.ToInt64(ItemList.First().Domain);
  490. long factory_id = domain % 1000;
  491. long tenant_id = domain - factory_id;
  492. var bom = new ic_bom(help.NextId())
  493. {
  494. bom_number = c,
  495. icitem_id = item==null ? help.NextId() : item.Id,
  496. item_name= item==null? "" : item.name,
  497. item_number =c,
  498. version= ItemList.First().Refs,
  499. factory_id = 1001,
  500. tenant_id = 1000,
  501. IsDeleted=false,
  502. use_status=1
  503. };
  504. ItemsAdd.Add(bom);
  505. foreach(var child in ItemList)
  506. {
  507. var itemchild = ic_itemList.Find(a => a.number == child.ComponentItem);
  508. var bomchild = new ic_bom_child(help.NextId())
  509. {
  510. bom_id= bom.Id,
  511. bom_number=bom.bom_number,
  512. icitem_id= itemchild==null ?long.MinValue : itemchild.Id,
  513. item_number= child.ComponentItem,
  514. item_name= itemchild==null? "" : item.name,
  515. unit=child.UM,
  516. qty=child.Qty,
  517. entryid=child.SequenceNum,
  518. erp_cls= itemchild==null ? 2: itemchild.erp_cls,
  519. begin_day=child.StartEff,
  520. end_day=child.EndEff,
  521. child_num= child.SequenceNum,
  522. version=child.Refs,
  523. factory_id = 1001,
  524. tenant_id = 1000,
  525. IsDeleted=false,
  526. use_status=1
  527. };
  528. childItemsAdd.Add(bomchild);
  529. }
  530. }
  531. _businessDbContext.BulkInsert(ItemsAdd);
  532. _businessDbContext.BulkInsert(childItemsAdd);
  533. }
  534. }
  535. }
  536. }