BackendMerchantController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.MerchantTypeEnum;
  6. import com.fuint.common.enums.StatusEnum;
  7. import com.fuint.common.service.MerchantService;
  8. import com.fuint.common.service.SettingService;
  9. import com.fuint.common.util.CommonUtil;
  10. import com.fuint.common.util.TokenUtil;
  11. import com.fuint.framework.exception.BusinessCheckException;
  12. import com.fuint.framework.pagination.PaginationRequest;
  13. import com.fuint.framework.pagination.PaginationResponse;
  14. import com.fuint.framework.web.BaseController;
  15. import com.fuint.framework.web.ResponseObject;
  16. import com.fuint.repository.model.MtMerchant;
  17. import com.fuint.utils.StringUtil;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import lombok.AllArgsConstructor;
  21. import org.springframework.security.access.prepost.PreAuthorize;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * 商户管理类controller
  29. *
  30. * Created by FSQ
  31. * CopyRight https://www.fuint.cn
  32. */
  33. @Api(tags="管理端-商户管理相关接口")
  34. @RestController
  35. @AllArgsConstructor
  36. @RequestMapping(value = "/backendApi/merchant")
  37. public class BackendMerchantController extends BaseController {
  38. /**
  39. * 商户服务接口
  40. */
  41. private MerchantService merchantService;
  42. /**
  43. * 系统设置服务接口
  44. * */
  45. private SettingService settingService;
  46. /**
  47. * 分页查询商户列表
  48. *
  49. * @param request HttpServletRequest对象
  50. * @return 商户列表
  51. */
  52. @ApiOperation(value = "分页查询商户列表")
  53. @RequestMapping(value = "/list")
  54. @CrossOrigin
  55. @PreAuthorize("@pms.hasPermission('merchant:index')")
  56. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  57. String token = request.getHeader("Access-Token");
  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. String merchantId = request.getParameter("id");
  61. String merchantName = request.getParameter("name");
  62. String status = request.getParameter("status");
  63. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  64. if (accountInfo == null) {
  65. return getFailureResult(1001, "请先登录");
  66. }
  67. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  68. merchantId = accountInfo.getMerchantId().toString();
  69. }
  70. PaginationRequest paginationRequest = new PaginationRequest();
  71. paginationRequest.setCurrentPage(page);
  72. paginationRequest.setPageSize(pageSize);
  73. Map<String, Object> params = new HashMap<>();
  74. if (StringUtil.isNotEmpty(merchantId)) {
  75. params.put("id", merchantId);
  76. }
  77. if (StringUtil.isNotEmpty(merchantName)) {
  78. params.put("name", merchantName);
  79. }
  80. if (StringUtil.isNotEmpty(status)) {
  81. params.put("status", status);
  82. }
  83. paginationRequest.setSearchParams(params);
  84. PaginationResponse<MtMerchant> paginationResponse = merchantService.queryMerchantListByPagination(paginationRequest);
  85. String imagePath = settingService.getUploadBasePath();
  86. // 商户类型列表
  87. List<ParamDto> typeList = MerchantTypeEnum.getMerchantTypeList();
  88. Map<String, Object> result = new HashMap<>();
  89. result.put("dataList", paginationResponse);
  90. result.put("imagePath", imagePath);
  91. result.put("typeList", typeList);
  92. return getSuccessResult(result);
  93. }
  94. /**
  95. * 查询商户列表
  96. * */
  97. @ApiOperation(value = "查询商户列表")
  98. @RequestMapping(value = "/searchMerchant", method = RequestMethod.GET)
  99. @CrossOrigin
  100. public ResponseObject searchMerchant(HttpServletRequest request) throws BusinessCheckException {
  101. String merchantId = request.getParameter("id") == null ? "" : request.getParameter("id");
  102. String name = request.getParameter("name") == null ? "" : request.getParameter("name");
  103. Map<String, Object> params = new HashMap<>();
  104. if (StringUtil.isNotEmpty(merchantId)) {
  105. params.put("merchantId", merchantId);
  106. }
  107. if (StringUtil.isNotEmpty(name)) {
  108. params.put("name", name);
  109. }
  110. params.put("status", StatusEnum.ENABLED.getKey());
  111. List<MtMerchant> merchantList = merchantService.queryMerchantByParams(params);
  112. Map<String, Object> result = new HashMap<>();
  113. result.put("merchantList", merchantList);
  114. return getSuccessResult(result);
  115. }
  116. /**
  117. * 更新商户状态
  118. *
  119. * @param request
  120. * @return
  121. */
  122. @ApiOperation(value = "更新商户状态")
  123. @RequestMapping(value = "/updateStatus")
  124. @CrossOrigin
  125. @PreAuthorize("@pms.hasPermission('merchant:index')")
  126. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  127. String token = request.getHeader("Access-Token");
  128. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  129. Integer merchantId = params.get("merchantId") == null ? 0 : Integer.parseInt(params.get("merchantId").toString());
  130. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  131. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  132. merchantId = accountInfo.getMerchantId();
  133. }
  134. String operator = accountInfo.getAccountName();
  135. merchantService.updateStatus(merchantId, operator, status);
  136. return getSuccessResult(true);
  137. }
  138. /**
  139. * 保存商户信息
  140. *
  141. * @param request HttpServletRequest对象
  142. * @return
  143. */
  144. @ApiOperation(value = "保存商户信息")
  145. @RequestMapping(value = "/save", method = RequestMethod.POST)
  146. @CrossOrigin
  147. @PreAuthorize("@pms.hasPermission('merchant:index')")
  148. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  149. String token = request.getHeader("Access-Token");
  150. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  151. Integer merchantId = StringUtil.isEmpty(params.get("id").toString()) ? Integer.parseInt("0") : Integer.parseInt(params.get("id").toString());
  152. String name = CommonUtil.replaceXSS(params.get("name").toString());
  153. String merchantNo = CommonUtil.replaceXSS(params.get("no").toString());
  154. String contact = params.get("contact") == null ? "" : CommonUtil.replaceXSS(params.get("contact").toString());
  155. String phone = params.get("phone") == null ? "" : CommonUtil.replaceXSS(params.get("phone").toString());
  156. String description = params.get("description") == null ? "" : CommonUtil.replaceXSS(params.get("description").toString());
  157. String address = params.get("address") == null ? "" : CommonUtil.replaceXSS(params.get("address").toString());
  158. String status = params.get("status") == null ? "" : CommonUtil.replaceXSS(params.get("status").toString());
  159. String logo = params.get("logo") == null ? "" : CommonUtil.replaceXSS(params.get("logo").toString());
  160. String type = params.get("type") == null ? MerchantTypeEnum.RETAIL.getKey() : CommonUtil.replaceXSS(params.get("type").toString());
  161. String wxAppId = params.get("wxAppId") == null ? "" : CommonUtil.replaceXSS(params.get("wxAppId").toString());
  162. String wxAppSecret = params.get("wxAppSecret") == null ? "" : CommonUtil.replaceXSS(params.get("wxAppSecret").toString());
  163. String wxOfficialAppId = params.get("wxOfficialAppId") == null ? "" : CommonUtil.replaceXSS(params.get("wxOfficialAppId").toString());
  164. String wxOfficialAppSecret = params.get("wxOfficialAppSecret") == null ? "" : CommonUtil.replaceXSS(params.get("wxOfficialAppSecret").toString());
  165. MtMerchant merchantInfo = new MtMerchant();
  166. merchantInfo.setType(type);
  167. merchantInfo.setName(name);
  168. merchantInfo.setNo(merchantNo);
  169. merchantInfo.setContact(contact);
  170. merchantInfo.setPhone(phone);
  171. merchantInfo.setDescription(description);
  172. merchantInfo.setAddress(address);
  173. merchantInfo.setLogo(logo);
  174. merchantInfo.setWxAppId(wxAppId);
  175. merchantInfo.setWxAppSecret(wxAppSecret);
  176. merchantInfo.setWxOfficialAppId(wxOfficialAppId);
  177. merchantInfo.setWxOfficialAppSecret(wxOfficialAppSecret);
  178. merchantInfo.setStatus(status);
  179. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  180. merchantInfo.setId(accountInfo.getMerchantId());
  181. }
  182. if (StringUtil.isEmpty(name)) {
  183. return getFailureResult(201, "商户名称不能为空");
  184. } else {
  185. MtMerchant merchant = merchantService.queryMerchantByName(name);
  186. if (null != merchant && !merchant.getId().equals(merchantId)) {
  187. return getFailureResult(201, "该商户名称已经存在");
  188. }
  189. }
  190. if (merchantId <= 0 && accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  191. return getFailureResult(201, "抱歉,您没有添加商户的权限");
  192. }
  193. // 修改商户信息
  194. if (merchantId > 0) {
  195. merchantInfo.setId(merchantId);
  196. }
  197. String operator = accountInfo.getAccountName();
  198. merchantInfo.setOperator(operator);
  199. merchantService.saveMerchant(merchantInfo);
  200. return getSuccessResult(true);
  201. }
  202. /**
  203. * 获取商户详情
  204. *
  205. * @param id
  206. * @return
  207. */
  208. @ApiOperation(value = "获取商户详情")
  209. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  210. @CrossOrigin
  211. @PreAuthorize("@pms.hasPermission('merchant:index')")
  212. public ResponseObject getMerchantInfo(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  213. String token = request.getHeader("Access-Token");
  214. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  215. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  216. id = accountInfo.getMerchantId();
  217. }
  218. MtMerchant merchantInfo = merchantService.queryMerchantById(id);
  219. Map<String, Object> result = new HashMap<>();
  220. result.put("merchantInfo", merchantInfo);
  221. return getSuccessResult(result);
  222. }
  223. }