promotionItems.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package restcontroller.webmanage.sale.promotion;
  2. import beans.brand.Brand;
  3. import beans.data.BatchDeleteErr;
  4. import beans.datacontrllog.DataContrlLog;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.annotation.API;
  10. import common.annotation.CACHEING;
  11. import common.annotation.CACHEING_CLEAN;
  12. import common.data.*;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.poi.ss.usermodel.CellStyle;
  15. import org.apache.poi.ss.usermodel.CellType;
  16. import org.apache.poi.ss.usermodel.DataFormat;
  17. import org.apache.poi.xssf.usermodel.*;
  18. import restcontroller.R;
  19. import restcontroller.webmanage.sale.itemgroup.itemgroup;
  20. import java.math.BigDecimal;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. public class promotionItems extends Controller {
  25. public promotionItems(JSONObject arg0) throws YosException {
  26. super(arg0);
  27. // TODO Auto-generated constructor stub
  28. }
  29. @API(title = "商品列表查询", apiversion = R.ID20230102144603.v1.class)
  30. @CACHEING
  31. public String queryItemgList() throws YosException {
  32. /*
  33. * 过滤条件设置
  34. */
  35. String where = " 1=1 ";
  36. String where1 = " 1=1 ";
  37. Long sa_promotionid = content.getLong("sa_promotionid");
  38. if (!beans.order.Order.getDefaultIsRepeatValue(siteid, "促销订单")) {
  39. where=where+" and t1.itemid not in (select itemid from sa_promotion_items where sa_promotionid="+sa_promotionid+") ";
  40. }
  41. if (content.containsKey("where")) {
  42. JSONObject whereObject = content.getJSONObject("where");
  43. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  44. where = where + "and (t1.itemname like'%" + whereObject.getString("condition") + "%'"
  45. + " or t1.model like'%" + whereObject.getString("condition") + "%'" + " or t1.itemno like'%"
  46. + whereObject.getString("condition") + "%'" + ")";
  47. }
  48. if (whereObject.containsKey("tradefield") && !whereObject.getJSONArray("tradefield").isEmpty()) {
  49. String where2 = " 1=1 ";
  50. if (whereObject.getJSONArray("tradefield").size() > 0) {
  51. for (Object object : whereObject.getJSONArray("tradefield")) {
  52. String str = String.valueOf(object);
  53. where2 = where2 + " or tradefield ='" + str + "' ";
  54. }
  55. }
  56. where = where + ("and t1.itemid in (SELECT itemid from plm_item_tradefield WHERE (" + where2 + ") and siteid = '" + siteid + "')");
  57. }
  58. }
  59. JSONArray itemclassids = content.getJSONArray("itemclassids");
  60. if (itemclassids.size() > 0) {
  61. String sql = " and t1.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + itemclassids + " and siteid='" + siteid + "')";
  62. sql = sql.replace("[", "(").replace("]", ")");
  63. where = where + sql;
  64. }
  65. Rows tradefieldrows = dbConnect.runSqlQuery("select tradefield from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  66. JSONArray tradefield = new JSONArray();
  67. if (!tradefieldrows.isEmpty()) {
  68. if (!StringUtils.isBlank(tradefieldrows.get(0).getString("tradefield"))) {
  69. tradefield = JSONArray.parseArray(tradefieldrows.get(0).getString("tradefield"));
  70. }
  71. }
  72. if (tradefield != null && !tradefield.isEmpty()) {
  73. for (Object object : tradefield) {
  74. String str = String.valueOf(object);
  75. where1 = where1 + " or tradefield ='" + str + "' ";
  76. }
  77. }
  78. // String hrid = content.getString("hrid");
  79. SQLFactory sqlFactory = new SQLFactory(this, "商品查询", pageSize, pageNumber, pageSorting);
  80. sqlFactory.addParameter_SQL("where", where);
  81. sqlFactory.addParameter_SQL("where1", where1);
  82. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  83. sqlFactory.addParameter("siteid", siteid);
  84. //System.out.println(sqlFactory.getSQL());
  85. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  86. // 附件
  87. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  88. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  89. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  90. Rows rowsitemclass = dbConnect.runSqlQuery(
  91. " 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='"
  92. + siteid + "'");
  93. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  94. for (Row row : rows) {
  95. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  96. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  97. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  98. }
  99. return getSucReturnObject().setData(rows).toString();
  100. }
  101. @API(title = "促销方案商品新增更新", apiversion = R.ID20230102142403.v1.class,intervaltime = 200)
  102. @CACHEING_CLEAN(apiversions = {R.ID20230102142503.v1.class, R.ID20230102144603.v1.class,
  103. R.ID20230102163103.v1.class})
  104. public String insertormodify_promotionItems() throws YosException {
  105. Long sa_promotionid = content.getLong("sa_promotionid");
  106. Long sa_promotion_itemgroupid = content.getLong("sa_promotion_itemgroupid");
  107. JSONArray iteminfos = content.getJSONArray("iteminfos");
  108. ArrayList<String> sqlList = new ArrayList<>();
  109. Rows rowscount = dbConnect
  110. .runSqlQuery("select status,sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid);
  111. Rows itempricerows = dbConnect
  112. .runSqlQuery(" select pricegrade,price,itemid from sa_itemprice where siteid='" + siteid + "'");
  113. RowsMap itempricerowsMap = itempricerows.toRowsMap("itemid");
  114. if (!rowscount.isEmpty()) {
  115. if (!rowscount.get(0).getString("status").equals("新建")) {
  116. return getErrReturnObject().setErrMsg("非新建状态的促销方案无法新增修改").toString();
  117. }
  118. }
  119. int i = 0;
  120. long[] sa_promotion_itemsid = createTableID("sa_promotion_items", iteminfos.size());
  121. for (Object obj : iteminfos) {
  122. JSONObject iteminfo = (JSONObject) obj;
  123. Rows itemrows = dbConnect.runSqlQuery("select itemid,orderminqty,orderaddqty,packageqty,itemno from plm_item where siteid='" + siteid + "'");
  124. RowsMap itemrowsMap = itemrows.toRowsMap("itemid");
  125. if (iteminfo.getLong("sa_promotion_itemsid") <= 0 || dbConnect
  126. .runSqlQuery("select sa_promotion_itemsid from sa_promotion_items where sa_promotion_itemsid="
  127. + iteminfo.getLong("sa_promotion_itemsid"))
  128. .isEmpty()) {
  129. SQLFactory saleFactory = new SQLFactory(this, "促销方案商品新增");
  130. saleFactory.addParameter("siteid", siteid);
  131. saleFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid[i]);
  132. saleFactory.addParameter("sa_promotionid", sa_promotionid);
  133. saleFactory.addParameter("sa_promotion_itemgroupid", sa_promotion_itemgroupid);
  134. saleFactory.addParameter("groupqty", iteminfo.getLong("groupqty"));
  135. saleFactory.addParameter("itemid", iteminfo.getLong("itemid"));
  136. saleFactory.addParameter("itemno", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getString("itemno"));
  137. if (itemrowsMap.containsKey(iteminfo.getString("itemid"))) {
  138. if(iteminfo.getBigDecimalValue("packageqty").compareTo(BigDecimal.ZERO)<=0){
  139. saleFactory.addParameter("packageqty", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("packageqty"));
  140. }else {
  141. saleFactory.addParameter("packageqty", iteminfo.getBigDecimalValue("packageqty"));
  142. }
  143. if(itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderaddqty").compareTo(BigDecimal.ZERO)<=0){
  144. saleFactory.addParameter("orderaddqty", 1);
  145. }else{
  146. saleFactory.addParameter("orderaddqty", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderaddqty"));
  147. }
  148. if(itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderminqty").compareTo(BigDecimal.ZERO)<=0){
  149. saleFactory.addParameter("orderminqty", 1);
  150. }else{
  151. saleFactory.addParameter("orderminqty", itemrowsMap.get(iteminfo.getString("itemid")).get(0).getBigDecimal("orderminqty"));
  152. }
  153. } else {
  154. saleFactory.addParameter("orderaddqty", 1);
  155. saleFactory.addParameter("orderminqty", 1);
  156. saleFactory.addParameter("packageqty", 1);
  157. }
  158. saleFactory.addParameter("islimit", iteminfo.getLong("islimit"));
  159. saleFactory.addParameter("signaturecode", iteminfo.getStringValue("signaturecode"));
  160. saleFactory.addParameter("packagetypemx", iteminfo.getStringValue("packagetypemx"));
  161. sqlList.add(saleFactory.getSQL());
  162. Rows rows = itempricerowsMap.get(iteminfo.getLong("itemid"));
  163. if (!rows.isEmpty()) {
  164. for (Row row : rows) {
  165. SQLFactory itempriceFactory = new SQLFactory(this, "促销方案商品价格新增");
  166. itempriceFactory.addParameter("siteid", siteid);
  167. itempriceFactory.addParameter("sa_promotion_itempriceid",
  168. createTableID("sa_promotion_itemprice"));
  169. itempriceFactory.addParameter("sa_promotionid", sa_promotionid);
  170. itempriceFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid[i]);
  171. itempriceFactory.addParameter("itemid", row.getLong("itemid"));
  172. itempriceFactory.addParameter("pricegrade", row.getBigDecimal("pricegrade"));
  173. itempriceFactory.addParameter("oldprice", row.getBigDecimal("price"));
  174. itempriceFactory.addParameter("price", row.getBigDecimal("price"));
  175. sqlList.add(itempriceFactory.getSQL());
  176. }
  177. }
  178. i++;
  179. } else {
  180. SQLFactory saleFactory = new SQLFactory(this, "促销方案商品更新");
  181. saleFactory.addParameter("sa_promotion_itemsid", iteminfo.getLong("sa_promotion_itemsid"));
  182. // saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  183. saleFactory.addParameter("siteid", siteid);
  184. saleFactory.addParameter("groupqty", iteminfo.getLong("groupqty"));
  185. saleFactory.addParameter("itemid", iteminfo.getLong("itemid"));
  186. saleFactory.addParameter("orderaddqty", iteminfo.getBigDecimal("orderaddqty"));
  187. saleFactory.addParameter("orderminqty", iteminfo.getBigDecimal("orderminqty"));
  188. saleFactory.addParameter("islimit", iteminfo.getLong("islimit"));
  189. saleFactory.addParameter("isonsale", iteminfo.getBooleanValue("isonsale"));
  190. saleFactory.addParameter("signaturecode", iteminfo.getStringValue("signaturecode"));
  191. saleFactory.addParameter("packagetypemx", iteminfo.getStringValue("packagetypemx"));
  192. saleFactory.addParameter("packageqty", iteminfo.getBigDecimalValue("packageqty"));
  193. sqlList.add(saleFactory.getSQL());
  194. }
  195. }
  196. dbConnect.runSqlUpdate(sqlList);
  197. return queryPromotionItemsList();
  198. }
  199. /**
  200. * 促销方案商品上下架
  201. *
  202. * @return
  203. */
  204. @API(title = "促销方案商品上下架", apiversion = R.ID20230927135803.v1.class)
  205. @CACHEING_CLEAN(apiClass = {promotionItems.class, promotion.class,restcontroller.sale.promotion.promotion.class})
  206. public String release() throws YosException {
  207. ArrayList<String> sqlList = new ArrayList<>();
  208. JSONArray sa_promotion_itemsids = content.getJSONArray("sa_promotion_itemsids");
  209. boolean isonsale = content.getBooleanValue("isonsale");
  210. List<Long> list = sa_promotion_itemsids.toJavaList(Long.class);
  211. list.add(0l);
  212. Long[] stringArray = list.toArray(new Long[0]);
  213. SQLFactory sqlFactoryupdate = new SQLFactory(this, "商品组上架");
  214. if(isonsale){
  215. sqlFactoryupdate = new SQLFactory(this, "促销方案商品上架");
  216. }else{
  217. sqlFactoryupdate = new SQLFactory(this, "促销方案商品下架");
  218. }
  219. sqlFactoryupdate.addParameter("siteid", siteid);
  220. sqlFactoryupdate.addParameter_in("sa_promotion_itemsids", stringArray);
  221. sqlList.add(sqlFactoryupdate.getSQL());
  222. // //发送消息
  223. // for (String id : stringArray) {
  224. // sendMsg(Long.parseLong(id));
  225. // }
  226. String sql = "select t1.itemid,t1.orderminqty,t1.orderaddqty,t1.itemname,t2.sa_promotionid,t3.issyncitemonsale from plm_item t1 inner join sa_promotion_items t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid inner join sa_promotion t3 on t2.sa_promotionid=t3.sa_promotionid and t2.siteid=t3.siteid where t1.siteid='" + siteid + "' and t2.sa_promotion_itemsid in " + sa_promotion_itemsids;
  227. sql = sql.replace("[", "(").replace("]", ")");
  228. Rows itemrows = dbConnect.runSqlQuery(sql);
  229. for (Row row :itemrows){
  230. if(isonsale){
  231. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"上架", "促销方案商品上下架").getSQL());
  232. }else{
  233. if(row.getBoolean("issyncitemonsale")){
  234. sqlList.add("update plm_item set isonsale=0 where itemid="+row.getLong("itemid"));
  235. }
  236. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"下架" , "促销方案商品上下架").getSQL());
  237. }
  238. }
  239. dbConnect.runSqlUpdate(sqlList);
  240. return getSucReturnObject().toString();
  241. }
  242. @API(title = "促销方案商品列表", apiversion = R.ID20230102142503.v1.class)
  243. @CACHEING
  244. public String queryPromotionItemsList() throws YosException {
  245. /*
  246. * 过滤条件设置
  247. */
  248. StringBuffer where = new StringBuffer(" 1=1 ");
  249. if (content.containsKey("where")) {
  250. JSONObject whereObject = content.getJSONObject("where");
  251. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  252. where.append(" and(");
  253. where.append("t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  254. where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  255. where.append("or t2.model like'%").append(whereObject.getString("condition")).append("%' ");
  256. where.append(")");
  257. }
  258. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  259. where.append(" and (t1.isonsale ='" + whereObject.getString("isonsale") + "') ");
  260. }
  261. }
  262. Long sa_promotionid = content.getLong("sa_promotionid");
  263. Long sa_promotion_itemgroupid = content.getLong("sa_promotion_itemgroupid");
  264. // SQLFactory sqlFactory = new SQLFactory(this, "促销方案商品查询", pageSize, pageNumber, pageSorting);
  265. // sqlFactory.addParameter_SQL("where", where);
  266. // sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  267. // sqlFactory.addParameter("sa_promotion_itemgroupid", sa_promotion_itemgroupid);
  268. // sqlFactory.addParameter("siteid", siteid);
  269. // Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  270. //
  271. QuerySQL querySQL = queryPromotionItemList(where.toString(),sa_promotionid,sa_promotion_itemgroupid);
  272. querySQL.setPage(pageSize, pageNumber);
  273. querySQL.setOrderBy(pageSorting);
  274. Rows rows = querySQL.query();
  275. ArrayList ids = rows.toArrayList("itemid", new ArrayList<>());
  276. // 附件
  277. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  278. for (Row row : rows) {
  279. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  280. }
  281. return getSucReturnObject().setData(rows).toString();
  282. }
  283. //查询发货单列表
  284. public QuerySQL queryPromotionItemList(String where,long sa_promotionid,long sa_promotion_itemgroupid) throws YosException {
  285. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_promotion_items","sa_promotion_itemsid", "siteid","sa_promotionid","itemid","itemid","orderminqty","orderaddqty","saledqty","islimit","isonsale","sa_promotion_itemgroupid","groupqty","signaturecode","packagetypemx","packageqty");
  286. querySQL.setTableAlias("t1");
  287. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t2", "t1.itemid = t2.itemid and t1.siteid = t2.siteid",
  288. "itemno", "itemname","spec","model","standards");
  289. querySQL.addJoinTable(JOINTYPE.left, "plm_unit", "t3", "t2.unitid = t3.unitid and t2.siteid = t3.siteid", "unitname");
  290. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_itemprice", "t4", "t1.sa_promotion_itemsid = t4.sa_promotion_itemsid and t1.sa_promotion_itemsid = t4.sa_promotion_itemsid and t1.siteid = t4.siteid and t4.pricegrade = 1");
  291. querySQL.addQueryFields("oldprice1", "ifnull(t4.oldprice, 0)");
  292. querySQL.addQueryFields("price1", "ifnull(t4.price, 0)");
  293. querySQL.addQueryFields("sa_promotion_itempriceid1", "ifnull(t4.sa_promotion_itempriceid, 0)");
  294. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_itemprice", "t5", "t1.sa_promotion_itemsid = t5.sa_promotion_itemsid and t1.sa_promotion_itemsid = t5.sa_promotion_itemsid and t1.siteid = t5.siteid and t5.pricegrade = 2");
  295. querySQL.addQueryFields("oldprice2", "ifnull(t5.oldprice, 0)");
  296. querySQL.addQueryFields("price2", "ifnull(t5.price, 0)");
  297. querySQL.addQueryFields("sa_promotion_itempriceid2", "ifnull(t5.sa_promotion_itempriceid, 0)");
  298. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_itemprice", "t6", "t1.sa_promotion_itemsid = t6.sa_promotion_itemsid and t1.sa_promotion_itemsid = t6.sa_promotion_itemsid and t1.siteid = t6.siteid and t6.pricegrade = 3");
  299. querySQL.addQueryFields("oldprice3", "ifnull(t6.oldprice, 0)");
  300. querySQL.addQueryFields("price3", "ifnull(t6.price, 0)");
  301. querySQL.addQueryFields("sa_promotion_itempriceid3", "ifnull(t6.sa_promotion_itempriceid, 0)");
  302. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_itemprice", "t7", "t1.sa_promotion_itemsid = t7.sa_promotion_itemsid and t1.sa_promotion_itemsid = t7.sa_promotion_itemsid and t1.siteid = t7.siteid and t7.pricegrade = 4");
  303. querySQL.addQueryFields("oldprice4", "ifnull(t7.oldprice, 0)");
  304. querySQL.addQueryFields("price4", "ifnull(t7.price, 0)");
  305. querySQL.addQueryFields("sa_promotion_itempriceid4", "ifnull(t7.sa_promotion_itempriceid, 0)");
  306. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_itemprice", "t8", "t1.sa_promotion_itemsid = t8.sa_promotion_itemsid and t1.sa_promotion_itemsid = t8.sa_promotion_itemsid and t1.siteid = t8.siteid and t8.pricegrade = 5");
  307. querySQL.addQueryFields("oldprice5", "ifnull(t8.oldprice, 0)");
  308. querySQL.addQueryFields("price5", "ifnull(t8.price, 0)");
  309. querySQL.addQueryFields("sa_promotion_itempriceid5", "ifnull(t8.sa_promotion_itempriceid, 0)");
  310. querySQL.addQueryFields("item_isonsale", "t2.isonsale");
  311. querySQL.setWhere("t1.siteid", siteid);
  312. querySQL.setWhere("t1.sa_promotionid",sa_promotionid);
  313. querySQL.setWhere("t1.sa_promotion_itemgroupid",sa_promotion_itemgroupid);
  314. querySQL.setWhere(where);
  315. return querySQL;
  316. }
  317. @API(title = "删除明细", apiversion = R.ID20230102142603.v1.class)
  318. @CACHEING_CLEAN(apiversions = {R.ID20230102142503.v1.class, R.ID20230102144603.v1.class,
  319. R.ID20230102163103.v1.class})
  320. public String deletemx() throws YosException {
  321. JSONArray sa_promotion_itemsids = content.getJSONArray("sa_promotion_itemsids");
  322. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotion_itemsids.size());
  323. for (Object o : sa_promotion_itemsids) {
  324. long sa_promotion_itemsid = Long.parseLong(o.toString());
  325. Rows RowsStatus = dbConnect.runSqlQuery(
  326. "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='"
  327. + siteid + "' and t1.sa_promotion_itemsid='" + sa_promotion_itemsid + "'");
  328. if (RowsStatus.isNotEmpty()) {
  329. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  330. batchDeleteErr.addErr(sa_promotion_itemsid, "非新建状态的促销方案商品无法删除");
  331. continue;
  332. }
  333. }
  334. ArrayList<String> list = new ArrayList<>();
  335. SQLFactory deletesql = new SQLFactory("sql:delete from sa_promotion_items where siteid='" + siteid
  336. + "' and sa_promotion_itemsid=" + sa_promotion_itemsid);
  337. list.add(deletesql.getSQL());
  338. list.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotion_itemsid="
  339. + sa_promotion_itemsid);
  340. dbConnect.runSqlUpdate(list);
  341. }
  342. return batchDeleteErr.getReturnObject().toString();
  343. }
  344. @API(title = "促销活动商品导入模板",apiversion = R.ID20230510153404.v1.class)
  345. public String downloadPromotionExcel() throws YosException {
  346. ExcelFactory excelFactory = new ExcelFactory("促销活动商品导入");
  347. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("Sheet1");
  348. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  349. CellStyle style = xssfFWorkbook.createCellStyle();
  350. // 设置为文本格式,防止身份证号变成科学计数法
  351. DataFormat format = xssfFWorkbook.createDataFormat();
  352. style.setDataFormat(format.getFormat("@"));
  353. // 对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
  354. sheet.setDefaultColumnStyle(0, style);
  355. ExportExcel.setBatchDetailSheetColumn1(sheet);// 设置工作薄列宽
  356. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  357. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  358. XSSFCellStyle titleCellStyle3 = ExportExcel.createBodyCellStyle(xssfFWorkbook);
  359. XSSFCellStyle titleCellStyle4 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  360. ExportExcel.batchDetail(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3,titleCellStyle4, xssfFWorkbook);// 写入标题
  361. Rows aa = uploadExcelToObs(excelFactory);
  362. String url = "";
  363. if (!aa.isEmpty()) {
  364. url = aa.get(0).getString("url");
  365. }
  366. return getSucReturnObject().setData(url).toString();
  367. }
  368. @API(title = "促销活动商品导入",apiversion = R.ID20230510153504.v1.class)
  369. @CACHEING_CLEAN(apiversions = {R.ID20230102142503.v1.class, R.ID20230102144603.v1.class,R.ID20230102163103.v1.class})
  370. public String uploadPromotionItems() throws YosException {
  371. Long sa_promotionid = content.getLong("sa_promotionid");
  372. Rows promotion = dbConnect.runSqlQuery("select status from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  373. if (promotion.isEmpty()) {
  374. return getErrReturnObject().setErrMsg("无效促销活动").toString();
  375. } else if (!promotion.get(0).getString("status").equals("新建")) {
  376. return getErrReturnObject().setErrMsg("非新建状态促销活动无法导入").toString();
  377. }
  378. ExcelFactory e;
  379. try {
  380. // 华为云
  381. e = getPostExcelFactory(content.getLong("attachmentid"));
  382. // 本地
  383. //e = getPostExcelFactory();
  384. ArrayList<String> keys = new ArrayList<>();
  385. ArrayList<String> sqllist = new ArrayList<>();
  386. keys.add("itemno");
  387. keys.add("orderminqty");
  388. keys.add("orderaddqty");
  389. keys.add("groupqty");
  390. keys.add("price1");
  391. keys.add("price2");
  392. keys.add("price3");
  393. keys.add("price4");
  394. keys.add("price5");
  395. keys.add("signaturecode");
  396. HashMap<String, CellType> cellMap = new HashMap<>();
  397. cellMap.put("itemno", CellType.STRING);
  398. cellMap.put("orderminqty", CellType.STRING);
  399. cellMap.put("orderaddqty", CellType.STRING);
  400. cellMap.put("groupqty", CellType.STRING);
  401. cellMap.put("price1", CellType.STRING);
  402. cellMap.put("price2", CellType.STRING);
  403. cellMap.put("price3", CellType.STRING);
  404. cellMap.put("price4", CellType.STRING);
  405. cellMap.put("price5", CellType.STRING);
  406. cellMap.put("signaturecode", CellType.STRING);
  407. int i = 0;
  408. int a = 0;
  409. Rows rows = e.getSheetRows(0, keys,cellMap, 3);
  410. boolean iserr = false;
  411. Rows rowserr = new Rows();
  412. Rows rowssuc = new Rows();
  413. ArrayList<String> itemnoList = rows.toArrayList("itemno");
  414. SQLFactory sqlFactory = new SQLFactory(this, "商品等级价格查询");
  415. sqlFactory.addParameter("siteid", siteid);
  416. sqlFactory.addParameter_in("itemno", itemnoList);
  417. RowsMap itemRowsMap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap("itemno");
  418. ArrayList<String> inItemnoList = dbConnect.runSqlQuery("select t2.itemno from sa_promotion_items t1 inner join plm_item t2 on t1.siteid=t2.siteid and t1.itemid=t2.itemid where t1.siteid='" + siteid + "' and t1.sa_promotionid=" + sa_promotionid).toArrayList("itemno");
  419. for (Row row : rows) {
  420. String itemno = row.getString("itemno");
  421. String orderminqty = row.getString("orderminqty");
  422. String orderaddqty = row.getString("orderaddqty");
  423. String groupqty = row.getString("groupqty");
  424. String price1 = row.getString("price1");
  425. String price2 = row.getString("price2");
  426. String price3 = row.getString("price3");
  427. String price4 = row.getString("price4");
  428. String price5 = row.getString("price5");
  429. String signaturecode = row.getString("signaturecode");
  430. if (StringUtils.isEmpty(itemno)) {
  431. iserr = true;
  432. row.put("msg", "产品编码不能为空");
  433. rowserr.add(row);
  434. continue;
  435. }
  436. if (!itemRowsMap.containsKey(itemno)) {
  437. iserr = true;
  438. row.put("msg", "产品不存在");
  439. rowserr.add(row);
  440. continue;
  441. }
  442. if(inItemnoList.contains(itemno)){
  443. iserr = true;
  444. row.put("msg", "产品已存在促销活动");
  445. rowserr.add(row);
  446. continue;
  447. }
  448. if(!isNumeric(groupqty)){
  449. iserr = true;
  450. row.put("msg", "限购数量不为数字格式,请检查");
  451. rowserr.add(row);
  452. continue;
  453. }
  454. if(!isNumeric(price1)){
  455. iserr = true;
  456. row.put("msg", "促销价格1不为数字格式,请检查");
  457. rowserr.add(row);
  458. continue;
  459. }
  460. if(!isNumeric(price2)){
  461. iserr = true;
  462. row.put("msg", "促销价格2不为数字格式,请检查");
  463. rowserr.add(row);
  464. continue;
  465. }
  466. if(!isNumeric(price3)){
  467. iserr = true;
  468. row.put("msg", "促销价格3不为数字格式,请检查");
  469. rowserr.add(row);
  470. continue;
  471. }
  472. if(!isNumeric(price4)){
  473. iserr = true;
  474. row.put("msg", "促销价格4不为数字格式,请检查");
  475. rowserr.add(row);
  476. continue;
  477. }
  478. if(!isNumeric(price5)){
  479. iserr = true;
  480. row.put("msg", "促销价格5不为数字格式,请检查");
  481. rowserr.add(row);
  482. continue;
  483. }
  484. Row item = itemRowsMap.get(itemno).get(0);
  485. row.put("itemid", item.getLong("itemid"));
  486. row.put("packageqty", item.getString("packageqty"));
  487. if (StringUtils.isEmpty(orderminqty) || (new BigDecimal(orderminqty)).compareTo(BigDecimal.ZERO)==0) {
  488. row.put("orderminqty", item.getDouble("orderminqty"));
  489. }
  490. if (StringUtils.isEmpty(orderaddqty) || (new BigDecimal(orderaddqty)).compareTo(BigDecimal.ZERO)==0) {
  491. row.put("orderaddqty", item.getDouble("orderaddqty"));
  492. }
  493. if (StringUtils.isEmpty(groupqty) || (new BigDecimal(groupqty)).compareTo(BigDecimal.ZERO)==0) {
  494. row.put("islimit", "0");
  495. }else {
  496. row.put("islimit", "1");
  497. }
  498. if (StringUtils.isEmpty(price1) || (new BigDecimal(price1)).compareTo(BigDecimal.ZERO)==0) {
  499. row.put("price1", item.getDouble("price1"));
  500. }
  501. if (StringUtils.isEmpty(price2) || (new BigDecimal(price2)).compareTo(BigDecimal.ZERO)==0) {
  502. row.put("price2", item.getDouble("price2"));
  503. }
  504. if (StringUtils.isEmpty(price3) || (new BigDecimal(price3)).compareTo(BigDecimal.ZERO)==0) {
  505. row.put("price3", item.getDouble("price3"));
  506. }
  507. if (StringUtils.isEmpty(price4) || (new BigDecimal(price4)).compareTo(BigDecimal.ZERO)==0) {
  508. row.put("price4", item.getDouble("price4"));
  509. }
  510. if (StringUtils.isEmpty(price5) || (new BigDecimal(price5)).compareTo(BigDecimal.ZERO)==0) {
  511. row.put("price5", item.getDouble("price5"));
  512. }
  513. row.put("oldprice1", item.getDouble("price1"));
  514. row.put("oldprice2", item.getDouble("price2"));
  515. row.put("oldprice3", item.getDouble("price3"));
  516. row.put("oldprice4", item.getDouble("price4"));
  517. row.put("oldprice5", item.getDouble("price5"));
  518. rowssuc.add(row);
  519. }
  520. if (iserr) {
  521. ExcelFactory excelFactory = new ExcelFactory("促销活动导入错误信息");
  522. HashMap<String, String> map = new HashMap<String, String>();
  523. map.put("itemno", "产品编码");
  524. map.put("orderminqty", "起订量");
  525. map.put("orderaddqty", "增量");
  526. map.put("groupqty", "限购数");
  527. map.put("price1", "价格一活动价");
  528. map.put("price2", "价格二活动价");
  529. map.put("price3", "价格三活动价");
  530. map.put("price4", "价格四活动价");
  531. map.put("price5", "价格五活动价");
  532. map.put("signaturecode", "特征码");
  533. map.put("msg", "错误信息");
  534. ArrayList<String> colNameList = new ArrayList<String>();
  535. HashMap<String, Class> keytypemap = new HashMap<String, Class>();
  536. colNameList.add("itemno");
  537. colNameList.add("orderminqty");
  538. colNameList.add("orderaddqty");
  539. colNameList.add("groupqty");
  540. colNameList.add("price1");
  541. colNameList.add("price2");
  542. colNameList.add("price3");
  543. colNameList.add("price4");
  544. colNameList.add("price5");
  545. colNameList.add("signaturecode");
  546. colNameList.add("msg");
  547. keytypemap.put("itemno", String.class);
  548. keytypemap.put("orderminqty", String.class);
  549. keytypemap.put("orderaddqty", String.class);
  550. keytypemap.put("groupqty", String.class);
  551. keytypemap.put("price1", String.class);
  552. keytypemap.put("price2", String.class);
  553. keytypemap.put("price3", String.class);
  554. keytypemap.put("price4", String.class);
  555. keytypemap.put("price5", String.class);
  556. keytypemap.put("signaturecode", String.class);
  557. keytypemap.put("msg", String.class);
  558. rowserr.setFieldList(colNameList);
  559. rowserr.setFieldTypeMap(keytypemap);
  560. addSheet(excelFactory, "Sheet1", rowserr, map);
  561. Rows aa = uploadExcelToObs(excelFactory);
  562. String url = "";
  563. if (!aa.isEmpty()) {
  564. url = aa.get(0).getString("url");
  565. }
  566. return getSucReturnObject().setData(url).toString();
  567. }
  568. long[] sa_promotion_itemsids = createTableID("sa_promotion_items", rowssuc.size());
  569. long[] sa_promotion_itempriceids = createTableID("sa_promotion_itemprice", rowssuc.size() * 5);
  570. if (!rowssuc.isEmpty()) {
  571. for (Row row : rowssuc) {
  572. long sa_promotion_itemsid = sa_promotion_itemsids[i];
  573. sqlFactory = new SQLFactory(this, "促销方案商品新增");
  574. sqlFactory.addParameter("siteid", siteid);
  575. sqlFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid);
  576. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  577. sqlFactory.addParameter("sa_promotion_itemgroupid", 0);
  578. sqlFactory.addParameter("groupqty", row.getString("groupqty"));
  579. sqlFactory.addParameter("itemid", row.getString("itemid"));
  580. sqlFactory.addParameter("itemno", row.getString("itemno"));
  581. sqlFactory.addParameter("orderaddqty", row.getString("orderaddqty"));
  582. sqlFactory.addParameter("orderminqty", row.getString("orderminqty"));
  583. sqlFactory.addParameter("islimit", row.getString("islimit"));
  584. sqlFactory.addParameter("signaturecode", row.getString("signaturecode"));
  585. sqlFactory.addParameter("packagetypemx", "");
  586. sqlFactory.addParameter("packageqty", row.getString("packageqty"));
  587. sqllist.add(sqlFactory.getSQL());
  588. for (int o = 1; o <= 5; o++) {
  589. long sa_promotion_itempriceid = sa_promotion_itempriceids[a];
  590. sqlFactory = new SQLFactory(this, "促销方案商品价格新增");
  591. sqlFactory.addParameter("siteid", siteid);
  592. sqlFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsid);
  593. sqlFactory.addParameter("sa_promotion_itempriceid", sa_promotion_itempriceid);
  594. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  595. sqlFactory.addParameter("itemid", row.getString("itemid"));
  596. sqlFactory.addParameter("pricegrade", o);
  597. sqlFactory.addParameter("oldprice", row.getDouble("oldprice" + o));
  598. sqlFactory.addParameter("price", row.getDouble("price" + o));
  599. sqllist.add(sqlFactory.getSQL());
  600. a++;
  601. }
  602. i++;
  603. }
  604. }
  605. if (!sqllist.isEmpty()) {
  606. sqllist.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "导入", "促销活动商品导入成功").getSQL());
  607. dbConnect.runSqlUpdate(sqllist);
  608. }
  609. } catch (Exception e1) {
  610. e1.printStackTrace();
  611. return getErrReturnObject().setErrMsg(e1.getMessage()).toString();
  612. }
  613. return getSucReturnObject().toString();
  614. }
  615. public XSSFSheet addSheet(ExcelFactory excelFactory, String sheetname, Rows datarows, HashMap<String, String> titlemap) {
  616. ArrayList<String> keylist = datarows.getFieldList();
  617. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  618. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  619. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  620. XSSFFont font = xssfFWorkbook.createFont();
  621. font.setColor((short) 0xa);
  622. font.setFontHeightInPoints((short) 12);
  623. font.setBold(true);
  624. xssfCellStyle1.setFont(font);
  625. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  626. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  627. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  628. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  629. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  630. for (int n = 0; n < datarows.size(); n++) {
  631. Row row = datarows.get(n);
  632. XSSFRow datarow = sheet.createRow(n + 3);
  633. for (int i1 = 0; i1 < keylist.size(); i1++) {
  634. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  635. if (fieldclazztype == Integer.class) {
  636. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)));
  637. } else if (fieldclazztype == Long.class) {
  638. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  639. } else if (fieldclazztype == Float.class) {
  640. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  641. } else if (fieldclazztype == Double.class) {
  642. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  643. } else {
  644. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  645. }
  646. if (i1 == 10) {
  647. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  648. }
  649. }
  650. }
  651. return sheet;
  652. }
  653. public static boolean isNumeric(String strNum) {
  654. if (strNum == null) {
  655. return false;
  656. }
  657. return strNum.matches("-?\\d+(\\.\\d+)?");
  658. }
  659. }