promotion.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package restcontroller.webmanage.sale.promotion;
  2. import java.math.BigDecimal;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import beans.accountbalance.Accountbalance;
  6. import beans.accountbalance.CashbillEntity;
  7. import common.data.*;
  8. import org.apache.commons.lang.StringUtils;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import beans.data.BatchDeleteErr;
  12. import beans.datacontrllog.DataContrlLog;
  13. import common.Controller;
  14. import common.YosException;
  15. import common.annotation.API;
  16. import common.annotation.CACHEING;
  17. import common.annotation.CACHEING_CLEAN;
  18. import restcontroller.R;
  19. import restcontroller.webmanage.sale.rebateaccount.rebateaccounthead;
  20. @API(title = "促销方案")
  21. public class promotion extends Controller {
  22. public promotion(JSONObject arg0) throws YosException {
  23. super(arg0);
  24. // TODO Auto-generated constructor stub
  25. }
  26. @API(title = "促销方案新增更新", apiversion = R.ID20221230144503.v1.class, intervaltime = 200)
  27. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  28. public String insertormodify_promotion() throws YosException {
  29. ArrayList<String> sqlList = new ArrayList<>();
  30. // 表名
  31. String tableName = "sa_promotion";
  32. Long sa_promotionid = content.getLong("sa_promotionid");
  33. Long sa_accountclassid = content.getLong("sa_accountclassid");
  34. Long sa_brandid = content.getLong("sa_brandid");
  35. String promname = content.getStringValue("promname");
  36. String remarks = content.getStringValue("remarks");
  37. String type = content.getStringValue("type");
  38. //String tradefield = content.getStringValue("tradefield");
  39. JSONArray tradefield = content.getJSONArray("tradefield");
  40. BigDecimal orderminqty = content.getBigDecimal("orderminqty");
  41. BigDecimal orderaddqty = content.getBigDecimal("orderaddqty");
  42. BigDecimal orderminamount = content.getBigDecimal("orderminamount");
  43. String begdate = content.getStringValue("begdate");
  44. String enddate = content.getStringValue("enddate");
  45. Long authmethod = content.getLongValue("authmethod");
  46. boolean isauth = content.getBooleanValue("isauth");
  47. BigDecimal associationamount = content.getBigDecimalValue("associationamount");
  48. Long associationaccountclassid = content.getLongValue("associationaccountclassid");
  49. BigDecimal rebateratio = content.getBigDecimalValue("rebateratio");
  50. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  51. if (sa_promotionid <= 0 || dbConnect
  52. .runSqlQuery("select sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid)
  53. .isEmpty()) {
  54. sa_promotionid = createTableID(tableName);
  55. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "新增", "促销方案新增成功").getSQL());
  56. } else {
  57. Rows rows = dbConnect
  58. .runSqlQuery("SELECT status,authmethod from sa_promotion WHERE sa_promotionid = " + sa_promotionid);
  59. if (rows.isNotEmpty()) {
  60. if (rows.get(0).getString("status").equals("新建")) {
  61. sqlFactory = new SQLFactory(this, "促销方案更新");
  62. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "更新", "促销方案更新成功").getSQL());
  63. if (rows.get(0).getLong("authmethod") != authmethod) {
  64. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  65. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  66. }
  67. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  68. }
  69. } else {
  70. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  71. }
  72. } else {
  73. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  74. }
  75. }
  76. sqlFactory.addParameter("siteid", siteid);
  77. sqlFactory.addParameter("userid", userid);
  78. sqlFactory.addParameter("username", username);
  79. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  80. // 促销方案号createBillCode("promotionbillno")
  81. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  82. sqlFactory.addParameter("promname", promname);
  83. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  84. sqlFactory.addParameter("sa_brandid", sa_brandid);
  85. sqlFactory.addParameter("type", type);
  86. sqlFactory.addParameter("tradefield", tradefield.toJSONString());
  87. sqlFactory.addParameter("orderminqty", orderminqty);
  88. sqlFactory.addParameter("orderaddqty", orderaddqty);
  89. sqlFactory.addParameter("orderminamount", orderminamount);
  90. sqlFactory.addParameter("sa_openpromotionid", content.getIntValue("sa_openpromotionid"));
  91. sqlFactory.addParameter("openamount", content.getBigDecimalValue("openamount"));
  92. sqlFactory.addParameter("isrelevancepromotion", content.getLongValue("isrelevancepromotion"));
  93. sqlFactory.addParameter("begdate", begdate);
  94. sqlFactory.addParameter("enddate", enddate);
  95. sqlFactory.addParameter("authmethod", authmethod);
  96. sqlFactory.addParameter("isauth", isauth);
  97. sqlFactory.addParameter("associationamount", associationamount);
  98. sqlFactory.addParameter("associationaccountclassid", associationaccountclassid);
  99. sqlFactory.addParameter("rebateratio", rebateratio);
  100. // 备注说明
  101. sqlFactory.addParameter("remarks", remarks);
  102. sqlList.add(sqlFactory.getSQL());
  103. dbConnect.runSqlUpdate(sqlList);
  104. content.put("sa_promotionid", sa_promotionid);
  105. return querypromotionMain();
  106. }
  107. @API(title = "促销方案详情", apiversion = R.ID20221230144703.v1.class)
  108. @CACHEING
  109. public String querypromotionMain() throws YosException {
  110. Long sa_promotionid = content.getLong("sa_promotionid");
  111. SQLFactory sqlFactory = new SQLFactory(this, "促销方案详情查询");
  112. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  113. sqlFactory.addParameter("siteid", siteid);
  114. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  115. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  116. //附件
  117. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  118. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  119. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  120. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  121. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  122. } else {
  123. row.put("tradefield", new JSONArray());
  124. }
  125. return getSucReturnObject().setData(row).toString();
  126. }
  127. @API(title = "一键复制促销方案", apiversion = R.ID20230713101303.v1.class)
  128. @CACHEING_CLEAN(apiClass = {promotion.class, promotionSalearea.class, promotionAuth.class, promotionItems.class, promotionItemPrice.class})
  129. public String copyPromotion() throws YosException {
  130. Long sa_promotionid = content.getLong("sa_promotionid");
  131. ArrayList<String> sqlList = new ArrayList<>();
  132. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_promotion WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  133. if (rows.isEmpty()) {
  134. return getErrReturnObject().setErrMsg("促销方案不存在").toString();
  135. }
  136. Row row = rows.get(0);
  137. Long sa_promotion_copy = createTableID("sa_promotion");
  138. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  139. sqlFactory.addParameter("siteid", siteid);
  140. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  141. sqlFactory.addParameter("userid", userid);
  142. sqlFactory.addParameter("username", username);
  143. //促销方案名称
  144. sqlFactory.addParameter("promname", row.getString("promname") + "复制");
  145. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  146. sqlFactory.addParameter("sa_accountclassid", row.getLong("sa_accountclassid"));
  147. sqlFactory.addParameter("sa_brandid", row.getLong("sa_brandid"));
  148. sqlFactory.addParameter("type", row.getString("type"));
  149. sqlFactory.addParameter("tradefield", row.getString("tradefield"));
  150. sqlFactory.addParameter("orderminqty", row.getBigDecimal("orderminqty"));
  151. sqlFactory.addParameter("orderaddqty", row.getBigDecimal("orderaddqty"));
  152. sqlFactory.addParameter("orderminamount", row.getBigDecimal("orderminamount"));
  153. sqlFactory.addParameter("sa_openpromotionid", row.getLong("sa_openpromotionid"));
  154. sqlFactory.addParameter("openamount", row.getBigDecimal("openamount"));
  155. sqlFactory.addParameter("isrelevancepromotion", row.getString("isrelevancepromotion"));
  156. sqlFactory.addParameter("associationamount", row.getString("associationamount"));
  157. sqlFactory.addParameter("associationaccountclassid", row.getString("associationaccountclassid"));
  158. sqlFactory.addParameter("begdate", row.getString("begdate"));
  159. sqlFactory.addParameter("enddate", row.getString("enddate"));
  160. sqlFactory.addParameter("authmethod", row.getString("authmethod"));
  161. sqlFactory.addParameter("associationamount", 0);
  162. sqlFactory.addParameter("associationaccountclassid", 0);
  163. sqlFactory.addParameter("rebateratio", 0);
  164. // 备注说明
  165. sqlFactory.addParameter("remarks", row.getString("remarks"));
  166. sqlList.add(sqlFactory.getSQL());
  167. //复制商品
  168. Rows promotionitemsRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_items WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  169. //复制商品
  170. Rows promotionitemspriceRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_itemprice WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  171. RowsMap promotionitemspriceRowsMap = promotionitemspriceRows.toRowsMap("sa_promotion_itemsid");
  172. for (Row promotionitemsRow : promotionitemsRows) {
  173. sqlFactory = new SQLFactory(this, "促销方案商品新增");
  174. sqlFactory.addParameter("siteid", siteid);
  175. long sa_promotion_itemsidcopy = createTableID("sa_promotion_items");
  176. sqlFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  177. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  178. sqlFactory.addParameter("sa_promotion_itemgroupid", promotionitemsRow.getLong("sa_promotion_itemgroupid"));
  179. sqlFactory.addParameter("groupqty", promotionitemsRow.getBigDecimal("groupqty"));
  180. sqlFactory.addParameter("itemid", promotionitemsRow.getLong("itemid"));
  181. sqlFactory.addParameter("orderaddqty", promotionitemsRow.getBigDecimal("orderaddqty"));
  182. sqlFactory.addParameter("orderminqty", promotionitemsRow.getBigDecimal("orderminqty"));
  183. sqlFactory.addParameter("islimit", promotionitemsRow.getLong("islimit"));
  184. sqlFactory.addParameter("signaturecode", promotionitemsRow.getString("signaturecode"));
  185. sqlList.add(sqlFactory.getSQL());
  186. if (promotionitemspriceRowsMap.containsKey(promotionitemsRow.getString("sa_promotion_itemsid")) && promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid")).isNotEmpty()) {
  187. for (Row rowdetail : promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid"))) {
  188. SQLFactory itempriceFactory = new SQLFactory(this, "促销方案商品价格新增");
  189. itempriceFactory.addParameter("siteid", siteid);
  190. itempriceFactory.addParameter("sa_promotion_itempriceid",
  191. createTableID("sa_promotion_itemprice"));
  192. itempriceFactory.addParameter("sa_promotionid", sa_promotion_copy);
  193. itempriceFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  194. itempriceFactory.addParameter("itemid", rowdetail.getLong("itemid"));
  195. itempriceFactory.addParameter("pricegrade", rowdetail.getBigDecimal("pricegrade"));
  196. itempriceFactory.addParameter("oldprice", rowdetail.getBigDecimal("oldprice"));
  197. itempriceFactory.addParameter("price", rowdetail.getBigDecimal("price"));
  198. sqlList.add(itempriceFactory.getSQL());
  199. }
  200. }
  201. }
  202. dbConnect.runSqlUpdate(sqlList);
  203. content.put("sa_promotionid", sa_promotion_copy);
  204. return querypromotionMain();
  205. }
  206. @API(title = "查询促销方案列表", apiversion = R.ID20221230144803.v1.class)
  207. @CACHEING
  208. public String querypromotionList() throws YosException {
  209. StringBuffer where = new StringBuffer(" 1=1 ");
  210. if (content.containsKey("where")) {
  211. JSONObject whereObject = content.getJSONObject("where");
  212. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  213. where.append(" and(");
  214. where.append("t1.promnum like'%").append(whereObject.getString("condition")).append("%' ");
  215. where.append("or t1.promname like'%").append(whereObject.getString("condition")).append("%' ");
  216. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  217. where.append(")");
  218. }
  219. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  220. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  221. }
  222. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  223. where.append(" and DATE_FORMAT(t1.begdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  224. }
  225. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  226. where.append(" and DATE_FORMAT(t1.enddate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  227. }
  228. }
  229. // SQLFactory sqlFactory = new SQLFactory(this, "促销方案列表查询", pageSize, pageNumber, pageSorting);
  230. // sqlFactory.addParameter("siteid", siteid);
  231. // sqlFactory.addParameter_SQL("where", where);
  232. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  233. QuerySQL querySQL = queryPomotionList(where.toString());
  234. querySQL.setOrderBy(pageSorting);
  235. querySQL.setPage(pageSize, pageNumber);
  236. Rows rows = querySQL.query();
  237. //附件
  238. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  239. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  240. for (Row row : rows) {
  241. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  242. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  243. } else {
  244. row.put("tradefield", new JSONArray());
  245. }
  246. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  247. }
  248. return getSucReturnObject().setData(rows).toString();
  249. }
  250. public QuerySQL queryPomotionList(String where) throws YosException {
  251. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_promotion");
  252. querySQL.setTableAlias("t1");
  253. querySQL.addJoinTable(JOINTYPE.left, "sa_brand", "t2", "t1.sa_brandid = t2.sa_brandid AND t1.siteid = t2.siteid",
  254. "brandname");
  255. querySQL.addJoinTable(JOINTYPE.left, "sa_accountclass", "t3", "t1.sa_accountclassid = t3.sa_accountclassid AND t1.siteid = t3.siteid",
  256. "accountno", "accountname");
  257. querySQL.setWhere("t1.siteid", siteid);
  258. querySQL.setWhere(where);
  259. return querySQL;
  260. }
  261. @API(title = "查询促销方案销售排行(经销商)", apiversion = R.ID20231007101703.v1.class)
  262. @CACHEING
  263. public String queryAgentSalesRanking() throws YosException {
  264. long sa_promotionid =content.getLongValue("sa_promotionid");
  265. StringBuffer where = new StringBuffer(" 1=1 ");
  266. if (content.containsKey("where")) {
  267. JSONObject whereObject = content.getJSONObject("where");
  268. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  269. where.append(" and(");
  270. where.append("t2.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  271. where.append("or t3.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  272. where.append(")");
  273. }
  274. }
  275. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(经销商)", pageSize, pageNumber, pageSorting);
  276. sqlFactory.addParameter("siteid", siteid);
  277. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  278. sqlFactory.addParameter_SQL("where", where);
  279. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  280. return getSucReturnObject().setData(rows).toString();
  281. }
  282. @API(title = "查询促销方案销售排行(商品)", apiversion = R.ID20231007101803.v1.class)
  283. @CACHEING
  284. public String queryItemSalesRanking() throws YosException {
  285. long sa_promotionid =content.getLongValue("sa_promotionid");
  286. StringBuffer where = new StringBuffer(" 1=1 ");
  287. if (content.containsKey("where")) {
  288. JSONObject whereObject = content.getJSONObject("where");
  289. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  290. where.append(" and(");
  291. where.append("t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  292. where.append("or t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  293. where.append(")");
  294. }
  295. }
  296. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(商品)", pageSize, pageNumber, pageSorting);
  297. sqlFactory.addParameter("siteid", siteid);
  298. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  299. sqlFactory.addParameter_SQL("where", where);
  300. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  301. // //附件
  302. // ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  303. // RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  304. // for (Row row : rows) {
  305. // if (!StringUtils.isBlank(row.getString("tradefield"))) {
  306. // row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  307. // } else {
  308. // row.put("tradefield", new JSONArray());
  309. // }
  310. // row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  311. // }
  312. return getSucReturnObject().setData(rows).toString();
  313. }
  314. @API(title = "删除", apiversion = R.ID20221230144903.v1.class)
  315. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  316. public String delete() throws YosException {
  317. ArrayList<String> sqlList = new ArrayList<>();
  318. JSONArray sa_promotionids = content.getJSONArray("sa_promotionids");
  319. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotionids.size());
  320. for (Object o : sa_promotionids) {
  321. long sa_promotionid = Long.parseLong(o.toString());
  322. Rows RowsStatus = dbConnect.runSqlQuery("select sa_promotionid,status from sa_promotion where siteid='"
  323. + siteid + "' and sa_promotionid='" + sa_promotionid + "'");
  324. Rows Rowsdetail = dbConnect.runSqlQuery("select sa_promotion_authid from sa_promotion_auth where siteid='"
  325. + siteid + "' and sa_promotionid='" + sa_promotionid + "' and saledamount>0");
  326. if (RowsStatus.isNotEmpty()) {
  327. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  328. batchDeleteErr.addErr(sa_promotionid, "非新建状态的促销方案无法删除");
  329. continue;
  330. }
  331. }
  332. if (Rowsdetail.isNotEmpty()) {
  333. batchDeleteErr.addErr(sa_promotionid, "存在已购金额的授权经销商禁止删除促销活动");
  334. continue;
  335. }
  336. sqlList.add("delete from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  337. sqlList.add(
  338. "delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  339. sqlList.add("delete from sa_promotion_itemgroup where siteid='" + siteid + "' and sa_promotionid="
  340. + sa_promotionid);
  341. sqlList.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotionid="
  342. + sa_promotionid);
  343. sqlList.add("delete from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid="
  344. + sa_promotionid);
  345. dbConnect.runSqlUpdate(sqlList);
  346. }
  347. return batchDeleteErr.getReturnObject().toString();
  348. }
  349. @API(title = "审核", apiversion = R.ID20221230145003.v1.class)
  350. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  351. public String check() throws YosException {
  352. Long sa_promotionid = content.getLong("sa_promotionid");
  353. Rows rows = dbConnect
  354. .runSqlQuery("select sa_promotionid,status,promnum,promname,type,associationaccountclassid,associationamount from sa_promotion where sa_promotionid ='"
  355. + sa_promotionid + "' and siteid='" + siteid + "'");
  356. if (rows.isEmpty()) {
  357. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  358. } else {
  359. if (!rows.get(0).getString("status").equals("新建")) {
  360. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非新建状态,无法审核")
  361. .toString();
  362. }
  363. if(rows.get(0).getString("type").equals("返利促销")){
  364. if(rows.get(0).getLong("associationaccountclassid")==0 || rows.get(0).getBigDecimal("associationamount").compareTo(BigDecimal.ZERO)==0){
  365. return getErrReturnObject().setErrMsg("返利促销必须选择关联支付账户和关联支付金额").toString();
  366. }
  367. Rows islimitRows = dbConnect.runSqlQuery("select ifnull(islimit,0) islimit,count(1) from sa_promotion_auth where siteid='"+siteid+"' and sa_promotionid="+sa_promotionid+" group by ifnull(islimit,0)");
  368. if(islimitRows.isEmpty()){
  369. return getErrReturnObject().setErrMsg("返利促销必须授权经销商").toString();
  370. } else if(islimitRows.size()>1 || (islimitRows.size()==1 && !islimitRows.get(0).getBoolean("islimit"))){
  371. return getErrReturnObject().setErrMsg("返利促销授权的经销商必须都为限购").toString();
  372. }
  373. }
  374. }
  375. ArrayList<String> sqlList = new ArrayList<>();
  376. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案审核");
  377. sqlFactoryupdate.addParameter("siteid", siteid);
  378. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  379. sqlFactoryupdate.addParameter("checkby", username);
  380. sqlList.add(sqlFactoryupdate.getSQL());
  381. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "审核", "促销方案审核成功").getSQL());
  382. dbConnect.runSqlUpdate(sqlList);
  383. return getSucReturnObject().toString();
  384. }
  385. @API(title = "反审核", apiversion = R.ID20221230145103.v1.class)
  386. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  387. public String uncheck() throws YosException {
  388. Long sa_promotionid = content.getLong("sa_promotionid");
  389. Rows rows = dbConnect
  390. .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
  391. + sa_promotionid + "' and siteid='" + siteid + "'");
  392. if (rows.isEmpty()) {
  393. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  394. } else {
  395. if (!rows.get(0).getString("status").equals("审核")) {
  396. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非审核状态,无法反审核")
  397. .toString();
  398. }
  399. // if(!dbConnect.runSqlQuery("select * from sa_order where sa_promotionid="+sa_promotionid+" and siteid='"+siteid+"'").isEmpty()){
  400. // return getErrReturnObject().setErrMsg("存在已使用方案的订单,无法反审核")
  401. // .toString();
  402. // }
  403. }
  404. ArrayList<String> sqlList = new ArrayList<>();
  405. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案反审核");
  406. sqlFactoryupdate.addParameter("siteid", siteid);
  407. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  408. sqlFactoryupdate.addParameter("checkby", username);
  409. sqlList.add(sqlFactoryupdate.getSQL());
  410. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反审核", "促销方案反审核成功").getSQL());
  411. dbConnect.runSqlUpdate(sqlList);
  412. return getSucReturnObject().toString();
  413. }
  414. @API(title = "发布反发布", apiversion = R.ID20221230145203.v1.class)
  415. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  416. public String recheck() throws YosException {
  417. Long sa_promotionid = content.getLong("sa_promotionid");
  418. boolean isrecheck = content.getBooleanValue("isrecheck");
  419. Rows rows = dbConnect
  420. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  421. + sa_promotionid + "' and siteid='" + siteid + "'");
  422. for (Row row : rows) {
  423. if (isrecheck) {
  424. if (!row.getString("status").equals("审核")) {
  425. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非审核状态,无法发布")
  426. .toString();
  427. }
  428. } else {
  429. if (!row.getString("status").equals("发布")) {
  430. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法反发布")
  431. .toString();
  432. }
  433. }
  434. }
  435. ArrayList<String> sqlList = new ArrayList<>();
  436. SQLFactory sqlFactoryupdate;
  437. if (isrecheck) {
  438. sqlFactoryupdate = new SQLFactory(this, "促销方案发布");
  439. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "发布", "促销方案发布成功").getSQL());
  440. } else {
  441. sqlFactoryupdate = new SQLFactory(this, "促销方案反发布");
  442. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反发布", "促销方案反发布成功").getSQL());
  443. }
  444. sqlFactoryupdate.addParameter("siteid", siteid);
  445. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  446. sqlFactoryupdate.addParameter("sendby", username);
  447. sqlList.add(sqlFactoryupdate.getSQL());
  448. dbConnect.runSqlUpdate(sqlList);
  449. return getSucReturnObject().toString();
  450. }
  451. @API(title = "关闭反关闭", apiversion = R.ID20230102095403.v1.class)
  452. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  453. public String close() throws YosException {
  454. Long sa_promotionid = content.getLong("sa_promotionid");
  455. boolean isclose = content.getBooleanValue("isclose");
  456. Rows rows = dbConnect
  457. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  458. + sa_promotionid + "' and siteid='" + siteid + "'");
  459. for (Row row : rows) {
  460. if (isclose) {
  461. if (!row.getString("status").equals("发布")) {
  462. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法关闭")
  463. .toString();
  464. }
  465. } else {
  466. if (!row.getString("status").equals("关闭")) {
  467. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非关闭状态,无法反关闭")
  468. .toString();
  469. }
  470. }
  471. }
  472. ArrayList<String> sqlList = new ArrayList<>();
  473. SQLFactory sqlFactoryupdate;
  474. if (isclose) {
  475. sqlFactoryupdate = new SQLFactory(this, "促销方案关闭");
  476. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "关闭", "促销方案关闭成功").getSQL());
  477. } else {
  478. sqlFactoryupdate = new SQLFactory(this, "促销方案反关闭");
  479. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反关闭", "促销方案反关闭成功").getSQL());
  480. }
  481. sqlFactoryupdate.addParameter("siteid", siteid);
  482. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  483. sqlFactoryupdate.addParameter("closeby", username);
  484. sqlList.add(sqlFactoryupdate.getSQL());
  485. dbConnect.runSqlUpdate(sqlList);
  486. return getSucReturnObject().toString();
  487. }
  488. @API(title = "切换授权方式", apiversion = R.ID20230509100504.v1.class)
  489. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20220103140003.v1.class})
  490. public String updateAuthMethod() throws YosException {
  491. Long sa_promotionid = content.getLong("sa_promotionid");
  492. Rows rows = dbConnect.runSqlQuery("select sa_promotionid,status,authmethod from sa_promotion where sa_promotionid ='" + sa_promotionid + "' and siteid='" + siteid + "'");
  493. if (rows.isEmpty()) {
  494. return getErrReturnObject().setErrMsg("无效促销方案").toString();
  495. }
  496. if (!rows.get(0).getString("status").equals("新建")) {
  497. return getErrReturnObject().setErrMsg("非新建状态不能修改授权方式").toString();
  498. }
  499. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  500. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  501. }
  502. ArrayList<String> sqlList = new ArrayList<>();
  503. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  504. if (rows.get(0).getLong("authmethod") == 0) {
  505. sqlList.add("update sa_promotion set authmethod=1 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  506. } else {
  507. sqlList.add("update sa_promotion set authmethod=0 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  508. }
  509. dbConnect.runSqlUpdate(sqlList);
  510. return getSucReturnObject().toString();
  511. }
  512. @API(title = "结算", apiversion = R.ID20230826091303.v1.class)
  513. @CACHEING_CLEAN(apiClass = {promotionAuth.class,promotion.class})
  514. public String settlement() throws YosException {
  515. Long sa_promotion_authid = content.getLong("sa_promotion_authid");
  516. ArrayList<String> sqlList = new ArrayList<>();
  517. Rows promotionauthRows = dbConnect.runSqlQuery(
  518. "select t1.*,t2.type,t2.rebateratio,t2.promname from sa_promotion_auth t1 inner join sa_promotion t2 on t1.siteid=t2.siteid and t1.sa_promotionid=t2.sa_promotionid where t1.sa_promotion_authid ='"
  519. + sa_promotion_authid + "' and t1.siteid='" + siteid + "'");
  520. if(promotionauthRows.isNotEmpty()){
  521. if(!promotionauthRows.get(0).getString("type").equals("返利促销")){
  522. return getErrReturnObject().setErrMsg("返利促销方案才能返利结算").toString();
  523. }
  524. if(promotionauthRows.get(0).getBoolean("settlestatus")){
  525. return getErrReturnObject().setErrMsg("该经销商结算已经结算成功,无法再次结算").toString();
  526. }
  527. }else{
  528. return getErrReturnObject().setErrMsg("该记录不存在").toString();
  529. }
  530. long sa_promotionid=promotionauthRows.get(0).getLong("sa_promotionid");
  531. long sys_enterpriseid=promotionauthRows.get(0).getLong("sys_enterpriseid");
  532. BigDecimal saledamount=promotionauthRows.get(0).getBigDecimal("saledamount");
  533. BigDecimal rebateratio=promotionauthRows.get(0).getBigDecimal("rebateratio");
  534. System.out.println(saledamount);
  535. System.out.println(rebateratio);
  536. String agentnum="";
  537. Rows enterpriserows = dbConnect.runSqlQuery("select * from sa_agents where sys_enterpriseid ='"+sys_enterpriseid+"' and siteid='"+siteid+"'");
  538. if(enterpriserows.isNotEmpty()){
  539. agentnum=enterpriserows.get(0).getString("agentnum");
  540. }
  541. //查询经销商未关闭的促销订单
  542. Rows rows = dbConnect.runSqlQuery("select * from sa_order where type ='促销订单' and deleted!=1 and sa_promotionid="+sa_promotionid+" and sys_enterpriseid ='"+sys_enterpriseid+"' and status not in('关闭','手工关闭') and siteid='"+siteid+"'");
  543. if (rows.isNotEmpty()) {
  544. return getErrReturnObject().setErrMsg("存在未关闭的返利促销订单,无法结算").toString();
  545. } else {
  546. Rows accountclassrows = dbConnect.runSqlQuery("select sa_accountclassid from sa_accountclass where accountname ='返利金账户' and siteid='"+siteid+"'");
  547. if(accountclassrows.isNotEmpty()){
  548. long sa_accountclassid=accountclassrows.get(0).getLong("sa_accountclassid");
  549. CashbillEntity entity = new CashbillEntity();
  550. entity.setAmount(saledamount.multiply(rebateratio));
  551. entity.setOwnerid(sa_promotion_authid);
  552. entity.setOwnertable("sa_promotion_auth");
  553. entity.setSource("返利");
  554. entity.setSourcenote("经销商【"+agentnum+"】,【"+promotionauthRows.get(0).getString("promname")+"】方案返利促销");
  555. entity.setRemarks("【"+promotionauthRows.get(0).getString("promname")+"】方案返利促销");
  556. sqlList.addAll(Accountbalance.createCashbillIncome(this, sys_enterpriseid,sa_accountclassid,entity,true,true));
  557. sqlList.add("update sa_promotion_auth set settlestatus=1 where sa_promotion_authid="+sa_promotion_authid+" and siteid='"+siteid+"'");
  558. }
  559. }
  560. dbConnect.runSqlUpdate(sqlList);
  561. return getSucReturnObject().toString();
  562. }
  563. }