AwardItems.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.setSiteid(siteid);
  102. querySQL.setWhere("sa_awardid", sa_awardid);
  103. querySQL.setWhere(where.toString());
  104. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  105. Rows rows = querySQL.query();
  106. RowsMap rowsMap = getAttachmentUrl("sa_awarditems", rows.toArrayList("sa_awarditemsid", new ArrayList<>()));
  107. for (Row row : rows) {
  108. row.put("attinfos", rowsMap.getOrDefault(row.getString("sa_awarditemsid"), new Rows()));
  109. }
  110. return getSucReturnObject().setData(rows).toString();
  111. }
  112. @API(title = "中奖明细列表", apiversion = R.ID20240514133902.v1.class)
  113. public String awardmxlist() throws YosException {
  114. /*
  115. 过滤条件设置
  116. */
  117. StringBuffer where = new StringBuffer(" 1=1 ");
  118. if (content.containsKey("where")) {
  119. JSONObject whereObject = content.getJSONObject("where");
  120. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  121. where.append(" and (");
  122. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  123. where.append(" or t1.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  124. where.append(" or t1.province like'%").append(whereObject.getString("condition")).append("%' ");
  125. where.append(" or t1.city like'%").append(whereObject.getString("condition")).append("%' ");
  126. where.append(" or t1.county like'%").append(whereObject.getString("condition")).append("%' ");
  127. where.append(" or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  128. where.append(" or t1.sharename like'%").append(whereObject.getString("condition")).append("%' ");
  129. where.append(")");
  130. }
  131. if (whereObject.containsKey("awardname") && !"".equals(whereObject.getString("awardname"))) {
  132. where.append(" and (");
  133. where.append("t1.awardname ='").append(whereObject.getString("awardname")).append("' ");
  134. where.append(")");
  135. }
  136. }
  137. Long sa_awardid = content.getLongValue("sa_awardid");
  138. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_awardmx")
  139. .setTableAlias("t1");
  140. querySQL.addQueryFields("addressdetail", "concat(province,city,county,address)");
  141. querySQL.setSiteid(siteid);
  142. querySQL.setWhere(where.toString());
  143. querySQL.setWhere("sa_awardid", sa_awardid);
  144. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  145. Rows rows = querySQL.query();
  146. return getSucReturnObject().setData(rows).toString();
  147. }
  148. @API(title = "中奖", apiversion = R.ID20240514134002.v1.class)
  149. public String awardmx() throws YosException {
  150. Long sa_awardid = content.getLongValue("sa_awardid");
  151. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_awarditems WHERE sa_awardid=" + sa_awardid + " and outcount>0 and siteid='" + siteid + "'");
  152. int max = 0;
  153. int firstnumber = 0;
  154. for (Row row : rows) {
  155. max = max + row.getInteger("outcount");
  156. row.put("min", firstnumber + 1);
  157. row.put("max", firstnumber + row.getInteger("outcount"));
  158. firstnumber = firstnumber + row.getInteger("outcount");
  159. }
  160. if (max == 0) {
  161. return getErrReturnObject().setErrMsg("未中奖").toString();
  162. }
  163. int number = getNumber(1, max);
  164. Long sa_awarditemsid = 0L;
  165. for (Row row : rows) {
  166. if (row.getLong("min") <= number && row.getLong("max") >= number) {
  167. sa_awarditemsid = row.getLong("sa_awarditemsid");
  168. }
  169. }
  170. Rows awarditemsRows = dbConnect.runSqlQuery("SELECT * from sa_awarditems WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  171. if (awarditemsRows.isEmpty()) {
  172. return getErrReturnObject().setErrMsg("奖项不存在").toString();
  173. }
  174. Long count = 0L;
  175. rows = dbConnect.runSqlQuery("SELECT count(0) count from sa_awardmx WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  176. count = rows.get(0).getLong("count");
  177. dbConnect.runSqlUpdate("UPDATE sa_awarditems SET outcount=totalcount-" + count + ",realcount=totalcount-" + count + " WHERE sa_awarditemsid=" + sa_awarditemsid + " and siteid='" + siteid + "'");
  178. Long shareuserid = content.getLongValue("shareuserid");
  179. Long sa_awardmxid = createTableID("sa_awardmx");
  180. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_awardmx");
  181. insertSQL.setSiteid(siteid);
  182. insertSQL.setUniqueid(sa_awardmxid);
  183. insertSQL.setValue("sa_awardid", sa_awardid);
  184. insertSQL.setValue("awardname", awarditemsRows.get(0).getString("name"));
  185. insertSQL.setValue("price", awarditemsRows.get(0).getBigDecimal("price"));
  186. insertSQL.setValue("type", awarditemsRows.get(0).getString("type"));
  187. insertSQL.setValue("sharename", getUser(shareuserid).getString("name"));
  188. insertSQL.setValue("shareuserid", shareuserid);
  189. insertSQL.setValue("sa_awarditemsid", sa_awarditemsid);
  190. insertSQL.setValue("name", content.getStringValue("name"));
  191. insertSQL.setValue("phonenumber", content.getStringValue("phonenumber"));
  192. insertSQL.setValue("province", content.getStringValue("province"));
  193. insertSQL.setValue("city", content.getStringValue("city"));
  194. insertSQL.setValue("county", content.getStringValue("county"));
  195. insertSQL.setValue("address", content.getStringValue("address"));
  196. insertSQL.insert();
  197. Rows mxRows = dbConnect.runSqlQuery("SELECT * from sa_awardmx WHERE sa_awardmxid=" + sa_awardmxid + " and siteid='" + siteid + "'");
  198. return getSucReturnObject().setData(mxRows.get(0)).toString();
  199. }
  200. public int getNumber(int min, int max) {
  201. Random rand = new Random();
  202. return rand.nextInt(max - min + 1) + min;
  203. }
  204. @API(title = "我的中奖", apiversion = R.ID20240515140302.v1.class)
  205. public String myaward() throws YosException {
  206. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_awardmx")
  207. .setTableAlias("t1");
  208. querySQL.addJoinTable(JOINTYPE.inner, "sa_award", "t2", "t2.sa_awardid=t2.sa_awardid and t2.siteid=t1.siteid");
  209. querySQL.addQueryFields("addressdetail", "concat(province,city,county,address)");
  210. querySQL.addQueryFields("awardheadname", "t2.name");
  211. querySQL.setSiteid(siteid);
  212. querySQL.setWhere("createuserid", userid);
  213. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  214. Rows rows = querySQL.query();
  215. return getSucReturnObject().setData(rows).toString();
  216. }
  217. @API(title = "更新中奖信息", apiversion = R.ID2024052910464202.v1.class)
  218. public String newApiMethod() throws YosException {
  219. long sa_awardmxid = content.getLong("sa_awardmxid");
  220. String name = content.getString("name");// 姓名
  221. String phonenumber = content.getString("phonenumber");// 手机号
  222. String address = content.getString("address");// 详细地址
  223. String city = content.getString("city");// 城市
  224. String county = content.getString("county");// 区县
  225. String province = content.getString("province");// 省份
  226. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_awardmx");
  227. updateSQL.setValue("name", name);
  228. updateSQL.setValue("phonenumber", phonenumber);
  229. updateSQL.setValue("address", address);
  230. updateSQL.setValue("city", city);
  231. updateSQL.setValue("county", county);
  232. updateSQL.setValue("province", province);
  233. updateSQL.setSiteid(siteid);
  234. updateSQL.setUniqueid(sa_awardmxid);
  235. updateSQL.update();
  236. return getSucReturnObject().toString();
  237. }
  238. }