invbal.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package restcontroller.webmanage.sale.invbal;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.HashMap;
  6. import beans.itemclass.ItemClass;
  7. import beans.parameter.Parameter;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import beans.invbal.Invbal;
  12. import common.Controller;
  13. import common.YosException;
  14. import common.annotation.API;
  15. import common.annotation.CACHEING_CLEAN;
  16. import common.data.Row;
  17. import common.data.Rows;
  18. import common.data.RowsMap;
  19. import common.data.SQLFactory;
  20. import common.data.db.DBConnect;
  21. import org.apache.commons.lang.StringUtils;
  22. import restcontroller.R;
  23. import utility.ERPDocking;
  24. @API(title = "库存管理")
  25. public class invbal extends Controller {
  26. public invbal(JSONObject content) throws YosException {
  27. super(content);
  28. // TODO Auto-generated constructor stub
  29. }
  30. public static HashMap<String, Long> itemhashmap = new HashMap<>(16);
  31. public static HashMap<Long, String> itemnomap = new HashMap<>(16);
  32. @API(title = "从erp新增更新库存", apiversion = R.ID20230222203603.v1.class)
  33. @CACHEING_CLEAN(apiversions = {R.ID20230222203803.class})
  34. public String caculate() throws YosException {
  35. JSONArray itemids = content.getJSONArray("itemids");
  36. boolean rightnow = content.getBooleanValue("rightnow");
  37. JSONArray erpitemnos = new JSONArray();
  38. if (itemnomap.isEmpty()) {
  39. Rows rows = dbConnect
  40. .runSqlQuery("select itemid,erpitemno from plm_itemextend where siteid='" + siteid + "'");
  41. for (Row row : rows) {
  42. itemnomap.put(row.getLong("itemid"), row.getString("erpitemno"));
  43. }
  44. }
  45. for (Object object : itemids) {
  46. long itemid =((Integer)object).longValue();
  47. if (itemnomap.containsKey(itemid)) {
  48. erpitemnos.add(itemnomap.get(itemid));
  49. } else {
  50. Rows rows = dbConnect.runSqlQuery("select erpitemno from plm_itemextend where itemid='" + itemid
  51. + "' and siteid='" + siteid + "'");
  52. if (!rows.isEmpty()) {
  53. erpitemnos.add(rows.get(0).getString("erpitemno"));
  54. itemnomap.put(itemid, rows.get(0).getString("erpitemno"));
  55. }
  56. }
  57. }
  58. Invbal.caculate_erpitemno(this, erpitemnos, rightnow);
  59. return getSucReturnObject().toString();
  60. }
  61. @API(title = "从erp新增更新库存(站点全部)", apiversion = R.ID20230222203703.v1.class)
  62. @CACHEING_CLEAN(apiversions = {R.ID20230222203803.class})
  63. public String caculateAll() throws YosException {
  64. if (itemnomap.isEmpty()) {
  65. Rows rows = dbConnect
  66. .runSqlQuery("select itemid,erpitemno from plm_itemextend where siteid='" + siteid + "'");
  67. for (Row row : rows) {
  68. itemnomap.put(row.getLong("itemid"), row.getString("erpitemno"));
  69. }
  70. }
  71. Invbal.caculate_erpitemno(this);
  72. return getSucReturnObject().toString();
  73. }
  74. @API(title = "查询库存列表", apiversion = R.ID20230222203803.v1.class)
  75. public String queryInvbalList() throws YosException {
  76. boolean istotal = content.getBooleanValue("istotal");
  77. StringBuffer where = new StringBuffer(" 1=1 ");
  78. if (content.containsKey("where")) {
  79. JSONObject whereObject = content.getJSONObject("where");
  80. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  81. where.append(" and(");
  82. where.append("t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  83. where.append("or t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  84. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  85. where.append("or t2.erpitemname like'%").append(whereObject.getString("condition")).append("%' ");
  86. where.append(")");
  87. }
  88. }
  89. SQLFactory sqlFactory;
  90. if(istotal) {
  91. sqlFactory = new SQLFactory(this, "销售库存列表查询", pageSize, pageNumber, pageSorting);
  92. }else {
  93. sqlFactory = new SQLFactory(this, "即时库存列表查询", pageSize, pageNumber, pageSorting);
  94. }
  95. sqlFactory.addParameter("siteid", siteid);
  96. sqlFactory.addParameter_SQL("where", where);
  97. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  98. return getSucReturnObject().setData(rows).toString();
  99. }
  100. @API(title = "查询Erp库存列表", apiversion = R.ID20230408091703.v1.class)
  101. public String queryErpInvbalList() throws YosException {
  102. String itemno = content.getString("itemno");
  103. String itemname = content.getString("itemname");
  104. boolean all = content.getBooleanValue("all");
  105. JSONArray itemclassids = content.getJSONArray("itemclassids");
  106. String where ="1=2";
  107. if(!StringUtils.isBlank(itemno)){
  108. where=where+" or t2.itemno like '%"+itemno+"%'";
  109. }
  110. if(!StringUtils.isBlank(itemname)){
  111. where=where+" or t2.itemname like '%"+itemname+"%' ";
  112. }
  113. if (itemclassids.size() > 0) {
  114. ArrayList<Long> itemclassList = new ArrayList<Long>();
  115. for (Object object : itemclassids) {
  116. //System.out.println(row.getLong("itemclassid"));
  117. long itemclassid = Long.valueOf(String.valueOf(object));
  118. itemclassList.add(itemclassid);
  119. itemclassList.addAll(ItemClass.getSubItemClassIds(this,itemclassid));
  120. }
  121. String sql = " or t1.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassList + " and siteid='" + siteid + "')";
  122. sql = sql.replace("[", "(").replace("]", ")");
  123. where=where+sql;
  124. }
  125. if(all){
  126. where=where+" or 1=1 ";
  127. }
  128. SQLFactory sqlFactory =new SQLFactory(this,"订单明细未发商品列表");
  129. sqlFactory.addParameter("siteid", siteid);
  130. sqlFactory.addParameter_SQL("where", where);
  131. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  132. 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 + "'");
  133. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  134. RowsMap rowsMap =rows.toRowsMap("itemno");
  135. SQLFactory sqlFactory1 =new SQLFactory(this,"商品发货数量汇总");
  136. sqlFactory1.addParameter("siteid", siteid);
  137. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  138. RowsMap sumQtyRowsMap= sumQtyRows.toRowsMap("itemno");
  139. SQLFactory sqlFactory2 =new SQLFactory(this,"商品未发货数量汇总");
  140. sqlFactory2.addParameter("siteid", siteid);
  141. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  142. RowsMap sumUnQtyRowsMap= sumUnQtyRows.toRowsMap("itemno");
  143. ERPDocking erpDocking =new ERPDocking();
  144. JSONArray jsonArray =new JSONArray();
  145. if(rows.toJsonArray("itemno").size()!=0){
  146. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  147. jsonArray=erpDocking.getErpIcinvbalRows(200000, 1,rows.toJsonArray("itemno"));
  148. }
  149. }
  150. if(!jsonArray.isEmpty()){
  151. for (Object object:jsonArray) {
  152. JSONObject jsonObject =(JSONObject)object;
  153. if(rowsMap.containsKey(jsonObject.getString("fitemno"))){
  154. if(rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()){
  155. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  156. }
  157. }
  158. }
  159. }
  160. for (Row row:rows) {
  161. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  162. if(sumQtyRowsMap.containsKey(row.getString("itemno"))){
  163. if(sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  164. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  165. }else {
  166. row.put("unsoldqty", BigDecimal.ZERO);
  167. }
  168. if(sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  169. row.put("undelqty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty"));
  170. }else {
  171. row.put("undelqty", BigDecimal.ZERO);
  172. }
  173. if(!row.containsKey("invbalqty")){
  174. row.put("invbalqty",BigDecimal.ZERO);
  175. }
  176. }
  177. }
  178. return getSucReturnObject().setData(rows).toString();
  179. }
  180. @API(title = "查询指定商品对应的未发货订单明细", apiversion = R.ID20230408101803.v1.class)
  181. public String queryOrderDetailList() throws YosException {
  182. String itemno = content.getString("itemno");
  183. SQLFactory sqlFactory =new SQLFactory(this,"未发货订单明细列表");
  184. sqlFactory.addParameter("siteid", siteid);
  185. sqlFactory.addParameter("itemno", itemno);
  186. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  187. return getSucReturnObject().setData(rows).toString();
  188. }
  189. }