promotionItems.java 35 KB

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