ItempriceadjustItemprice.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package restcontroller.webmanage.sale.itempriceadjust;
  2. import beans.datacontrllog.DataContrlLog;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import common.Controller;
  6. import common.YosException;
  7. import common.annotation.API;
  8. import common.annotation.CACHEING;
  9. import common.annotation.CACHEING_CLEAN;
  10. import common.data.*;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.apache.poi.ss.usermodel.CellStyle;
  13. import org.apache.poi.ss.usermodel.CellType;
  14. import org.apache.poi.ss.usermodel.DataFormat;
  15. import org.apache.poi.xssf.usermodel.*;
  16. import restcontroller.R;
  17. import restcontroller.system.attachment.Attachment;
  18. import java.math.BigDecimal;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Set;
  22. @API(title = "商品调价单商品价格明细")
  23. public class ItempriceadjustItemprice extends Controller {
  24. /**
  25. * 构造函数
  26. *
  27. * @param content
  28. */
  29. public ItempriceadjustItemprice(JSONObject content) throws YosException {
  30. super(content);
  31. }
  32. @API(title = "商品调价单商品明细新增或更新", apiversion = R.ID20221025095104.v1.class, intervaltime = 200)
  33. @CACHEING_CLEAN(apiversions = {R.ID20221025160904.v1.class})
  34. public String insertOrUpdate_sa_itempriceadjust_itemprice() throws YosException {
  35. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  36. Rows sa_itempriceadjustrow = dbConnect.runSqlQuery("select sa_itempriceadjustid,status,billno from sa_itempriceadjust where sa_itempriceadjustid=" + sa_itempriceadjustid + " and siteid='" + siteid + "'");
  37. if (sa_itempriceadjustid <= 0 || sa_itempriceadjustrow.isEmpty()) {
  38. return getErrReturnObject().setErrMsg("未选择商品调价单").toString();
  39. }
  40. if (sa_itempriceadjustrow.get(0).getString("status").equals("审核")) {
  41. return getErrReturnObject().setErrMsg("审核中无法更改").toString();
  42. }
  43. JSONArray sa_itempriceadjust_items = content.getJSONArray("sa_itempriceadjust_items");
  44. Rows items = dbConnect.runSqlQuery("select sa_itempriceadjust_itemsid,itemid,rowno from sa_itempriceadjust_items where siteid = '" + siteid + "' and sa_itempriceadjustid = " + sa_itempriceadjustid);
  45. RowsMap itemRowsMap = items.toRowsMap("itemid");
  46. ArrayList<String> itempriceadjustItemsidList = items.toArrayList("sa_itempriceadjust_itemsid");
  47. RowsMap itempriceRowsMap = dbConnect.runSqlQuery("select itemid,pricegrade,price from sa_itemprice where siteid = '" + siteid + "'").toRowsMap("itemid");
  48. RowsMap item = dbConnect.runSqlQuery("select itemid,itemno,marketprice from plm_item where siteid='" + siteid + "'").toRowsMap("itemid");
  49. RowsMap itempriceadjustItempriceRowsMap = dbConnect.runSqlQuery("select sa_itempriceadjust_itemsid from sa_itempriceadjust_itemprice where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid).toRowsMap("sa_itempriceadjust_itemsid");
  50. ArrayList<String> sqlList = new ArrayList();
  51. Long a = 0L;
  52. if (!items.isEmpty()) {
  53. a = items.getLastRow().getLong("rowno");
  54. }
  55. for (Object o : sa_itempriceadjust_items) {
  56. JSONObject jsonObject = (JSONObject) o;
  57. if (!item.containsKey(jsonObject.getString("itemid"))) {
  58. continue;
  59. }
  60. SQLFactory sqlFactory = new SQLFactory(this, "商品调价单商品明细新增");
  61. Long sa_itempriceadjust_itemsid = jsonObject.getLong("sa_itempriceadjust_itemsid");
  62. if (itemRowsMap.containsKey(jsonObject.getString("itemid"))) {
  63. sa_itempriceadjust_itemsid = itemRowsMap.get(jsonObject.getString("itemid")).get(0).getLong("sa_itempriceadjust_itemsid");
  64. }
  65. if (sa_itempriceadjust_itemsid <= 0 || !itempriceadjustItemsidList.contains(sa_itempriceadjust_itemsid.toString())) {
  66. sa_itempriceadjust_itemsid = createTableID("sa_itempriceadjust_items");
  67. DataContrlLog.createLog(this, "sa_itempriceadjust_items", sa_itempriceadjust_itemsid, "新增", "新增:" + sa_itempriceadjust_itemsid);
  68. a++;
  69. sqlFactory.addParameter("rowno", a);
  70. } else {
  71. sqlFactory = new SQLFactory(this, "商品调价单商品明细更新");
  72. DataContrlLog.createLog(this, "sa_itempriceadjust_items", sa_itempriceadjust_itemsid, "更新", "更新:" + sa_itempriceadjust_itemsid);
  73. }
  74. sqlFactory.addParameter("itemid", jsonObject.getLong("itemid"));
  75. sqlFactory.addParameter("billno", sa_itempriceadjustrow.get(0).getString("billno"));
  76. sqlFactory.addParameter("itemno", item.get(jsonObject.getString("itemid")).get(0).getString("itemno"));
  77. sqlFactory.addParameter("siteid", siteid);
  78. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  79. sqlFactory.addParameter("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsid);
  80. sqlFactory.addParameter("remarks", jsonObject.getString("remarks"));
  81. sqlFactory.addParameter("oldmarketprice", item.get(jsonObject.getString("itemid")).get(0).getBigDecimal("marketprice"));
  82. sqlFactory.addParameter("marketprice", item.get(jsonObject.getString("itemid")).get(0).getBigDecimal("marketprice"));
  83. sqlList.add(sqlFactory.getSQL());
  84. if (!itempriceadjustItempriceRowsMap.containsKey(sa_itempriceadjust_itemsid.toString())) {
  85. if (itempriceRowsMap.containsKey(jsonObject.getString("itemid"))) {
  86. Rows itemprice = itempriceRowsMap.get(jsonObject.getString("itemid"));
  87. for (Row row : itemprice) {
  88. sqlFactory = new SQLFactory(this, "商品调价单商品价格明细新增");
  89. long sa_itempriceadjust_itempriceid = createTableID("sa_itempriceadjust_itemprice");
  90. sqlFactory.addParameter("sa_itempriceadjust_itempriceid", sa_itempriceadjust_itempriceid);
  91. sqlFactory.addParameter("siteid", siteid);
  92. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  93. sqlFactory.addParameter("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsid);
  94. sqlFactory.addParameter("pricegrade", row.getLong("pricegrade"));
  95. sqlFactory.addParameter("oldprice", row.getBigDecimal("price"));
  96. sqlFactory.addParameter("price", row.getBigDecimal("price"));
  97. sqlList.add(sqlFactory.getSQL());
  98. }
  99. }
  100. }
  101. }
  102. dbConnect.runSqlUpdate(sqlList);
  103. return getSucReturnObject().toString();
  104. }
  105. @API(title = "商品调价单商品明细列表查询", apiversion = R.ID20221025160904.v1.class)
  106. @CACHEING
  107. public String query() throws YosException {
  108. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  109. StringBuffer where = new StringBuffer(" 1=1 ");
  110. if (content.containsKey("where")) {
  111. JSONObject whereObject = content.getJSONObject("where");
  112. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  113. where.append(" and (").append("t1.remarks like '%").append(whereObject.getString("condition")).append("%'");
  114. where.append(" or ").append("t7.itemno like '%").append(whereObject.getString("condition")).append("%'");
  115. where.append(" or ").append("t7.itemname like '%").append(whereObject.getString("condition")).append("%')");
  116. }
  117. }
  118. SQLFactory sqlFactory = new SQLFactory(this, "商品调价单商品明细列表情查询", pageSize, pageNumber, pageSorting);
  119. sqlFactory.addParameter("siteid", siteid);
  120. sqlFactory.addParameter_SQL("where", where);
  121. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  122. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  123. for (Row row : rows) {
  124. row.put("oldprice1", row.getBigDecimal("oldprice1"));
  125. row.put("price1", row.getBigDecimal("price1"));
  126. row.put("oldprice2", row.getBigDecimal("oldprice2"));
  127. row.put("price2", row.getBigDecimal("price2"));
  128. row.put("oldprice3", row.getBigDecimal("oldprice3"));
  129. row.put("price3", row.getBigDecimal("price3"));
  130. row.put("oldprice4", row.getBigDecimal("oldprice4"));
  131. row.put("price4", row.getBigDecimal("price4"));
  132. row.put("oldprice5", row.getBigDecimal("oldprice5"));
  133. row.put("price5", row.getBigDecimal("price5"));
  134. }
  135. return getSucReturnObject().setData(rows).toString();
  136. }
  137. @API(title = "商品调价单商品价格明细新增或更新", apiversion = R.ID20221025165904.v1.class)
  138. @CACHEING_CLEAN(apiversions = {R.ID20221025160904.v1.class})
  139. public String update() throws YosException {
  140. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  141. ArrayList<String> sqlList = new ArrayList<>();
  142. JSONArray sa_itempriceadjust_itemprice = content.getJSONArray("sa_itempriceadjust_itemprice");
  143. if (dbConnect.runSqlQuery("select status from sa_itempriceadjust where sa_itempriceadjustid="
  144. + sa_itempriceadjustid
  145. + " and siteid='"
  146. + siteid
  147. + "'").get(0).getString("status").equals("审核")) {
  148. return getErrReturnObject().setErrMsg("审核中无法更改").toString();
  149. }
  150. RowsMap itempriceRowsMap = dbConnect.runSqlQuery("select sa_itempriceadjust_itemsid,pricegrade from sa_itempriceadjust_itemprice where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid).toRowsMap("sa_itempriceadjust_itemsid");
  151. String sa_itempriceadjust_itemsid = content.getString("sa_itempriceadjust_itemsid");
  152. BigDecimal marketprice = content.getBigDecimal("marketprice");
  153. sqlList.add("update sa_itempriceadjust_items set marketprice=" + marketprice + " where siteid='" + siteid + "' and sa_itempriceadjust_itemsid=" + sa_itempriceadjust_itemsid);
  154. for (Object object : sa_itempriceadjust_itemprice) {
  155. JSONObject jsonObject = (JSONObject) object;
  156. BigDecimal price;
  157. try {
  158. price = jsonObject.getBigDecimal("price");
  159. } catch (NumberFormatException e) {
  160. return getErrReturnObject().setErrMsg("价格填写格式异常").toString();
  161. }
  162. if (price.compareTo(new BigDecimal("0")) == -1) {
  163. price = new BigDecimal("0");
  164. }
  165. Long pricegrade = jsonObject.getLong("pricegrade");
  166. SQLFactory sqlFactory = new SQLFactory(this, "商品调价单商品价格明细新增");
  167. if (!itempriceRowsMap.containsKey(sa_itempriceadjust_itemsid) ||
  168. !itempriceRowsMap.get(sa_itempriceadjust_itemsid).toArrayList("pricegrade").contains(pricegrade.toString())) {
  169. Long sa_itempriceadjust_itempriceid = createTableID("sa_itempriceadjust_itemprice");
  170. sqlFactory.addParameter("sa_itempriceadjust_itempriceid", sa_itempriceadjust_itempriceid);
  171. sqlFactory.addParameter("oldprice", 0);
  172. } else {
  173. sqlFactory = new SQLFactory(this, "商品调价单商品价格明细更新");
  174. }
  175. sqlFactory.addParameter("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsid);
  176. sqlFactory.addParameter("price", price);
  177. sqlFactory.addParameter("pricegrade", pricegrade);
  178. sqlFactory.addParameter("siteid", siteid);
  179. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  180. sqlList.add(sqlFactory.getSQL());
  181. String sql = DataContrlLog.createLog(this, "sa_itempriceadjust_itemprice", Long.parseLong(sa_itempriceadjust_itemsid), "更新", "商品调价单商品价格更新:价格等级" + pricegrade).getSQL();
  182. sqlList.add(sql);
  183. }
  184. dbConnect.runSqlUpdate(sqlList);
  185. return getSucReturnObject().toString();
  186. }
  187. @API(title = "删除商品价格调整单商品明细", apiversion = R.ID20221025095404.v1.class)
  188. @CACHEING_CLEAN(apiversions = {R.ID20221025160904.v1.class})
  189. public String delete() throws YosException {
  190. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  191. JSONArray sa_itempriceadjust_itemsids = content.getJSONArray("sa_itempriceadjust_itemsids");
  192. Rows rows = dbConnect.runSqlQuery("select status from sa_itempriceadjust where sa_itempriceadjustid=" + sa_itempriceadjustid + " and siteid='" + siteid + "'");
  193. if (rows.isEmpty()) {
  194. return getErrReturnObject().setErrMsg("无效调价单").toString();
  195. }
  196. if (rows.get(0).getString("status").equals("审核")) {
  197. return getErrReturnObject().setErrMsg("审核中无法删除").toString();
  198. }
  199. ArrayList<String> sqlList = new ArrayList<>();
  200. SQLFactory sqlFactory = new SQLFactory(this, "商品价格调整单商品明细删除");
  201. sqlFactory.addParameter_in("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsids.toArray());
  202. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  203. sqlFactory.addParameter("siteid", siteid);
  204. sqlList.add(sqlFactory.getSQL());
  205. sqlFactory = new SQLFactory(this, "商品价格调整单商品价格明细删除");
  206. sqlFactory.addParameter_in("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsids.toArray());
  207. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  208. sqlFactory.addParameter("siteid", siteid);
  209. sqlList.add(sqlFactory.getSQL());
  210. for (Object o : sa_itempriceadjust_itemsids) {
  211. String sql = "update sa_itempriceadjust_items set rowno=rowno-1 where siteid = '" + siteid + "' and sa_itempriceadjust_itemsid > " + o;
  212. sqlList.add(sql);
  213. sql = DataContrlLog.createLog(this, "sa_itempriceadjust_itemprice", Long.parseLong(o.toString()), "更新", "商品调价单商品删除:" + o).getSQL();
  214. sqlList.add(sql);
  215. }
  216. dbConnect.runSqlUpdate(sqlList);
  217. return getSucReturnObject().setData("删除成功").toString();
  218. }
  219. @API(title = "导入商品调价单", apiversion = R.ID20221026142804.v1.class)
  220. @CACHEING_CLEAN(apiversions = {R.ID20221024163504.v1.class, R.ID20221025160904.v1.class})
  221. public String importItempriceadjust() throws YosException {
  222. ArrayList<String> sqlList = new ArrayList<>();
  223. Long sa_itempriceadjustid = createTableID("sa_itempriceadjust");
  224. String billno = createBillCode("itempriceadjust");
  225. SQLFactory sqlFactory = new SQLFactory(this, "商品价格调整单新增");
  226. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  227. sqlFactory.addParameter("siteid", siteid);
  228. sqlFactory.addParameter("username", username);
  229. sqlFactory.addParameter("userid", userid);
  230. sqlFactory.addParameter("billno", billno);
  231. sqlFactory.addParameter("remarks", "");
  232. dbConnect.runSqlUpdate(sqlFactory.getSQL());
  233. JSONArray jsonArray = new JSONArray();
  234. jsonArray.add(content.getLong("attachmentid"));
  235. content.put("ownertable", "sa_itempriceadjust");
  236. content.put("ownerid", sa_itempriceadjustid);
  237. content.put("usetype", "default");
  238. content.put("attachmentids", jsonArray);
  239. Attachment attachment = new Attachment(content);
  240. attachment.createFileLink();
  241. ExcelFactory e;
  242. try {
  243. // 华为云
  244. e = getPostExcelFactory(content.getLong("attachmentid"));
  245. // 本地
  246. // e = getPostExcelFactory();
  247. ArrayList<String> keys = new ArrayList<>();
  248. ArrayList<String> sqllist = new ArrayList<>();
  249. keys.add("itemno");
  250. keys.add("remarks");
  251. keys.add("price1");
  252. keys.add("price2");
  253. keys.add("price3");
  254. keys.add("price4");
  255. keys.add("price5");
  256. HashMap<String, CellType> cellMap = new HashMap<>();
  257. cellMap.put("itemno", CellType.STRING);
  258. cellMap.put("remarks", CellType.STRING);
  259. cellMap.put("price1", CellType.STRING);
  260. cellMap.put("price2", CellType.STRING);
  261. cellMap.put("price3", CellType.STRING);
  262. cellMap.put("price4", CellType.STRING);
  263. cellMap.put("price5", CellType.STRING);
  264. Rows rows = e.getSheetRows(0, keys, cellMap, 3);
  265. ArrayList<String> itemnoList = rows.toArrayList("itemno", false);
  266. itemnoList.add("-1");
  267. boolean iserr = false;
  268. Rows rowserr = new Rows();
  269. Rows rowssuc = new Rows();
  270. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item", "itemid", "itemno", "marketprice");
  271. querySQL.setSiteid(siteid);
  272. querySQL.setWhere("itemno", itemnoList);
  273. RowsMap itemnoRowsMap = querySQL.query().toRowsMap("itemno");
  274. for (Row row : rows) {
  275. if (StringUtils.isEmpty(row.getString("itemno"))) {
  276. iserr = true;
  277. row.put("msg", "错误信息:商品编号不能为空");
  278. rowserr.add(row);
  279. continue;
  280. } else {
  281. String itemno = row.getString("itemno");
  282. if (itemnoList.indexOf(itemno) != itemnoList.lastIndexOf(itemno)) {
  283. iserr = true;
  284. row.put("msg", "错误信息:导入文件存在相同编码的商品");
  285. rowserr.add(row);
  286. continue;
  287. }
  288. if (itemnoRowsMap.containsKey(itemno)) {
  289. row.put("itemid", itemnoRowsMap.get(itemno).get(0).getLong("itemid"));
  290. row.put("oldmarketprice", itemnoRowsMap.get(itemno).get(0).getBigDecimal("marketprice"));
  291. } else {
  292. iserr = true;
  293. row.put("msg", "错误信息:不存在商品编号为" + itemno + "的商品");
  294. rowserr.add(row);
  295. continue;
  296. }
  297. }
  298. if (StringUtils.isEmpty(row.getString("price1")) ||
  299. StringUtils.isEmpty(row.getString("price2")) ||
  300. StringUtils.isEmpty(row.getString("price3")) ||
  301. StringUtils.isEmpty(row.getString("price4")) ||
  302. StringUtils.isEmpty(row.getString("price5"))) {
  303. iserr = true;
  304. row.put("msg", "错误信息:商品调整价不能为空");
  305. rowserr.add(row);
  306. continue;
  307. }
  308. rowssuc.add(row);
  309. }
  310. if (!rowssuc.isEmpty()) {
  311. sqlFactory = new SQLFactory(this, "查询指定商品原价");
  312. sqlFactory.addParameter("siteid", siteid);
  313. sqlFactory.addParameter_in("itemids", rowssuc.toArray("itemid"));
  314. RowsMap itemPriceRowsMap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap("itemid");
  315. Long rowno = 0L;
  316. for (Row row : rowssuc) {
  317. Long sa_itempriceadjust_itemsid = createTableID("sa_itempriceadjust_items");
  318. SQLFactory sqlFactoryupload = new SQLFactory(this, "商品调价单商品明细新增");
  319. sqlFactoryupload.addParameter("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsid);
  320. sqlFactoryupload.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  321. sqlFactoryupload.addParameter("remarks", row.getString("remarks"));
  322. rowno++;
  323. sqlFactoryupload.addParameter("rowno", rowno);
  324. sqlFactoryupload.addParameter("siteid", siteid);
  325. sqlFactoryupload.addParameter("itemid", row.getString("itemid"));
  326. sqlFactoryupload.addParameter("billno", billno);
  327. sqlFactoryupload.addParameter("itemno", row.getString("itemno"));
  328. sqlFactoryupload.addParameter("oldmarketprice", row.getBigDecimal("oldmarketprice"));
  329. sqlFactoryupload.addParameter("marketprice", row.getBigDecimal("oldmarketprice"));
  330. sqllist.add(sqlFactoryupload.getSQL());
  331. RowsMap pricegrade = new RowsMap();
  332. if (itemPriceRowsMap.containsKey(row.getString("itemid"))) {
  333. Rows itemPriceRows = itemPriceRowsMap.get(row.getString("itemid"));
  334. pricegrade = itemPriceRows.toRowsMap("pricegrade");
  335. }
  336. for (int i = 1; i <= 5; i++) {
  337. // if (pricegrade.containsKey(String.valueOf(i)) || !row.getString("price" + i).equals("0")) {
  338. sqlFactoryupload = new SQLFactory(this, "商品调价单商品价格明细新增");
  339. long sa_itempriceadjust_itempriceid = createTableID("sa_itempriceadjust_itemprice");
  340. sqlFactoryupload.addParameter("sa_itempriceadjust_itempriceid", sa_itempriceadjust_itempriceid);
  341. sqlFactoryupload.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  342. sqlFactoryupload.addParameter("sa_itempriceadjust_itemsid", sa_itempriceadjust_itemsid);
  343. sqlFactoryupload.addParameter("siteid", siteid);
  344. sqlFactoryupload.addParameter("pricegrade", i);
  345. BigDecimal oldprice = new BigDecimal("0");
  346. if (pricegrade.containsKey(String.valueOf(i))) {
  347. oldprice = pricegrade.get(String.valueOf(i)).get(0).getBigDecimal("price");
  348. }
  349. sqlFactoryupload.addParameter("oldprice", oldprice);
  350. BigDecimal price = new BigDecimal(row.getString("price" + i));
  351. sqlFactoryupload.addParameter("price", price);
  352. sqllist.add(sqlFactoryupload.getSQL());
  353. // }
  354. }
  355. }
  356. }
  357. if (!sqllist.isEmpty()) {
  358. String sql = DataContrlLog.createLog(this, "sa_itempriceadjust", sa_itempriceadjustid, "新增", "商品调价单导入:" + sa_itempriceadjustid).getSQL();
  359. sqlList.add(sql);
  360. dbConnect.runSqlUpdate(sqllist);
  361. } else {
  362. dbConnect.runSqlUpdate("delete from sa_itempriceadjust where sa_itempriceadjustid=" + sa_itempriceadjustid);
  363. }
  364. if (iserr) {
  365. ExcelFactory excelFactory = new ExcelFactory("调价单导入错误信息");
  366. HashMap<String, String> map = new HashMap<String, String>();
  367. map.put("itemno", "商品编号");
  368. map.put("remarks", "备注");
  369. map.put("price1", "调整价1");
  370. map.put("price2", "调整价2");
  371. map.put("price3", "调整价3");
  372. map.put("price4", "调整价4");
  373. map.put("price5", "调整价5");
  374. map.put("msg", "错误信息");
  375. ArrayList<String> colNameList = new ArrayList<String>();
  376. HashMap<String, Class> keytypemap = new HashMap<String, Class>();
  377. colNameList.add("itemno");
  378. colNameList.add("remarks");
  379. colNameList.add("price1");
  380. colNameList.add("price2");
  381. colNameList.add("price3");
  382. colNameList.add("price4");
  383. colNameList.add("price5");
  384. colNameList.add("msg");
  385. keytypemap.put("itemno", String.class);
  386. keytypemap.put("remarks", String.class);
  387. keytypemap.put("price1", BigDecimal.class);
  388. keytypemap.put("price2", BigDecimal.class);
  389. keytypemap.put("price3", BigDecimal.class);
  390. keytypemap.put("price4", BigDecimal.class);
  391. keytypemap.put("price5", BigDecimal.class);
  392. keytypemap.put("msg", String.class);
  393. rowserr.setFieldList(colNameList);
  394. rowserr.setFieldTypeMap(keytypemap);
  395. addSheet(excelFactory, "Sheet1", rowserr, map);
  396. Rows aa = uploadExcelToObs(excelFactory);
  397. String url = "";
  398. if (!aa.isEmpty()) {
  399. url = aa.get(0).getString("url");
  400. }
  401. return getSucReturnObject().setData(url).toString();
  402. }
  403. } catch (Exception e1) {
  404. dbConnect.runSqlUpdate("delete from sa_itempriceadjust where sa_itempriceadjustid=" + sa_itempriceadjustid);
  405. e1.printStackTrace();
  406. return getErrReturnObject().setErrMsg(e1.getMessage()).toString();
  407. }
  408. return getSucReturnObject().toString();
  409. }
  410. /**
  411. * 下载商品调价单导入模板
  412. *
  413. * @return
  414. */
  415. @API(title = "下载商品调价单导入模板", apiversion = R.ID20221026152404.v1.class)
  416. public String downloadItempriceadjustExcel() throws YosException {
  417. ExcelFactory excelFactory = new ExcelFactory("商品调价单导入模板");
  418. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("Sheet1");
  419. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  420. CellStyle style = xssfFWorkbook.createCellStyle();
  421. // 设置为文本格式,防止身份证号变成科学计数法
  422. DataFormat format = xssfFWorkbook.createDataFormat();
  423. style.setDataFormat(format.getFormat("@"));
  424. // 对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
  425. sheet.setDefaultColumnStyle(0, style);
  426. sheet.setDefaultColumnStyle(2, style);
  427. sheet.setDefaultColumnStyle(3, style);
  428. sheet.setDefaultColumnStyle(4, style);
  429. sheet.setDefaultColumnStyle(5, style);
  430. sheet.setDefaultColumnStyle(6, style);
  431. ExportExcel.setBatchDetailSheetColumn1(sheet);// 设置工作薄列宽
  432. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  433. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  434. XSSFCellStyle titleCellStyle3 = ExportExcel.createBodyCellStyle(xssfFWorkbook);
  435. XSSFCellStyle titleCellStyle4 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  436. ExportExcel.batchDetail(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, titleCellStyle4,
  437. xssfFWorkbook);// 写入标题
  438. Rows aa = uploadExcelToObs(excelFactory);
  439. String url = "";
  440. if (!aa.isEmpty()) {
  441. url = aa.get(0).getString("url");
  442. }
  443. return getSucReturnObject().setData(url).toString();
  444. }
  445. @API(title = "调价助手", apiversion = R.ID20221027095504.v1.class)
  446. @CACHEING_CLEAN(apiversions = {R.ID20221025160904.v1.class})
  447. public String itempriceadjustAides() throws YosException {
  448. ArrayList<String> sqlList = new ArrayList<>();
  449. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  450. Long manner = content.getLong("manner"); // 调价方式 0 按百分比 1 定额
  451. Long cardinality = content.getLong("cardinality"); //调价规则 0 下调 1上浮
  452. BigDecimal coefficient = content.getBigDecimal("coefficient"); //系数 下调的金额
  453. Rows rows = dbConnect.runSqlQuery("select status from sa_itempriceadjust where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  454. if (rows.isEmpty()) {
  455. return getErrReturnObject().setErrMsg("无效调价单").toString();
  456. }
  457. if (rows.get(0).getString("status").equals("审核")) {
  458. return getErrReturnObject().setErrMsg("审核状态无法快速调价").toString();
  459. }
  460. if (manner == 0 && cardinality == 0) {
  461. sqlList.add("update sa_itempriceadjust_itemprice set price=oldprice*(1-" + coefficient + ") where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid + " and oldprice>0");
  462. sqlList.add("update sa_itempriceadjust_items set marketprice=oldmarketprice*(1-" + coefficient + ") where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid + " and oldmarketprice>0");
  463. } else if (manner == 1 && cardinality == 0) {
  464. sqlList.add("update sa_itempriceadjust_itemprice set price=if(oldprice-" + coefficient + ">0,oldprice-" + coefficient + ",0) where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  465. sqlList.add("update sa_itempriceadjust_items set marketprice=if(oldmarketprice-" + coefficient + ">0,oldmarketprice-" + coefficient + ",0) where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  466. } else if (manner == 0 && cardinality == 1) {
  467. sqlList.add("update sa_itempriceadjust_itemprice set price=oldprice*(1+" + coefficient + ") where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid + " and oldprice>0");
  468. sqlList.add("update sa_itempriceadjust_items set marketprice=oldmarketprice*(1+" + coefficient + ") where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid + " and oldmarketprice>0");
  469. } else if (manner == 1 && cardinality == 1) {
  470. sqlList.add("update sa_itempriceadjust_itemprice set price=oldprice+" + coefficient + " where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  471. sqlList.add("update sa_itempriceadjust_items set marketprice=oldmarketprice+" + coefficient + " where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  472. }
  473. String sql = DataContrlLog.createLog(this, "sa_itempriceadjust", sa_itempriceadjustid, "修改", "商品调价:" + sa_itempriceadjustid).getSQL();
  474. sqlList.add(sql);
  475. dbConnect.runSqlUpdate(sqlList);
  476. return getSucReturnObject().toString();
  477. }
  478. @API(title = "查询可添加的商品", apiversion = R.ID20221028154004.v1.class)
  479. public String queryItem() throws YosException {
  480. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  481. StringBuffer where = new StringBuffer(" 1=1 ");
  482. if (content.containsKey("where")) {
  483. JSONObject whereObject = content.getJSONObject("where");
  484. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  485. where.append(" and (").append("t1.itemno like '%").append(whereObject.getString("condition")).append("%'");
  486. where.append(" or ").append("t1.itemname like '%").append(whereObject.getString("condition")).append("%')");
  487. }
  488. if (whereObject.containsKey("itemclassid") && 0 != whereObject.getLong("itemclassid")) {
  489. where.append("and t3.itemclassid in((with recursive itemclass as (select itemclassid from plm_itemclass where siteid = 'YOSTEST2' and itemclassid =").append(whereObject.getLong("itemclassid"))
  490. .append(" union all select t2.itemclassid from itemclass t1 inner join plm_itemclass t2 on t1.itemclassid = t2.parentid and t2.siteid = 'YOSTEST2') select itemclassid from itemclass))");
  491. }
  492. }
  493. SQLFactory sqlFactory = new SQLFactory(this, "查询可添加商品", pageSize, pageNumber, pageSorting);
  494. sqlFactory.addParameter("siteid", siteid);
  495. sqlFactory.addParameter_SQL("where", where);
  496. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  497. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  498. return getSucReturnObject().setData(rows).toString();
  499. }
  500. @API(title = "刷新原价", apiversion = R.ID202212003011904.v1.class)
  501. @CACHEING_CLEAN(apiversions = {R.ID20221025160904.v1.class})
  502. public String refreshPrice() throws YosException {
  503. ArrayList<String> sqlList = new ArrayList();
  504. Long sa_itempriceadjustid = content.getLong("sa_itempriceadjustid");
  505. Rows rows = dbConnect.runSqlQuery("select status from sa_itempriceadjust where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid);
  506. if (rows.isEmpty()) {
  507. return getErrReturnObject().setErrMsg("无效调价单").toString();
  508. }
  509. if (rows.get(0).getString("status").equals("审核")) {
  510. return getErrReturnObject().setErrMsg("审核状态不能刷新原价").toString();
  511. }
  512. RowsMap itemRowsMap = dbConnect.runSqlQuery("select sa_itempriceadjust_itemsid,itemid,oldmarketprice from sa_itempriceadjust_items where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid).toRowsMap("itemid");
  513. String replace = itemRowsMap.keySet().toString().replace("[", "(").replace("]", ")");
  514. RowsMap itemidRowsMap = dbConnect.runSqlQuery("select itemid,itemno,marketprice from plm_item where siteid='" + siteid + "' and itemid in " + replace).toRowsMap("itemid");
  515. for (String itemid : itemidRowsMap.keySet()) {
  516. if (itemidRowsMap.get(itemid).get(0).getBigDecimal("marketprice").compareTo(itemRowsMap.get(itemid).get(0).getBigDecimal("oldmarketprice")) != 0) {
  517. sqlList.add("update sa_itempriceadjust_items set oldmarketprice=" + itemidRowsMap.get(itemid).get(0).getBigDecimal("marketprice") + " where siteid='" + siteid + "' and sa_itempriceadjustid=" + sa_itempriceadjustid + " and itemid=" + itemid);
  518. }
  519. }
  520. SQLFactory sqlFactory = new SQLFactory(this, "查询指定商品原价");
  521. sqlFactory.addParameter("siteid", siteid);
  522. sqlFactory.addParameter_in("itemids", itemRowsMap.keySet().toArray());
  523. RowsMap itemOldpriceRowsMap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap(new String[]{"itemid", "pricegrade"});
  524. sqlFactory = new SQLFactory(this, "商品调价单商品价格列表查询");
  525. sqlFactory.addParameter("siteid", siteid);
  526. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  527. RowsMap itempriceadjustOldPriceRowsMap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap(new String[]{"itemid", "pricegrade"});
  528. Set<String> itemPricegradeSet = itemOldpriceRowsMap.keySet();
  529. for (String itemPricegrade : itemPricegradeSet) {
  530. Row row1 = itemOldpriceRowsMap.get(itemPricegrade).get(0);
  531. BigDecimal price = row1.getBigDecimal("price");
  532. if (itempriceadjustOldPriceRowsMap.containsKey(itemPricegrade)) {
  533. BigDecimal oldprice = itempriceadjustOldPriceRowsMap.get(itemPricegrade).get(0).getBigDecimal("oldprice");
  534. if (oldprice.compareTo(price) != 0) {
  535. sqlList.add("update sa_itempriceadjust_itemprice set oldprice=" + price + " where siteid='" + siteid + "' and sa_itempriceadjust_itempriceid=" + itempriceadjustOldPriceRowsMap.get(itemPricegrade).get(0).getLong("sa_itempriceadjust_itempriceid"));
  536. }
  537. } else {
  538. sqlFactory = new SQLFactory(this, "商品调价单商品价格明细新增");
  539. sqlFactory.addParameter("siteid", siteid);
  540. sqlFactory.addParameter("sa_itempriceadjust_itempriceid", createTableID("sa_itempriceadjust_itemprice"));
  541. sqlFactory.addParameter("sa_itempriceadjustid", sa_itempriceadjustid);
  542. sqlFactory.addParameter("sa_itempriceadjust_itemsid", itemRowsMap.get(row1.getString("itemid")).get(0).getLong("sa_itempriceadjust_itemsid"));
  543. sqlFactory.addParameter("pricegrade", row1.getLong("pricegrade"));
  544. sqlFactory.addParameter("price", price);
  545. sqlFactory.addParameter("oldprice", price);
  546. sqlList.add(sqlFactory.getSQL());
  547. }
  548. }
  549. String sql = DataContrlLog.createLog(this, "sa_itempriceadjust", sa_itempriceadjustid, "更新", "刷新原价:" + sa_itempriceadjustid).getSQL();
  550. sqlList.add(sql);
  551. dbConnect.runSqlUpdate(sqlList);
  552. return getSucReturnObject().toString();
  553. }
  554. public XSSFSheet addSheet(ExcelFactory excelFactory, String sheetname, Rows datarows,
  555. HashMap<String, String> titlemap) {
  556. ArrayList<String> keylist = datarows.getFieldList();
  557. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  558. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  559. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  560. XSSFFont font = xssfFWorkbook.createFont();
  561. font.setColor((short) 0xa);
  562. font.setFontHeightInPoints((short) 12);
  563. font.setBold(true);
  564. xssfCellStyle1.setFont(font);
  565. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  566. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  567. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  568. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  569. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  570. for (int n = 0; n < datarows.size(); n++) {
  571. Row row = datarows.get(n);
  572. XSSFRow datarow = sheet.createRow(n + 3);
  573. for (int i1 = 0; i1 < keylist.size(); i1++) {
  574. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  575. if (fieldclazztype == Integer.class) {
  576. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)).intValue());
  577. } else if (fieldclazztype == Long.class) {
  578. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  579. } else if (fieldclazztype == Float.class) {
  580. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  581. } else if (fieldclazztype == Double.class) {
  582. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  583. } else {
  584. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  585. }
  586. if (i1 == 7) {
  587. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  588. }
  589. }
  590. }
  591. return sheet;
  592. }
  593. }