ClientGoodsController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package com.fuint.module.clientApi.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fuint.common.Constants;
  4. import com.fuint.common.dto.*;
  5. import com.fuint.common.enums.StatusEnum;
  6. import com.fuint.common.enums.YesOrNoEnum;
  7. import com.fuint.common.param.GoodsInfoParam;
  8. import com.fuint.common.service.CateService;
  9. import com.fuint.common.service.GoodsService;
  10. import com.fuint.common.service.MerchantService;
  11. import com.fuint.common.service.SettingService;
  12. import com.fuint.common.util.CommonUtil;
  13. import com.fuint.framework.exception.BusinessCheckException;
  14. import com.fuint.framework.pagination.PaginationRequest;
  15. import com.fuint.framework.pagination.PaginationResponse;
  16. import com.fuint.framework.web.BaseController;
  17. import com.fuint.framework.web.ResponseObject;
  18. import com.fuint.module.clientApi.request.GoodsSearchRequest;
  19. import com.fuint.repository.model.MtGoods;
  20. import com.fuint.repository.model.MtGoodsCate;
  21. import com.fuint.repository.model.MtGoodsSku;
  22. import com.fuint.repository.model.MtGoodsSpec;
  23. import com.fuint.utils.StringUtil;
  24. import io.swagger.annotations.Api;
  25. import io.swagger.annotations.ApiOperation;
  26. import lombok.AllArgsConstructor;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.lang.reflect.InvocationTargetException;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * 商品类controller
  36. *
  37. * Created by FSQ
  38. * CopyRight https://www.fuint.cn
  39. */
  40. @Api(tags="会员端-商品相关接口")
  41. @RestController
  42. @AllArgsConstructor
  43. @RequestMapping(value = "/clientApi/goodsApi")
  44. public class ClientGoodsController extends BaseController {
  45. /**
  46. * 商品服务接口
  47. * */
  48. private GoodsService goodsService;
  49. /**
  50. * 商品类别服务接口
  51. * */
  52. private CateService cateService;
  53. /**
  54. * 系统设置服务接口
  55. * */
  56. private SettingService settingService;
  57. /**
  58. * 商户服务接口
  59. */
  60. private MerchantService merchantService;
  61. /**
  62. * 获取商品分类列表
  63. */
  64. @ApiOperation(value = "获取商品分类列表")
  65. @RequestMapping(value = "/cateList", method = RequestMethod.GET)
  66. @CrossOrigin
  67. public ResponseObject cateList(HttpServletRequest request) throws BusinessCheckException {
  68. String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
  69. Integer storeId = request.getHeader("storeId") == null ? 0 : Integer.parseInt(request.getHeader("storeId"));
  70. String platform = request.getHeader("platform") == null ? "" : request.getHeader("platform");
  71. Integer merchantId = merchantService.getMerchantId(merchantNo);
  72. List<MtGoodsCate> cateList = cateService.getCateList(merchantId, storeId, null, StatusEnum.ENABLED.getKey());
  73. Map<String, Object> goodsData = goodsService.getStoreGoodsList(storeId, "", platform, 0, 1, 500);
  74. List<MtGoods> goodsList = (ArrayList)goodsData.get("goodsList");
  75. String baseImage = settingService.getUploadBasePath();
  76. if (goodsList.size() > 0) {
  77. for (MtGoods goods : goodsList) {
  78. goods.setLogo(baseImage + goods.getLogo());
  79. }
  80. }
  81. List<ResCateDto> result = new ArrayList<>();
  82. for (MtGoodsCate cate : cateList) {
  83. ResCateDto dto = new ResCateDto();
  84. dto.setCateId(cate.getId());
  85. dto.setName(cate.getName());
  86. dto.setLogo(baseImage + cate.getLogo());
  87. List<MtGoods> goodsArr = new ArrayList<>();
  88. for (MtGoods goods : goodsList) {
  89. if (goods.getCateId().compareTo(cate.getId()) == 0) {
  90. goodsArr.add(goods);
  91. }
  92. }
  93. dto.setGoodsList(goodsArr);
  94. result.add(dto);
  95. }
  96. return getSuccessResult(result);
  97. }
  98. /**
  99. * 获取商品列表
  100. */
  101. @ApiOperation(value = "获取商品列表")
  102. @RequestMapping(value = "/list", method = RequestMethod.GET)
  103. @CrossOrigin
  104. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  105. Integer storeId = request.getHeader("storeId") == null ? 0 : Integer.parseInt(request.getHeader("storeId"));
  106. String platform = request.getHeader("platform") == null ? "" : request.getHeader("platform");
  107. Map<String, Object> goodsData = goodsService.getStoreGoodsList(storeId, "", platform, 0,1, 200);
  108. return getSuccessResult(goodsData.get("goodsList"));
  109. }
  110. /**
  111. * 搜索商品
  112. */
  113. @ApiOperation(value = "搜索商品")
  114. @RequestMapping(value = "/search", method = RequestMethod.POST)
  115. @CrossOrigin
  116. public ResponseObject search(HttpServletRequest request, @RequestBody GoodsSearchRequest params) throws BusinessCheckException {
  117. Integer storeId = request.getHeader("storeId") == null ? 0 : Integer.parseInt(request.getHeader("storeId"));
  118. String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
  119. String platform = request.getHeader("platform") == null ? "" : request.getHeader("platform");
  120. Integer page = params.getPage() == null ? 1 : params.getPage();
  121. Integer pageSize = params.getPageSize() == null ? Constants.PAGE_SIZE : params.getPageSize();
  122. String name = params.getName() == null ? "" : params.getName();
  123. Integer cateId = params.getCateId() == null ? 0 : params.getCateId();
  124. String sortType = params.getSortType() == null ? "all" : params.getSortType();
  125. String sortPrice = params.getSortPrice() == null ? "0" : params.getSortPrice();
  126. PaginationRequest paginationRequest = new PaginationRequest();
  127. paginationRequest.setCurrentPage(page);
  128. paginationRequest.setPageSize(pageSize);
  129. Map<String, Object> searchParams = new HashMap<>();
  130. searchParams.put("status", StatusEnum.ENABLED.getKey());
  131. searchParams.put("hasPrice", YesOrNoEnum.YES.getKey());
  132. if (storeId > 0) {
  133. searchParams.put("storeId", storeId.toString());
  134. }
  135. if (cateId > 0) {
  136. searchParams.put("cateId", cateId);
  137. }
  138. if (StringUtil.isNotEmpty(name)) {
  139. searchParams.put("name", name);
  140. }
  141. Integer merchantId = merchantService.getMerchantId(merchantNo);
  142. if (merchantId > 0 ) {
  143. searchParams.put("merchantId", merchantId);
  144. }
  145. if (StringUtil.isNotEmpty(sortType)) {
  146. searchParams.put("sortType", sortType);
  147. }
  148. if (StringUtil.isNotEmpty(sortPrice)) {
  149. searchParams.put("sortPrice", sortPrice);
  150. }
  151. if (StringUtil.isNotEmpty(platform)) {
  152. searchParams.put("platform", platform);
  153. }
  154. paginationRequest.setSearchParams(searchParams);
  155. PaginationResponse<GoodsDto> paginationResponse = goodsService.queryGoodsListByPagination(paginationRequest);
  156. return getSuccessResult(paginationResponse);
  157. }
  158. /**
  159. * 获取商品详情
  160. */
  161. @ApiOperation(value = "获取商品详情")
  162. @RequestMapping(value = "/detail", method = RequestMethod.POST)
  163. @CrossOrigin
  164. public ResponseObject detail(@RequestBody GoodsInfoParam goodsInfoParam) throws BusinessCheckException, InvocationTargetException, IllegalAccessException {
  165. String goodsId = goodsInfoParam.getGoodsId() == null ? "0" : goodsInfoParam.getGoodsId();
  166. if (StringUtil.isEmpty(goodsId)) {
  167. return getFailureResult(2000, "商品ID不能为空");
  168. }
  169. GoodsDto goodsDto = goodsService.getGoodsDetail(Integer.parseInt(goodsId), false);
  170. GoodsDetailDto goodsDetailDto = new GoodsDetailDto();
  171. goodsDetailDto.setGoodsNo(goodsDto.getGoodsNo());
  172. goodsDetailDto.setGoodsId(goodsDto.getId());
  173. goodsDetailDto.setName(goodsDto.getName());
  174. goodsDetailDto.setCateId(goodsDto.getCateId());
  175. goodsDetailDto.setPrice(goodsDto.getPrice());
  176. goodsDetailDto.setLinePrice(goodsDto.getLinePrice());
  177. goodsDetailDto.setSalePoint(goodsDto.getSalePoint());
  178. goodsDetailDto.setSort(goodsDto.getSort());
  179. goodsDetailDto.setCanUsePoint(goodsDto.getCanUsePoint());
  180. goodsDetailDto.setIsMemberDiscount(goodsDto.getIsMemberDiscount());
  181. List<String> images = JSONObject.parseArray(goodsDto.getImages(), String.class);
  182. List<String> imageList = new ArrayList<>();
  183. String baseImage = settingService.getUploadBasePath();
  184. for (String image : images) {
  185. imageList.add((baseImage + image));
  186. }
  187. goodsDetailDto.setImages(imageList);
  188. goodsDetailDto.setIsSingleSpec(goodsDto.getIsSingleSpec());
  189. goodsDetailDto.setLogo(goodsDto.getLogo());
  190. goodsDetailDto.setStock(goodsDto.getStock());
  191. goodsDetailDto.setWeight(goodsDto.getWeight());
  192. goodsDetailDto.setDescription(goodsDto.getDescription());
  193. if (StringUtil.isNotEmpty(goodsDetailDto.getDescription())) {
  194. goodsDetailDto.setDescription(CommonUtil.fixVideo(goodsDetailDto.getDescription()));
  195. }
  196. goodsDetailDto.setInitSale(goodsDto.getInitSale());
  197. goodsDetailDto.setStatus(goodsDto.getStatus());
  198. // 商品规格列表
  199. List<MtGoodsSpec> goodsSpecList = goodsDto.getSpecList();
  200. List<String> specNameArr = new ArrayList<>();
  201. List<MtGoodsSpec> specArr = new ArrayList<>();
  202. for (MtGoodsSpec mtGoodsSpec : goodsSpecList) {
  203. if (!specNameArr.contains(mtGoodsSpec.getName())) {
  204. MtGoodsSpec spec = new MtGoodsSpec();
  205. spec.setId(mtGoodsSpec.getId());
  206. spec.setName(mtGoodsSpec.getName());
  207. specArr.add(spec);
  208. specNameArr.add(mtGoodsSpec.getName());
  209. }
  210. }
  211. List<GoodsSpecDto> specDtoList = new ArrayList<>();
  212. for (MtGoodsSpec mtSpec : specArr) {
  213. GoodsSpecDto dto = new GoodsSpecDto();
  214. dto.setSpecId(mtSpec.getId());
  215. dto.setName(mtSpec.getName());
  216. List<GoodsSpecValueDto> valueList = new ArrayList<>();
  217. for (MtGoodsSpec spec : goodsSpecList) {
  218. if (spec.getName().equals(mtSpec.getName())) {
  219. GoodsSpecValueDto valueDto = new GoodsSpecValueDto();
  220. valueDto.setSpecValue(spec.getValue());
  221. valueDto.setSpecValueId(spec.getId());
  222. valueList.add(valueDto);
  223. }
  224. }
  225. dto.setValueList(valueList);
  226. if (!goodsDetailDto.getIsSingleSpec().equals(YesOrNoEnum.YES.getKey())) {
  227. specDtoList.add(dto);
  228. }
  229. }
  230. // sku列表
  231. List<MtGoodsSku> goodsSkuList = goodsDto.getSkuList();
  232. List<GoodsSkuDto> skuDtoList = new ArrayList<>();
  233. String basePath = settingService.getUploadBasePath();
  234. for (MtGoodsSku sku : goodsSkuList) {
  235. GoodsSkuDto dto = new GoodsSkuDto();
  236. dto.setId(sku.getId());
  237. if (sku.getLogo() != null && StringUtil.isNotEmpty(sku.getLogo())) {
  238. dto.setLogo(basePath + sku.getLogo());
  239. } else {
  240. dto.setLogo(goodsDetailDto.getLogo());
  241. }
  242. dto.setGoodsId(sku.getGoodsId());
  243. dto.setSkuNo(sku.getSkuNo());
  244. dto.setPrice(sku.getPrice());
  245. dto.setLinePrice(sku.getLinePrice());
  246. dto.setStock(sku.getStock());
  247. dto.setWeight(sku.getWeight());
  248. dto.setSpecIds(sku.getSpecIds());
  249. skuDtoList.add(dto);
  250. }
  251. if (goodsDetailDto.getIsSingleSpec().equals(YesOrNoEnum.YES.getKey())) {
  252. GoodsSkuDto dto = new GoodsSkuDto();
  253. dto.setId(0);
  254. dto.setLogo(goodsDetailDto.getLogo());
  255. dto.setGoodsId(goodsDetailDto.getGoodsId());
  256. dto.setSkuNo("");
  257. dto.setPrice(goodsDetailDto.getPrice());
  258. dto.setLinePrice(goodsDetailDto.getLinePrice());
  259. dto.setStock(goodsDetailDto.getStock());
  260. dto.setWeight(goodsDetailDto.getWeight());
  261. dto.setSpecIds("");
  262. skuDtoList.add(dto);
  263. }
  264. goodsDetailDto.setSpecList(specDtoList);
  265. goodsDetailDto.setSkuList(skuDtoList);
  266. return getSuccessResult(goodsDetailDto);
  267. }
  268. /**
  269. * 通过sku编码获取商品信息
  270. * */
  271. @ApiOperation(value = "通过sku编码获取商品信息")
  272. @RequestMapping(value = "/getGoodsInfoBySkuNo", method = RequestMethod.POST)
  273. @CrossOrigin
  274. public ResponseObject getGoodsInfoBySkuNo(HttpServletRequest request, @RequestBody GoodsInfoParam goodsInfoParam) throws BusinessCheckException, InvocationTargetException, IllegalAccessException {
  275. String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
  276. String skuNo = goodsInfoParam.getSkuNo() == null ? "" : goodsInfoParam.getSkuNo();
  277. if (StringUtil.isEmpty(skuNo)) {
  278. return getFailureResult(201, "商品编码不能为空");
  279. }
  280. Integer merchantId = merchantService.getMerchantId(merchantNo);
  281. Integer goodsId = 0;
  282. Integer skuId = 0;
  283. MtGoodsSku mtGoodsSku = goodsService.getSkuInfoBySkuNo(skuNo);
  284. if (mtGoodsSku == null) {
  285. MtGoods mtGoods = goodsService.queryGoodsByGoodsNo(merchantId, skuNo);
  286. if (mtGoods != null) {
  287. goodsId = mtGoods.getId();
  288. }
  289. } else {
  290. goodsId = mtGoodsSku.getGoodsId();
  291. skuId = mtGoodsSku.getId();
  292. }
  293. if (goodsId > 0) {
  294. GoodsDto goodsDto = goodsService.getGoodsDetail(goodsId, false);
  295. Map<String, Object> data = new HashMap();
  296. data.put("skuId", skuId);
  297. data.put("goodsInfo", goodsDto);
  298. return getSuccessResult(data);
  299. } else {
  300. return getFailureResult(201, "未查询到商品信息");
  301. }
  302. }
  303. }