GetOutCodeFromTQWms.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package service;
  2. import common.ServiceController;
  3. import common.YosException;
  4. import common.data.*;
  5. import common.data.db.DBConnect;
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. public class GetOutCodeFromTQWms extends ServiceController {
  9. private DBConnect TQWMS = new DBConnect("TQWMS");//谈桥立体仓库中间服务
  10. @Override
  11. public void serviceRun() throws Exception {
  12. getOutCode();
  13. }
  14. @Override
  15. public ServiceParam paramSet() {
  16. return new ServiceParam("获取谈桥WMS出库单序列号", 1, RunType.minute);
  17. }
  18. public void getOutCode() throws YosException {
  19. Rows wmsrows = TQWMS.runSqlQuery("select distinct t1.billno,t2.materialcode,t3.serialno from TOutInt t1 inner join TOutDetailInt t2 on t1.OutPlanID=t2.OutPlanID inner join TOutSerialnoDetail t3 on t2.DetailID=t3.DetailID where t2.Status=2 and t2.IsDeleted=0and t1.IsDeleted=0 and year(t3.CreateDate)>=" + (Calendar.getInstance().get(Calendar.YEAR) - 1) + " and isnull(t3.status,0)=0");
  20. if (wmsrows.isNotEmpty()) {
  21. logger.info("监测到有{}个出库序列号", wmsrows.size());
  22. QuerySQL billCheckSQL = SQLFactory.createQuerySQL(dbConnect, "st_stockbill", "billno", "st_stockbillid", "sys_enterpriseid", "status", "iswx").setTableAlias("t1");
  23. billCheckSQL.addJoinTable(JOINTYPE.left, "st_stockbill_items_sku", "t2", "t1.st_stockbillid=t2.st_stockbillid", "sku");
  24. billCheckSQL.addJoinTable(JOINTYPE.left, "sa_agents", "t3", "t1.sys_enterpriseid=t3.sys_enterpriseid", "entity");
  25. billCheckSQL.setWhere("t1.siteid", "MD").setWhere("t1.billno", wmsrows.toArrayList("billno"));
  26. RowsMap billRowsMap = billCheckSQL.query().toRowsMap("billno");
  27. QuerySQL codeQuery = SQLFactory.createQuerySQL(dbConnect, "sa_itemsku", "sku", "stockid", "itemid").setTableAlias("t1");
  28. codeQuery.addJoinTable(JOINTYPE.inner, "st_stock", "t2", "stockid=:stockid");
  29. codeQuery.addJoinTable(JOINTYPE.inner, "plm_item", "t3", "t1.itemid=t3.itemid", "stockno");
  30. codeQuery.setWhere("t1.sku", wmsrows.toArrayList("serialno")).setWhere("t2.stockno", new String[]{"101", "103"});
  31. RowsMap codeRowsMap = codeQuery.query().toRowsMap("sku");
  32. RowsMap stockRowsMap = SQLFactory.createQuerySQL(dbConnect, "st_stock", "stockid", "stockno").query().toRowsMap("stockno");
  33. RowsMap wmsbillnoMap = wmsrows.toRowsMap("billno");
  34. for (String billno : wmsbillnoMap.keySet()) {
  35. logger.info("开始处理单据{}", billno);
  36. if (!billRowsMap.containsKey(billno)) {
  37. //单据不存在
  38. TQWMS.runSqlUpdate("update TOutSerialnoDetail set status=-1 where BillNo='" + billno + "'");
  39. logger.info("E-订单单据{}不存在,将中间表状态改为-1", billno);
  40. continue;
  41. }
  42. Row billrow = billRowsMap.get(billno).get(0);
  43. long st_stockbillid = billrow.getLong("st_stockbillid");
  44. long sys_enterpriseid = billrow.getLong("sys_enterpriseid");
  45. String entity = billrow.getString("entity");
  46. String status = billrow.getString("status");
  47. boolean iswx = billrow.getBoolean("iswx");
  48. for (Row row : wmsbillnoMap.get(billno)) {
  49. String sku = row.getString("serialno");
  50. logger.info("开始处理序列号{}", sku);
  51. if (billRowsMap.containsKey(billno) && billRowsMap.get(billno).toArrayList("sku").contains(sku)) {
  52. //出库单序列号已存在
  53. TQWMS.runSqlUpdate("update TOutSerialnoDetail set status=1 where BillNo='" + billno + "' and SerialNo='" + sku + "'");
  54. logger.info("E-订单单据{},序列号{}已存在,将中间表状态改为1", billno, sku);
  55. continue;
  56. }
  57. if (!codeRowsMap.containsKey(sku)) {
  58. //序列号不存在或者不在101-103仓库中
  59. logger.info("E-订单序列号{}不存在于仓库101或103中", sku);
  60. continue;
  61. }
  62. Row coderow = codeRowsMap.get(sku).get(0);
  63. long stockid = entity.equals("实业") ? stockRowsMap.get(coderow.getString("stockno")).get(0).getLong("stockid") : stockRowsMap.get("101").get(0).getLong("stockid");
  64. long itemid = coderow.getLong("itemid");
  65. ArrayList<Long> stockbill_itemsids = dbConnect.runSqlQuery("select t.st_stockbill_itemsid,sum(t.qty)as qty,sum(t.skucount)as skucount from (\n" +
  66. "select st_stockbill_itemsid,qty,0 as skucount from st_stockbill_items t1 where st_stockbillid=" + st_stockbillid + " and wmsuploadflag=2 and itemid=" + itemid + "\n" +
  67. "union all\n" +
  68. "select st_stockbill_itemsid,0,1 as skucount from st_stockbill_items_sku where st_stockbillid=" + st_stockbillid + " and itemid=" + itemid + " and ifnull(st_stockbill_itemsid,0)>0 \n" +
  69. ")t group by t.st_stockbill_itemsid having sum(t.qty)>sum(t.skucount)").toArrayList("st_stockbill_itemsid", new ArrayList<>());
  70. SQLDump sqlDump = new SQLDump();
  71. //创建入库单序列号
  72. InsertSQL stStockbillItemsSku = SQLFactory.createInsertSQL(dbConnect, "st_stockbill_items_sku");
  73. stStockbillItemsSku.setValue("siteid", "MD");
  74. stStockbillItemsSku.setValue("createby", "WMS");
  75. stStockbillItemsSku.setValue("itemid", itemid);
  76. stStockbillItemsSku.setValue("st_stockbillid", st_stockbillid);
  77. if (!stockbill_itemsids.isEmpty()) {
  78. stStockbillItemsSku.setValue("st_stockbill_itemsid", stockbill_itemsids.get(0));
  79. }
  80. stStockbillItemsSku.setValue("stockid", stockid);
  81. stStockbillItemsSku.setValue("sku", sku);
  82. sqlDump.add(stStockbillItemsSku);
  83. if (status.equals("审核")) {
  84. //如果表单状态是审核的,则更新序列号表
  85. UpdateSQL saItemsku = SQLFactory.createUpdateSQL(dbConnect, "sa_itemsku");
  86. saItemsku.setValue("stockid", iswx ? stockRowsMap.get("00").get(0).getLong("stockid") : "null");//如果是网销销售出库单,则仓库置为00
  87. saItemsku.setValue("sys_enterpriseid", sys_enterpriseid);
  88. saItemsku.setWhere("sku", sku).setWhere("ifnull(stockid,0)>0");
  89. sqlDump.add(saItemsku);
  90. }
  91. try {
  92. sqlDump.commit();
  93. TQWMS.runSqlUpdate("update TOutSerialnoDetail set status=1 where BillNo='" + billno + "' and SerialNo='" + sku + "'");
  94. } catch (Exception e) {
  95. logger.error("处理单据{},序列号{}出错", billno, sku, e);
  96. }
  97. }
  98. try {
  99. //判断关闭中间表状态
  100. Rows skucountRows = dbConnect.runSqlQuery("select sum(1) as skucount,sum(case when createby='WMS' then 1 else 0 end ) as wmsskucount from st_stockbill_items_sku where st_stockbillid=" + st_stockbillid);
  101. Rows skuitemcountRows = dbConnect.runSqlQuery("select sum(qty) as totalcount,sum(case when t1.wmsuploadflag=2 then qty else 0 end ) as wmscount from st_stockbill_items t1 inner join plm_item t2 on t1.itemid=t2.itemid and t2.skucontrol=1 where t1.st_stockbillid=" + st_stockbillid);
  102. if (skucountRows.isNotEmpty() && skuitemcountRows.isNotEmpty()) {
  103. int skucount = skucountRows.get(0).getInteger("skucount");//已收集的序列号数
  104. int wmsskucount = skucountRows.get(0).getInteger("wmsskucount");//从立库收集的序列号数
  105. int totalcount = skuitemcountRows.get(0).getInteger("totalcount");//出库总数
  106. int wmscount = skuitemcountRows.get(0).getInteger("wmscount");//已下发立库的总数
  107. if (wmsskucount >= wmscount || totalcount >= skucount) {
  108. //当从立库收集的序列号数大于等于已下发立库的总数时或者出库总数大于等于已收集的序列号数时,表示本次立库收集完成
  109. ArrayList<String> sqlist = new ArrayList<>();
  110. sqlist.add("update TOutInt set status=10 ,ModifyDate=getdate() where BillNo='" + billno + "'");
  111. sqlist.add("update TOutDetailInt set status=10,ModifyDate=getdate() where BillNo='" + billno + "'");
  112. sqlist.add("update TOutInt set Status=10 where OutPlanID in (select OutPlanID from TOutDetailInt where Status=2 group by OutPlanID having sum(PlanOutQuantity)=0)");
  113. sqlist.add("update TOutDetailInt set Status=10 where OutPlanID in (select OutPlanID from TOutDetailInt where Status=2 group by OutPlanID having sum(PlanOutQuantity)=0)");
  114. TQWMS.runSqlUpdate(sqlist);
  115. }
  116. }
  117. } catch (Exception e) {
  118. logger.error("关闭中间表状态{}出错", billno, e);
  119. }
  120. }
  121. }
  122. }
  123. }