promotionItems.java 39 KB

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