AwardItems.java 11 KB

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