invbal.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. where=" 1=1 ";
  149. if(!StringUtils.isBlank(agentinfo)){
  150. where=where+" and t6.agentnum like '%"+agentinfo+"%' or t5.enterprisename like '%"+agentinfo+"%' ";
  151. }
  152. sqlFactory1.addParameter_SQL("where", where);
  153. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  154. RowsMap sumQtyRowsMap= sumQtyRows.toRowsMap("itemno");
  155. SQLFactory sqlFactory2 =new SQLFactory(this,"商品未发货数量汇总");
  156. sqlFactory2.addParameter("siteid", siteid);
  157. where=" 1=1 ";
  158. if(!StringUtils.isBlank(agentinfo)){
  159. where=where+" and t6.agentnum like '%"+agentinfo+"%' or t5.enterprisename like '%"+agentinfo+"%' ";
  160. }
  161. sqlFactory2.addParameter_SQL("where", where);
  162. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  163. RowsMap sumUnQtyRowsMap= sumUnQtyRows.toRowsMap("itemno");
  164. ERPDocking erpDocking =new ERPDocking();
  165. JSONArray jsonArray =new JSONArray();
  166. if(rows.toJsonArray("itemno").size()!=0){
  167. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  168. jsonArray=erpDocking.getErpIcinvbalRows(200000, 1,rows.toJsonArray("itemno"));
  169. }
  170. }
  171. if(!jsonArray.isEmpty()){
  172. for (Object object:jsonArray) {
  173. JSONObject jsonObject =(JSONObject)object;
  174. if(rowsMap.containsKey(jsonObject.getString("fitemno"))){
  175. if(rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()){
  176. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  177. }
  178. }
  179. }
  180. }
  181. for (Row row:rows) {
  182. String itemclass=itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().substring(1,itemclassRowsMap.get(row.getString("itemid")).toJsonArray("itemclassname").toString().length()-1);
  183. row.put("itemclass", itemclass.replaceAll("\"", ""));
  184. if(sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  185. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  186. }else {
  187. row.put("unsoldqty", BigDecimal.ZERO);
  188. }
  189. if(sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  190. row.put("undelqty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty"));
  191. row.put("qty", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  192. }else {
  193. row.put("undelqty", BigDecimal.ZERO);
  194. row.put("qty", BigDecimal.ZERO);
  195. }
  196. if(!row.containsKey("invbalqty")){
  197. row.put("invbalqty",BigDecimal.ZERO);
  198. }
  199. }
  200. return getSucReturnObject().setData(rows).toString();
  201. }
  202. @API(title = "查询指定商品对应的未发货订单明细", apiversion = R.ID20230408101803.v1.class)
  203. public String queryOrderDetailList() throws YosException {
  204. String itemno = content.getString("itemno");
  205. StringBuffer where = new StringBuffer(" 1=1 ");
  206. if (content.containsKey("where")) {
  207. JSONObject whereObject = content.getJSONObject("where");
  208. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  209. where.append(" and(");
  210. where.append("t5.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  211. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  212. where.append(")");
  213. }
  214. }
  215. SQLFactory sqlFactory =new SQLFactory(this,"未发货订单明细列表", pageSize, pageNumber, pageSorting);
  216. sqlFactory.addParameter("siteid", siteid);
  217. sqlFactory.addParameter("itemno", itemno);
  218. sqlFactory.addParameter_SQL("where", where);
  219. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  220. return getSucReturnObject().setData(rows).toString();
  221. }
  222. }