invbal.java 38 KB

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