AwardItems.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package restcontroller.webmanage.saletool.award;
  2. import beans.datacontrllog.DataContrlLog;
  3. import beans.time.Time;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.annotation.API;
  9. import common.data.*;
  10. import restcontroller.R;
  11. import restcontroller.webmanage.saletool.fad.FadHelper;
  12. import java.util.ArrayList;
  13. import java.util.Random;
  14. public class AwardItems extends Controller {
  15. /**
  16. * 构造函数
  17. *
  18. * @param content
  19. */
  20. public AwardItems(JSONObject content) throws YosException {
  21. super(content);
  22. }
  23. @API(title = "奖品项新增或编辑", apiversion = R.ID20240514131202.v1.class)
  24. public String insertOrUpdate() throws YosException {
  25. Long sa_awardid = content.getLongValue("sa_awardid");
  26. Long sa_awarditemsid = content.getLongValue("sa_awarditemsid");
  27. if (sa_awarditemsid <= 0) {
  28. sa_awarditemsid = createTableID("sa_awarditems");
  29. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_awarditems");
  30. insertSQL.setSiteid(siteid);
  31. insertSQL.setUniqueid(sa_awarditemsid);
  32. insertSQL.setValue("sequence", content.getLongValue("sequence"));
  33. insertSQL.setValue("name", content.getStringValue("name"));
  34. insertSQL.setValue("price", content.getBigDecimalValue("price"));
  35. insertSQL.setValue("percentage", content.getBigDecimalValue("percentage"));
  36. insertSQL.setValue("type", content.getStringValue("type"));
  37. insertSQL.setValue("realcount", content.getLongValue("totalcount"));
  38. insertSQL.setValue("outcount", content.getLongValue("outcount"));
  39. insertSQL.setValue("totalcount", content.getLongValue("totalcount"));
  40. insertSQL.setValue("sa_awardid", sa_awardid);
  41. insertSQL.insert();
  42. DataContrlLog.createLog(this, "sa_award", sa_awardid, "新建", "新建奖项 " + content.getStringValue("name")).insert();
  43. } else {
  44. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_awarditems");
  45. updateSQL.setSiteid(siteid);
  46. updateSQL.setUniqueid(sa_awarditemsid);
  47. updateSQL.setValue("sequence", content.getLongValue("sequence"));
  48. updateSQL.setValue("name", content.getStringValue("name"));
  49. updateSQL.setValue("price", content.getBigDecimalValue("price"));
  50. updateSQL.setValue("percentage", content.getBigDecimalValue("percentage"));
  51. updateSQL.setValue("type", content.getStringValue("type"));
  52. updateSQL.setValue("outcount", content.getLongValue("outcount"));
  53. updateSQL.setValue("totalcount", content.getLongValue("totalcount"));
  54. updateSQL.setValue("sa_awardid", sa_awardid);
  55. updateSQL.update();
  56. DataContrlLog.createLog(this, "sa_award", sa_awardid, "编辑", "编辑奖项 " + content.getStringValue("name")).insert();
  57. }
  58. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_awarditems WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  59. return getSucReturnObject().setData(rows.isNotEmpty() ? rows.get(0) : new Row()).toString();
  60. }
  61. @API(title = "奖品项删除", apiversion = R.ID20240514131302.v1.class)
  62. public String delete() throws YosException {
  63. JSONArray sa_awarditemsids = content.getJSONArray("sa_awarditemsids");
  64. if (sa_awarditemsids.size() == 0) {
  65. return getErrReturnObject().setErrMsg("请选择要删除的数据").toString();
  66. }
  67. //操作记录
  68. for (Object object : sa_awarditemsids) {
  69. long sa_awarditemsid = Long.parseLong(object.toString());
  70. Row row = dbConnect.runSqlQuery(0, "SELECT * from sa_awarditems WHERE siteid='" + siteid + "' and sa_awarditemsid=" + sa_awarditemsid);
  71. DataContrlLog.createLog(this, "sa_award", row.getLong("sa_awardid"), "删除", "删除奖项 " + row.getString("name")).insert();
  72. }
  73. DeleteSQL sqlFactory = SQLFactory.createDeleteSQL(this, "sa_awarditems");
  74. sqlFactory.setSiteid(siteid);
  75. sqlFactory.setWhere("sa_awarditemsid", sa_awarditemsids.toArray());
  76. sqlFactory.delete();
  77. return getSucReturnObject().toString();
  78. }
  79. @API(title = "奖品项列表", apiversion = R.ID20240514131402.v1.class)
  80. public String list() throws YosException {
  81. /*
  82. 过滤条件设置
  83. */
  84. StringBuffer where = new StringBuffer(" 1=1 ");
  85. if (content.containsKey("where")) {
  86. JSONObject whereObject = content.getJSONObject("where");
  87. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  88. where.append(" and (");
  89. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  90. where.append(")");
  91. }
  92. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  93. where.append(" and (");
  94. where.append("t1.type ='").append(whereObject.getString("type")).append("' ");
  95. where.append(")");
  96. }
  97. }
  98. Long sa_awardid = content.getLongValue("sa_awardid");
  99. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_awarditems")
  100. .setTableAlias("t1");
  101. querySQL.addJoinTable(JOINTYPE.inner, "sa_award", "t2", "t2.sa_awardid=t1.sa_awardid and t2.siteid=t1.siteid");
  102. querySQL.setSiteid(siteid);
  103. if (systemclient.equals("marketingtool")) {
  104. querySQL.setWhere("t2.isonsale", 1);
  105. }
  106. querySQL.setWhere("t1.sa_awardid", sa_awardid);
  107. querySQL.setWhere(where.toString());
  108. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  109. Rows rows = querySQL.query();
  110. RowsMap rowsMap = getAttachmentUrl("sa_awarditems", rows.toArrayList("sa_awarditemsid", new ArrayList<>()));
  111. for (Row row : rows) {
  112. row.put("attinfos", rowsMap.getOrDefault(row.getString("sa_awarditemsid"), new Rows()));
  113. }
  114. return getSucReturnObject().setData(rows).toString();
  115. }
  116. @API(title = "中奖明细列表", apiversion = R.ID20240514133902.v1.class)
  117. public String awardmxlist() throws YosException {
  118. /*
  119. 过滤条件设置
  120. */
  121. StringBuffer where = new StringBuffer(" 1=1 ");
  122. if (content.containsKey("where")) {
  123. JSONObject whereObject = content.getJSONObject("where");
  124. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  125. where.append(" and (");
  126. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  127. where.append(" or t1.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  128. where.append(" or t1.province like'%").append(whereObject.getString("condition")).append("%' ");
  129. where.append(" or t1.city like'%").append(whereObject.getString("condition")).append("%' ");
  130. where.append(" or t1.county like'%").append(whereObject.getString("condition")).append("%' ");
  131. where.append(" or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  132. where.append(" or t1.sharename like'%").append(whereObject.getString("condition")).append("%' ");
  133. where.append(")");
  134. }
  135. if (whereObject.containsKey("awardname") && !"".equals(whereObject.getString("awardname"))) {
  136. where.append(" and (");
  137. where.append("t1.awardname ='").append(whereObject.getString("awardname")).append("' ");
  138. where.append(")");
  139. }
  140. }
  141. Long sa_awardid = content.getLongValue("sa_awardid");
  142. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_awardmx")
  143. .setTableAlias("t1");
  144. querySQL.addQueryFields("addressdetail", "concat(province,city,county,address)");
  145. querySQL.setSiteid(siteid);
  146. querySQL.setWhere(where.toString());
  147. querySQL.setWhere("sa_awardid", sa_awardid);
  148. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  149. Rows rows = querySQL.query();
  150. return getSucReturnObject().setData(rows).toString();
  151. }
  152. @API(title = "中奖", apiversion = R.ID20240514134002.v1.class)
  153. public String awardmx() throws YosException {
  154. Long sa_awardid = content.getLongValue("sa_awardid");
  155. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_awarditems WHERE sa_awardid=" + sa_awardid + " and outcount>0 and siteid='" + siteid + "'");
  156. int max = 0;
  157. int firstnumber = 0;
  158. for (Row row : rows) {
  159. max = max + row.getInteger("outcount");
  160. row.put("min", firstnumber + 1);
  161. row.put("max", firstnumber + row.getInteger("outcount"));
  162. firstnumber = firstnumber + row.getInteger("outcount");
  163. }
  164. if (max == 0) {
  165. return getErrReturnObject().setErrMsg("未中奖").toString();
  166. }
  167. int number = getNumber(1, max);
  168. Long sa_awarditemsid = 0L;
  169. for (Row row : rows) {
  170. if (row.getLong("min") <= number && row.getLong("max") >= number) {
  171. sa_awarditemsid = row.getLong("sa_awarditemsid");
  172. }
  173. }
  174. Rows awarditemsRows = dbConnect.runSqlQuery("SELECT * from sa_awarditems WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  175. if (awarditemsRows.isEmpty()) {
  176. return getErrReturnObject().setErrMsg("奖项不存在").toString();
  177. }
  178. Long count = 0L;
  179. rows = dbConnect.runSqlQuery("SELECT count(0) count from sa_awardmx WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  180. count = rows.get(0).getLong("count");
  181. dbConnect.runSqlUpdate("UPDATE sa_awarditems SET outcount=totalcount-" + count + ",realcount=totalcount-" + count + " WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  182. Long shareuserid = content.getLongValue("shareuserid");
  183. Long sa_awardmxid = createTableID("sa_awardmx");
  184. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_awardmx");
  185. insertSQL.setSiteid(siteid);
  186. insertSQL.setUniqueid(sa_awardmxid);
  187. insertSQL.setValue("sa_awardid", sa_awardid);
  188. insertSQL.setValue("awardname", awarditemsRows.get(0).getString("name"));
  189. insertSQL.setValue("price", awarditemsRows.get(0).getBigDecimal("price"));
  190. insertSQL.setValue("type", awarditemsRows.get(0).getString("type"));
  191. insertSQL.setValue("sharename", getUser(shareuserid).getString("name"));
  192. insertSQL.setValue("shareuserid", shareuserid);
  193. insertSQL.setValue("sa_awarditemsid", sa_awarditemsid);
  194. insertSQL.setValue("name", content.getStringValue("name"));
  195. insertSQL.setValue("phonenumber", content.getStringValue("phonenumber"));
  196. insertSQL.setValue("province", content.getStringValue("province"));
  197. insertSQL.setValue("city", content.getStringValue("city"));
  198. insertSQL.setValue("county", content.getStringValue("county"));
  199. insertSQL.setValue("address", content.getStringValue("address"));
  200. insertSQL.insert();
  201. Rows mxRows = dbConnect.runSqlQuery("SELECT * from sa_awardmx WHERE sa_awardmxid=" + sa_awardmxid + " and siteid='" + siteid + "'");
  202. return getSucReturnObject().setData(mxRows.get(0)).toString();
  203. }
  204. public int getNumber(int min, int max) {
  205. Random rand = new Random();
  206. return rand.nextInt(max - min + 1) + min;
  207. }
  208. @API(title = "我的中奖", apiversion = R.ID20240515140302.v1.class)
  209. public String myaward() throws YosException {
  210. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_awardmx")
  211. .setTableAlias("t1");
  212. querySQL.addJoinTable(JOINTYPE.inner, "sa_award", "t2", "t2.sa_awardid=t1.sa_awardid and t2.siteid=t1.siteid");
  213. querySQL.addQueryFields("addressdetail", "concat(province,city,county,address)");
  214. querySQL.addQueryFields("awardheadname", "t2.name");
  215. querySQL.setSiteid(siteid);
  216. querySQL.setWhere("createuserid", userid);
  217. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  218. Rows rows = querySQL.query();
  219. return getSucReturnObject().setData(rows).toString();
  220. }
  221. @API(title = "更新中奖信息", apiversion = R.ID2024052910464202.v1.class)
  222. public String newApiMethod() throws YosException {
  223. long sa_awardmxid = content.getLong("sa_awardmxid");
  224. String name = content.getString("name");// 姓名
  225. String phonenumber = content.getString("phonenumber");// 手机号
  226. String address = content.getString("address");// 详细地址
  227. String city = content.getString("city");// 城市
  228. String county = content.getString("county");// 区县
  229. String province = content.getString("province");// 省份
  230. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_awardmx");
  231. updateSQL.setValue("name", name);
  232. updateSQL.setValue("phonenumber", phonenumber);
  233. updateSQL.setValue("address", address);
  234. updateSQL.setValue("city", city);
  235. updateSQL.setValue("county", county);
  236. updateSQL.setValue("province", province);
  237. updateSQL.setSiteid(siteid);
  238. updateSQL.setUniqueid(sa_awardmxid);
  239. updateSQL.update();
  240. return getSucReturnObject().toString();
  241. }
  242. }