GetStockBillFromU8.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package service;
  2. import beans.stockbill.Stockbill;
  3. import common.ServiceController;
  4. import common.YosException;
  5. import common.data.*;
  6. import common.data.db.DBConnect;
  7. import java.util.ArrayList;
  8. /**
  9. * 美大U8erp出入库单同步
  10. */
  11. public class GetStockBillFromU8 extends ServiceController {
  12. private DBConnect YUNl_DB = new DBConnect("U8-YUNl_DB");
  13. @Override
  14. public void serviceRun() throws Exception {
  15. getProdIN();
  16. }
  17. @Override
  18. public ServiceParam paramSet() {
  19. return new ServiceParam("获取U8入库单(产品入库)", 1, RunType.minute);
  20. }
  21. /**
  22. * 产品入库单
  23. */
  24. public void getProdIN() throws YosException {
  25. //产品入库表头
  26. Rows u8billRows = YUNl_DB.runSqlQuery("select top(50) cvencode,ccode,ddate,cwhcode,cmemo,id,dnverifytime,chandler from rdrecord where status=0 and billtype='10' and dnverifytime is not null and accid in('666','888') and isnull(errtimes,0)<=5 order by errtimes");
  27. if (u8billRows.isNotEmpty()) {
  28. logger.info("监测到有{}个u8产品入库单", u8billRows.size());
  29. //产品入库表体
  30. Rows u8billmxAllRows = SQLFactory.createQuerySQL(YUNl_DB, "RdRecords", "autoid,cinvcode,iquantity,iunitcost,iprice,cbmemo,cbatch").setWhere("billtype", "10").setWhere("accid in('666','888')").setWhere("id", u8billRows.toArrayList("id")).query();
  31. RowsMap u8billmxRowsMap = u8billmxAllRows.toRowsMap("id");
  32. //产品入库序列号
  33. RowsMap u8billmxCodeRowsMap = SQLFactory.createQuerySQL(YUNl_DB, "ST_SNDetail", "autoid,ivouchsid,ivouchid,irowno,cinvcode,cwhcode,cposition,cinvsn,cbatch").setWhere("billtype", "10").setWhere("accid in('666','888')").setWhere("ivouchid", u8billRows.toArrayList("id")).setWhere("ivouchsid", u8billmxAllRows.toArrayList("autoid")).query().toRowsMap("ivouchid");
  34. RowsMap stockRowsMap = SQLFactory.createQuerySQL(dbConnect, "st_stock", "stockid", "stockno").query().toRowsMap("stockno");
  35. RowsMap itemRowsMap = SQLFactory.createQuerySQL(dbConnect, "plm_item", "itemid", "itemno").setWhere("itemno", u8billmxAllRows.toArrayList("cinvcode")).query().toRowsMap("itemno");
  36. RowsMap unitRowsMap = SQLFactory.createQuerySQL(dbConnect, "plm_unit", "unitid", "unitname").query().toRowsMap("unitid");
  37. RowsMap departmentRowsMap = SQLFactory.createQuerySQL(dbConnect, "sys_department", "departmentid", "depno").query().toRowsMap("depno");
  38. ArrayList<String> drpbillList = SQLFactory.createQuerySQL(dbConnect, "st_stockbill", "billno").setWhere("siteid", "MD").setWhere("status", "审核").setWhere("typemx", "生产入库u8").setWhere("billno", u8billRows.toArrayList("ccode")).query().toArrayList("billno");
  39. foreachbill:
  40. for (Row cInvCodeRow : u8billRows) {
  41. String id = cInvCodeRow.getString("id");
  42. try {
  43. String ccode = cInvCodeRow.getString("ccode");
  44. if (drpbillList.contains(ccode)) {
  45. //已存在审核状态的单据,则直接判定成功
  46. YUNl_DB.runSqlUpdate("update RdRecord set status=1 where ID =" + id + " and billtype='10'");
  47. logger.info("入库单{}已存在,将中间表状态改为1", ccode);
  48. continue;
  49. }
  50. String ddate = getDate_Str(cInvCodeRow.getDate("ddate"));
  51. if (dbConnect.runSqlQuery("select * from st_period where isclose=0 and begdate<='" + ddate + "' and enddate>='" + ddate + "'").isEmpty()) {
  52. //会计期间不存在,直接判定失败
  53. YUNl_DB.runSqlUpdate("update RdRecord set ErrTimes=10,failreason='找不到有效的会计期间' where ID =" + id + " and billtype='10' ");
  54. logger.info("入库单{}单据日期{}所在会计期间不存在", ccode, ddate);
  55. continue;
  56. }
  57. String cwhcode = cInvCodeRow.getString("cwhcode");
  58. if (stockRowsMap.get(cwhcode).isEmpty()) {
  59. //仓库不存在,失败次数+1
  60. YUNl_DB.runSqlUpdate("update RdRecord set ErrTimes=isnull(ErrTimes,0)+1,failreason='仓库不存在' where ID =" + id + " and billtype='10' ");
  61. logger.info("入库单{}仓库{}不存在", ccode, cwhcode);
  62. continue;
  63. }
  64. Rows u8billmxRows = u8billmxRowsMap.get(id);//获取表体
  65. for (Row u8billmxRow : u8billmxRows) {
  66. String cinvcode = u8billmxRow.getString("cinvcode");
  67. if (itemRowsMap.get(cinvcode).isEmpty()) {
  68. //商品编号不存在,失败次数+1
  69. YUNl_DB.runSqlUpdate("update RdRecord set ErrTimes=isnull(ErrTimes,0)+1,failreason='商品不存在' where ID =" + id + " and billtype='10' ");
  70. logger.info("入库单{}商品{}不存在", ccode, cinvcode);
  71. continue foreachbill;
  72. }
  73. }
  74. RowsMap u8billmxCodeMXRowsMap = u8billmxCodeRowsMap.get(id).toRowsMap("ivouchsid");//获取表体序列号
  75. String cmemo = cInvCodeRow.getString("cmemo");
  76. String dnverifytime = cInvCodeRow.getString("dnverifytime");
  77. String chandler = cInvCodeRow.getString("chandler");
  78. long stockid = stockRowsMap.get(cwhcode).get(0).getLong("stockid");
  79. SQLDump sqlDump = new SQLDump();
  80. long st_stockbillid = createTableID("st_stockbill");
  81. InsertSQL stStockbill = SQLFactory.createInsertSQL(dbConnect, "st_stockbill");
  82. stStockbill.setValue("st_stockbillid", st_stockbillid);
  83. stStockbill.setValue("type", "其他入库");
  84. stStockbill.setValue("typemx", "生产入库u8");
  85. stStockbill.setValue("billno", ccode);
  86. stStockbill.setValue("siteid", "MD");
  87. stStockbill.setValue("departmentid", departmentRowsMap.get("103").get(0).getLong("departmentid"));
  88. stStockbill.setValue("billdate", ddate);
  89. stStockbill.setValue("stockid", stockid);
  90. stStockbill.setValue("remarks", cmemo);
  91. stStockbill.setValue("createby", "u8");
  92. stStockbill.setValue("status", "新建");
  93. stStockbill.setValue("rb", 1);
  94. for (Row u8billmxRow : u8billmxRows) {
  95. String cinvcode = u8billmxRow.getString("cinvcode");
  96. String autoid = u8billmxRow.getString("autoid");
  97. double iquantity = u8billmxRow.getDouble("iquantity");
  98. // String iunitcost = u8billmxRow.getString("iunitcost");
  99. // String iprice = u8billmxRow.getString("iprice");
  100. String cbmemo = u8billmxRow.getString("cbmemo");
  101. String cbatch = u8billmxRow.getString("cbatch");
  102. String rowindex = u8billmxRow.getString("rowindex");
  103. Row itemrow = itemRowsMap.get(cinvcode).get(0);
  104. long st_stockbill_itemsid = createTableID("st_stockbill_items");
  105. InsertSQL stStockbillitems = SQLFactory.createInsertSQL(dbConnect, "st_stockbill_items");
  106. stStockbillitems.setValue("st_stockbill_itemsid", st_stockbill_itemsid);
  107. stStockbillitems.setValue("st_stockbillid", st_stockbillid);
  108. stStockbillitems.setValue("siteid", "MD");
  109. stStockbillitems.setValue("rowno", rowindex);
  110. stStockbillitems.setValue("itemid", itemrow.getLong("itemid"));
  111. stStockbillitems.setValue("skucontrol", true);
  112. stStockbillitems.setValue("itemno", itemrow.getString("itemno"));
  113. stStockbillitems.setValue("itemname", itemrow.getString("itemname"));
  114. stStockbillitems.setValue("model", itemrow.getString("model"));
  115. if (unitRowsMap.get(itemrow.getString("unitid")).isNotEmpty()) {
  116. stStockbillitems.setValue("unit", unitRowsMap.get(itemrow.getString("unitid")).get(0).getString("unitname"));
  117. }
  118. stStockbillitems.setValue("qty", iquantity < 0 ? -iquantity : iquantity);
  119. if (!cbatch.isEmpty()) {
  120. stStockbillitems.setValue("batchno", cbatch);
  121. }
  122. stStockbillitems.setValue("stockid", stockid);
  123. stStockbillitems.setValue("remarks", cbmemo);
  124. stStockbill.setValue("rb", iquantity < 0 ? "0" : "1");
  125. sqlDump.add(stStockbillitems);
  126. for (Row codeRow : u8billmxCodeMXRowsMap.get(autoid)) {
  127. String cinvsn = codeRow.getString("cinvsn");
  128. InsertSQL stStockbillItemsSku = SQLFactory.createInsertSQL(dbConnect, "st_stockbill_items_sku");
  129. stStockbillItemsSku.setValue("siteid", "MD");
  130. stStockbillItemsSku.setValue("createby", "u8");
  131. stStockbillItemsSku.setValue("itemid", itemrow.getLong("itemid"));
  132. stStockbillItemsSku.setValue("st_stockbillid", st_stockbillid);
  133. stStockbillItemsSku.setValue("st_stockbill_itemsid", st_stockbill_itemsid);
  134. stStockbillItemsSku.setValue("stockid", stockid);
  135. stStockbillItemsSku.setValue("sku", cinvsn);
  136. sqlDump.add(stStockbillItemsSku);
  137. }
  138. }
  139. sqlDump.add(stStockbill);
  140. sqlDump.commit();
  141. try {
  142. Stockbill.check(getSysController("MD"), st_stockbillid, true);
  143. YUNl_DB.runSqlUpdate("update RdRecord set status=1,failreason='' where ID =" + id + " and billtype='10'");
  144. } catch (Exception e) {
  145. dbConnect.runSqlUpdate("delete from st_stockbill where st_stockbillid=" + st_stockbillid);
  146. dbConnect.runSqlUpdate("delete from st_stockbill_items where st_stockbillid=" + st_stockbillid);
  147. dbConnect.runSqlUpdate("delete from st_stockbill_items_sku where st_stockbillid=" + st_stockbillid);
  148. YUNl_DB.runSqlUpdate("update RdRecord set ErrTimes=isnull(ErrTimes,0)+1,failreason='" + e.getMessage() + "' where ID =" + id + " and billtype='10' ");
  149. logger.info("产品入库单{}审核失败", ccode, e);
  150. }
  151. } catch (Exception e) {
  152. YUNl_DB.runSqlUpdate("update RdRecord set ErrTimes=isnull(ErrTimes,0)+1,failreason='" + e.getMessage() + "' where ID =" + id + " and billtype='10' ");
  153. logger.info("产品入库单ID{}同步失败", cInvCodeRow.toJsonObject(), e);
  154. }
  155. }
  156. }
  157. }
  158. }