promotion.java 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  55. if (sa_promotionid <= 0 || dbConnect
  56. .runSqlQuery("select sa_promotionid from sa_promotion where sa_promotionid=" + sa_promotionid)
  57. .isEmpty()) {
  58. sa_promotionid = createTableID(tableName);
  59. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "新增", "促销方案新增成功").getSQL());
  60. } else {
  61. Rows rows = dbConnect
  62. .runSqlQuery("SELECT status,authmethod,rebateratio from sa_promotion WHERE sa_promotionid = " + sa_promotionid);
  63. if (rows.isNotEmpty()) {
  64. if (rows.get(0).getString("status").equals("新建")) {
  65. sqlFactory = new SQLFactory(this, "促销方案更新");
  66. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "更新", "促销方案更新成功").getSQL());
  67. if (rows.get(0).getLong("authmethod") != authmethod) {
  68. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  69. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  70. }
  71. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  72. }
  73. if (rebateratio.compareTo(rows.get(0).getBigDecimal("rebateratio")) != 0) {
  74. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "促销方案由返点比例:" + rows.get(0).getBigDecimal("rebateratio") + "修改为返点比例:" + rebateratio, "促销方案修改返点比例").getSQL());
  75. }
  76. } else {
  77. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  78. }
  79. } else {
  80. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  81. }
  82. }
  83. sqlFactory.addParameter("siteid", siteid);
  84. sqlFactory.addParameter("userid", userid);
  85. sqlFactory.addParameter("username", username);
  86. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  87. // 促销方案号createBillCode("promotionbillno")
  88. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  89. sqlFactory.addParameter("promname", promname);
  90. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  91. sqlFactory.addParameter("sa_brandid", sa_brandid);
  92. sqlFactory.addParameter("type", type);
  93. sqlFactory.addParameter("packagetype", packagetype);
  94. sqlFactory.addParameter("tradefield", tradefield.toJSONString());
  95. sqlFactory.addParameter("orderminqty", orderminqty);
  96. sqlFactory.addParameter("orderaddqty", orderaddqty);
  97. sqlFactory.addParameter("orderminamount", orderminamount);
  98. sqlFactory.addParameter("sa_openpromotionid", content.getIntValue("sa_openpromotionid"));
  99. sqlFactory.addParameter("openamount", content.getBigDecimalValue("openamount"));
  100. sqlFactory.addParameter("isrelevancepromotion", content.getLongValue("isrelevancepromotion"));
  101. sqlFactory.addParameter("begdate", begdate);
  102. sqlFactory.addParameter("enddate", enddate);
  103. sqlFactory.addParameter("authmethod", authmethod);
  104. sqlFactory.addParameter("isauth", isauth);
  105. sqlFactory.addParameter("associationamount", associationamount);
  106. sqlFactory.addParameter("associationaccountclassid", associationaccountclassid);
  107. sqlFactory.addParameter("rebateratio", rebateratio);
  108. // 备注说明
  109. sqlFactory.addParameter("remarks", remarks);
  110. sqlList.add(sqlFactory.getSQL());
  111. dbConnect.runSqlUpdate(sqlList);
  112. content.put("sa_promotionid", sa_promotionid);
  113. return querypromotionMain();
  114. }
  115. @API(title = "促销方案详情", apiversion = R.ID20221230144703.v1.class)
  116. @CACHEING
  117. public String querypromotionMain() throws YosException {
  118. Long sa_promotionid = content.getLong("sa_promotionid");
  119. SQLFactory sqlFactory = new SQLFactory(this, "促销方案详情查询");
  120. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  121. sqlFactory.addParameter("siteid", siteid);
  122. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  123. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  124. //附件
  125. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  126. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  127. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  128. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  129. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  130. } else {
  131. row.put("tradefield", new JSONArray());
  132. }
  133. return getSucReturnObject().setData(row).toString();
  134. }
  135. @API(title = "促销方案商品上下架", apiversion = R.ID2024052710133903.v1.class)
  136. public String releases() throws YosException {
  137. ArrayList<String> sqlList = new ArrayList<>();
  138. JSONArray itemids = content.getJSONArray("itemids");
  139. boolean isonsale = content.getBooleanValue("isonsale");
  140. List<Long> list = itemids.toJavaList(Long.class);
  141. list.add(0l);
  142. Long[] stringArray = list.toArray(new Long[0]);
  143. SQLFactory sqlFactoryupdate = new SQLFactory(this, "商品组上架");
  144. if(isonsale){
  145. sqlFactoryupdate = new SQLFactory(this, "促销方案商品上架_通过商品");
  146. }else{
  147. sqlFactoryupdate = new SQLFactory(this, "促销方案商品下架_通过商品");
  148. }
  149. sqlFactoryupdate.addParameter("siteid", siteid);
  150. sqlFactoryupdate.addParameter_in("itemids", stringArray);
  151. sqlList.add(sqlFactoryupdate.getSQL());
  152. // //发送消息
  153. // for (String id : stringArray) {
  154. // sendMsg(Long.parseLong(id));
  155. // }
  156. 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;
  157. sql = sql.replace("[", "(").replace("]", ")");
  158. Rows itemrows = dbConnect.runSqlQuery(sql);
  159. for (Row row :itemrows){
  160. if(isonsale){
  161. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"上架", "促销方案商品上下架").getSQL());
  162. }else{
  163. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", row.getLong("sa_promotionid"), "促销方案商品【"+row.getString("itemname")+"】由"+username+"下架" , "促销方案商品上下架").getSQL());
  164. }
  165. }
  166. dbConnect.runSqlUpdate(sqlList);
  167. return getSucReturnObject().toString();
  168. }
  169. @API(title = "查询所有促销方案商品列表", apiversion = R.ID2024052710185203.v1.class)
  170. @CACHEING
  171. public String queryItemList() throws YosException {
  172. StringBuffer where = new StringBuffer(" 1=1 ");
  173. if (content.containsKey("where")) {
  174. JSONObject whereObject = content.getJSONObject("where");
  175. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  176. where.append(" and(");
  177. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  178. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  179. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  180. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  181. where.append("or t1.standards like'%").append(whereObject.getString("condition")).append("%' ");
  182. where.append("or t1.color like'%").append(whereObject.getString("condition")).append("%' ");
  183. where.append(")");
  184. }
  185. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  186. where.append(" and(");
  187. where.append("t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  188. where.append(")");
  189. }
  190. if (whereObject.containsKey("isservice") && !"".equals(whereObject.getString("isservice"))) {
  191. where.append(" and(");
  192. where.append("t1.isservice ='").append(whereObject.getString("isservice")).append("' ");
  193. where.append(")");
  194. }
  195. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  196. where.append(" and(");
  197. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  198. where.append(")");
  199. }
  200. if (whereObject.containsKey("isonsalebutnotingroup") && !"".equals(whereObject.getString("isonsalebutnotingroup"))) {
  201. if (whereObject.getBooleanValue("isonsalebutnotingroup")) {
  202. where.append(" and(");
  203. 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 )");
  204. where.append(")");
  205. }
  206. }
  207. if (whereObject.containsKey("hasprice") && !"".equals(whereObject.getString("hasprice"))) {
  208. if (whereObject.getBooleanValue("hasprice")) {
  209. where.append(" and(");
  210. where.append(" exists(select * from sa_itemprice where sa_itemprice.siteid=t1.siteid and sa_itemprice.itemid=t1.itemid and price>0)");
  211. where.append(")");
  212. }
  213. }
  214. if (whereObject.containsKey("itemclassid") && !"".equals(whereObject.getString("itemclassid"))) {
  215. ArrayList<Long> list = ItemClass.getSubItemClassIds(this, whereObject.getLong("itemclassid"));
  216. list.add(whereObject.getLong("itemclassid"));
  217. String sql = " and t1.itemid in ( SELECT itemid from sa_itemsaleclass WHERE itemclassid IN " + list + " and siteid='" + siteid + "')";
  218. sql = sql.replace("[", "(").replace("]", ")");
  219. where.append(sql);
  220. }
  221. if (whereObject.containsKey("tradefield") && !whereObject.getString("tradefield").isEmpty()) {
  222. where.append(" and exists(");
  223. 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("' ");
  224. where.append(")");
  225. }
  226. if (whereObject.containsKey("itemclass") && !whereObject.getString("itemclass").isEmpty()) {
  227. String itemclass = whereObject.getString("itemclass");
  228. where.append(" and t1.itemid in (");
  229. 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 + "%' ");
  230. where.append(")");
  231. }
  232. }
  233. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item");
  234. 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)", "t2", "t1.itemid=t2.itemid and t1.siteid=t2.siteid ");
  235. querySQL.setWhere(where);
  236. querySQL.setWhere("t1.siteid", siteid);
  237. querySQL.setTableAlias("t1");
  238. querySQL.setPage(pageSize, pageNumber);
  239. Rows rows = querySQL.query();
  240. return getSucReturnObject().setData(rows).toString();
  241. }
  242. @API(title = "一键复制促销方案", apiversion = R.ID20230713101303.v1.class)
  243. @CACHEING_CLEAN(apiClass = {promotion.class, promotionSalearea.class, promotionAuth.class, promotionItems.class, promotionItemPrice.class})
  244. public String copyPromotion() throws YosException {
  245. Long sa_promotionid = content.getLong("sa_promotionid");
  246. ArrayList<String> sqlList = new ArrayList<>();
  247. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_promotion WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  248. if (rows.isEmpty()) {
  249. return getErrReturnObject().setErrMsg("促销方案不存在").toString();
  250. }
  251. Row row = rows.get(0);
  252. Long sa_promotion_copy = createTableID("sa_promotion");
  253. SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
  254. sqlFactory.addParameter("siteid", siteid);
  255. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  256. sqlFactory.addParameter("userid", userid);
  257. sqlFactory.addParameter("username", username);
  258. //促销方案名称
  259. sqlFactory.addParameter("promname", row.getString("promname") + "复制");
  260. sqlFactory.addParameter("promnum", createBillCode("promotionbill"));
  261. sqlFactory.addParameter("sa_accountclassid", row.getLong("sa_accountclassid"));
  262. sqlFactory.addParameter("sa_brandid", row.getLong("sa_brandid"));
  263. sqlFactory.addParameter("type", row.getString("type"));
  264. sqlFactory.addParameter("tradefield", row.getString("tradefield"));
  265. sqlFactory.addParameter("orderminqty", row.getBigDecimal("orderminqty"));
  266. sqlFactory.addParameter("orderaddqty", row.getBigDecimal("orderaddqty"));
  267. sqlFactory.addParameter("orderminamount", row.getBigDecimal("orderminamount"));
  268. sqlFactory.addParameter("sa_openpromotionid", row.getLong("sa_openpromotionid"));
  269. sqlFactory.addParameter("openamount", row.getBigDecimal("openamount"));
  270. sqlFactory.addParameter("isrelevancepromotion", row.getString("isrelevancepromotion"));
  271. sqlFactory.addParameter("associationamount", row.getString("associationamount"));
  272. sqlFactory.addParameter("associationaccountclassid", row.getString("associationaccountclassid"));
  273. sqlFactory.addParameter("begdate", row.getString("begdate"));
  274. sqlFactory.addParameter("enddate", row.getString("enddate"));
  275. sqlFactory.addParameter("authmethod", row.getString("authmethod"));
  276. sqlFactory.addParameter("associationamount", 0);
  277. sqlFactory.addParameter("associationaccountclassid", 0);
  278. sqlFactory.addParameter("rebateratio", 0);
  279. sqlFactory.addParameter("packagetype", row.getString("packagetype"));
  280. // 备注说明
  281. sqlFactory.addParameter("remarks", row.getString("remarks"));
  282. sqlList.add(sqlFactory.getSQL());
  283. //复制商品
  284. Rows promotionitemsRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_items WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  285. //复制商品
  286. Rows promotionitemspriceRows = dbConnect.runSqlQuery("SELECT * from sa_promotion_itemprice WHERE sa_promotionid = " + sa_promotionid + " and siteid = '" + siteid + "'");
  287. RowsMap promotionitemspriceRowsMap = promotionitemspriceRows.toRowsMap("sa_promotion_itemsid");
  288. for (Row promotionitemsRow : promotionitemsRows) {
  289. sqlFactory = new SQLFactory(this, "促销方案商品新增");
  290. sqlFactory.addParameter("siteid", siteid);
  291. long sa_promotion_itemsidcopy = createTableID("sa_promotion_items");
  292. sqlFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  293. sqlFactory.addParameter("sa_promotionid", sa_promotion_copy);
  294. sqlFactory.addParameter("sa_promotion_itemgroupid", promotionitemsRow.getLong("sa_promotion_itemgroupid"));
  295. sqlFactory.addParameter("groupqty", promotionitemsRow.getBigDecimal("groupqty"));
  296. sqlFactory.addParameter("itemid", promotionitemsRow.getLong("itemid"));
  297. sqlFactory.addParameter("orderaddqty", promotionitemsRow.getBigDecimal("orderaddqty"));
  298. sqlFactory.addParameter("orderminqty", promotionitemsRow.getBigDecimal("orderminqty"));
  299. sqlFactory.addParameter("islimit", promotionitemsRow.getLong("islimit"));
  300. sqlFactory.addParameter("signaturecode", promotionitemsRow.getString("signaturecode"));
  301. sqlFactory.addParameter("packagetypemx", promotionitemsRow.getString("packagetypemx"));
  302. sqlFactory.addParameter("itemno", promotionitemsRow.getString("itemno"));
  303. sqlList.add(sqlFactory.getSQL());
  304. if (promotionitemspriceRowsMap.containsKey(promotionitemsRow.getString("sa_promotion_itemsid")) && promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid")).isNotEmpty()) {
  305. for (Row rowdetail : promotionitemspriceRowsMap.get(promotionitemsRow.getString("sa_promotion_itemsid"))) {
  306. SQLFactory itempriceFactory = new SQLFactory(this, "促销方案商品价格新增");
  307. itempriceFactory.addParameter("siteid", siteid);
  308. itempriceFactory.addParameter("sa_promotion_itempriceid",
  309. createTableID("sa_promotion_itemprice"));
  310. itempriceFactory.addParameter("sa_promotionid", sa_promotion_copy);
  311. itempriceFactory.addParameter("sa_promotion_itemsid", sa_promotion_itemsidcopy);
  312. itempriceFactory.addParameter("itemid", rowdetail.getLong("itemid"));
  313. itempriceFactory.addParameter("pricegrade", rowdetail.getBigDecimal("pricegrade"));
  314. itempriceFactory.addParameter("oldprice", rowdetail.getBigDecimal("oldprice"));
  315. itempriceFactory.addParameter("price", rowdetail.getBigDecimal("price"));
  316. sqlList.add(itempriceFactory.getSQL());
  317. }
  318. }
  319. }
  320. dbConnect.runSqlUpdate(sqlList);
  321. content.put("sa_promotionid", sa_promotion_copy);
  322. return querypromotionMain();
  323. }
  324. @API(title = "查询促销方案列表", apiversion = R.ID20221230144803.v1.class)
  325. @CACHEING
  326. public String querypromotionList() throws YosException {
  327. StringBuffer where = new StringBuffer(" 1=1 ");
  328. StringBuffer wherebegindate = new StringBuffer(" 1=1 ");
  329. StringBuffer whereenddate = new StringBuffer(" 1=1 ");
  330. if (content.containsKey("where")) {
  331. JSONObject whereObject = content.getJSONObject("where");
  332. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  333. where.append(" and(");
  334. where.append("t1.promnum like'%").append(whereObject.getString("condition")).append("%' ");
  335. where.append("or t1.promname like'%").append(whereObject.getString("condition")).append("%' ");
  336. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  337. where.append(")");
  338. }
  339. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  340. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  341. }
  342. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  343. wherebegindate.append(" and DATE_FORMAT(t1.begdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  344. whereenddate.append(" and DATE_FORMAT(t1.enddate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  345. }
  346. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  347. wherebegindate.append(" and DATE_FORMAT(t1.begdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  348. whereenddate.append(" and DATE_FORMAT(t1.enddate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  349. }
  350. }
  351. where.append("and ((").append(wherebegindate).append(") or (").append(whereenddate).append("))");
  352. // SQLFactory sqlFactory = new SQLFactory(this, "促销方案列表查询", pageSize, pageNumber, pageSorting);
  353. // sqlFactory.addParameter("siteid", siteid);
  354. // sqlFactory.addParameter_SQL("where", where);
  355. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  356. QuerySQL querySQL = queryPomotionList(where.toString());
  357. querySQL.setOrderBy(pageSorting);
  358. querySQL.setPage(pageSize, pageNumber);
  359. Rows rows = querySQL.query();
  360. //附件
  361. ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  362. RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  363. for (Row row : rows) {
  364. if (!StringUtils.isBlank(row.getString("tradefield"))) {
  365. row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  366. } else {
  367. row.put("tradefield", new JSONArray());
  368. }
  369. row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  370. }
  371. return getSucReturnObject().setData(rows).toString();
  372. }
  373. public QuerySQL queryPomotionList(String where) throws YosException {
  374. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_promotion");
  375. querySQL.setTableAlias("t1");
  376. querySQL.addJoinTable(JOINTYPE.left, "sa_brand", "t2", "t1.sa_brandid = t2.sa_brandid AND t1.siteid = t2.siteid",
  377. "brandname");
  378. querySQL.addJoinTable(JOINTYPE.left, "sa_accountclass", "t3", "t1.sa_accountclassid = t3.sa_accountclassid AND t1.siteid = t3.siteid",
  379. "accountno", "accountname");
  380. querySQL.setWhere("t1.siteid", siteid);
  381. querySQL.setWhere(where);
  382. return querySQL;
  383. }
  384. @API(title = "查询促销方案销售排行(经销商)", apiversion = R.ID20231007101703.v1.class)
  385. @CACHEING
  386. public String queryAgentSalesRanking() throws YosException {
  387. long sa_promotionid = content.getLongValue("sa_promotionid");
  388. StringBuffer where = new StringBuffer(" 1=1 ");
  389. if (content.containsKey("where")) {
  390. JSONObject whereObject = content.getJSONObject("where");
  391. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  392. where.append(" and(");
  393. where.append("t2.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  394. where.append("or t3.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  395. where.append(")");
  396. }
  397. }
  398. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(经销商)", pageSize, pageNumber, pageSorting);
  399. sqlFactory.addParameter("siteid", siteid);
  400. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  401. sqlFactory.addParameter_SQL("where", where);
  402. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  403. return getSucReturnObject().setData(rows).toString();
  404. }
  405. @API(title = "查询促销方案销售排行(商品)", apiversion = R.ID20231007101803.v1.class)
  406. @CACHEING
  407. public String queryItemSalesRanking() throws YosException {
  408. long sa_promotionid = content.getLongValue("sa_promotionid");
  409. StringBuffer where = new StringBuffer(" 1=1 ");
  410. if (content.containsKey("where")) {
  411. JSONObject whereObject = content.getJSONObject("where");
  412. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  413. where.append(" and(");
  414. where.append("t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  415. where.append("or t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  416. where.append(")");
  417. }
  418. }
  419. SQLFactory sqlFactory = new SQLFactory(this, "促销方案销售排行查询(商品)", pageSize, pageNumber, pageSorting);
  420. sqlFactory.addParameter("siteid", siteid);
  421. sqlFactory.addParameter("sa_promotionid", sa_promotionid);
  422. sqlFactory.addParameter_SQL("where", where);
  423. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  424. // //附件
  425. // ArrayList<Long> ids = rows.toArrayList("sa_promotionid", new ArrayList<>());
  426. // RowsMap attRowsMap = getAttachmentUrl("sa_promotion", ids);
  427. // for (Row row : rows) {
  428. // if (!StringUtils.isBlank(row.getString("tradefield"))) {
  429. // row.put("tradefield", JSONArray.parseArray(row.getString("tradefield")));
  430. // } else {
  431. // row.put("tradefield", new JSONArray());
  432. // }
  433. // row.put("attinfos", attRowsMap.getOrDefault(row.getString("sa_promotionid"), new Rows()));
  434. // }
  435. return getSucReturnObject().setData(rows).toString();
  436. }
  437. @API(title = "删除", apiversion = R.ID20221230144903.v1.class)
  438. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  439. public String delete() throws YosException {
  440. ArrayList<String> sqlList = new ArrayList<>();
  441. JSONArray sa_promotionids = content.getJSONArray("sa_promotionids");
  442. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_promotionids.size());
  443. for (Object o : sa_promotionids) {
  444. long sa_promotionid = Long.parseLong(o.toString());
  445. Rows RowsStatus = dbConnect.runSqlQuery("select sa_promotionid,status from sa_promotion where siteid='"
  446. + siteid + "' and sa_promotionid='" + sa_promotionid + "'");
  447. Rows Rowsdetail = dbConnect.runSqlQuery("select sa_promotion_authid from sa_promotion_auth where siteid='"
  448. + siteid + "' and sa_promotionid='" + sa_promotionid + "' and saledamount>0");
  449. if (RowsStatus.isNotEmpty()) {
  450. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  451. batchDeleteErr.addErr(sa_promotionid, "非新建状态的促销方案无法删除");
  452. continue;
  453. }
  454. }
  455. if (Rowsdetail.isNotEmpty()) {
  456. batchDeleteErr.addErr(sa_promotionid, "存在已购金额的授权经销商禁止删除促销活动");
  457. continue;
  458. }
  459. sqlList.add("delete from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  460. sqlList.add(
  461. "delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  462. sqlList.add("delete from sa_promotion_itemgroup where siteid='" + siteid + "' and sa_promotionid="
  463. + sa_promotionid);
  464. sqlList.add("delete from sa_promotion_itemprice where siteid='" + siteid + "' and sa_promotionid="
  465. + sa_promotionid);
  466. sqlList.add("delete from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid="
  467. + sa_promotionid);
  468. dbConnect.runSqlUpdate(sqlList);
  469. }
  470. return batchDeleteErr.getReturnObject().toString();
  471. }
  472. @API(title = "审核", apiversion = R.ID20221230145003.v1.class)
  473. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  474. public String check() throws YosException {
  475. Long sa_promotionid = content.getLong("sa_promotionid");
  476. Rows rows = dbConnect
  477. .runSqlQuery("select sa_promotionid,status,promnum,promname,type,associationaccountclassid,associationamount from sa_promotion where sa_promotionid ='"
  478. + sa_promotionid + "' and siteid='" + siteid + "'");
  479. Rows rowsdetails = dbConnect
  480. .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 ='"
  481. + sa_promotionid + "' and t1.siteid='" + siteid + "'");
  482. if (rows.isEmpty()) {
  483. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  484. } else {
  485. if (!rows.get(0).getString("status").equals("新建")) {
  486. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非新建状态,无法审核")
  487. .toString();
  488. }
  489. if (rows.get(0).getString("type").equals("返利促销")) {
  490. if (rows.get(0).getLong("associationaccountclassid") == 0 || rows.get(0).getBigDecimal("associationamount").compareTo(BigDecimal.ZERO) == 0) {
  491. return getErrReturnObject().setErrMsg("返利促销必须选择关联支付账户和关联支付金额").toString();
  492. }
  493. 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)");
  494. if (islimitRows.isEmpty()) {
  495. return getErrReturnObject().setErrMsg("返利促销必须授权经销商").toString();
  496. } else if (islimitRows.size() > 1 || (islimitRows.size() == 1 && !islimitRows.get(0).getBoolean("islimit"))) {
  497. return getErrReturnObject().setErrMsg("返利促销授权的经销商必须都为限购").toString();
  498. }
  499. }
  500. if (rows.get(0).getString("type").equals("套餐活动")) {
  501. if (StringUtils.isBlank(rows.get(0).getString("packagetype"))) {
  502. return getErrReturnObject().setErrMsg("套餐活动的方案必须选择套餐类型").toString();
  503. }
  504. Rows promotionItemsRows = dbConnect.runSqlQuery("select ifnull(t1.packagetypemx,'kong') 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 ifnull(t1.packagetypemx,'kong')");
  505. for(Row row : promotionItemsRows){
  506. if(row.getString("packagetypemx").equals("kong")){
  507. return getErrReturnObject().setErrMsg("品号:"+row.getString("itemno")+"未设置套餐分类,无法审核").toString();
  508. }
  509. }
  510. for(Row row : promotionItemsRows){
  511. if(row.getLong("count")>100){
  512. return getErrReturnObject().setErrMsg("套餐名:"+row.getString("packagetypemx")+" 商品添加数量已超上限(同一套餐最多只能添加100个商品)").toString();
  513. }
  514. }
  515. }
  516. for (Row row:rowsdetails) {
  517. if(row.getBigDecimal("orderminqty").compareTo(BigDecimal.ZERO)==0 || row.getBigDecimal("orderaddqty").compareTo(BigDecimal.ZERO)==0){
  518. return getErrReturnObject().setErrMsg("该促销方案【"+row.getString("itemno")+"】商品起订量或增量为0,无法审核").toString();
  519. }
  520. }
  521. }
  522. ArrayList<String> sqlList = new ArrayList<>();
  523. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案审核");
  524. sqlFactoryupdate.addParameter("siteid", siteid);
  525. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  526. sqlFactoryupdate.addParameter("checkby", username);
  527. sqlList.add(sqlFactoryupdate.getSQL());
  528. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "审核", "促销方案审核成功").getSQL());
  529. dbConnect.runSqlUpdate(sqlList);
  530. return getSucReturnObject().toString();
  531. }
  532. @API(title = "反审核", apiversion = R.ID20221230145103.v1.class)
  533. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  534. public String uncheck() throws YosException {
  535. Long sa_promotionid = content.getLong("sa_promotionid");
  536. Rows rows = dbConnect
  537. .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
  538. + sa_promotionid + "' and siteid='" + siteid + "'");
  539. if (rows.isEmpty()) {
  540. return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
  541. } else {
  542. if (!rows.get(0).getString("status").equals("审核")) {
  543. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非审核状态,无法反审核")
  544. .toString();
  545. }
  546. // if(!dbConnect.runSqlQuery("select * from sa_order where sa_promotionid="+sa_promotionid+" and siteid='"+siteid+"'").isEmpty()){
  547. // return getErrReturnObject().setErrMsg("存在已使用方案的订单,无法反审核")
  548. // .toString();
  549. // }
  550. }
  551. ArrayList<String> sqlList = new ArrayList<>();
  552. SQLFactory sqlFactoryupdate = new SQLFactory(this, "促销方案反审核");
  553. sqlFactoryupdate.addParameter("siteid", siteid);
  554. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  555. sqlFactoryupdate.addParameter("checkby", username);
  556. sqlList.add(sqlFactoryupdate.getSQL());
  557. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反审核", "促销方案反审核成功").getSQL());
  558. dbConnect.runSqlUpdate(sqlList);
  559. return getSucReturnObject().toString();
  560. }
  561. @API(title = "发布反发布", apiversion = R.ID20221230145203.v1.class)
  562. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  563. public String recheck() throws YosException {
  564. Long sa_promotionid = content.getLong("sa_promotionid");
  565. boolean isrecheck = content.getBooleanValue("isrecheck");
  566. Rows rows = dbConnect
  567. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  568. + sa_promotionid + "' and siteid='" + siteid + "'");
  569. for (Row row : rows) {
  570. if (isrecheck) {
  571. if (!row.getString("status").equals("审核")) {
  572. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非审核状态,无法发布")
  573. .toString();
  574. }
  575. } else {
  576. if (!row.getString("status").equals("发布")) {
  577. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法反发布")
  578. .toString();
  579. }
  580. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_promotionid="+sa_promotionid+" and siteid='"+siteid+"'");
  581. if(orderRows.isNotEmpty()){
  582. return getErrReturnObject().setErrMsg("该方案已存在订单,无法取消发布")
  583. .toString();
  584. }
  585. }
  586. }
  587. ArrayList<String> sqlList = new ArrayList<>();
  588. SQLFactory sqlFactoryupdate;
  589. if (isrecheck) {
  590. sqlFactoryupdate = new SQLFactory(this, "促销方案发布");
  591. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "发布", "促销方案发布成功").getSQL());
  592. } else {
  593. sqlFactoryupdate = new SQLFactory(this, "促销方案反发布");
  594. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反发布", "促销方案反发布成功").getSQL());
  595. }
  596. sqlFactoryupdate.addParameter("siteid", siteid);
  597. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  598. sqlFactoryupdate.addParameter("sendby", username);
  599. sqlList.add(sqlFactoryupdate.getSQL());
  600. dbConnect.runSqlUpdate(sqlList);
  601. return getSucReturnObject().toString();
  602. }
  603. @API(title = "关闭反关闭", apiversion = R.ID20230102095403.v1.class)
  604. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20221230144803.v1.class, R.ID20220103140003.v1.class})
  605. public String close() throws YosException {
  606. Long sa_promotionid = content.getLong("sa_promotionid");
  607. boolean isclose = content.getBooleanValue("isclose");
  608. Rows rows = dbConnect
  609. .runSqlQuery("select sa_promotionid,status,promnum from sa_promotion where sa_promotionid ='"
  610. + sa_promotionid + "' and siteid='" + siteid + "'");
  611. for (Row row : rows) {
  612. if (isclose) {
  613. if (!row.getString("status").equals("发布")) {
  614. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非发布状态,无法关闭")
  615. .toString();
  616. }
  617. } else {
  618. if (!row.getString("status").equals("关闭")) {
  619. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("promnum") + "】的促销方案为非关闭状态,无法反关闭")
  620. .toString();
  621. }
  622. }
  623. }
  624. ArrayList<String> sqlList = new ArrayList<>();
  625. SQLFactory sqlFactoryupdate;
  626. if (isclose) {
  627. sqlFactoryupdate = new SQLFactory(this, "促销方案关闭");
  628. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "关闭", "促销方案关闭成功").getSQL());
  629. } else {
  630. sqlFactoryupdate = new SQLFactory(this, "促销方案反关闭");
  631. sqlList.add(DataContrlLog.createLog(this, "sa_promotion", sa_promotionid, "反关闭", "促销方案反关闭成功").getSQL());
  632. }
  633. sqlFactoryupdate.addParameter("siteid", siteid);
  634. sqlFactoryupdate.addParameter("sa_promotionid", sa_promotionid);
  635. sqlFactoryupdate.addParameter("closeby", username);
  636. sqlList.add(sqlFactoryupdate.getSQL());
  637. dbConnect.runSqlUpdate(sqlList);
  638. return getSucReturnObject().toString();
  639. }
  640. @API(title = "切换授权方式", apiversion = R.ID20230509100504.v1.class)
  641. @CACHEING_CLEAN(apiversions = {R.ID20221230144703.v1.class, R.ID20220103140003.v1.class})
  642. public String updateAuthMethod() throws YosException {
  643. Long sa_promotionid = content.getLong("sa_promotionid");
  644. Rows rows = dbConnect.runSqlQuery("select sa_promotionid,status,authmethod from sa_promotion where sa_promotionid ='" + sa_promotionid + "' and siteid='" + siteid + "'");
  645. if (rows.isEmpty()) {
  646. return getErrReturnObject().setErrMsg("无效促销方案").toString();
  647. }
  648. if (!rows.get(0).getString("status").equals("新建")) {
  649. return getErrReturnObject().setErrMsg("非新建状态不能修改授权方式").toString();
  650. }
  651. if (dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid).isNotEmpty()) {
  652. return getErrReturnObject().setErrMsg("已有授权经销商,无法更改授权方式").toString();
  653. }
  654. ArrayList<String> sqlList = new ArrayList<>();
  655. sqlList.add("delete from sa_promotion_auth where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  656. if (rows.get(0).getLong("authmethod") == 0) {
  657. sqlList.add("update sa_promotion set authmethod=1 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  658. } else {
  659. sqlList.add("update sa_promotion set authmethod=0 where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  660. }
  661. dbConnect.runSqlUpdate(sqlList);
  662. return getSucReturnObject().toString();
  663. }
  664. @API(title = "结算", apiversion = R.ID20230826091303.v1.class)
  665. @CACHEING_CLEAN(apiClass = {promotionAuth.class, promotion.class})
  666. public String settlement() throws YosException {
  667. Long sa_promotion_authid = content.getLong("sa_promotion_authid");
  668. ArrayList<String> sqlList = new ArrayList<>();
  669. Rows promotionauthRows = dbConnect.runSqlQuery(
  670. "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 ='"
  671. + sa_promotion_authid + "' and t1.siteid='" + siteid + "'");
  672. if (promotionauthRows.isNotEmpty()) {
  673. if (!promotionauthRows.get(0).getString("type").equals("返利促销")) {
  674. return getErrReturnObject().setErrMsg("返利促销方案才能返利结算").toString();
  675. }
  676. if (promotionauthRows.get(0).getBoolean("settlestatus")) {
  677. return getErrReturnObject().setErrMsg("该经销商结算已经结算成功,无法再次结算").toString();
  678. }
  679. } else {
  680. return getErrReturnObject().setErrMsg("该记录不存在").toString();
  681. }
  682. long sa_promotionid = promotionauthRows.get(0).getLong("sa_promotionid");
  683. long sys_enterpriseid = promotionauthRows.get(0).getLong("sys_enterpriseid");
  684. BigDecimal saledamount = promotionauthRows.get(0).getBigDecimal("saledamount");
  685. BigDecimal rebateratio = promotionauthRows.get(0).getBigDecimal("rebateratio");
  686. System.out.println(saledamount);
  687. System.out.println(rebateratio);
  688. String agentnum = "";
  689. Rows enterpriserows = dbConnect.runSqlQuery("select * from sa_agents where sys_enterpriseid ='" + sys_enterpriseid + "' and siteid='" + siteid + "'");
  690. if (enterpriserows.isNotEmpty()) {
  691. agentnum = enterpriserows.get(0).getString("agentnum");
  692. }
  693. //查询经销商未关闭的促销订单
  694. 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 + "'");
  695. Boolean sendRemind = false;
  696. Long sa_cashbillid = 0L;
  697. if (rows.isNotEmpty()) {
  698. return getErrReturnObject().setErrMsg("存在未关闭的返利促销订单,无法结算").toString();
  699. } else {
  700. Rows accountclassrows = dbConnect.runSqlQuery("select sa_accountclassid from sa_accountclass where accountname ='返利金账户' and siteid='" + siteid + "'");
  701. if (accountclassrows.isNotEmpty()) {
  702. long sa_accountclassid = accountclassrows.get(0).getLong("sa_accountclassid");
  703. CashbillEntity entity = new CashbillEntity();
  704. entity.setAmount(saledamount.multiply(rebateratio));
  705. entity.setOwnerid(sa_promotion_authid);
  706. entity.setOwnertable("sa_promotion_auth");
  707. entity.setSource("返利");
  708. entity.setSourcenote("经销商【" + agentnum + "】,【" + promotionauthRows.get(0).getString("promname") + "】方案返利促销");
  709. entity.setRemarks("由活动单号" + promotionauthRows.get(0).getString("promnum") + "结算时生成");
  710. JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, sa_accountclassid, entity, true, true);
  711. sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  712. sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  713. sendRemind = true;
  714. sqlList.add("update sa_promotion_auth set settlestatus=1 where sa_promotion_authid=" + sa_promotion_authid + " and siteid='" + siteid + "'");
  715. }
  716. }
  717. dbConnect.runSqlUpdate(sqlList);
  718. if (sendRemind) {
  719. Accountbalance.remindSend(this, sys_enterpriseid, sa_cashbillid);
  720. }
  721. return getSucReturnObject().toString();
  722. }
  723. }