promotionItems.java 37 KB

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