promotionItems.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package restcontroller.webmanage.sale.promotion;
  2. import beans.brand.Brand;
  3. import beans.data.BatchDeleteErr;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.annotation.API;
  9. import common.annotation.CACHEING;
  10. import common.annotation.CACHEING_CLEAN;
  11. import common.data.Row;
  12. import common.data.Rows;
  13. import common.data.RowsMap;
  14. import common.data.SQLFactory;
  15. import org.apache.commons.lang.StringUtils;
  16. import restcontroller.R;
  17. import java.util.ArrayList;
  18. public class promotionItems extends Controller {
  19. public promotionItems(JSONObject arg0) throws YosException {
  20. super(arg0);
  21. // TODO Auto-generated constructor stub
  22. }
  23. @API(title = "商品列表查询", apiversion = R.ID20230102144603.v1.class)
  24. @CACHEING
  25. public String queryItemgList() throws YosException {
  26. /*
  27. * 过滤条件设置
  28. */
  29. String where = " 1=1 ";
  30. String where1 = " 1=1 ";
  31. if (content.containsKey("where")) {
  32. JSONObject whereObject = content.getJSONObject("where");
  33. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  34. where = where + "and (t1.itemname like'%" + whereObject.getString("condition") + "%'"
  35. + " or t1.model like'%" + whereObject.getString("condition") + "%'" + " or t1.spec like'%"
  36. + whereObject.getString("condition") + "%'" + ")";
  37. }
  38. if (whereObject.containsKey("tradefield") && !whereObject.getJSONArray("tradefield").isEmpty()) {
  39. String where2 =" 1=1 ";
  40. if ( whereObject.getJSONArray("tradefield").size() > 0) {
  41. for (Object object:whereObject.getJSONArray("tradefield")) {
  42. String str = String.valueOf(object);
  43. where2=where2 +" or tradefield ='"+str+"' ";
  44. }
  45. }
  46. where=where+("and t1.itemid in (SELECT itemid from plm_item_tradefield WHERE ("+where2+") and siteid = '"+siteid+"')");
  47. }
  48. }
  49. JSONArray itemclassids = content.getJSONArray("itemclassids");
  50. if (itemclassids.size() > 0) {
  51. String sql = " and t1.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassids + " and siteid='" + siteid + "')";
  52. sql = sql.replace("[", "(").replace("]", ")");
  53. where=where+sql;
  54. }
  55. Long sa_promotionid = content.getLong("sa_promotionid");
  56. Rows tradefieldrows =dbConnect.runSqlQuery("select tradefield from sa_promotion where siteid='"+siteid+"' and sa_promotionid="+sa_promotionid);
  57. JSONArray tradefield=new JSONArray();
  58. if(!tradefieldrows.isEmpty()){
  59. if (!StringUtils.isBlank(tradefieldrows.get(0).getString("tradefield"))) {
  60. tradefield=JSONArray.parseArray(tradefieldrows.get(0).getString("tradefield"));
  61. }
  62. }
  63. if(tradefield!=null && !tradefield.isEmpty()){
  64. for (Object object:tradefield) {
  65. String str = String.valueOf(object);
  66. where1=where1 +" or tradefield ='"+str+"' ";
  67. }
  68. }
  69. // String hrid = content.getString("hrid");
  70. SQLFactory sqlFactory = new SQLFactory(this, "商品查询", pageSize, pageNumber, pageSorting);
  71. sqlFactory.addParameter_SQL("where", where);
  72. sqlFactory.addParameter_SQL("where1", where1);
  73. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  74. sqlFactory.addParameter("siteid", siteid);
  75. System.out.println(sqlFactory.getSQL());
  76. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  77. // 附件
  78. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  79. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  80. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  81. Rows rowsitemclass = dbConnect.runSqlQuery(
  82. " 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='"
  83. + siteid + "'");
  84. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  85. for (Row row : rows) {
  86. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  87. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  88. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  89. }
  90. return getSucReturnObject().setData(rows).toString();
  91. }
  92. @API(title = "促销方案商品新增更新", apiversion = R.ID20230102142403.v1.class)
  93. @CACHEING_CLEAN(apiversions = {R.ID20230102142503.v1.class, R.ID20230102144603.v1.class,
  94. R.ID20230102163103.v1.class})
  95. public String insertormodify_promotionItems() throws YosException {
  96. Long sa_promotionid = content.getLong("sa_promotionid");
  97. Long sa_promotion_itemgroupid = content.getLong("sa_promotion_itemgroupid");
  98. JSONArray iteminfos = content.getJSONArray("iteminfos");
  99. ArrayList<String> sqlList = new ArrayList<>();
  100. Rows rowscount = dbConnect
  101. .runSqlQuery("select status,sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid);
  102. Rows itempricerows = dbConnect
  103. .runSqlQuery(" select pricegrade,price,itemid from sa_itemprice where siteid='" + siteid + "'");
  104. RowsMap itempricerowsMap = itempricerows.toRowsMap("itemid");
  105. if (!rowscount.isEmpty()) {
  106. if (!rowscount.get(0).getString("status").equals("新建")) {
  107. return getErrReturnObject().setErrMsg("非新建状态的促销方案无法新增修改").toString();
  108. }
  109. }
  110. int i = 0;
  111. long[] sa_promotion_itemsid = createTableID("sa_promotion_items", iteminfos.size());
  112. for (Object obj : iteminfos) {
  113. JSONObject iteminfo = (JSONObject) obj;
  114. Rows itemrows = dbConnect.runSqlQuery("select itemid,orderminqty,orderaddqty from plm_item where siteid='"+siteid+"'");
  115. RowsMap itemrowsMap = itemrows.toRowsMap("itemid");
  116. if (iteminfo.getLong("sa_promotion_itemsid") <= 0 || dbConnect
  117. .runSqlQuery("select sa_promotion_itemsid from sa_promotion_items where sa_promotion_itemsid="
  118. + iteminfo.getLong("sa_promotion_itemsid"))
  119. .isEmpty()) {
  120. SQLFactory saleFactory = new SQLFactory(this, "促销方案商品新增");
  121. saleFactory.addParameter("siteid", siteid);
  122. saleFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid[i]);
  123. saleFactory.addParameter("sa_promotionid", sa_promotionid);
  124. saleFactory.addParameter("sa_promotion_itemgroupid", sa_promotion_itemgroupid);
  125. saleFactory.addParameter("groupqty", iteminfo.getLong("groupqty"));
  126. saleFactory.addParameter("itemid", iteminfo.getLong("itemid"));
  127. if(itemrowsMap.containsKey(iteminfo.getString("itemid"))){
  128. saleFactory.addParameter("orderaddqty", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderaddqty"));
  129. saleFactory.addParameter("orderminqty", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderminqty"));
  130. }else {
  131. saleFactory.addParameter("orderaddqty", 1);
  132. saleFactory.addParameter("orderminqty", 1);
  133. }
  134. saleFactory.addParameter("islimit", iteminfo.getLong("islimit"));
  135. sqlList.add(saleFactory.getSQL());
  136. Rows rows = itempricerowsMap.get(iteminfo.getLong("itemid"));
  137. if (!rows.isEmpty()) {
  138. for (Row row : rows) {
  139. SQLFactory itempriceFactory = new SQLFactory(this, "促销方案商品价格新增");
  140. itempriceFactory.addParameter("siteid", siteid);
  141. itempriceFactory.addParameter("sa_promotion_itempriceid",
  142. createTableID("sa_promotion_itemprice"));
  143. itempriceFactory.addParameter("sa_promotionid", sa_promotionid);
  144. itempriceFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid[i]);
  145. itempriceFactory.addParameter("itemid", row.getLong("itemid"));
  146. itempriceFactory.addParameter("pricegrade", row.getBigDecimal("pricegrade"));
  147. itempriceFactory.addParameter("oldprice", row.getBigDecimal("price"));
  148. itempriceFactory.addParameter("price", row.getBigDecimal("price"));
  149. sqlList.add(itempriceFactory.getSQL());
  150. }
  151. }
  152. i++;
  153. } else {
  154. SQLFactory saleFactory = new SQLFactory(this, "促销方案商品更新");
  155. saleFactory.addParameter("sa_promotion_itemsid", iteminfo.getLong("sa_promotion_itemsid"));
  156. // saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  157. saleFactory.addParameter("siteid", siteid);
  158. saleFactory.addParameter("groupqty", iteminfo.getLong("groupqty"));
  159. saleFactory.addParameter("itemid", iteminfo.getLong("itemid"));
  160. saleFactory.addParameter("orderaddqty", iteminfo.getBigDecimal("orderaddqty"));
  161. saleFactory.addParameter("orderminqty", iteminfo.getBigDecimal("orderminqty"));
  162. saleFactory.addParameter("islimit", iteminfo.getLong("islimit"));
  163. saleFactory.addParameter("isonsale", iteminfo.getBooleanValue("isonsale"));
  164. sqlList.add(saleFactory.getSQL());
  165. }
  166. }
  167. dbConnect.runSqlUpdate(sqlList);
  168. return queryPromotionItemsList();
  169. }
  170. @API(title = "促销方案商品列表", apiversion = R.ID20230102142503.v1.class)
  171. @CACHEING
  172. public String queryPromotionItemsList() throws YosException {
  173. /*
  174. * 过滤条件设置
  175. */
  176. StringBuffer where = new StringBuffer(" 1=1 ");
  177. if (content.containsKey("where")) {
  178. JSONObject whereObject = content.getJSONObject("where");
  179. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  180. where.append(" and(");
  181. where.append("t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  182. where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  183. where.append("or t2.model like'%").append(whereObject.getString("condition")).append("%' ");
  184. where.append(")");
  185. }
  186. }
  187. Long sa_promotionid = content.getLong("sa_promotionid");
  188. Long sa_promotion_itemgroupid = content.getLong("sa_promotion_itemgroupid");
  189. SQLFactory sqlFactory = new SQLFactory(this, "促销方案商品查询", pageSize, pageNumber, pageSorting);
  190. sqlFactory.addParameter_SQL("where", where);
  191. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  192. sqlFactory.addParameter("sa_promotion_itemgroupid", sa_promotion_itemgroupid);
  193. sqlFactory.addParameter("siteid", siteid);
  194. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  195. ArrayList ids = rows.toArrayList("itemid", new ArrayList<>());
  196. // 附件
  197. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  198. for (Row row : rows) {
  199. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  200. }
  201. return getSucReturnObject().setData(rows).toString();
  202. }
  203. @API(title = "删除明细", apiversion = R.ID20230102142603.v1.class)
  204. @CACHEING_CLEAN(apiversions = {R.ID20230102142503.v1.class, R.ID20230102144603.v1.class,
  205. R.ID20230102163103.v1.class})
  206. public String deletemx() throws YosException {
  207. JSONArray sa_promotion_itemsids = content.getJSONArray("sa_promotion_itemsids");
  208. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotion_itemsids.size());
  209. for (Object o : sa_promotion_itemsids) {
  210. long sa_promotion_itemsid = Long.parseLong(o.toString());
  211. Rows RowsStatus = dbConnect.runSqlQuery(
  212. "select t1.sa_promotion_itemsid,t2.status,t1.sa_promotionid from sa_promotion_items t1 left join sa_promotion t2 on t1.sa_promotionid=t2.sa_promotionid and t1.siteid=t2.siteid where t1.siteid='"
  213. + siteid + "' and t1.sa_promotion_itemsid='" + sa_promotion_itemsid + "'");
  214. if (RowsStatus.isNotEmpty()) {
  215. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  216. batchDeleteErr.addErr(sa_promotion_itemsid, "非新建状态的促销方案商品无法删除");
  217. continue;
  218. }
  219. }
  220. ArrayList<String> list = new ArrayList<>();
  221. SQLFactory deletesql = new SQLFactory("sql:delete from sa_promotion_items where siteid='" + siteid
  222. + "' and sa_promotion_itemsid=" + sa_promotion_itemsid);
  223. list.add(deletesql.getSQL());
  224. list.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotion_itemsid="
  225. + sa_promotion_itemsid);
  226. dbConnect.runSqlUpdate(list);
  227. }
  228. return batchDeleteErr.getReturnObject().toString();
  229. }
  230. }