promotionItems.java 36 KB

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