BackendCateController.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.GoodsCateDto;
  5. import com.fuint.common.enums.StatusEnum;
  6. import com.fuint.common.service.AccountService;
  7. import com.fuint.common.service.CateService;
  8. import com.fuint.common.service.SettingService;
  9. import com.fuint.common.service.StoreService;
  10. import com.fuint.common.util.CommonUtil;
  11. import com.fuint.common.util.TokenUtil;
  12. import com.fuint.framework.exception.BusinessCheckException;
  13. import com.fuint.framework.pagination.PaginationRequest;
  14. import com.fuint.framework.pagination.PaginationResponse;
  15. import com.fuint.framework.web.BaseController;
  16. import com.fuint.framework.web.ResponseObject;
  17. import com.fuint.repository.model.MtGoodsCate;
  18. import com.fuint.repository.model.MtStore;
  19. import com.fuint.repository.model.TAccount;
  20. import com.fuint.utils.StringUtil;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiOperation;
  23. import lombok.AllArgsConstructor;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.servlet.http.HttpServletRequest;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * 商品分类管理controller
  32. *
  33. * Created by FSQ
  34. * CopyRight https://www.fuint.cn
  35. */
  36. @Api(tags="管理端-商品分类相关接口")
  37. @RestController
  38. @AllArgsConstructor
  39. @RequestMapping(value = "/backendApi/goods/cate")
  40. public class BackendCateController extends BaseController {
  41. /**
  42. * 商品分类服务接口
  43. */
  44. private CateService cateService;
  45. /**
  46. * 配置服务接口
  47. * */
  48. private SettingService settingService;
  49. /**
  50. * 后台账户服务接口
  51. */
  52. private AccountService accountService;
  53. /**
  54. * 店铺服务接口
  55. */
  56. private StoreService storeService;
  57. /**
  58. * 获取商品分类列表
  59. *
  60. * @param request
  61. * @return
  62. * @throws BusinessCheckException
  63. */
  64. @ApiOperation(value = "获取商品分类列表")
  65. @RequestMapping(value = "/list", method = RequestMethod.GET)
  66. @CrossOrigin
  67. @PreAuthorize("@pms.hasPermission('goods:cate:index')")
  68. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  69. String token = request.getHeader("Access-Token");
  70. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  71. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  72. String name = request.getParameter("name");
  73. String status = request.getParameter("status");
  74. String searchStoreId = request.getParameter("storeId");
  75. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  76. if (accountInfo == null) {
  77. return getFailureResult(1001, "请先登录");
  78. }
  79. TAccount account = accountService.getAccountInfoById(accountInfo.getId());
  80. Integer storeId = account.getStoreId() == null ? 0 : account.getStoreId();
  81. PaginationRequest paginationRequest = new PaginationRequest();
  82. paginationRequest.setCurrentPage(page);
  83. paginationRequest.setPageSize(pageSize);
  84. Map<String, Object> params = new HashMap<>();
  85. if (StringUtil.isNotEmpty(name)) {
  86. params.put("name", name);
  87. }
  88. if (StringUtil.isNotEmpty(status)) {
  89. params.put("status", status);
  90. }
  91. if (StringUtil.isNotEmpty(searchStoreId)) {
  92. params.put("storeId", searchStoreId);
  93. }
  94. if (storeId > 0) {
  95. params.put("storeId", storeId);
  96. }
  97. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  98. params.put("merchantId", accountInfo.getMerchantId());
  99. }
  100. paginationRequest.setSearchParams(params);
  101. PaginationResponse<GoodsCateDto> paginationResponse = cateService.queryCateListByPagination(paginationRequest);
  102. Map<String, Object> paramsStore = new HashMap<>();
  103. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  104. if (storeId != null && storeId > 0) {
  105. paramsStore.put("storeId", storeId.toString());
  106. }
  107. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  108. paramsStore.put("merchantId", accountInfo.getMerchantId());
  109. }
  110. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  111. String imagePath = settingService.getUploadBasePath();
  112. Map<String, Object> result = new HashMap<>();
  113. result.put("imagePath", imagePath);
  114. result.put("storeList", storeList);
  115. result.put("paginationResponse", paginationResponse);
  116. return getSuccessResult(result);
  117. }
  118. /**
  119. * 更新商品分类状态
  120. *
  121. * @return
  122. */
  123. @ApiOperation(value = "更新商品分类状态")
  124. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  125. @CrossOrigin
  126. @PreAuthorize("@pms.hasPermission('goods:cate:index')")
  127. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  128. String token = request.getHeader("Access-Token");
  129. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  130. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  131. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  132. if (accountInfo == null) {
  133. return getFailureResult(1001, "请先登录");
  134. }
  135. MtGoodsCate mtCate = cateService.queryCateById(id);
  136. if (mtCate == null) {
  137. return getFailureResult(201, "该类别不存在");
  138. }
  139. String operator = accountInfo.getAccountName();
  140. MtGoodsCate cate = new MtGoodsCate();
  141. cate.setOperator(operator);
  142. cate.setId(id);
  143. cate.setStatus(status);
  144. try {
  145. cateService.updateCate(cate);
  146. } catch (BusinessCheckException e) {
  147. return getFailureResult(201, e.getMessage() == null ? "操作失败" : e.getMessage());
  148. }
  149. return getSuccessResult(true);
  150. }
  151. /**
  152. * 保存商品分类
  153. *
  154. * @param request HttpServletRequest对象
  155. * @return
  156. */
  157. @ApiOperation(value = "保存商品分类")
  158. @RequestMapping(value = "/save", method = RequestMethod.POST)
  159. @PreAuthorize("@pms.hasPermission('goods:cate:index')")
  160. public ResponseObject save(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  161. String token = request.getHeader("Access-Token");
  162. String id = params.get("id") == null ? "" : params.get("id").toString();
  163. String name = params.get("name") == null ? "" : CommonUtil.replaceXSS(params.get("name").toString());
  164. String description = params.get("description") == null ? "" : CommonUtil.replaceXSS(params.get("description").toString());
  165. String logo = params.get("logo") == null ? "" : CommonUtil.replaceXSS(params.get("logo").toString());
  166. String sort = params.get("sort") == null ? "0" : params.get("sort").toString();
  167. String status = params.get("status") == null ? StatusEnum.ENABLED.getKey() : params.get("status").toString();
  168. Integer storeId = (params.get("storeId") == null || StringUtil.isEmpty(params.get("storeId").toString())) ? 0 : Integer.parseInt(params.get("storeId").toString());
  169. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  170. if (accountDto == null) {
  171. return getFailureResult(1001, "请先登录");
  172. }
  173. Integer myStoreId = accountDto.getStoreId();
  174. if (myStoreId > 0) {
  175. storeId = myStoreId;
  176. }
  177. MtGoodsCate info = new MtGoodsCate();
  178. info.setName(name);
  179. info.setDescription(description);
  180. info.setLogo(logo);
  181. info.setSort(Integer.parseInt(sort));
  182. info.setStatus(status);
  183. info.setMerchantId(accountDto.getMerchantId());
  184. info.setStoreId(storeId);
  185. String operator = accountDto.getAccountName();
  186. info.setOperator(operator);
  187. if (StringUtil.isNotEmpty(id)) {
  188. info.setId(Integer.parseInt(id));
  189. cateService.updateCate(info);
  190. } else {
  191. cateService.addCate(info);
  192. }
  193. return getSuccessResult(true);
  194. }
  195. /**
  196. * 商品分类详情
  197. *
  198. * @param request
  199. * @return
  200. */
  201. @ApiOperation(value = "商品分类详情")
  202. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  203. @CrossOrigin
  204. @PreAuthorize("@pms.hasPermission('goods:cate:index')")
  205. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  206. String token = request.getHeader("Access-Token");
  207. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  208. if (accountDto == null) {
  209. return getFailureResult(1001, "请先登录");
  210. }
  211. MtGoodsCate mtCate = cateService.queryCateById(id);
  212. String imagePath = settingService.getUploadBasePath();
  213. Map<String, Object> result = new HashMap<>();
  214. result.put("cateInfo", mtCate);
  215. result.put("imagePath", imagePath);
  216. return getSuccessResult(result);
  217. }
  218. }