promotion.java 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. package restcontroller.webmanage.sale.promotion;
  2. import java.math.BigDecimal;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import beans.accountbalance.Accountbalance;
  7. import beans.accountbalance.CashbillEntity;
  8. import beans.itemclass.ItemClass;
  9. import common.BaseClass;
  10. import common.data.*;
  11. import org.apache.commons.lang.StringUtils;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.alibaba.fastjson.JSONObject;
  14. import beans.data.BatchDeleteErr;
  15. import beans.datacontrllog.DataContrlLog;
  16. import common.Controller;
  17. import common.YosException;
  18. import common.annotation.API;
  19. import common.annotation.CACHEING;
  20. import common.annotation.CACHEING_CLEAN;
  21. import restcontroller.R;
  22. import restcontroller.webmanage.sale.rebateaccount.rebateaccounthead;
  23. @API(title = "促销方案")
  24. public class promotion extends Controller {
  25. public promotion(JSONObject arg0) throws YosException {
  26. super(arg0);
  27. // TODO Auto-generated constructor stub
  28. }
  29. @API(title = "促销方案新增更新", apiversion = R.ID20221230144503.v1.class, intervaltime = 200)
  30. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  31. public String insertormodify_promotion() throws YosException {
  32. ArrayList<String> sqlList = new ArrayList<>();
  33. // 表名
  34. String tableName = "sa_promotion";
  35. Long sa_promotionid = content.getLong("sa_promotionid");
  36. Long sa_accountclassid = content.getLong("sa_accountclassid");
  37. Long sa_brandid = content.getLong("sa_brandid");
  38. String promname = content.getStringValue("promname");
  39. String remarks = content.getStringValue("remarks");
  40. String type = content.getStringValue("type");
  41. //String tradefield = content.getStringValue("tradefield");
  42. JSONArray tradefield = content.getJSONArray("tradefield");
  43. BigDecimal orderminqty = content.getBigDecimal("orderminqty");
  44. BigDecimal orderaddqty = content.getBigDecimal("orderaddqty");
  45. BigDecimal orderminamount = content.getBigDecimal("orderminamount");
  46. String begdate = content.getStringValue("begdate");
  47. String enddate = content.getStringValue("enddate");
  48. Long authmethod = content.getLongValue("authmethod");
  49. boolean isauth = content.getBooleanValue("isauth");
  50. BigDecimal associationamount = content.getBigDecimalValue("associationamount");
  51. Long associationaccountclassid = content.getLongValue("associationaccountclassid");
  52. BigDecimal rebateratio = content.getBigDecimalValue("rebateratio");
  53. String packagetype = content.getStringValue("packagetype");
  54. boolean eachneedadd=content.getBooleanValue("eachneedadd");
  55. Long eachlimitqty = content.getLongValue("eachlimitqty");
  56. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  57. if (sa_promotionid <= 0 || dbConnect
  58. .runSqlQuery("select sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid)
  59. .isEmpty()) {
  60. sa_promotionid = createTableID(tableName);
  61. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "新增", "促销方案新增成功").getSQL());
  62. } else {
  63. Rows rows = dbConnect
  64. .runSqlQuery("SELECT status,authmethod,rebateratio from sa_promotion WHERE sa_promotionid = " + sa_promotionid);
  65. if (rows.isNotEmpty()) {
  66. if (rows.get(0).getString("status").equals("新建")) {
  67. sqlFactory = new SQLFactory(this, "促销方案更新");
  68. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "更新", "促销方案更新成功").getSQL());
  69. if (rows.get(0).getLong("authmethod") != authmethod) {
  70. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  71. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  72. }
  73. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  74. }
  75. if (rebateratio.compareTo(rows.get(0).getBigDecimal("rebateratio")) != 0) {
  76. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "促销方案由返点比例:" + rows.get(0).getBigDecimal("rebateratio") + "修改为返点比例:" + rebateratio, "促销方案修改返点比例").getSQL());
  77. }
  78. } else {
  79. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  80. }
  81. } else {
  82. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  83. }
  84. }
  85. sqlFactory.addParameter("siteid", siteid);
  86. sqlFactory.addParameter("userid", userid);
  87. sqlFactory.addParameter("username", username);
  88. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  89. // 促销方案号createBillCode("promotionbillno")
  90. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  91. sqlFactory.addParameter("promname", promname);
  92. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  93. sqlFactory.addParameter("sa_brandid", sa_brandid);
  94. sqlFactory.addParameter("type", type);
  95. sqlFactory.addParameter("packagetype", packagetype);
  96. sqlFactory.addParameter("tradefield", tradefield.toJSONString());
  97. sqlFactory.addParameter("orderminqty", orderminqty);
  98. sqlFactory.addParameter("orderaddqty", orderaddqty);
  99. sqlFactory.addParameter("orderminamount", orderminamount);
  100. sqlFactory.addParameter("sa_openpromotionid", content.getIntValue("sa_openpromotionid"));
  101. sqlFactory.addParameter("openamount", content.getBigDecimalValue("openamount"));
  102. sqlFactory.addParameter("isrelevancepromotion", content.getLongValue("isrelevancepromotion"));
  103. sqlFactory.addParameter("begdate", begdate);
  104. sqlFactory.addParameter("enddate", enddate);
  105. sqlFactory.addParameter("authmethod", authmethod);
  106. sqlFactory.addParameter("isauth", isauth);
  107. sqlFactory.addParameter("associationamount", associationamount);
  108. sqlFactory.addParameter("associationaccountclassid", associationaccountclassid);
  109. sqlFactory.addParameter("rebateratio", rebateratio);
  110. sqlFactory.addParameter("eachneedadd", eachneedadd);
  111. sqlFactory.addParameter("eachlimitqty", eachlimitqty);
  112. // 备注说明
  113. sqlFactory.addParameter("remarks", remarks);
  114. sqlList.add(sqlFactory.getSQL());
  115. dbConnect.runSqlUpdate(sqlList);
  116. content.put("sa_promotionid", sa_promotionid);
  117. return querypromotionMain();
  118. }
  119. @API(title = "促销方案详情", apiversion = R.ID20221230144703.v1.class)
  120. @CACHEING
  121. public String querypromotionMain() throws YosException {
  122. Long sa_promotionid = content.getLong("sa_promotionid");
  123. SQLFactory sqlFactory = new SQLFactory(this, "促销方案详情查询");
  124. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  125. sqlFactory.addParameter("siteid", siteid);
  126. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  127. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  128. //附件
  129. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  130. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  131. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  132. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  133. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  134. } else {
  135. row.put("tradefield", new JSONArray());
  136. }
  137. return getSucReturnObject().setData(row).toString();
  138. }
  139. @API(title = "促销方案商品上下架", apiversion = R.ID2024052710133903.v1.class)
  140. public String releases() throws YosException {
  141. ArrayList<String> sqlList = new ArrayList<>();
  142. JSONArray itemids = content.getJSONArray("itemids");
  143. boolean isonsale = content.getBooleanValue("isonsale");
  144. List<Long> list = itemids.toJavaList(Long.class);
  145. list.add(0l);
  146. Long[] stringArray = list.toArray(new Long[0]);
  147. SQLFactory sqlFactoryupdate = new SQLFactory(this, "商品组上架");
  148. if(isonsale){
  149. sqlFactoryupdate = new SQLFactory(this, "促销方案商品上架_通过商品");
  150. }else{
  151. sqlFactoryupdate = new SQLFactory(this, "促销方案商品下架_通过商品");
  152. }
  153. sqlFactoryupdate.addParameter("siteid", siteid);
  154. sqlFactoryupdate.addParameter_in("itemids", stringArray);
  155. sqlList.add(sqlFactoryupdate.getSQL());
  156. // //发送消息
  157. // for (String id : stringArray) {
  158. // sendMsg(Long.parseLong(id));
  159. // }
  160. 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.itemid in " + itemids;
  161. sql = sql.replace("[", "(").replace("]", ")");
  162. Rows itemrows = dbConnect.runSqlQuery(sql);
  163. for (Row row :itemrows){
  164. if(isonsale){
  165. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"上架", "促销方案商品上下架").getSQL());
  166. }else{
  167. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"下架" , "促销方案商品上下架").getSQL());
  168. }
  169. }
  170. dbConnect.runSqlUpdate(sqlList);
  171. return getSucReturnObject().toString();
  172. }
  173. @API(title = "查询所有促销方案商品列表", apiversion = R.ID2024052710185203.v1.class)
  174. @CACHEING
  175. public String queryItemList() throws YosException {
  176. StringBuffer where = new StringBuffer(" 1=1 ");
  177. StringBuffer where1 = new StringBuffer(" 1=1 ");
  178. if (content.containsKey("where")) {
  179. JSONObject whereObject = content.getJSONObject("where");
  180. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  181. where.append(" and(");
  182. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  183. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  184. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  185. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  186. where.append("or t1.standards like'%").append(whereObject.getString("condition")).append("%' ");
  187. where.append("or t1.color like'%").append(whereObject.getString("condition")).append("%' ");
  188. where.append(")");
  189. }
  190. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  191. where1.append(" and(");
  192. where1.append("t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  193. where1.append(")");
  194. }
  195. if (whereObject.containsKey("isservice") && !"".equals(whereObject.getString("isservice"))) {
  196. where.append(" and(");
  197. where.append("t1.isservice ='").append(whereObject.getString("isservice")).append("' ");
  198. where.append(")");
  199. }
  200. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  201. where.append(" and(");
  202. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  203. where.append(")");
  204. }
  205. if (whereObject.containsKey("isonsalebutnotingroup") && !"".equals(whereObject.getString("isonsalebutnotingroup"))) {
  206. if (whereObject.getBooleanValue("isonsalebutnotingroup")) {
  207. where.append(" and(");
  208. where.append("t1.isonsale =1 and t1.itemid not in (select t1.itemid from sa_itemgroupmx t1 inner join sa_itemgroup t2 on t1.sa_itemgroupid=t2.sa_itemgroupid and t1.siteid=t2.siteid where t2.isonsale=1 )");
  209. where.append(")");
  210. }
  211. }
  212. if (whereObject.containsKey("hasprice") && !"".equals(whereObject.getString("hasprice"))) {
  213. if (whereObject.getBooleanValue("hasprice")) {
  214. where.append(" and(");
  215. where.append(" exists(select * from sa_itemprice where sa_itemprice.siteid=t1.siteid and sa_itemprice.itemid=t1.itemid and price>0)");
  216. where.append(")");
  217. }
  218. }
  219. if (whereObject.containsKey("itemclassid") && !"".equals(whereObject.getString("itemclassid"))) {
  220. ArrayList<Long> list = ItemClass.getSubItemClassIds(this, whereObject.getLong("itemclassid"));
  221. list.add(whereObject.getLong("itemclassid"));
  222. String sql = " and t1.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + list + " and siteid='" + siteid + "')";
  223. sql = sql.replace("[", "(").replace("]", ")");
  224. where.append(sql);
  225. }
  226. if (whereObject.containsKey("tradefield") && !whereObject.getString("tradefield").isEmpty()) {
  227. where.append(" and exists(");
  228. where.append(" select 1 from plm_item_tradefield t3 where t3.siteid=t1.siteid and t3.itemid=t1.itemid and t3.tradefield='").append(whereObject.getString("tradefield")).append("' ");
  229. where.append(")");
  230. }
  231. if (whereObject.containsKey("itemclass") && !whereObject.getString("itemclass").isEmpty()) {
  232. String itemclass = whereObject.getString("itemclass");
  233. where.append(" and t1.itemid in (");
  234. where.append(" SELECT t1.itemid FROM sa_itemsaleclass t1 INNER JOIN plm_itemclass t2 ON t2.itemclassid=t1.itemclassid AND t2.siteid=t1.siteid WHERE t2.itemclassname LIKE '%" + itemclass + "%' ");
  235. where.append(")");
  236. }
  237. }
  238. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item");
  239. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "(select DISTINCT t1.itemid,t1.siteid from sa_promotion_items t1 inner join sa_promotion t2 on t1.sa_promotionid=t2.sa_promotionid and t1.siteid=t2.siteid where "+where1+")", "t2", "t1.itemid=t2.itemid and t1.siteid=t2.siteid ");
  240. querySQL.setWhere(where);
  241. querySQL.setWhere("t1.siteid", siteid);
  242. querySQL.setTableAlias("t1");
  243. querySQL.setPage(pageSize, pageNumber);
  244. Rows rows = querySQL.query();
  245. return getSucReturnObject().setData(rows).toString();
  246. }
  247. @API(title = "一键复制促销方案", apiversion = R.ID20230713101303.v1.class)
  248. @CACHEING_CLEAN(apiClass = {promotion.class, promotionSalearea.class, promotionAuth.class, promotionItems.class, promotionItemPrice.class})
  249. public String copyPromotion() throws YosException {
  250. Long sa_promotionid = content.getLong("sa_promotionid");
  251. ArrayList<String> sqlList = new ArrayList<>();
  252. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_promotion WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  253. if (rows.isEmpty()) {
  254. return getErrReturnObject().setErrMsg("促销方案不存在").toString();
  255. }
  256. Row row = rows.get(0);
  257. Long sa_promotion_copy = createTableID("sa_promotion");
  258. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  259. sqlFactory.addParameter("siteid", siteid);
  260. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  261. sqlFactory.addParameter("userid", userid);
  262. sqlFactory.addParameter("username", username);
  263. //促销方案名称
  264. sqlFactory.addParameter("promname", row.getString("promname") + "复制");
  265. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  266. sqlFactory.addParameter("sa_accountclassid", row.getLong("sa_accountclassid"));
  267. sqlFactory.addParameter("sa_brandid", row.getLong("sa_brandid"));
  268. sqlFactory.addParameter("type", row.getString("type"));
  269. sqlFactory.addParameter("tradefield", row.getString("tradefield"));
  270. sqlFactory.addParameter("orderminqty", row.getBigDecimal("orderminqty"));
  271. sqlFactory.addParameter("orderaddqty", row.getBigDecimal("orderaddqty"));
  272. sqlFactory.addParameter("orderminamount", row.getBigDecimal("orderminamount"));
  273. sqlFactory.addParameter("sa_openpromotionid", row.getLong("sa_openpromotionid"));
  274. sqlFactory.addParameter("openamount", row.getBigDecimal("openamount"));
  275. sqlFactory.addParameter("isrelevancepromotion", row.getString("isrelevancepromotion"));
  276. sqlFactory.addParameter("associationamount", row.getString("associationamount"));
  277. sqlFactory.addParameter("associationaccountclassid", row.getString("associationaccountclassid"));
  278. sqlFactory.addParameter("begdate", row.getString("begdate"));
  279. sqlFactory.addParameter("enddate", row.getString("enddate"));
  280. sqlFactory.addParameter("authmethod", row.getString("authmethod"));
  281. sqlFactory.addParameter("associationamount", 0);
  282. sqlFactory.addParameter("associationaccountclassid", 0);
  283. sqlFactory.addParameter("rebateratio", 0);
  284. sqlFactory.addParameter("packagetype", row.getString("packagetype"));
  285. sqlFactory.addParameter("eachneedadd", row.getLong("eachneedadd"));
  286. sqlFactory.addParameter("eachlimitqty", row.getLong("eachlimitqty"));
  287. // 备注说明
  288. sqlFactory.addParameter("remarks", row.getString("remarks"));
  289. sqlList.add(sqlFactory.getSQL());
  290. //复制商品
  291. Rows promotionitemsRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_items WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  292. //复制商品
  293. Rows promotionitemspriceRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_itemprice WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  294. RowsMap promotionitemspriceRowsMap = promotionitemspriceRows.toRowsMap("sa_promotion_itemsid");
  295. for (Row promotionitemsRow : promotionitemsRows) {
  296. sqlFactory = new SQLFactory(this, "促销方案商品新增");
  297. sqlFactory.addParameter("siteid", siteid);
  298. long sa_promotion_itemsidcopy = createTableID("sa_promotion_items");
  299. sqlFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  300. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  301. sqlFactory.addParameter("sa_promotion_itemgroupid", promotionitemsRow.getLong("sa_promotion_itemgroupid"));
  302. sqlFactory.addParameter("groupqty", promotionitemsRow.getBigDecimal("groupqty"));
  303. sqlFactory.addParameter("itemid", promotionitemsRow.getLong("itemid"));
  304. sqlFactory.addParameter("orderaddqty", promotionitemsRow.getBigDecimal("orderaddqty"));
  305. sqlFactory.addParameter("orderminqty", promotionitemsRow.getBigDecimal("orderminqty"));
  306. sqlFactory.addParameter("islimit", promotionitemsRow.getLong("islimit"));
  307. sqlFactory.addParameter("signaturecode", promotionitemsRow.getString("signaturecode"));
  308. sqlFactory.addParameter("packagetypemx", promotionitemsRow.getString("packagetypemx"));
  309. sqlFactory.addParameter("packageqty", promotionitemsRow.getString("packageqty"));
  310. sqlFactory.addParameter("itemno", promotionitemsRow.getString("itemno"));
  311. sqlList.add(sqlFactory.getSQL());
  312. if (promotionitemspriceRowsMap.containsKey(promotionitemsRow.getString("sa_promotion_itemsid")) && promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid")).isNotEmpty()) {
  313. for (Row rowdetail : promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid"))) {
  314. SQLFactory itempriceFactory = new SQLFactory(this, "促销方案商品价格新增");
  315. itempriceFactory.addParameter("siteid", siteid);
  316. itempriceFactory.addParameter("sa_promotion_itempriceid",
  317. createTableID("sa_promotion_itemprice"));
  318. itempriceFactory.addParameter("sa_promotionid", sa_promotion_copy);
  319. itempriceFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  320. itempriceFactory.addParameter("itemid", rowdetail.getLong("itemid"));
  321. itempriceFactory.addParameter("pricegrade", rowdetail.getBigDecimal("pricegrade"));
  322. itempriceFactory.addParameter("oldprice", rowdetail.getBigDecimal("oldprice"));
  323. itempriceFactory.addParameter("price", rowdetail.getBigDecimal("price"));
  324. sqlList.add(itempriceFactory.getSQL());
  325. }
  326. }
  327. }
  328. dbConnect.runSqlUpdate(sqlList);
  329. content.put("sa_promotionid", sa_promotion_copy);
  330. return querypromotionMain();
  331. }
  332. @API(title = "查询促销方案列表", apiversion = R.ID20221230144803.v1.class)
  333. @CACHEING
  334. public String querypromotionList() throws YosException {
  335. StringBuffer where = new StringBuffer(" 1=1 ");
  336. if (content.containsKey("where")) {
  337. JSONObject whereObject = content.getJSONObject("where");
  338. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  339. where.append(" and(");
  340. where.append("t1.promnum like'%").append(whereObject.getString("condition")).append("%' ");
  341. where.append("or t1.promname like'%").append(whereObject.getString("condition")).append("%' ");
  342. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  343. where.append(")");
  344. }
  345. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  346. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  347. }
  348. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  349. where.append(" and DATE_FORMAT(t1.begdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  350. }
  351. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  352. where.append(" and DATE_FORMAT(t1.enddate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  353. }
  354. }
  355. // SQLFactory sqlFactory = new SQLFactory(this, "促销方案列表查询", pageSize, pageNumber, pageSorting);
  356. // sqlFactory.addParameter("siteid", siteid);
  357. // sqlFactory.addParameter_SQL("where", where);
  358. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  359. QuerySQL querySQL = queryPomotionList(where.toString());
  360. querySQL.setOrderBy(pageSorting);
  361. querySQL.setPage(pageSize, pageNumber);
  362. Rows rows = querySQL.query();
  363. //附件
  364. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  365. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  366. for (Row row : rows) {
  367. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  368. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  369. } else {
  370. row.put("tradefield", new JSONArray());
  371. }
  372. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  373. }
  374. return getSucReturnObject().setData(rows).toString();
  375. }
  376. public QuerySQL queryPomotionList(String where) throws YosException {
  377. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_promotion");
  378. querySQL.setTableAlias("t1");
  379. querySQL.addJoinTable(JOINTYPE.left, "sa_brand", "t2", "t1.sa_brandid = t2.sa_brandid AND t1.siteid = t2.siteid",
  380. "brandname");
  381. querySQL.addJoinTable(JOINTYPE.left, "sa_accountclass", "t3", "t1.sa_accountclassid = t3.sa_accountclassid AND t1.siteid = t3.siteid",
  382. "accountno", "accountname");
  383. querySQL.setWhere("t1.siteid", siteid);
  384. querySQL.setWhere(where);
  385. return querySQL;
  386. }
  387. @API(title = "查询促销方案销售排行(经销商)", apiversion = R.ID20231007101703.v1.class)
  388. @CACHEING
  389. public String queryAgentSalesRanking() throws YosException {
  390. long sa_promotionid = content.getLongValue("sa_promotionid");
  391. StringBuffer where = new StringBuffer(" 1=1 ");
  392. if (content.containsKey("where")) {
  393. JSONObject whereObject = content.getJSONObject("where");
  394. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  395. where.append(" and(");
  396. where.append("t2.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  397. where.append("or t3.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  398. where.append(")");
  399. }
  400. }
  401. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(经销商)", pageSize, pageNumber, pageSorting);
  402. sqlFactory.addParameter("siteid", siteid);
  403. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  404. sqlFactory.addParameter_SQL("where", where);
  405. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  406. return getSucReturnObject().setData(rows).toString();
  407. }
  408. @API(title = "查询促销方案销售排行(商品)", apiversion = R.ID20231007101803.v1.class)
  409. @CACHEING
  410. public String queryItemSalesRanking() throws YosException {
  411. long sa_promotionid = content.getLongValue("sa_promotionid");
  412. StringBuffer where = new StringBuffer(" 1=1 ");
  413. if (content.containsKey("where")) {
  414. JSONObject whereObject = content.getJSONObject("where");
  415. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  416. where.append(" and(");
  417. where.append("t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  418. where.append("or t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  419. where.append(")");
  420. }
  421. }
  422. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(商品)", pageSize, pageNumber, pageSorting);
  423. sqlFactory.addParameter("siteid", siteid);
  424. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  425. sqlFactory.addParameter_SQL("where", where);
  426. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  427. // //附件
  428. // ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  429. // RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  430. // for (Row row : rows) {
  431. // if (!StringUtils.isBlank(row.getString("tradefield"))) {
  432. // row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  433. // } else {
  434. // row.put("tradefield", new JSONArray());
  435. // }
  436. // row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  437. // }
  438. return getSucReturnObject().setData(rows).toString();
  439. }
  440. @API(title = "删除", apiversion = R.ID20221230144903.v1.class)
  441. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  442. public String delete() throws YosException {
  443. ArrayList<String> sqlList = new ArrayList<>();
  444. JSONArray sa_promotionids = content.getJSONArray("sa_promotionids");
  445. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotionids.size());
  446. for (Object o : sa_promotionids) {
  447. long sa_promotionid = Long.parseLong(o.toString());
  448. Rows RowsStatus = dbConnect.runSqlQuery("select sa_promotionid,status from sa_promotion where siteid='"
  449. + siteid + "' and sa_promotionid='" + sa_promotionid + "'");
  450. Rows Rowsdetail = dbConnect.runSqlQuery("select sa_promotion_authid from sa_promotion_auth where siteid='"
  451. + siteid + "' and sa_promotionid='" + sa_promotionid + "' and saledamount>0");
  452. if (RowsStatus.isNotEmpty()) {
  453. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  454. batchDeleteErr.addErr(sa_promotionid, "非新建状态的促销方案无法删除");
  455. continue;
  456. }
  457. }
  458. if (Rowsdetail.isNotEmpty()) {
  459. batchDeleteErr.addErr(sa_promotionid, "存在已购金额的授权经销商禁止删除促销活动");
  460. continue;
  461. }
  462. sqlList.add("delete from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  463. sqlList.add(
  464. "delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  465. sqlList.add("delete from sa_promotion_itemgroup where siteid='" + siteid + "' and sa_promotionid="
  466. + sa_promotionid);
  467. sqlList.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotionid="
  468. + sa_promotionid);
  469. sqlList.add("delete from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid="
  470. + sa_promotionid);
  471. dbConnect.runSqlUpdate(sqlList);
  472. }
  473. return batchDeleteErr.getReturnObject().toString();
  474. }
  475. @API(title = "审核", apiversion = R.ID20221230145003.v1.class)
  476. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  477. public String check() throws YosException {
  478. Long sa_promotionid = content.getLong("sa_promotionid");
  479. Rows rows = dbConnect
  480. .runSqlQuery("select sa_promotionid,status,promnum,promname,type,associationaccountclassid,associationamount,packagetype,eachlimitqty,authmethod from sa_promotion where sa_promotionid ='"
  481. + sa_promotionid + "' and siteid='" + siteid + "'");
  482. Rows rowsdetails = dbConnect
  483. .runSqlQuery("select t2.itemno,t2.itemname,ifnull(t1.orderminqty,0) orderminqty, ifnull(t1.orderaddqty,0) orderaddqty from sa_promotion_items t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.sa_promotionid ='"
  484. + sa_promotionid + "' and t1.siteid='" + siteid + "'");
  485. if (rows.isEmpty()) {
  486. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  487. } else {
  488. if (!rows.get(0).getString("status").equals("新建")) {
  489. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非新建状态,无法审核")
  490. .toString();
  491. }
  492. if (rows.get(0).getString("type").equals("返利促销")) {
  493. if (rows.get(0).getLong("associationaccountclassid") == 0 || rows.get(0).getBigDecimal("associationamount").compareTo(BigDecimal.ZERO) == 0) {
  494. return getErrReturnObject().setErrMsg("返利促销必须选择关联支付账户和关联支付金额").toString();
  495. }
  496. 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)");
  497. if (islimitRows.isEmpty()) {
  498. return getErrReturnObject().setErrMsg("返利促销必须授权经销商").toString();
  499. } else if (islimitRows.size() > 1 || (islimitRows.size() == 1 && !islimitRows.get(0).getBoolean("islimit"))) {
  500. return getErrReturnObject().setErrMsg("返利促销授权的经销商必须都为限购").toString();
  501. }
  502. }
  503. if(rows.get(0).getBoolean("authmethod") && dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isEmpty()){
  504. return getErrReturnObject().setErrMsg("请添加范围内非授权经销商名单!").toString();
  505. }
  506. for (Row row:rowsdetails) {
  507. if(row.getBigDecimal("orderminqty").compareTo(BigDecimal.ZERO)==0 || row.getBigDecimal("orderaddqty").compareTo(BigDecimal.ZERO)==0){
  508. return getErrReturnObject().setErrMsg("该促销方案【"+row.getString("itemno")+"】商品起订量或增量为0,无法审核").toString();
  509. }
  510. }
  511. if (rows.get(0).getString("type").equals("套餐活动")) {
  512. if (StringUtils.isBlank(rows.get(0).getString("packagetype"))) {
  513. return getErrReturnObject().setErrMsg("套餐活动的方案必须选择套餐类型").toString();
  514. }
  515. Rows promotionItemsRows = dbConnect.runSqlQuery("select if(ifnull(t1.packagetypemx,'')='','kong',packagetypemx) packagetypemx,count(1) count,min(t2.itemno) itemno from sa_promotion_items t1 inner join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.siteid='"+siteid+"' and t1.sa_promotionid="+sa_promotionid+" group by if(ifnull(t1.packagetypemx,'')='','kong',packagetypemx)");
  516. for(Row row : promotionItemsRows){
  517. if(row.getString("packagetypemx").equals("kong")){
  518. return getErrReturnObject().setErrMsg("品号:"+row.getString("itemno")+"未设置套餐分类,无法审核").toString();
  519. }
  520. }
  521. for(Row row : promotionItemsRows){
  522. if(row.getLong("count")>100){
  523. return getErrReturnObject().setErrMsg("套餐名:"+row.getString("packagetypemx")+" 商品添加数量已超上限(同一套餐最多只能添加100个商品)").toString();
  524. }
  525. if(row.getLong("count")<rows.get(0).getLong("eachlimitqty")){
  526. return getErrReturnObject().setErrMsg("限购商品数大于"+row.getString("packagetypemx")+"套餐商品数,请调整后重试!").toString();
  527. }
  528. }
  529. Rows promotioncountRows = dbConnect.runSqlQuery("select distinct groupqty/(ifnull(t1.orderminqty,1)) count from sa_promotion_items t1 where t1.siteid='"+siteid+"' and t1.sa_promotionid="+sa_promotionid);
  530. if(promotioncountRows.size()>1){
  531. return getErrReturnObject().setErrMsg("限购数量设置不合理,请重新设置!").toString();
  532. }
  533. }
  534. Rows rowsdetailshuizong = dbConnect.runSqlQuery("select * from (select t2.itemno,count(1) count from sa_promotion_items t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.sa_promotionid ='"
  535. + sa_promotionid + "' and t1.siteid='" + siteid + "' group by t1.itemid,t2.itemno,t1.orderminqty,t1.orderaddqty,t1.groupqty,t1.packagetypemx) t where t.count>1");
  536. if(rowsdetailshuizong.isNotEmpty()){
  537. return getErrReturnObject().setErrMsg("商品编码:"+rowsdetailshuizong.get(0).getString("itemno")+"活动条件设置重复,禁止审核!").toString();
  538. }
  539. }
  540. ArrayList<String> sqlList = new ArrayList<>();
  541. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案审核");
  542. sqlFactoryupdate.addParameter("siteid", siteid);
  543. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  544. sqlFactoryupdate.addParameter("checkby", username);
  545. sqlList.add(sqlFactoryupdate.getSQL());
  546. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "审核", "促销方案审核成功").getSQL());
  547. dbConnect.runSqlUpdate(sqlList);
  548. return getSucReturnObject().toString();
  549. }
  550. @API(title = "反审核", apiversion = R.ID20221230145103.v1.class)
  551. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  552. public String uncheck() throws YosException {
  553. Long sa_promotionid = content.getLong("sa_promotionid");
  554. Rows rows = dbConnect
  555. .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
  556. + sa_promotionid + "' and siteid='" + siteid + "'");
  557. if (rows.isEmpty()) {
  558. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  559. } else {
  560. if (!rows.get(0).getString("status").equals("审核")) {
  561. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非审核状态,无法反审核")
  562. .toString();
  563. }
  564. // if(!dbConnect.runSqlQuery("select * from sa_order where sa_promotionid="+sa_promotionid+" and siteid='"+siteid+"'").isEmpty()){
  565. // return getErrReturnObject().setErrMsg("存在已使用方案的订单,无法反审核")
  566. // .toString();
  567. // }
  568. }
  569. ArrayList<String> sqlList = new ArrayList<>();
  570. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案反审核");
  571. sqlFactoryupdate.addParameter("siteid", siteid);
  572. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  573. sqlFactoryupdate.addParameter("checkby", username);
  574. sqlList.add(sqlFactoryupdate.getSQL());
  575. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反审核", "促销方案反审核成功").getSQL());
  576. dbConnect.runSqlUpdate(sqlList);
  577. return getSucReturnObject().toString();
  578. }
  579. @API(title = "发布反发布", apiversion = R.ID20221230145203.v1.class)
  580. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  581. public String recheck() throws YosException {
  582. Long sa_promotionid = content.getLong("sa_promotionid");
  583. boolean isrecheck = content.getBooleanValue("isrecheck");
  584. Rows rows = dbConnect
  585. .runSqlQuery("select sa_promotionid,status,promnum,type from sa_promotion where sa_promotionid ='"
  586. + sa_promotionid + "' and siteid='" + siteid + "'");
  587. for (Row row : rows) {
  588. if (isrecheck) {
  589. if (!row.getString("status").equals("审核")) {
  590. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非审核状态,无法发布")
  591. .toString();
  592. }
  593. } else {
  594. if (!row.getString("status").equals("发布")) {
  595. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法反发布")
  596. .toString();
  597. }
  598. if(row.getString("type").equals("套餐活动")){
  599. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_promotionid="+sa_promotionid+" and siteid='"+siteid+"' and deleted=0");
  600. if(orderRows.isNotEmpty()){
  601. return getErrReturnObject().setErrMsg("该方案已存在订单,无法取消发布")
  602. .toString();
  603. }
  604. }
  605. }
  606. }
  607. ArrayList<String> sqlList = new ArrayList<>();
  608. SQLFactory sqlFactoryupdate;
  609. if (isrecheck) {
  610. sqlFactoryupdate = new SQLFactory(this, "促销方案发布");
  611. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "发布", "促销方案发布成功").getSQL());
  612. } else {
  613. sqlFactoryupdate = new SQLFactory(this, "促销方案反发布");
  614. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反发布", "促销方案反发布成功").getSQL());
  615. }
  616. sqlFactoryupdate.addParameter("siteid", siteid);
  617. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  618. sqlFactoryupdate.addParameter("sendby", username);
  619. sqlList.add(sqlFactoryupdate.getSQL());
  620. dbConnect.runSqlUpdate(sqlList);
  621. return getSucReturnObject().toString();
  622. }
  623. @API(title = "关闭反关闭", apiversion = R.ID20230102095403.v1.class)
  624. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  625. public String close() throws YosException {
  626. Long sa_promotionid = content.getLong("sa_promotionid");
  627. boolean isclose = content.getBooleanValue("isclose");
  628. Rows rows = dbConnect
  629. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  630. + sa_promotionid + "' and siteid='" + siteid + "'");
  631. for (Row row : rows) {
  632. if (isclose) {
  633. if (!row.getString("status").equals("发布")) {
  634. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法关闭")
  635. .toString();
  636. }
  637. } else {
  638. if (!row.getString("status").equals("关闭")) {
  639. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非关闭状态,无法反关闭")
  640. .toString();
  641. }
  642. }
  643. }
  644. ArrayList<String> sqlList = new ArrayList<>();
  645. SQLFactory sqlFactoryupdate;
  646. if (isclose) {
  647. sqlFactoryupdate = new SQLFactory(this, "促销方案关闭");
  648. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "关闭", "促销方案关闭成功").getSQL());
  649. } else {
  650. sqlFactoryupdate = new SQLFactory(this, "促销方案反关闭");
  651. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反关闭", "促销方案反关闭成功").getSQL());
  652. }
  653. sqlFactoryupdate.addParameter("siteid", siteid);
  654. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  655. sqlFactoryupdate.addParameter("closeby", username);
  656. sqlList.add(sqlFactoryupdate.getSQL());
  657. dbConnect.runSqlUpdate(sqlList);
  658. return getSucReturnObject().toString();
  659. }
  660. @API(title = "切换授权方式", apiversion = R.ID20230509100504.v1.class)
  661. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20220103140003.v1.class})
  662. public String updateAuthMethod() throws YosException {
  663. Long sa_promotionid = content.getLong("sa_promotionid");
  664. Rows rows = dbConnect.runSqlQuery("select sa_promotionid,status,authmethod from sa_promotion where sa_promotionid ='" + sa_promotionid + "' and siteid='" + siteid + "'");
  665. if (rows.isEmpty()) {
  666. return getErrReturnObject().setErrMsg("无效促销方案").toString();
  667. }
  668. if (!rows.get(0).getString("status").equals("新建")) {
  669. return getErrReturnObject().setErrMsg("非新建状态不能修改授权方式").toString();
  670. }
  671. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  672. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  673. }
  674. ArrayList<String> sqlList = new ArrayList<>();
  675. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  676. if (rows.get(0).getLong("authmethod") == 0) {
  677. sqlList.add("update sa_promotion set authmethod=1 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  678. } else {
  679. sqlList.add("update sa_promotion set authmethod=0 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  680. }
  681. dbConnect.runSqlUpdate(sqlList);
  682. return getSucReturnObject().toString();
  683. }
  684. @API(title = "结算", apiversion = R.ID20230826091303.v1.class)
  685. @CACHEING_CLEAN(apiClass = {promotionAuth.class, promotion.class})
  686. public String settlement() throws YosException {
  687. Long sa_promotion_authid = content.getLong("sa_promotion_authid");
  688. ArrayList<String> sqlList = new ArrayList<>();
  689. Rows promotionauthRows = dbConnect.runSqlQuery(
  690. "select t1.*,t2.type,t2.rebateratio,t2.promname,t2.promnum 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 ='"
  691. + sa_promotion_authid + "' and t1.siteid='" + siteid + "'");
  692. if (promotionauthRows.isNotEmpty()) {
  693. if (!promotionauthRows.get(0).getString("type").equals("返利促销")) {
  694. return getErrReturnObject().setErrMsg("返利促销方案才能返利结算").toString();
  695. }
  696. if (promotionauthRows.get(0).getBoolean("settlestatus")) {
  697. return getErrReturnObject().setErrMsg("该经销商结算已经结算成功,无法再次结算").toString();
  698. }
  699. } else {
  700. return getErrReturnObject().setErrMsg("该记录不存在").toString();
  701. }
  702. long sa_promotionid = promotionauthRows.get(0).getLong("sa_promotionid");
  703. long sys_enterpriseid = promotionauthRows.get(0).getLong("sys_enterpriseid");
  704. BigDecimal saledamount = promotionauthRows.get(0).getBigDecimal("saledamount");
  705. BigDecimal rebateratio = promotionauthRows.get(0).getBigDecimal("rebateratio");
  706. System.out.println(saledamount);
  707. System.out.println(rebateratio);
  708. String agentnum = "";
  709. Rows enterpriserows = dbConnect.runSqlQuery("select * from sa_agents where sys_enterpriseid ='" + sys_enterpriseid + "' and siteid='" + siteid + "'");
  710. if (enterpriserows.isNotEmpty()) {
  711. agentnum = enterpriserows.get(0).getString("agentnum");
  712. }
  713. //查询经销商未关闭的促销订单
  714. 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!='新建' and status not in('关闭','手工关闭') and siteid='" + siteid + "'");
  715. Boolean sendRemind = false;
  716. Long sa_cashbillid = 0L;
  717. if (rows.isNotEmpty()) {
  718. return getErrReturnObject().setErrMsg("存在未关闭的返利促销订单,无法结算").toString();
  719. } else {
  720. Rows accountclassrows = dbConnect.runSqlQuery("select sa_accountclassid from sa_accountclass where accountname ='返利金账户' and siteid='" + siteid + "'");
  721. if (accountclassrows.isNotEmpty()) {
  722. long sa_accountclassid = accountclassrows.get(0).getLong("sa_accountclassid");
  723. CashbillEntity entity = new CashbillEntity();
  724. entity.setAmount(saledamount.multiply(rebateratio));
  725. entity.setOwnerid(sa_promotion_authid);
  726. entity.setOwnertable("sa_promotion_auth");
  727. entity.setSource("返利");
  728. entity.setSourcenote("经销商【" + agentnum + "】,【" + promotionauthRows.get(0).getString("promname") + "】方案返利促销");
  729. entity.setRemarks("由活动单号" + promotionauthRows.get(0).getString("promnum") + "结算时生成");
  730. JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, sa_accountclassid, entity, true, true);
  731. sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  732. sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  733. sendRemind = true;
  734. sqlList.add("update sa_promotion_auth set settlestatus=1 where sa_promotion_authid=" + sa_promotion_authid + " and siteid='" + siteid + "'");
  735. }
  736. }
  737. dbConnect.runSqlUpdate(sqlList);
  738. if (sendRemind) {
  739. Accountbalance.remindSend(this, sys_enterpriseid, sa_cashbillid);
  740. }
  741. return getSucReturnObject().toString();
  742. }
  743. @API(title = "查询促销方案里的套餐明细", apiversion = R.ID2024080614131103.v1.class)
  744. @CACHEING
  745. public String queryPackagetypemx() throws YosException {
  746. Rows optiontypeRows = dbConnect.runSqlQuery("select optiontypeid,issql,issubvaluemag from sys_optiontype where typename='package'");
  747. if (optiontypeRows.isEmpty()) {
  748. return getErrReturnObject().setErrMsg("找不到指定的选项分类").toString();
  749. }
  750. long optiontypeid = optiontypeRows.get(0).getLong("optiontypeid");
  751. String packagetype=content.getStringValue("packagetype");
  752. boolean issubvaluemag = optiontypeRows.get(0).getBoolean("issubvaluemag");
  753. JSONArray valueArray = new JSONArray();
  754. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sys_optiontypemx", "value,remarks,subvalues");
  755. querySQL.setWhere("isused", 1).setWhere("optiontypeid", optiontypeid).setWhere("value",packagetype);
  756. querySQL.setWhere("(ifnull(siteid,'')='' or siteid='" + siteid + "')");
  757. querySQL.setCondition("value", "remarks");
  758. querySQL.setOrderBy("sequence");
  759. Rows valuerows = querySQL.query();
  760. long sa_promotionid =content.getLong("sa_promotionid");
  761. Rows rows = dbConnect.runSqlQuery("select distinct packagetypemx from sa_promotion_items where siteid='"+siteid+"' and ifnull(packagetypemx,'')!='' and sa_promotionid="+sa_promotionid);
  762. RowsMap rowsMap = rows.toRowsMap("packagetypemx");
  763. for (Row valuerow : valuerows) {
  764. JSONObject object = new JSONObject();
  765. object.put("value", valuerow.getString("value"));
  766. object.put("remarks", valuerow.getString("remarks"));
  767. if (issubvaluemag) {
  768. try {
  769. JSONArray subvaluesArray = valuerow.getJSONArray("subvalues");
  770. JSONArray subvaluesArrayResult = new JSONArray();
  771. for(Object obj :subvaluesArray){
  772. String subvalue =(String)obj;
  773. if(rowsMap.containsKey(subvalue)) {
  774. subvaluesArrayResult.add(subvalue);
  775. }
  776. }
  777. object.put("subvalues", subvaluesArrayResult);
  778. } catch (Exception e) {
  779. object.put("subvalues", new JSONArray());
  780. }
  781. }
  782. valueArray.add(object);
  783. }
  784. return getSucReturnObject().setData(valueArray).toString();
  785. }
  786. @API(title = "判断套餐促销方案是否满足条件", apiversion = R.ID2024091213511703.v1.class)
  787. @CACHEING
  788. public String jundgePackagetype() throws YosException {
  789. long sa_promotionid =content.getLong("sa_promotionid");
  790. JSONArray items =content.getJSONArray("items");
  791. ArrayList<Long> sa_promotion_itemsids = new ArrayList<Long>();
  792. sa_promotion_itemsids.add((long) 0);
  793. for (Object obj : items) {
  794. JSONObject item=(JSONObject)obj;
  795. sa_promotion_itemsids.add(item.getLong("sa_promotion_itemsid"));
  796. }
  797. Rows rows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='"+siteid+"' and type='套餐活动' and sa_promotionid="+sa_promotionid);
  798. if(rows.isNotEmpty()){
  799. int eachlimitqty=rows.get(0).getInteger("eachlimitqty");
  800. if(rows.get(0).getBoolean("eachneedadd") && eachlimitqty>0){
  801. String sql ="select ifnull(packagetypemx,'') packagetypemx,count(1) count from sa_promotion_items where siteid='"+siteid+"' and sa_promotionid="+sa_promotionid +" and sa_promotion_itemsid in "+sa_promotion_itemsids +" group by ifnull(packagetypemx,'')";
  802. sql = sql.replace("[", "(").replace("]", ")");
  803. Rows promotionItemsRows = dbConnect.runSqlQuery(sql);
  804. for(Row row :promotionItemsRows){
  805. if(StringUtils.isBlank(row.getString("packagetypemx"))){
  806. return getErrReturnObject().setErrMsg("促销方案维护出错").toString();
  807. }
  808. if(eachlimitqty!=row.getInteger("count")){
  809. return getErrReturnObject().setErrMsg(row.getString("packagetypemx")+"套餐未满足限购品数"+eachlimitqty+"的要求,禁止创建订单,请修改后重试!").toString();
  810. }
  811. }
  812. }
  813. }
  814. return getSucReturnObject().setData(rows).toString();
  815. }
  816. }