invbal.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 com.sun.tools.internal.xjc.reader.gbind.ElementSets;
  13. import common.Controller;
  14. import common.YosException;
  15. import common.annotation.API;
  16. import common.annotation.CACHEING_CLEAN;
  17. import common.data.Row;
  18. import common.data.Rows;
  19. import common.data.RowsMap;
  20. import common.data.SQLFactory;
  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 t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  85. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  86. where.append("or t2.erpitemname like'%").append(whereObject.getString("condition")).append("%' ");
  87. where.append(")");
  88. }
  89. }
  90. SQLFactory sqlFactory;
  91. if(istotal) {
  92. sqlFactory = new SQLFactory(this, "销售库存列表查询", pageSize, pageNumber, pageSorting);
  93. }else {
  94. sqlFactory = new SQLFactory(this, "即时库存列表查询", pageSize, pageNumber, pageSorting);
  95. }
  96. sqlFactory.addParameter("siteid", siteid);
  97. sqlFactory.addParameter_SQL("where", where);
  98. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  99. return getSucReturnObject().setData(rows).toString();
  100. }
  101. @API(title = "查询Erp库存列表", apiversion = R.ID20230408091703.v1.class)
  102. public String queryErpInvbalList() throws YosException {
  103. String itemno = content.getStringValue("itemno");
  104. String itemname = content.getStringValue("itemname");
  105. String agentinfo = content.getStringValue("agentinfo");
  106. boolean all = content.getBooleanValue("all");
  107. JSONArray itemclassids = content.getJSONArray("itemclassids");
  108. String where ="1=2";
  109. SQLFactory sqlFactory =new SQLFactory(this,"订单明细未发商品列表", pageSize, pageNumber, pageSorting);
  110. if(!StringUtils.isBlank(itemno)){
  111. sqlFactory=new SQLFactory(this,"商品列表", pageSize, pageNumber, pageSorting);
  112. where=where+" or t2.itemno like '%"+itemno+"%'";
  113. }
  114. if(!StringUtils.isBlank(itemname)){
  115. sqlFactory=new SQLFactory(this,"商品列表", pageSize, pageNumber, pageSorting);
  116. where=where+" or t2.itemname like '%"+itemname+"%' ";
  117. }
  118. if (itemclassids.size() > 0) {
  119. sqlFactory=new SQLFactory(this,"商品列表", pageSize, pageNumber, pageSorting);
  120. ArrayList<Long> itemclassList = new ArrayList<Long>();
  121. for (Object object : itemclassids) {
  122. //System.out.println(row.getLong("itemclassid"));
  123. long itemclassid = Long.valueOf(String.valueOf(object));
  124. itemclassList.add(itemclassid);
  125. itemclassList.addAll(ItemClass.getSubItemClassIds(this,itemclassid));
  126. }
  127. String sql = " or t2.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassList + " and siteid='" + siteid + "')";
  128. sql = sql.replace("[", "(").replace("]", ")");
  129. where=where+sql;
  130. }
  131. if(all){
  132. where=where+" or 1=1 ";
  133. }
  134. if(!StringUtils.isBlank(agentinfo)){
  135. sqlFactory =new SQLFactory(this,"订单明细未发商品列表", pageSize, pageNumber, pageSorting);
  136. where ="1=2";
  137. where=where+" or t6.agentnum like '%"+agentinfo+"%' or t5.enterprisename like '%"+agentinfo+"%' ";
  138. }
  139. sqlFactory.addParameter("siteid", siteid);
  140. sqlFactory.addParameter_SQL("where", where);
  141. System.out.println(sqlFactory.getSQL());
  142. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  143. 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 + "'");
  144. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  145. RowsMap rowsMap =rows.toRowsMap("itemno");
  146. SQLFactory sqlFactory1 =new SQLFactory(this,"商品发货数量汇总");
  147. sqlFactory1.addParameter("siteid", siteid);
  148. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  149. RowsMap sumQtyRowsMap= sumQtyRows.toRowsMap("itemno");
  150. SQLFactory sqlFactory2 =new SQLFactory(this,"商品未发货数量汇总");
  151. sqlFactory2.addParameter("siteid", siteid);
  152. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  153. RowsMap sumUnQtyRowsMap= sumUnQtyRows.toRowsMap("itemno");
  154. ERPDocking erpDocking =new ERPDocking();
  155. JSONArray jsonArray =new JSONArray();
  156. if(rows.toJsonArray("itemno").size()!=0){
  157. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  158. jsonArray=erpDocking.getErpIcinvbalRows(200000, 1,rows.toJsonArray("itemno"));
  159. }
  160. }
  161. if(!jsonArray.isEmpty()){
  162. for (Object object:jsonArray) {
  163. JSONObject jsonObject =(JSONObject)object;
  164. if(rowsMap.containsKey(jsonObject.getString("fitemno"))){
  165. if(rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()){
  166. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  167. }
  168. }
  169. }
  170. }
  171. for (Row row:rows) {
  172. String itemclass=itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().substring(1,itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().length()-1);
  173. row.put("itemclass", itemclass.replaceAll("\"", ""));
  174. if(sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  175. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  176. }else {
  177. row.put("unsoldqty", BigDecimal.ZERO);
  178. }
  179. if(sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  180. row.put("undelqty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty"));
  181. row.put("qty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  182. }else {
  183. row.put("undelqty", BigDecimal.ZERO);
  184. row.put("qty", BigDecimal.ZERO);
  185. }
  186. if(!row.containsKey("invbalqty")){
  187. row.put("invbalqty",BigDecimal.ZERO);
  188. }
  189. }
  190. return getSucReturnObject().setData(rows).toString();
  191. }
  192. @API(title = "查询指定商品对应的未发货订单明细", apiversion = R.ID20230408101803.v1.class)
  193. public String queryOrderDetailList() throws YosException {
  194. String itemno = content.getString("itemno");
  195. StringBuffer where = new StringBuffer(" 1=1 ");
  196. if (content.containsKey("where")) {
  197. JSONObject whereObject = content.getJSONObject("where");
  198. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  199. where.append(" and(");
  200. where.append("t5.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  201. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  202. where.append(")");
  203. }
  204. }
  205. SQLFactory sqlFactory =new SQLFactory(this,"未发货订单明细列表", pageSize, pageNumber, pageSorting);
  206. sqlFactory.addParameter("siteid", siteid);
  207. sqlFactory.addParameter("itemno", itemno);
  208. sqlFactory.addParameter_SQL("where", where);
  209. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  210. return getSucReturnObject().setData(rows).toString();
  211. }
  212. }