invbal.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package restcontroller.webmanage.sale.invbal;
  2. import java.io.IOException;
  3. import java.math.BigDecimal;
  4. import java.time.LocalDate;
  5. import java.time.format.DateTimeFormatter;
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import beans.itemclass.ItemClass;
  11. import beans.parameter.Parameter;
  12. import com.alibaba.fastjson.JSON;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import beans.invbal.Invbal;
  16. import com.sun.tools.internal.xjc.reader.gbind.ElementSets;
  17. import common.Controller;
  18. import common.YosException;
  19. import common.annotation.API;
  20. import common.annotation.CACHEING_CLEAN;
  21. import common.data.Row;
  22. import common.data.Rows;
  23. import common.data.RowsMap;
  24. import common.data.SQLFactory;
  25. import common.data.db.DBConnect;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.poi.hslf.dev.UserEditAndPersistListing;
  28. import restcontroller.R;
  29. import utility.ERPDocking;
  30. @API(title = "库存管理")
  31. public class invbal extends Controller {
  32. public invbal(JSONObject content) throws YosException {
  33. super(content);
  34. // TODO Auto-generated constructor stub
  35. }
  36. public static HashMap<String, Long> itemhashmap = new HashMap<>(16);
  37. public static HashMap<Long, String> itemnomap = new HashMap<>(16);
  38. @API(title = "从erp新增更新库存", apiversion = R.ID20230222203603.v1.class)
  39. @CACHEING_CLEAN(apiversions = {R.ID20230222203803.class})
  40. public String caculate() throws YosException {
  41. JSONArray itemids = content.getJSONArray("itemids");
  42. boolean rightnow = content.getBooleanValue("rightnow");
  43. JSONArray erpitemnos = new JSONArray();
  44. if (itemnomap.isEmpty()) {
  45. Rows rows = dbConnect
  46. .runSqlQuery("select itemid,erpitemno from plm_itemextend where siteid='" + siteid + "'");
  47. for (Row row : rows) {
  48. itemnomap.put(row.getLong("itemid"), row.getString("erpitemno"));
  49. }
  50. }
  51. for (Object object : itemids) {
  52. long itemid = ((Integer) object).longValue();
  53. if (itemnomap.containsKey(itemid)) {
  54. erpitemnos.add(itemnomap.get(itemid));
  55. } else {
  56. Rows rows = dbConnect.runSqlQuery("select erpitemno from plm_itemextend where itemid='" + itemid
  57. + "' and siteid='" + siteid + "'");
  58. if (!rows.isEmpty()) {
  59. erpitemnos.add(rows.get(0).getString("erpitemno"));
  60. itemnomap.put(itemid, rows.get(0).getString("erpitemno"));
  61. }
  62. }
  63. }
  64. Invbal.caculate_erpitemno(this, erpitemnos, rightnow);
  65. return getSucReturnObject().toString();
  66. }
  67. @API(title = "从erp新增更新库存(站点全部)", apiversion = R.ID20230222203703.v1.class)
  68. @CACHEING_CLEAN(apiversions = {R.ID20230222203803.class})
  69. public String caculateAll() throws YosException {
  70. if (itemnomap.isEmpty()) {
  71. Rows rows = dbConnect
  72. .runSqlQuery("select itemid,erpitemno from plm_itemextend where siteid='" + siteid + "'");
  73. for (Row row : rows) {
  74. itemnomap.put(row.getLong("itemid"), row.getString("erpitemno"));
  75. }
  76. }
  77. Invbal.caculate_erpitemno(this);
  78. return getSucReturnObject().toString();
  79. }
  80. @API(title = "查询库存列表", apiversion = R.ID20230222203803.v1.class)
  81. public String queryInvbalList() throws YosException {
  82. boolean istotal = content.getBooleanValue("istotal");
  83. StringBuffer where = new StringBuffer(" 1=1 ");
  84. if (content.containsKey("where")) {
  85. JSONObject whereObject = content.getJSONObject("where");
  86. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  87. where.append(" and(");
  88. where.append("t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  89. where.append("or t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  90. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  91. where.append("or t2.erpitemname like'%").append(whereObject.getString("condition")).append("%' ");
  92. where.append(")");
  93. }
  94. }
  95. SQLFactory sqlFactory;
  96. if (istotal) {
  97. sqlFactory = new SQLFactory(this, "销售库存列表查询", pageSize, pageNumber, pageSorting);
  98. } else {
  99. sqlFactory = new SQLFactory(this, "即时库存列表查询", pageSize, pageNumber, pageSorting);
  100. }
  101. sqlFactory.addParameter("siteid", siteid);
  102. sqlFactory.addParameter_SQL("where", where);
  103. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  104. return getSucReturnObject().setData(rows).toString();
  105. }
  106. @API(title = "查询Erp库存列表", apiversion = R.ID20230408091703.v1.class)
  107. public String queryErpInvbalList() throws YosException, IOException {
  108. // boolean isExport = content.getBooleanValue("isExport");
  109. //String iteminfo = content.getStringValue("iteminfo");
  110. //String agentinfo = content.getStringValue("agentinfo");
  111. //boolean all = content.getBooleanValue("all");
  112. //JSONArray itemclassids = content.getJSONArray("itemclassids");
  113. String where = " 1=1 ";
  114. SQLFactory sqlFactory = new SQLFactory(this, "商品列表", pageSize, pageNumber, pageSorting);
  115. // if (isExport) {
  116. // sqlFactory = new SQLFactory(this, "商品列表");
  117. // }
  118. if (content.containsKey("where")) {
  119. JSONObject whereObject = content.getJSONObject("where");
  120. if (whereObject.containsKey("iteminfo")) {
  121. if (!StringUtils.isBlank(whereObject.getStringValue("iteminfo"))) {
  122. where = where + " and (t2.itemno like '%" + whereObject.getStringValue("iteminfo") + "%' or t2.itemname like '%" + whereObject.getStringValue("iteminfo") + "%') ";
  123. }
  124. }
  125. if (whereObject.containsKey("itemclassids")) {
  126. if (whereObject.getLongValue("itemclassids") != 0) {
  127. ArrayList<Long> itemclassList = new ArrayList<Long>();
  128. //System.out.println(row.getLong("itemclassid"));
  129. long itemclassid = whereObject.getLong("itemclassids");
  130. itemclassList.add(itemclassid);
  131. itemclassList.addAll(ItemClass.getSubItemClassIds(this, itemclassid));
  132. String sql = " and t2.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassList + " and siteid='" + siteid + "')";
  133. sql = sql.replace("[", "(").replace("]", ")");
  134. where = where + sql;
  135. }
  136. }
  137. if (whereObject.containsKey("all") && !"".equals(whereObject.getString("all"))) {
  138. if (!whereObject.getBooleanValue("all")) {
  139. where = where + " and 1=2 ";
  140. }
  141. }
  142. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  143. where = where + " and t2.isonsale ='" + whereObject.getString("isonsale") + "' ";
  144. }
  145. if (whereObject.containsKey("agentinfo")) {
  146. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  147. sqlFactory = new SQLFactory(this, "订单明细未发商品列表", pageSize, pageNumber, pageSorting);
  148. // if (isExport) {
  149. // sqlFactory = new SQLFactory(this, "订单明细未发商品列表");
  150. // }
  151. //System.out.println(StringUtils.isBlank(itemname) && StringUtils.isBlank(itemno) && itemclassids.size()==0);
  152. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  153. where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  154. }
  155. }
  156. }
  157. }
  158. sqlFactory.addParameter("siteid", siteid);
  159. sqlFactory.addParameter_SQL("where", where);
  160. //System.out.println(sqlFactory.getSQL());
  161. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  162. Rows rowsitemclass = dbConnect.runSqlQuery(" select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 LEFT JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='" + siteid + "'");
  163. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  164. RowsMap rowsMap = rows.toRowsMap("itemno");
  165. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  166. sqlFactory1.addParameter("siteid", siteid);
  167. where = " 1=1 ";
  168. sqlFactory1.addParameter_SQL("where", where);
  169. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  170. RowsMap sumAllQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  171. if (content.containsKey("where")) {
  172. JSONObject whereObject = content.getJSONObject("where");
  173. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  174. //where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  175. }
  176. }
  177. sqlFactory1.addParameter_SQL("where", where);
  178. sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  179. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  180. SQLFactory sqlFactory2 = new SQLFactory(this, "商品未发货数量汇总");
  181. sqlFactory2.addParameter("siteid", siteid);
  182. where = " 1=1 ";
  183. sqlFactory2.addParameter_SQL("where", where);
  184. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  185. RowsMap sumUnAllQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  186. if (content.containsKey("where")) {
  187. JSONObject whereObject = content.getJSONObject("where");
  188. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  189. where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  190. }
  191. }
  192. sqlFactory2.addParameter_SQL("where", where);
  193. sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  194. RowsMap sumUnQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  195. ERPDocking erpDocking = new ERPDocking(siteid);
  196. JSONArray jsonArray = new JSONArray();
  197. if (rows.toJsonArray("itemno").size() != 0) {
  198. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  199. if (rows.toJsonArray("itemno").size() <= 2000) {
  200. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  201. } else {
  202. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, new JSONArray());
  203. }
  204. }
  205. }
  206. if (!jsonArray.isEmpty()) {
  207. for (Object object : jsonArray) {
  208. JSONObject jsonObject = (JSONObject) object;
  209. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  210. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  211. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  212. }
  213. }
  214. }
  215. }
  216. for (Row row : rows) {
  217. String itemclass = itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().substring(1, itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().length() - 1);
  218. row.put("itemclass", itemclass.replaceAll("\"", ""));
  219. if (sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  220. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  221. } else {
  222. row.put("unsoldqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  223. }
  224. if (sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  225. row.put("undelqty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty").stripTrailingZeros().toPlainString());
  226. row.put("qty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  227. } else {
  228. row.put("undelqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  229. row.put("qty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  230. }
  231. if (sumUnAllQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  232. row.put("undelqtysum", sumUnAllQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty").stripTrailingZeros().toPlainString());
  233. } else {
  234. row.put("undelqtysum", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  235. }
  236. if (sumAllQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  237. row.put("unsoldqtysum", sumAllQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  238. } else {
  239. row.put("unsoldqtysum", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  240. }
  241. if (!row.containsKey("invbalqty")) {
  242. row.put("invbalqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  243. }
  244. }
  245. for (Row row : rows) {
  246. row.put("canbesent", (row.getBigDecimal("invbalqty").subtract(row.getBigDecimal("unsoldqty"))).stripTrailingZeros().toPlainString());
  247. row.put("canbesale", ((row.getBigDecimal("invbalqty").subtract(row.getBigDecimal("unsoldqty"))).subtract(row.getBigDecimal("undelqtysum"))).stripTrailingZeros().toPlainString());
  248. // if (row.getString("itemno").equals("10901371")) {
  249. // System.out.println(row.getString("canbesent"));
  250. // System.out.println(row.getString("canbesale"));
  251. // System.out.println(row.toJsonObject().toJSONString());
  252. // }
  253. }
  254. // if (isExport) {
  255. // //去除不需要导出项
  256. // rows.getFieldList().remove("itemid");
  257. // rows.getFieldList().remove("packageqty");
  258. // rows.getFieldList().remove("qty");
  259. //// for (Row row : rows) {
  260. //// if (row.getString("itemno").equals("10901371")) {
  261. //// System.out.println(row.getString("canbesent"));
  262. //// System.out.println(row.getString("canbesale"));
  263. //// System.out.println(row.toJsonObject().toJSONString());
  264. //// }
  265. ////
  266. //// }
  267. // Rows uploadRows = uploadExcelToObs("invbal", "库存列表", rows, getTitleMap());
  268. // return getSucReturnObject().setData(uploadRows).toString();
  269. // }
  270. return getSucReturnObject().setData(rows).toString();
  271. }
  272. //返回导出的标题
  273. // public HashMap<String, String> getTitleMap() {
  274. // HashMap<String, String> titleMap = new HashMap<>();
  275. // titleMap.put("itemname", "产品名称");
  276. // titleMap.put("itemno", "产品编号");
  277. // titleMap.put("undelqty", "未发数量");
  278. // titleMap.put("invbalqty", "库存数");
  279. // titleMap.put("canbesent", "预计可发量");
  280. // titleMap.put("canbesale", "预计可售量");
  281. // titleMap.put("unitname", "单位");
  282. // titleMap.put("itemclass", "营销分类");
  283. // titleMap.put("isonsale", "是否上架");
  284. // return titleMap;
  285. // }
  286. @API(title = "查询指定商品对应的未发货订单明细", apiversion = R.ID20230408101803.v1.class)
  287. public String queryOrderDetailList() throws YosException {
  288. String itemno = content.getString("itemno");
  289. StringBuffer where = new StringBuffer(" 1=1 ");
  290. if (content.containsKey("where")) {
  291. JSONObject whereObject = content.getJSONObject("where");
  292. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  293. where.append(" and(");
  294. where.append("t5.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  295. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  296. where.append(")");
  297. }
  298. }
  299. SQLFactory sqlFactory = new SQLFactory(this, "未发货订单明细列表", pageSize, pageNumber, pageSorting);
  300. sqlFactory.addParameter("siteid", siteid);
  301. sqlFactory.addParameter("itemno", itemno);
  302. sqlFactory.addParameter_SQL("where", where);
  303. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  304. return getSucReturnObject().setData(rows).toString();
  305. }
  306. @API(title = "商品补货分析", apiversion = R.ID20231228102402.v1.class)
  307. public String queryItemSalesList() throws YosException, IOException {
  308. // boolean isExport = content.getBooleanValue("isExport");
  309. int year = content.getIntValue("year");
  310. if (year <= 0) {
  311. Calendar calendar = Calendar.getInstance();
  312. year = calendar.get(Calendar.YEAR);
  313. }
  314. // String begindate = year+"-01-01";
  315. // String enddate = (year+1)+"-01-01";
  316. String where = " 1=1 ";
  317. SQLFactory sqlFactory = new SQLFactory(this, "商品列表", pageSize, pageNumber, pageSorting);
  318. // if (isExport) {
  319. // sqlFactory = new SQLFactory(this, "商品列表");
  320. // }
  321. if (content.containsKey("where")) {
  322. JSONObject whereObject = content.getJSONObject("where");
  323. if (whereObject.containsKey("iteminfo")) {
  324. if (!StringUtils.isBlank(whereObject.getStringValue("iteminfo"))) {
  325. where = where + " and (t2.itemno like '%" + whereObject.getStringValue("iteminfo") + "%' or t2.itemname like '%" + whereObject.getStringValue("iteminfo") + "%') ";
  326. }
  327. }
  328. if (whereObject.containsKey("itemclassids")) {
  329. if (whereObject.getLong("itemclassids") != 0) {
  330. ArrayList<Long> itemclassList = new ArrayList<Long>();
  331. long itemclassid = whereObject.getLong("itemclassids");
  332. itemclassList.add(itemclassid);
  333. itemclassList.addAll(ItemClass.getSubItemClassIds(this, itemclassid));
  334. String sql = " and t2.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassList + " and siteid='" + siteid + "')";
  335. sql = sql.replace("[", "(").replace("]", ")");
  336. where = where + sql;
  337. }
  338. }
  339. if (whereObject.containsKey("all") && !"".equals(whereObject.getString("all"))) {
  340. if (!whereObject.getBooleanValue("all")) {
  341. where = where + " and 1=2 ";
  342. }
  343. }
  344. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  345. where = where + " and t2.isonsale ='" + whereObject.getString("isonsale") + "' ";
  346. }
  347. if (whereObject.containsKey("agentinfo")) {
  348. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  349. sqlFactory = new SQLFactory(this, "订单明细未发商品列表", pageSize, pageNumber, pageSorting);
  350. // if (isExport) {
  351. // sqlFactory = new SQLFactory(this, "订单明细未发商品列表");
  352. // }
  353. //System.out.println(StringUtils.isBlank(itemname) && StringUtils.isBlank(itemno) && itemclassids.size()==0);
  354. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  355. where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  356. }
  357. }
  358. }
  359. }
  360. sqlFactory.addParameter("siteid", siteid);
  361. sqlFactory.addParameter_SQL("where", where);
  362. //System.out.println(sqlFactory.getSQL());
  363. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  364. Rows rowsitemclass = dbConnect.runSqlQuery(" select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 LEFT JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='" + siteid + "'");
  365. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  366. RowsMap rowsMap = rows.toRowsMap("itemno");
  367. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  368. sqlFactory1.addParameter("siteid", siteid);
  369. where = " 1=1 ";
  370. sqlFactory1.addParameter_SQL("where", where);
  371. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  372. RowsMap sumAllQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  373. if (content.containsKey("where")) {
  374. JSONObject whereObject = content.getJSONObject("where");
  375. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  376. // where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  377. }
  378. }
  379. sqlFactory1.addParameter_SQL("where", where);
  380. sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  381. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  382. SQLFactory sqlFactory2 = new SQLFactory(this, "商品未发货数量汇总");
  383. sqlFactory2.addParameter("siteid", siteid);
  384. where = " 1=1 ";
  385. sqlFactory2.addParameter_SQL("where", where);
  386. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  387. RowsMap sumUnAllQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  388. if (content.containsKey("where")) {
  389. JSONObject whereObject = content.getJSONObject("where");
  390. if (!StringUtils.isBlank(whereObject.getStringValue("agentinfo"))) {
  391. where = where + " and (t6.agentnum like '%" + whereObject.getStringValue("agentinfo") + "%' or t5.enterprisename like '%" + whereObject.getStringValue("agentinfo") + "%') ";
  392. }
  393. }
  394. sqlFactory2.addParameter_SQL("where", where);
  395. sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  396. RowsMap sumUnQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  397. ERPDocking erpDocking = new ERPDocking(siteid);
  398. JSONArray jsonArray = new JSONArray();
  399. if (rows.toJsonArray("itemno").size() != 0) {
  400. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  401. if (rows.toJsonArray("itemno").size() <= 2000) {
  402. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  403. } else {
  404. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, new JSONArray());
  405. }
  406. }
  407. }
  408. if (!jsonArray.isEmpty()) {
  409. for (Object object : jsonArray) {
  410. JSONObject jsonObject = (JSONObject) object;
  411. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  412. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  413. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  414. }
  415. }
  416. }
  417. }
  418. for (Row row : rows) {
  419. String itemclass = itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().substring(1, itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().length() - 1);
  420. row.put("itemclass", itemclass.replaceAll("\"", ""));
  421. if (sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  422. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  423. } else {
  424. row.put("unsoldqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  425. }
  426. if (sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  427. row.put("undelqty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty").stripTrailingZeros().toPlainString());
  428. row.put("qty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  429. } else {
  430. row.put("undelqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  431. row.put("qty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  432. }
  433. if (sumUnAllQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  434. row.put("undelqtysum", sumUnAllQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty").stripTrailingZeros().toPlainString());
  435. } else {
  436. row.put("undelqtysum", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  437. }
  438. if (sumAllQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  439. row.put("unsoldqtysum", sumAllQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty").stripTrailingZeros().toPlainString());
  440. } else {
  441. row.put("unsoldqtysum", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  442. }
  443. if (!row.containsKey("invbalqty")) {
  444. row.put("invbalqty", BigDecimal.ZERO.stripTrailingZeros().toPlainString());
  445. }
  446. }
  447. //查询统计商品月销量销
  448. SQLFactory sqlFactory3 = new SQLFactory(this, "查询统计商品月销量销");
  449. sqlFactory3.addParameter("siteid", siteid);
  450. sqlFactory3.addParameter_in("itemid", rows.toArray("itemid"));
  451. Rows rows3 = dbConnect.runSqlQuery(sqlFactory3);
  452. RowsMap monthRowsMap = rows3.toRowsMap("itemid");
  453. SQLFactory sqlFactory4 = new SQLFactory(this, "查询退货统计");
  454. sqlFactory4.addParameter("siteid", siteid);
  455. sqlFactory4.addParameter_in("itemid", rows.toArray("itemid"));
  456. Rows rows4 = dbConnect.runSqlQuery(sqlFactory4);
  457. RowsMap returnRowsMap = rows4.toRowsMap("itemid");
  458. SQLFactory sqlFactory5 = new SQLFactory(this, "查询手工关闭统计");
  459. sqlFactory5.addParameter("siteid", siteid);
  460. sqlFactory5.addParameter_in("itemid", rows.toArray("itemid"));
  461. Rows rows5 = dbConnect.runSqlQuery(sqlFactory4);
  462. RowsMap closeRowsMap = rows5.toRowsMap("itemid");
  463. for (Row row : rows) {
  464. row.put("canbesent", (row.getBigDecimal("invbalqty").subtract(row.getBigDecimal("unsoldqty"))).stripTrailingZeros().toPlainString());
  465. row.put("canbesale", ((row.getBigDecimal("invbalqty").subtract(row.getBigDecimal("unsoldqty"))).subtract(row.getBigDecimal("undelqtysum"))).stripTrailingZeros().toPlainString());
  466. Rows monthRows = monthRowsMap.getOrDefault(row.getString("itemid"), new Rows());
  467. for (Row month : monthRows) {
  468. row.put(month.getString("month"), month.getBigDecimal("qty"));
  469. }
  470. //补足月份
  471. row = initMonthRow(row);
  472. //处理退货
  473. Rows returnRows = returnRowsMap.getOrDefault(row.getString("itemid"), new Rows());
  474. for (Row returnRow : returnRows) {
  475. String key = returnRow.getString("month");
  476. row.replace(key, row.getBigDecimal(key).subtract(returnRow.getBigDecimal("qty")));
  477. }
  478. //
  479. Rows closeRows = closeRowsMap.getOrDefault(row.getString("itemid"), new Rows());
  480. for (Row closeRow : closeRows) {
  481. String key = closeRow.getString("month");
  482. row.replace(key, row.getBigDecimal(key).subtract(closeRow.getBigDecimal("qty")));
  483. }
  484. }
  485. for (Row row : rows) {
  486. for (int i = 0; i < 12; i++) {
  487. if (i < 9) {
  488. row.replace("month_0" + (i + 1), row.get(getLast12Months().get(i)));
  489. } else {
  490. row.replace("month_" + (i + 1), row.get(getLast12Months().get(i)));
  491. }
  492. }
  493. }
  494. // if (isExport) {
  495. // //去除不需要导出项
  496. // rows.getFieldList().remove("itemid");
  497. // rows.getFieldList().remove("packageqty");
  498. // rows.getFieldList().remove("qty");
  499. // Rows uploadRows = uploadExcelToObs("invbal", "商品补货分析", rows, getTitleMap());
  500. // return getSucReturnObject().setData(uploadRows).toString();
  501. // }
  502. return getSucReturnObject().setData(rows).toString();
  503. }
  504. public Row initMonthRow(Row row) {
  505. for (String key : getLast12Months()) {
  506. row.putIfAbsent(key, 0);
  507. }
  508. //初始化
  509. row.putIfAbsent("month_01", 0);
  510. row.putIfAbsent("month_02", 0);
  511. row.putIfAbsent("month_03", 0);
  512. row.putIfAbsent("month_04", 0);
  513. row.putIfAbsent("month_05", 0);
  514. row.putIfAbsent("month_06", 0);
  515. row.putIfAbsent("month_07", 0);
  516. row.putIfAbsent("month_08", 0);
  517. row.putIfAbsent("month_09", 0);
  518. row.putIfAbsent("month_10", 0);
  519. row.putIfAbsent("month_11", 0);
  520. row.putIfAbsent("month_12", 0);
  521. return row;
  522. }
  523. public List<String> getLast12Months() {
  524. // 获取当前日期
  525. LocalDate currentDate = LocalDate.now();
  526. // 创建一个存储日期的列表
  527. List<String> last12Months = new ArrayList<>();
  528. // 获取最近12个月的日期
  529. for (int i = 0; i < 12; i++) {
  530. // 使用DateTimeFormatter将日期格式化为字符串
  531. String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
  532. last12Months.add(formattedDate);
  533. // 将当前日期减去一个月
  534. currentDate = currentDate.minusMonths(1);
  535. }
  536. return last12Months;
  537. }
  538. }