BackendMerchantController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.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/merchant")
  38. public class BackendMerchantController extends BaseController {
  39. /**
  40. * 商户服务接口
  41. */
  42. private MerchantService merchantService;
  43. /**
  44. * 系统设置服务接口
  45. * */
  46. private SettingService settingService;
  47. /**
  48. * 分页查询商户列表
  49. *
  50. * @param request HttpServletRequest对象
  51. * @return 商户列表
  52. */
  53. @ApiOperation(value = "分页查询商户列表")
  54. @RequestMapping(value = "/list")
  55. @CrossOrigin
  56. @PreAuthorize("@pms.hasPermission('merchant:index')")
  57. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  58. String token = request.getHeader("Access-Token");
  59. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  60. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  61. String merchantId = request.getParameter("id");
  62. String merchantName = request.getParameter("name");
  63. String status = request.getParameter("status");
  64. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  65. if (accountInfo == null) {
  66. return getFailureResult(1001, "请先登录");
  67. }
  68. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  69. merchantId = accountInfo.getMerchantId().toString();
  70. }
  71. PaginationRequest paginationRequest = new PaginationRequest();
  72. paginationRequest.setCurrentPage(page);
  73. paginationRequest.setPageSize(pageSize);
  74. Map<String, Object> params = new HashMap<>();
  75. if (StringUtil.isNotEmpty(merchantId)) {
  76. params.put("id", merchantId);
  77. }
  78. if (StringUtil.isNotEmpty(merchantName)) {
  79. params.put("name", merchantName);
  80. }
  81. if (StringUtil.isNotEmpty(status)) {
  82. params.put("status", status);
  83. }
  84. paginationRequest.setSearchParams(params);
  85. PaginationResponse<MtMerchant> paginationResponse = merchantService.queryMerchantListByPagination(paginationRequest);
  86. String imagePath = settingService.getUploadBasePath();
  87. // 商户类型列表
  88. MerchantTypeEnum[] typeListEnum = MerchantTypeEnum.values();
  89. List<ParamDto> typeList = new ArrayList<>();
  90. for (MerchantTypeEnum enumItem : typeListEnum) {
  91. ParamDto paramDto = new ParamDto();
  92. paramDto.setKey(enumItem.getKey());
  93. paramDto.setName(enumItem.getValue());
  94. paramDto.setValue(enumItem.getKey());
  95. typeList.add(paramDto);
  96. }
  97. Map<String, Object> result = new HashMap<>();
  98. result.put("dataList", paginationResponse);
  99. result.put("imagePath", imagePath);
  100. result.put("typeList", typeList);
  101. return getSuccessResult(result);
  102. }
  103. /**
  104. * 查询商户列表
  105. * */
  106. @ApiOperation(value = "查询商户列表")
  107. @RequestMapping(value = "/searchMerchant", method = RequestMethod.GET)
  108. @CrossOrigin
  109. public ResponseObject searchMerchant(HttpServletRequest request) throws BusinessCheckException {
  110. String merchantId = request.getParameter("id") == null ? "" : request.getParameter("id");
  111. String name = request.getParameter("name") == null ? "" : request.getParameter("name");
  112. Map<String, Object> params = new HashMap<>();
  113. if (StringUtil.isNotEmpty(merchantId)) {
  114. params.put("merchantId", merchantId);
  115. }
  116. if (StringUtil.isNotEmpty(name)) {
  117. params.put("name", name);
  118. }
  119. params.put("status", StatusEnum.ENABLED.getKey());
  120. List<MtMerchant> merchantList = merchantService.queryMerchantByParams(params);
  121. Map<String, Object> result = new HashMap<>();
  122. result.put("merchantList", merchantList);
  123. return getSuccessResult(result);
  124. }
  125. /**
  126. * 更新商户状态
  127. *
  128. * @param request
  129. * @return
  130. */
  131. @ApiOperation(value = "更新商户状态")
  132. @RequestMapping(value = "/updateStatus")
  133. @CrossOrigin
  134. @PreAuthorize("@pms.hasPermission('merchant:index')")
  135. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  136. String token = request.getHeader("Access-Token");
  137. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  138. Integer merchantId = params.get("merchantId") == null ? 0 : Integer.parseInt(params.get("merchantId").toString());
  139. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  140. if (accountInfo == null) {
  141. return getFailureResult(1001, "请先登录");
  142. }
  143. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  144. merchantId = accountInfo.getMerchantId();
  145. }
  146. String operator = accountInfo.getAccountName();
  147. merchantService.updateStatus(merchantId, operator, status);
  148. return getSuccessResult(true);
  149. }
  150. /**
  151. * 保存商户信息
  152. *
  153. * @param request HttpServletRequest对象
  154. * @return
  155. */
  156. @ApiOperation(value = "保存商户信息")
  157. @RequestMapping(value = "/save", method = RequestMethod.POST)
  158. @CrossOrigin
  159. @PreAuthorize("@pms.hasPermission('merchant:index')")
  160. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  161. String token = request.getHeader("Access-Token");
  162. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  163. if (accountInfo == null) {
  164. return getFailureResult(1001, "请先登录");
  165. }
  166. Integer merchantId = StringUtil.isEmpty(params.get("id").toString()) ? Integer.parseInt("0") : Integer.parseInt(params.get("id").toString());
  167. String name = CommonUtil.replaceXSS(params.get("name").toString());
  168. String merchantNo = CommonUtil.replaceXSS(params.get("no").toString());
  169. String contact = params.get("contact") == null ? "" : CommonUtil.replaceXSS(params.get("contact").toString());
  170. String phone = params.get("phone") == null ? "" : CommonUtil.replaceXSS(params.get("phone").toString());
  171. String description = params.get("description") == null ? "" : CommonUtil.replaceXSS(params.get("description").toString());
  172. String address = params.get("address") == null ? "" : CommonUtil.replaceXSS(params.get("address").toString());
  173. String status = params.get("status") == null ? "" : CommonUtil.replaceXSS(params.get("status").toString());
  174. String logo = params.get("logo") == null ? "" : CommonUtil.replaceXSS(params.get("logo").toString());
  175. String type = params.get("type") == null ? MerchantTypeEnum.RETAIL.getKey() : CommonUtil.replaceXSS(params.get("type").toString());
  176. String wxAppId = params.get("wxAppId") == null ? "" : CommonUtil.replaceXSS(params.get("wxAppId").toString());
  177. String wxAppSecret = params.get("wxAppSecret") == null ? "" : CommonUtil.replaceXSS(params.get("wxAppSecret").toString());
  178. String wxOfficialAppId = params.get("wxOfficialAppId") == null ? "" : CommonUtil.replaceXSS(params.get("wxOfficialAppId").toString());
  179. String wxOfficialAppSecret = params.get("wxOfficialAppSecret") == null ? "" : CommonUtil.replaceXSS(params.get("wxOfficialAppSecret").toString());
  180. MtMerchant merchantInfo = new MtMerchant();
  181. merchantInfo.setType(type);
  182. merchantInfo.setName(name);
  183. merchantInfo.setNo(merchantNo);
  184. merchantInfo.setContact(contact);
  185. merchantInfo.setPhone(phone);
  186. merchantInfo.setDescription(description);
  187. merchantInfo.setAddress(address);
  188. merchantInfo.setLogo(logo);
  189. merchantInfo.setWxAppId(wxAppId);
  190. merchantInfo.setWxAppSecret(wxAppSecret);
  191. merchantInfo.setWxOfficialAppId(wxOfficialAppId);
  192. merchantInfo.setWxOfficialAppSecret(wxOfficialAppSecret);
  193. merchantInfo.setStatus(status);
  194. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  195. merchantInfo.setId(accountInfo.getMerchantId());
  196. }
  197. if (StringUtil.isEmpty(name)) {
  198. return getFailureResult(201, "商户名称不能为空");
  199. } else {
  200. MtMerchant merchant = merchantService.queryMerchantByName(name);
  201. if (null != merchant && !merchant.getId().equals(merchantId)) {
  202. return getFailureResult(201, "该商户名称已经存在");
  203. }
  204. }
  205. if (merchantId <= 0 && accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  206. return getFailureResult(201, "抱歉,您没有添加商户的权限");
  207. }
  208. // 修改商户信息
  209. if (merchantId > 0) {
  210. merchantInfo.setId(merchantId);
  211. }
  212. String operator = accountInfo.getAccountName();
  213. merchantInfo.setOperator(operator);
  214. merchantService.saveMerchant(merchantInfo);
  215. return getSuccessResult(true);
  216. }
  217. /**
  218. * 获取商户详情
  219. *
  220. * @param id
  221. * @return
  222. */
  223. @ApiOperation(value = "获取商户详情")
  224. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  225. @CrossOrigin
  226. @PreAuthorize("@pms.hasPermission('merchant:index')")
  227. public ResponseObject getMerchantInfo(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  228. String token = request.getHeader("Access-Token");
  229. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  230. if (accountInfo == null) {
  231. return getFailureResult(1001, "请先登录");
  232. }
  233. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  234. id = accountInfo.getMerchantId();
  235. }
  236. MtMerchant merchantInfo = merchantService.queryMerchantById(id);;
  237. Map<String, Object> result = new HashMap<>();
  238. result.put("merchantInfo", merchantInfo);
  239. return getSuccessResult(result);
  240. }
  241. }