BackendUserGradeController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.Constants;
  3. import com.fuint.common.dto.AccountInfo;
  4. import com.fuint.common.dto.ParamDto;
  5. import com.fuint.common.enums.StatusEnum;
  6. import com.fuint.common.enums.UserGradeCatchTypeEnum;
  7. import com.fuint.common.service.UserGradeService;
  8. import com.fuint.common.util.CommonUtil;
  9. import com.fuint.common.util.TokenUtil;
  10. import com.fuint.framework.exception.BusinessCheckException;
  11. import com.fuint.framework.pagination.PaginationRequest;
  12. import com.fuint.framework.pagination.PaginationResponse;
  13. import com.fuint.framework.web.BaseController;
  14. import com.fuint.framework.web.ResponseObject;
  15. import com.fuint.repository.model.MtUserGrade;
  16. import com.fuint.utils.StringUtil;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.AllArgsConstructor;
  20. import org.springframework.security.access.prepost.PreAuthorize;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.servlet.http.HttpServletRequest;
  23. import java.math.BigDecimal;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * 会员等级管理controller
  30. *
  31. * Created by FSQ
  32. * CopyRight https://www.fuint.cn
  33. */
  34. @Api(tags="管理端-会员等级相关接口")
  35. @RestController
  36. @AllArgsConstructor
  37. @RequestMapping(value = "/backendApi/userGrade")
  38. public class BackendUserGradeController extends BaseController {
  39. /**
  40. * 会员等级服务接口
  41. */
  42. private UserGradeService userGradeService;
  43. /**
  44. * 会员等级列表查询
  45. *
  46. * @param request HttpServletRequest对象
  47. * @return 会员等级列表
  48. */
  49. @ApiOperation(value = "会员等级列表查询")
  50. @RequestMapping(value = "/list", method = RequestMethod.GET)
  51. @CrossOrigin
  52. @PreAuthorize("@pms.hasPermission('userGrade:index')")
  53. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  54. String token = request.getHeader("Access-Token");
  55. String name = request.getParameter("name");
  56. String status = request.getParameter("status");
  57. String catchTypeKey = request.getParameter("catchType");
  58. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  59. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  60. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  61. PaginationRequest paginationRequest = new PaginationRequest();
  62. paginationRequest.setCurrentPage(page);
  63. paginationRequest.setPageSize(pageSize);
  64. Map<String, Object> params = new HashMap<>();
  65. if (StringUtil.isNotEmpty(name)) {
  66. params.put("name", name);
  67. }
  68. if (StringUtil.isNotEmpty(status)) {
  69. params.put("status", status);
  70. }
  71. if (StringUtil.isNotEmpty(catchTypeKey)) {
  72. params.put("catchType", catchTypeKey);
  73. }
  74. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  75. params.put("merchantId", accountInfo.getMerchantId());
  76. }
  77. paginationRequest.setSearchParams(params);
  78. PaginationResponse<MtUserGrade> paginationResponse = userGradeService.queryUserGradeListByPagination(paginationRequest);
  79. List<MtUserGrade> dataList = paginationResponse.getContent();
  80. List<MtUserGrade> content = new ArrayList<>();
  81. UserGradeCatchTypeEnum[] catchTypeList = UserGradeCatchTypeEnum.values();
  82. for (MtUserGrade grade : dataList) {
  83. for (UserGradeCatchTypeEnum catchType : catchTypeList) {
  84. if (grade.getCatchType().equals(catchType.getKey())) {
  85. grade.setCatchType(catchType.getValue());
  86. continue;
  87. }
  88. }
  89. content.add(grade);
  90. }
  91. paginationResponse.setContent(content);
  92. List<ParamDto> catchTypes = UserGradeCatchTypeEnum.getUserGradeCatchTypeList();
  93. Map<String, Object> result = new HashMap<>();
  94. result.put("paginationResponse", paginationResponse);
  95. result.put("catchTypeList", catchTypes);
  96. return getSuccessResult(result);
  97. }
  98. /**
  99. * 更新会员等级状态
  100. *
  101. * @return
  102. */
  103. @ApiOperation(value = "更新会员等级状态")
  104. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  105. @CrossOrigin
  106. @PreAuthorize("@pms.hasPermission('userGrade:index')")
  107. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  108. String token = request.getHeader("Access-Token");
  109. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  110. Integer userGradeId = param.get("userGradeId") == null ? 0 : Integer.parseInt(param.get("userGradeId").toString());
  111. String status = param.get("status") == null ? StatusEnum.ENABLED.getKey() : param.get("status").toString();
  112. MtUserGrade gradeInfo = userGradeService.queryUserGradeById(accountInfo.getMerchantId(), userGradeId, 0);
  113. if (gradeInfo == null) {
  114. return getFailureResult(201, "会员等级不存在");
  115. }
  116. gradeInfo.setStatus(status);
  117. userGradeService.updateUserGrade(gradeInfo);
  118. return getSuccessResult(true);
  119. }
  120. /**
  121. * 删除会员等级
  122. *
  123. * @param request
  124. * @return
  125. */
  126. @ApiOperation(value = "删除会员等级")
  127. @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
  128. @CrossOrigin
  129. @PreAuthorize("@pms.hasPermission('userGrade:index')")
  130. public ResponseObject delete(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  131. String token = request.getHeader("Access-Token");
  132. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  133. MtUserGrade mtUserGrade = userGradeService.queryUserGradeById(0, id, 0);
  134. if (mtUserGrade == null || !mtUserGrade.getMerchantId().equals(accountInfo.getMerchantId())) {
  135. return getFailureResult(201, "您没有删除权限");
  136. }
  137. userGradeService.deleteUserGrade(id, accountInfo.getAccountName());
  138. return getSuccessResult(true);
  139. }
  140. /**
  141. * 保存会员等级
  142. *
  143. * @param request HttpServletRequest对象
  144. * @return
  145. */
  146. @ApiOperation(value = "保存会员等级")
  147. @RequestMapping(value = "/save", method = RequestMethod.POST)
  148. @CrossOrigin
  149. @PreAuthorize("@pms.hasPermission('userGrade:add')")
  150. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  151. String token = request.getHeader("Access-Token");
  152. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  153. String grade = param.get("grade") == null ? "0" : param.get("grade").toString();
  154. String name = CommonUtil.replaceXSS(param.get("name").toString());
  155. String catchType = CommonUtil.replaceXSS(param.get("catchType").toString());
  156. String catchValue = CommonUtil.replaceXSS(param.get("catchValue").toString());
  157. String validDay = CommonUtil.replaceXSS(param.get("validDay").toString());
  158. String discount = CommonUtil.replaceXSS(param.get("discount").toString());
  159. String speedPoint = CommonUtil.replaceXSS(param.get("speedPoint").toString());
  160. String condition = param.get("catchCondition") == null ? "" : CommonUtil.replaceXSS(param.get("catchCondition").toString());
  161. String privilege = param.get("userPrivilege") == null ? "" : CommonUtil.replaceXSS(param.get("userPrivilege").toString());
  162. String status = param.get("status") == null ? StatusEnum.ENABLED.getKey() : CommonUtil.replaceXSS(param.get("status").toString());
  163. String id = param.get("id") == null ? "" : param.get("id").toString();
  164. if (StringUtil.isEmpty(grade) || StringUtil.isEmpty(name)) {
  165. return getFailureResult(201, "参数有误");
  166. }
  167. if (!CommonUtil.isNumeric(grade) || Integer.parseInt(grade) < 1) {
  168. return getFailureResult(201, "会员等级必须为正整数");
  169. }
  170. if (!CommonUtil.isNumeric(validDay) || Integer.parseInt(validDay) < 0) {
  171. return getFailureResult(201, "有效天数必须为正整数");
  172. }
  173. if (!CommonUtil.isNumeric(speedPoint) || Integer.parseInt(speedPoint) < 0) {
  174. return getFailureResult(201, "积分加速必须是数字");
  175. }
  176. if (!CommonUtil.isNumeric(catchValue)) {
  177. return getFailureResult(201, "升级条件值必须是数字");
  178. }
  179. MtUserGrade mtUserGrade;
  180. if (StringUtil.isEmpty(id)) {
  181. mtUserGrade = new MtUserGrade();
  182. } else {
  183. mtUserGrade = userGradeService.queryUserGradeById(accountInfo.getMerchantId(), Integer.parseInt(id), 0);
  184. }
  185. mtUserGrade.setGrade(Integer.parseInt(grade));
  186. mtUserGrade.setName(name);
  187. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  188. mtUserGrade.setMerchantId(accountInfo.getMerchantId());
  189. }
  190. if (StringUtil.isNotEmpty(catchType)) {
  191. mtUserGrade.setCatchType(catchType);
  192. }
  193. if (StringUtil.isNotEmpty(condition)) {
  194. mtUserGrade.setCatchCondition(condition);
  195. }
  196. if (StringUtil.isNotEmpty(privilege)) {
  197. mtUserGrade.setUserPrivilege(privilege);
  198. }
  199. if (StringUtil.isNotEmpty(catchValue)) {
  200. mtUserGrade.setCatchValue(new BigDecimal(catchValue));
  201. }
  202. if (StringUtil.isNotEmpty(validDay)) {
  203. mtUserGrade.setValidDay(Integer.parseInt(validDay));
  204. }
  205. if (StringUtil.isNotEmpty(discount)) {
  206. mtUserGrade.setDiscount(Float.parseFloat(discount));
  207. }
  208. if (StringUtil.isNotEmpty(speedPoint)) {
  209. mtUserGrade.setSpeedPoint(Float.parseFloat(speedPoint));
  210. }
  211. mtUserGrade.setStatus(status);
  212. if (StringUtil.isEmpty(id)) {
  213. userGradeService.addUserGrade(mtUserGrade);
  214. } else {
  215. mtUserGrade.setId(Integer.parseInt(id));
  216. userGradeService.updateUserGrade(mtUserGrade);
  217. }
  218. return getSuccessResult(true);
  219. }
  220. /**
  221. * 获取会员等级信息
  222. *
  223. * @param request
  224. * @return
  225. */
  226. @ApiOperation(value = "获取会员等级信息")
  227. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  228. @CrossOrigin
  229. @PreAuthorize("@pms.hasPermission('userGrade:index')")
  230. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  231. String token = request.getHeader("Access-Token");
  232. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  233. MtUserGrade userGradeInfo = userGradeService.queryUserGradeById(accountInfo.getMerchantId(), id, 0);
  234. Map<String, Object> result = new HashMap<>();
  235. result.put("userGradeInfo", userGradeInfo);
  236. return getSuccessResult(result);
  237. }
  238. }