BackendCateController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.web.bind.annotation.*;
  25. import javax.servlet.http.HttpServletRequest;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * 商品分类管理controller
  31. *
  32. * Created by FSQ
  33. * CopyRight https://www.fuint.cn
  34. */
  35. @Api(tags="管理端-商品分类相关接口")
  36. @RestController
  37. @RequestMapping(value = "/backendApi/goods/cate")
  38. public class BackendCateController extends BaseController {
  39. /**
  40. * 商品分类服务接口
  41. */
  42. @Autowired
  43. private CateService cateService;
  44. /**
  45. * 配置服务接口
  46. * */
  47. @Autowired
  48. private SettingService settingService;
  49. /**
  50. * 后台账户服务接口
  51. */
  52. @Autowired
  53. private AccountService accountService;
  54. /**
  55. * 店铺服务接口
  56. */
  57. @Autowired
  58. private StoreService storeService;
  59. /**
  60. * 获取商品分类列表
  61. *
  62. * @param request
  63. * @return
  64. * @throws BusinessCheckException
  65. */
  66. @ApiOperation(value = "获取商品分类列表")
  67. @RequestMapping(value = "/list", method = RequestMethod.GET)
  68. @CrossOrigin
  69. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  70. String token = request.getHeader("Access-Token");
  71. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  72. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  73. String name = request.getParameter("name");
  74. String status = request.getParameter("status");
  75. String searchStoreId = request.getParameter("storeId");
  76. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  77. if (accountInfo == null) {
  78. return getFailureResult(1001, "请先登录");
  79. }
  80. TAccount account = accountService.getAccountInfoById(accountInfo.getId());
  81. Integer storeId = account.getStoreId() == null ? 0 : account.getStoreId();
  82. PaginationRequest paginationRequest = new PaginationRequest();
  83. paginationRequest.setCurrentPage(page);
  84. paginationRequest.setPageSize(pageSize);
  85. Map<String, Object> params = new HashMap<>();
  86. if (StringUtil.isNotEmpty(name)) {
  87. params.put("name", name);
  88. }
  89. if (StringUtil.isNotEmpty(status)) {
  90. params.put("status", status);
  91. }
  92. if (StringUtil.isNotEmpty(searchStoreId)) {
  93. params.put("storeId", searchStoreId);
  94. }
  95. if (storeId > 0) {
  96. params.put("storeId", storeId);
  97. }
  98. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  99. params.put("merchantId", accountInfo.getMerchantId());
  100. }
  101. paginationRequest.setSearchParams(params);
  102. paginationRequest.setSortColumn(new String[]{"sort asc", "status asc"});
  103. PaginationResponse<GoodsCateDto> paginationResponse = cateService.queryCateListByPagination(paginationRequest);
  104. Map<String, Object> paramsStore = new HashMap<>();
  105. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  106. if (storeId != null && storeId > 0) {
  107. paramsStore.put("storeId", storeId.toString());
  108. }
  109. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  110. paramsStore.put("merchantId", accountInfo.getMerchantId());
  111. }
  112. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  113. String imagePath = settingService.getUploadBasePath();
  114. Map<String, Object> result = new HashMap<>();
  115. result.put("imagePath", imagePath);
  116. result.put("storeList", storeList);
  117. result.put("paginationResponse", paginationResponse);
  118. return getSuccessResult(result);
  119. }
  120. /**
  121. * 更新商品分类状态
  122. *
  123. * @return
  124. */
  125. @ApiOperation(value = "更新商品分类状态")
  126. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  127. @CrossOrigin
  128. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  129. String token = request.getHeader("Access-Token");
  130. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  131. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  132. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  133. if (accountInfo == null) {
  134. return getFailureResult(1001, "请先登录");
  135. }
  136. MtGoodsCate mtCate = cateService.queryCateById(id);
  137. if (mtCate == null) {
  138. return getFailureResult(201, "该类别不存在");
  139. }
  140. String operator = accountInfo.getAccountName();
  141. MtGoodsCate cate = new MtGoodsCate();
  142. cate.setOperator(operator);
  143. cate.setId(id);
  144. cate.setStatus(status);
  145. try {
  146. cateService.updateCate(cate);
  147. } catch (BusinessCheckException e) {
  148. return getFailureResult(201, e.getMessage() == null ? "操作失败" : e.getMessage());
  149. }
  150. return getSuccessResult(true);
  151. }
  152. /**
  153. * 保存商品分类
  154. *
  155. * @param request HttpServletRequest对象
  156. * @return
  157. */
  158. @ApiOperation(value = "保存商品分类")
  159. @RequestMapping(value = "/save", method = RequestMethod.POST)
  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. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  205. String token = request.getHeader("Access-Token");
  206. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  207. if (accountDto == null) {
  208. return getFailureResult(1001, "请先登录");
  209. }
  210. MtGoodsCate mtCate = cateService.queryCateById(id);
  211. String imagePath = settingService.getUploadBasePath();
  212. Map<String, Object> result = new HashMap<>();
  213. result.put("cateInfo", mtCate);
  214. result.put("imagePath", imagePath);
  215. return getSuccessResult(result);
  216. }
  217. }