promotion.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package restcontroller.webmanage.sale.promotion;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import org.apache.commons.lang.StringUtils;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import beans.data.BatchDeleteErr;
  8. import beans.datacontrllog.DataContrlLog;
  9. import common.Controller;
  10. import common.YosException;
  11. import common.annotation.API;
  12. import common.annotation.CACHEING;
  13. import common.annotation.CACHEING_CLEAN;
  14. import common.data.Row;
  15. import common.data.Rows;
  16. import common.data.RowsMap;
  17. import common.data.SQLFactory;
  18. import restcontroller.R;
  19. @API(title = "促销方案")
  20. public class promotion extends Controller {
  21. public promotion(JSONObject arg0) throws YosException {
  22. super(arg0);
  23. // TODO Auto-generated constructor stub
  24. }
  25. @API(title = "促销方案新增更新", apiversion = R.ID20221230144503.v1.class)
  26. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  27. public String insertormodify_promotion() throws YosException {
  28. ArrayList<String> sqlList = new ArrayList<>();
  29. // 表名
  30. String tableName = "sa_promotion";
  31. Long sa_promotionid = content.getLong("sa_promotionid");
  32. Long sa_accountclassid = content.getLong("sa_accountclassid");
  33. Long sa_brandid = content.getLong("sa_brandid");
  34. String promname = content.getStringValue("promname");
  35. String remarks = content.getStringValue("remarks");
  36. String type = content.getStringValue("type");
  37. //String tradefield = content.getStringValue("tradefield");
  38. JSONArray tradefield = content.getJSONArray("tradefield");
  39. BigDecimal orderminqty = content.getBigDecimal("orderminqty");
  40. BigDecimal orderaddqty = content.getBigDecimal("orderaddqty");
  41. BigDecimal orderminamount = content.getBigDecimal("orderminamount");
  42. String begdate = content.getStringValue("begdate");
  43. String enddate = content.getStringValue("enddate");
  44. Long authmethod = content.getLongValue("authmethod");
  45. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  46. if (sa_promotionid <= 0 || dbConnect
  47. .runSqlQuery("select sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid)
  48. .isEmpty()) {
  49. sa_promotionid = createTableID(tableName);
  50. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "新增", "促销方案新增成功").getSQL());
  51. } else {
  52. Rows rows = dbConnect
  53. .runSqlQuery("SELECT status from sa_promotion WHERE sa_promotionid = " + sa_promotionid);
  54. if (rows.isNotEmpty()) {
  55. if (rows.get(0).getString("status").equals("新建")) {
  56. sqlFactory = new SQLFactory(this, "促销方案更新");
  57. sqlList.add(
  58. DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "更新", "促销方案更新成功").getSQL());
  59. } else {
  60. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  61. }
  62. } else {
  63. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  64. }
  65. }
  66. sqlFactory.addParameter("siteid", siteid);
  67. sqlFactory.addParameter("userid", userid);
  68. sqlFactory.addParameter("username", username);
  69. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  70. // 促销方案号createBillCode("promotionbillno")
  71. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  72. sqlFactory.addParameter("promname", promname);
  73. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  74. sqlFactory.addParameter("sa_brandid", sa_brandid);
  75. sqlFactory.addParameter("type", type);
  76. sqlFactory.addParameter("tradefield", tradefield.toJSONString());
  77. sqlFactory.addParameter("orderminqty", orderminqty);
  78. sqlFactory.addParameter("orderaddqty", orderaddqty);
  79. sqlFactory.addParameter("orderminamount", orderminamount);
  80. sqlFactory.addParameter("sa_openpromotionid", content.getIntValue("sa_openpromotionid"));
  81. sqlFactory.addParameter("openamount", content.getBigDecimalValue("openamount"));
  82. sqlFactory.addParameter("isrelevancepromotion", content.getLongValue("isrelevancepromotion"));
  83. sqlFactory.addParameter("begdate", begdate);
  84. sqlFactory.addParameter("enddate", enddate);
  85. sqlFactory.addParameter("authmethod", authmethod);
  86. // 备注说明
  87. sqlFactory.addParameter("remarks", remarks);
  88. sqlList.add(sqlFactory.getSQL());
  89. dbConnect.runSqlUpdate(sqlList);
  90. content.put("sa_promotionid", sa_promotionid);
  91. return querypromotionMain();
  92. }
  93. @API(title = "促销方案详情", apiversion = R.ID20221230144703.v1.class)
  94. @CACHEING
  95. public String querypromotionMain() throws YosException {
  96. Long sa_promotionid = content.getLong("sa_promotionid");
  97. SQLFactory sqlFactory = new SQLFactory(this, "促销方案详情查询");
  98. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  99. sqlFactory.addParameter("siteid", siteid);
  100. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  101. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  102. //附件
  103. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  104. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  105. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  106. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  107. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  108. } else {
  109. row.put("tradefield", new JSONArray());
  110. }
  111. return getSucReturnObject().setData(row).toString();
  112. }
  113. @API(title = "查询促销方案列表", apiversion = R.ID20221230144803.v1.class)
  114. @CACHEING
  115. public String querypromotionList() throws YosException {
  116. StringBuffer where = new StringBuffer(" 1=1 ");
  117. if (content.containsKey("where")) {
  118. JSONObject whereObject = content.getJSONObject("where");
  119. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  120. where.append(" and(");
  121. where.append("t1.promnum like'%").append(whereObject.getString("condition")).append("%' ");
  122. where.append("or t1.promname like'%").append(whereObject.getString("condition")).append("%' ");
  123. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  124. where.append(")");
  125. }
  126. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  127. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  128. }
  129. }
  130. SQLFactory sqlFactory = new SQLFactory(this, "促销方案列表查询", pageSize, pageNumber, pageSorting);
  131. sqlFactory.addParameter("siteid", siteid);
  132. sqlFactory.addParameter_SQL("where", where);
  133. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  134. //附件
  135. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  136. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  137. for (Row row : rows) {
  138. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  139. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  140. } else {
  141. row.put("tradefield", new JSONArray());
  142. }
  143. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  144. }
  145. return getSucReturnObject().setData(rows).toString();
  146. }
  147. @API(title = "删除", apiversion = R.ID20221230144903.v1.class)
  148. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  149. public String delete() throws YosException {
  150. ArrayList<String> sqlList = new ArrayList<>();
  151. JSONArray sa_promotionids = content.getJSONArray("sa_promotionids");
  152. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotionids.size());
  153. for (Object o : sa_promotionids) {
  154. long sa_promotionid = Long.parseLong(o.toString());
  155. Rows RowsStatus = dbConnect.runSqlQuery("select sa_promotionid,status from sa_promotion where siteid='"
  156. + siteid + "' and sa_promotionid='" + sa_promotionid + "'");
  157. if (RowsStatus.isNotEmpty()) {
  158. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  159. batchDeleteErr.addErr(sa_promotionid, "非新建状态的促销方案无法删除");
  160. continue;
  161. }
  162. }
  163. sqlList.add("delete from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  164. sqlList.add(
  165. "delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  166. sqlList.add("delete from sa_promotion_itemgroup where siteid='" + siteid + "' and sa_promotionid="
  167. + sa_promotionid);
  168. sqlList.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotionid="
  169. + sa_promotionid);
  170. sqlList.add("delete from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid="
  171. + sa_promotionid);
  172. dbConnect.runSqlUpdate(sqlList);
  173. }
  174. return batchDeleteErr.getReturnObject().toString();
  175. }
  176. @API(title = "审核", apiversion = R.ID20221230145003.v1.class)
  177. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  178. public String check() throws YosException {
  179. Long sa_promotionid = content.getLong("sa_promotionid");
  180. Rows rows = dbConnect
  181. .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
  182. + sa_promotionid + "' and siteid='" + siteid + "'");
  183. if (rows.isEmpty()) {
  184. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  185. } else {
  186. if (!rows.get(0).getString("status").equals("新建")) {
  187. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非新建状态,无法审核")
  188. .toString();
  189. }
  190. }
  191. ArrayList<String> sqlList = new ArrayList<>();
  192. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案审核");
  193. sqlFactoryupdate.addParameter("siteid", siteid);
  194. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  195. sqlFactoryupdate.addParameter("checkby", username);
  196. sqlList.add(sqlFactoryupdate.getSQL());
  197. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "审核", "促销方案审核成功").getSQL());
  198. dbConnect.runSqlUpdate(sqlList);
  199. return getSucReturnObject().toString();
  200. }
  201. @API(title = "反审核", apiversion = R.ID20221230145103.v1.class)
  202. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  203. public String uncheck() throws YosException {
  204. Long sa_promotionid = content.getLong("sa_promotionid");
  205. Rows rows = dbConnect
  206. .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
  207. + sa_promotionid + "' and siteid='" + siteid + "'");
  208. if (rows.isEmpty()) {
  209. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  210. } else {
  211. if (!rows.get(0).getString("status").equals("审核")) {
  212. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非审核状态,无法反审核")
  213. .toString();
  214. }
  215. }
  216. ArrayList<String> sqlList = new ArrayList<>();
  217. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案反审核");
  218. sqlFactoryupdate.addParameter("siteid", siteid);
  219. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  220. sqlFactoryupdate.addParameter("checkby", username);
  221. sqlList.add(sqlFactoryupdate.getSQL());
  222. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反审核", "促销方案反审核成功").getSQL());
  223. dbConnect.runSqlUpdate(sqlList);
  224. return getSucReturnObject().toString();
  225. }
  226. @API(title = "发布反发布", apiversion = R.ID20221230145203.v1.class)
  227. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  228. public String recheck() throws YosException {
  229. Long sa_promotionid = content.getLong("sa_promotionid");
  230. boolean isrecheck = content.getBooleanValue("isrecheck");
  231. Rows rows = dbConnect
  232. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  233. + sa_promotionid + "' and siteid='" + siteid + "'");
  234. for (Row row : rows) {
  235. if (isrecheck) {
  236. if (!row.getString("status").equals("审核")) {
  237. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非审核状态,无法发布")
  238. .toString();
  239. }
  240. } else {
  241. if (!row.getString("status").equals("发布")) {
  242. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法反发布")
  243. .toString();
  244. }
  245. }
  246. }
  247. ArrayList<String> sqlList = new ArrayList<>();
  248. SQLFactory sqlFactoryupdate;
  249. if (isrecheck) {
  250. sqlFactoryupdate = new SQLFactory(this, "促销方案发布");
  251. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "发布", "促销方案发布成功").getSQL());
  252. } else {
  253. sqlFactoryupdate = new SQLFactory(this, "促销方案反发布");
  254. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反发布", "促销方案反发布成功").getSQL());
  255. }
  256. sqlFactoryupdate.addParameter("siteid", siteid);
  257. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  258. sqlFactoryupdate.addParameter("sendby", username);
  259. sqlList.add(sqlFactoryupdate.getSQL());
  260. dbConnect.runSqlUpdate(sqlList);
  261. return getSucReturnObject().toString();
  262. }
  263. @API(title = "关闭反关闭", apiversion = R.ID20230102095403.v1.class)
  264. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  265. public String close() throws YosException {
  266. Long sa_promotionid = content.getLong("sa_promotionid");
  267. boolean isclose = content.getBooleanValue("isclose");
  268. Rows rows = dbConnect
  269. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  270. + sa_promotionid + "' and siteid='" + siteid + "'");
  271. for (Row row : rows) {
  272. if (isclose) {
  273. if (!row.getString("status").equals("发布")) {
  274. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法关闭")
  275. .toString();
  276. }
  277. } else {
  278. if (!row.getString("status").equals("关闭")) {
  279. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非关闭状态,无法反关闭")
  280. .toString();
  281. }
  282. }
  283. }
  284. ArrayList<String> sqlList = new ArrayList<>();
  285. SQLFactory sqlFactoryupdate;
  286. if (isclose) {
  287. sqlFactoryupdate = new SQLFactory(this, "促销方案关闭");
  288. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "关闭", "促销方案关闭成功").getSQL());
  289. } else {
  290. sqlFactoryupdate = new SQLFactory(this, "促销方案反关闭");
  291. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反关闭", "促销方案反关闭成功").getSQL());
  292. }
  293. sqlFactoryupdate.addParameter("siteid", siteid);
  294. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  295. sqlFactoryupdate.addParameter("closeby", username);
  296. sqlList.add(sqlFactoryupdate.getSQL());
  297. dbConnect.runSqlUpdate(sqlList);
  298. return getSucReturnObject().toString();
  299. }
  300. @API(title = "切换授权方式", apiversion = R.ID20230509100504.v1.class)
  301. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class})
  302. public String updateAuthMethod() throws YosException {
  303. Long sa_promotionid = content.getLong("sa_promotionid");
  304. Rows rows = dbConnect.runSqlQuery("select sa_promotionid,status,authmethod from sa_promotion where sa_promotionid ='" + sa_promotionid + "' and siteid='" + siteid + "'");
  305. if (rows.isEmpty()) {
  306. return getErrReturnObject().setErrMsg("无效促销方案").toString();
  307. }
  308. if (!rows.get(0).getString("status").equals("新建")) {
  309. return getErrReturnObject().setErrMsg("非新建状态不能修改授权方式").toString();
  310. }
  311. if (rows.get(0).getLong("authmethod") == 0) {
  312. dbConnect.runSqlUpdate("update sa_promotion set authmethod=1 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  313. } else {
  314. dbConnect.runSqlUpdate("update sa_promotion set authmethod=0 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  315. }
  316. return getSucReturnObject().toString();
  317. }
  318. }