invbal.java 38 KB

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