BackendStockController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.GoodsDto;
  5. import com.fuint.common.enums.StatusEnum;
  6. import com.fuint.common.service.*;
  7. import com.fuint.common.util.CommonUtil;
  8. import com.fuint.common.util.TokenUtil;
  9. import com.fuint.framework.exception.BusinessCheckException;
  10. import com.fuint.framework.pagination.PaginationRequest;
  11. import com.fuint.framework.pagination.PaginationResponse;
  12. import com.fuint.framework.web.BaseController;
  13. import com.fuint.framework.web.ResponseObject;
  14. import com.fuint.repository.mapper.MtGoodsMapper;
  15. import com.fuint.repository.mapper.MtGoodsSkuMapper;
  16. import com.fuint.repository.model.*;
  17. import com.fuint.utils.StringUtil;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.util.*;
  25. /**
  26. * 商品库存管理管理controller
  27. *
  28. * Created by FSQ
  29. * CopyRight https://www.fuint.cn
  30. */
  31. @Api(tags="管理端-商品库存管理相关接口")
  32. @RestController
  33. @RequestMapping(value = "/backendApi/stock")
  34. public class BackendStockController extends BaseController {
  35. @Resource
  36. private MtGoodsMapper mtGoodsMapper;
  37. @Resource
  38. private MtGoodsSkuMapper mtGoodsSkuMapper;
  39. /**
  40. * 商品分类服务接口
  41. */
  42. @Autowired
  43. private StockService stockService;
  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 description = request.getParameter("description");
  74. String status = request.getParameter("status");
  75. String searchStoreId = request.getParameter("storeId");
  76. String type = request.getParameter("type");
  77. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  78. if (accountInfo == null) {
  79. return getFailureResult(1001, "请先登录");
  80. }
  81. TAccount account = accountService.getAccountInfoById(accountInfo.getId());
  82. Integer storeId = account.getStoreId() == null ? 0 : account.getStoreId();
  83. PaginationRequest paginationRequest = new PaginationRequest();
  84. paginationRequest.setCurrentPage(page);
  85. paginationRequest.setPageSize(pageSize);
  86. Map<String, Object> params = new HashMap<>();
  87. if (StringUtil.isNotEmpty(description)) {
  88. params.put("description", description);
  89. }
  90. if (StringUtil.isNotEmpty(status)) {
  91. params.put("status", status);
  92. }
  93. if (StringUtil.isNotEmpty(type)) {
  94. params.put("type", type);
  95. }
  96. if (account.getMerchantId() != null && account.getMerchantId() > 0) {
  97. params.put("merchantId", account.getMerchantId());
  98. }
  99. if (StringUtil.isNotEmpty(searchStoreId)) {
  100. params.put("storeId", searchStoreId);
  101. }
  102. if (storeId > 0) {
  103. params.put("storeId", storeId);
  104. }
  105. paginationRequest.setSearchParams(params);
  106. paginationRequest.setSortColumn(new String[]{"sort asc", "status asc"});
  107. PaginationResponse<MtStock> paginationResponse = stockService.queryStockListByPagination(paginationRequest);
  108. Map<String, Object> paramsStore = new HashMap<>();
  109. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  110. if (storeId != null && storeId > 0) {
  111. paramsStore.put("storeId", storeId.toString());
  112. }
  113. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  114. paramsStore.put("merchantId", accountInfo.getMerchantId());
  115. }
  116. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  117. String imagePath = settingService.getUploadBasePath();
  118. Map<String, Object> result = new HashMap<>();
  119. result.put("imagePath", imagePath);
  120. result.put("storeList", storeList);
  121. result.put("paginationResponse", paginationResponse);
  122. return getSuccessResult(result);
  123. }
  124. /**
  125. * 删除库存管理记录状态
  126. *
  127. * @return
  128. */
  129. @ApiOperation(value = "删除库存管理记录状态")
  130. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  131. @CrossOrigin
  132. public ResponseObject delete(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  133. String token = request.getHeader("Access-Token");
  134. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  135. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  136. if (accountInfo == null) {
  137. return getFailureResult(1001, "请先登录");
  138. }
  139. MtStock mtStock = stockService.queryStockById(id.longValue());
  140. if (mtStock == null) {
  141. return getFailureResult(201, "该数据不存在");
  142. }
  143. String operator = accountInfo.getAccountName();
  144. stockService.delete(id, operator);
  145. return getSuccessResult(true);
  146. }
  147. /**
  148. * 保存库存管理记录
  149. *
  150. * @param request HttpServletRequest对象
  151. * @return
  152. */
  153. @ApiOperation(value = "保存库存管理记录")
  154. @RequestMapping(value = "/save", method = RequestMethod.POST)
  155. public ResponseObject save(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  156. String token = request.getHeader("Access-Token");
  157. String type = params.get("type") == null ? "" : CommonUtil.replaceXSS(params.get("type").toString());
  158. String description = params.get("description") == null ? "" : CommonUtil.replaceXSS(params.get("description").toString());
  159. String status = params.get("status") == null ? StatusEnum.ENABLED.getKey() : params.get("status").toString();
  160. Integer storeId = (params.get("storeId") == null || StringUtil.isEmpty(params.get("storeId").toString())) ? 0 : Integer.parseInt(params.get("storeId").toString());
  161. List<LinkedHashMap> goodsList = (List) params.get("goodsList");
  162. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  163. if (accountDto == null) {
  164. return getFailureResult(1001, "请先登录");
  165. }
  166. Integer myStoreId = accountDto.getStoreId();
  167. if (myStoreId > 0) {
  168. storeId = myStoreId;
  169. }
  170. MtStock info = new MtStock();
  171. info.setDescription(description);
  172. info.setStatus(status);
  173. info.setStoreId(storeId);
  174. info.setType(type);
  175. String operator = accountDto.getAccountName();
  176. info.setOperator(operator);
  177. try {
  178. stockService.addStock(info, goodsList);
  179. } catch (BusinessCheckException e) {
  180. return getFailureResult(201, e.getMessage());
  181. }
  182. return getSuccessResult(true);
  183. }
  184. /**
  185. * 获取库存管理记录详情
  186. *
  187. * @param request
  188. * @return
  189. */
  190. @ApiOperation(value = "获取库存管理记录详情")
  191. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  192. @CrossOrigin
  193. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  194. String token = request.getHeader("Access-Token");
  195. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  196. if (accountDto == null) {
  197. return getFailureResult(1001, "请先登录");
  198. }
  199. MtStock mtStock = stockService.queryStockById(id.longValue());
  200. Map<String, Object> param = new HashMap<>();
  201. param.put("STOCK_ID", mtStock.getId());
  202. List<MtStockItem> stockItems = stockService.queryItemByParams(param);
  203. List<GoodsDto> goodsList = new ArrayList<>();
  204. if (stockItems != null && stockItems.size() > 0) {
  205. for (MtStockItem stockItem : stockItems) {
  206. MtGoods mtGoods = mtGoodsMapper.selectById(stockItem.getGoodsId());
  207. GoodsDto goodsDto = new GoodsDto();
  208. goodsDto.setGoodsNo(mtGoods.getGoodsNo());
  209. goodsDto.setName(mtGoods.getName());
  210. goodsDto.setLogo(mtGoods.getLogo());
  211. goodsDto.setNum(stockItem.getNum());
  212. goodsDto.setStatus(mtGoods.getStatus());
  213. if (stockItem.getSkuId() != null && stockItem.getSkuId() > 0) {
  214. MtGoodsSku mtGoodsSku = mtGoodsSkuMapper.selectById(stockItem.getSkuId());
  215. if (mtGoodsSku != null) {
  216. goodsDto.setGoodsNo(mtGoodsSku.getSkuNo());
  217. if (StringUtil.isNotEmpty(mtGoodsSku.getLogo())) {
  218. goodsDto.setLogo(mtGoodsSku.getLogo());
  219. }
  220. }
  221. }
  222. goodsList.add(goodsDto);
  223. }
  224. }
  225. Map<String, Object> result = new HashMap<>();
  226. result.put("stockInfo", mtStock);
  227. result.put("goodsList", goodsList);
  228. return getSuccessResult(result);
  229. }
  230. }