invbal.java 38 KB

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