ApiGoodsController.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. package com.kmall.api.api;
  2. import com.google.common.collect.Maps;
  3. import com.kmall.api.annotation.IgnoreAuth;
  4. import com.kmall.api.annotation.LoginUser;
  5. import com.kmall.api.entity.*;
  6. import com.kmall.api.service.*;
  7. import com.kmall.api.util.ApiBaseAction;
  8. import com.kmall.api.util.ApiPageUtils;
  9. import com.kmall.common.utils.DateUtils;
  10. import com.kmall.common.utils.Query;
  11. import com.kmall.common.utils.enums.CouponTypeEnum;
  12. import com.qiniu.util.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * 作者: @author Scott <br>
  24. * 时间: 2017-08-11 08:32<br>
  25. * 描述: ApiIndexController <br>
  26. */
  27. @RestController
  28. @RequestMapping("/api/goods")
  29. public class ApiGoodsController extends ApiBaseAction {
  30. @Autowired
  31. private ApiGoodsService goodsService;
  32. @Autowired
  33. private ApiGoodsSpecificationService goodsSpecificationService;
  34. @Autowired
  35. private ApiProductService productService;
  36. @Autowired
  37. private ApiGoodsGalleryService goodsGalleryService;
  38. @Autowired
  39. private ApiGoodsIssueService goodsIssueService;
  40. @Autowired
  41. private ApiAttributeService attributeService;
  42. @Autowired
  43. private ApiCommentService commentService;
  44. @Autowired
  45. private ApiUserService userService;
  46. @Autowired
  47. private ApiCommentPictureService commentPictureService;
  48. @Autowired
  49. private ApiCollectService collectService;
  50. @Autowired
  51. private ApiFootprintService footprintService;
  52. @Autowired
  53. private ApiCategoryService categoryService;
  54. @Autowired
  55. private ApiSearchHistoryService searchHistoryService;
  56. @Autowired
  57. private ApiRelatedGoodsService relatedGoodsService;
  58. @Autowired
  59. private ApiCouponService apiCouponService;
  60. @Autowired
  61. private ApiUserCouponService apiUserCouponService;
  62. @Autowired
  63. private ApiCartService cartService;
  64. @Autowired
  65. private ApiAdService apiAdService;
  66. @Autowired
  67. private ApiBrandService apiBrandService;
  68. @Autowired
  69. private ApiFreightService apiFreightService;
  70. /**
  71. */
  72. @IgnoreAuth
  73. @GetMapping("index")
  74. public Object index(@LoginUser UserVo loginUser) {
  75. //
  76. Map param = Maps.newHashMap();
  77. List<GoodsVo> goodsList = goodsService.queryList(param);
  78. //
  79. return toResponsSuccess(goodsList);
  80. }
  81. /**
  82. * 获取sku信息,用于购物车编辑时选择规格
  83. */
  84. @IgnoreAuth
  85. @GetMapping("sku")
  86. public Object sku(@LoginUser UserVo loginUser, Long goodsId) {
  87. Map<String, Object> resultObj = Maps.newHashMap();
  88. //
  89. GoodsVo goodsVo = goodsService.queryObjectByStoreId(goodsId, getStoreId());
  90. //
  91. List<Map> goodsSpecificationEntityList = goodsSpecificationService.queryByGoodsIdGroupByNames(goodsId);
  92. //
  93. Map param = Maps.newHashMap();
  94. param.put("goods_id", goodsId);
  95. param.put("store_id", getStoreId());
  96. List<ProductVo> productEntityList = productService.queryList(param);
  97. CartVo cartVo = cartService.queryObjectByGoodsIdAndUserId(goodsId, getUserId(), getStoreId());
  98. if(cartVo == null){
  99. resultObj.put("cartNumber", 0);
  100. }else{
  101. resultObj.put("cartNumber", cartVo.getNumber());
  102. }
  103. resultObj.put("goodsVo", goodsVo);
  104. resultObj.put("specificationList", goodsSpecificationEntityList);
  105. resultObj.put("productList", productEntityList);
  106. return toResponsSuccess(resultObj);
  107. }
  108. /**
  109. * 商品画廊数据
  110. */
  111. @IgnoreAuth
  112. @GetMapping("goodsGallery")
  113. public Object goodsGallery(Integer goods_id) {
  114. Map param = Maps.newHashMap();
  115. param.put("goods_id", goods_id);
  116. List<GoodsGalleryVo> gallery = goodsGalleryService.queryList(param);
  117. return toResponsSuccess(gallery);
  118. }
  119. /**
  120. * 商品详情页数据
  121. * */
  122. @GetMapping("detail")
  123. public Object detail(Long id, Long referrer,String merchSn) {
  124. Map<String, Object> resultObj = Maps.newHashMap();
  125. //
  126. Long userId = getUserId();
  127. Long storeId = getStoreId();
  128. GoodsVo info = goodsService.queryObjectByStoreId(id, storeId);
  129. if(info == null){
  130. return toResponsFail("此商品不存在");
  131. }
  132. BrandVo brand = apiBrandService.queryObject(info.getBrand_id());
  133. resultObj.put("brand", brand);
  134. Map param = Maps.newHashMap();
  135. param.put("goods_id", id);
  136. param.put("store_id", getStoreId());
  137. List<Map> specificationList = goodsSpecificationService.queryByGoodsIdGroupByNames(id);
  138. //
  139. List<ProductVo> productEntityList = productService.queryList(param);
  140. //
  141. List<GoodsGalleryVo> gallery = goodsGalleryService.queryList(param);
  142. resultObj.put("gallery", gallery);
  143. Map ngaParam = Maps.newHashMap();
  144. ngaParam.put("fields", "nga.value, na.name");
  145. ngaParam.put("sidx", "nga.id");
  146. ngaParam.put("order", "asc");
  147. ngaParam.put("goods_id", id);
  148. List<AttributeVo> attribute = attributeService.queryList(ngaParam);
  149. //
  150. Map issueParam = Maps.newHashMap();
  151. issueParam.put("merch_sn", merchSn);
  152. List<GoodsIssueVo> issue = goodsIssueService.queryList(issueParam);
  153. //
  154. param.put("value_id", id);
  155. param.put("type_id", 0);
  156. Integer commentCount = commentService.queryTotal(param);
  157. List<CommentVo> hotComment = commentService.queryList(param);
  158. Map commentInfo = Maps.newHashMap();
  159. if (null != hotComment && hotComment.size() > 0) {
  160. UserVo commentUser = userService.queryObject(hotComment.get(0).getUserId());
  161. commentInfo.put("content", hotComment.get(0).getContent());
  162. commentInfo.put("add_time", DateUtils.timeToStr(hotComment.get(0).getAddTime(), DateUtils.PATTERN_YYYY_MM_DD_HH_MM));
  163. commentInfo.put("nickname", commentUser.getNickname());
  164. commentInfo.put("avatar", commentUser.getAvatar());
  165. Map paramPicture = Maps.newHashMap();
  166. paramPicture.put("comment_id", hotComment.get(0).getId());
  167. List<CommentPictureVo> commentPictureEntities = commentPictureService.queryList(paramPicture);
  168. commentInfo.put("pic_list", commentPictureEntities);
  169. }
  170. Map comment = Maps.newHashMap();
  171. comment.put("count", commentCount);
  172. comment.put("data", commentInfo);
  173. //当前用户是否收藏
  174. Map collectParam = Maps.newHashMap();
  175. collectParam.put("user_id", getUserId());
  176. collectParam.put("value_id", id);
  177. collectParam.put("type_id", 0);
  178. Integer userHasCollect = collectService.queryTotal(collectParam);
  179. if (userHasCollect > 0) {
  180. userHasCollect = 1;
  181. }
  182. //记录用户的足迹
  183. FootprintVo footprintEntity = new FootprintVo();
  184. footprintEntity.setAdd_time(System.currentTimeMillis() / 1000);
  185. footprintEntity.setGoods_brief(info.getGoods_brief());
  186. footprintEntity.setList_pic_url(info.getList_pic_url());
  187. footprintEntity.setGoods_id(info.getId());
  188. footprintEntity.setName(info.getName());
  189. footprintEntity.setRetail_price(info.getRetail_price());
  190. footprintEntity.setUser_id(userId);
  191. if (null != referrer) {
  192. footprintEntity.setReferrer(referrer);
  193. } else {
  194. footprintEntity.setReferrer(0L);
  195. }
  196. footprintService.save(footprintEntity);
  197. Map cartMap= Maps.newHashMap();
  198. cartMap.put("user_id",userId);
  199. cartMap.put("goods_id",info.getId());
  200. List<CartVo> cartVoList = cartService.queryList(cartMap);
  201. FreightEntity freightEntity = apiFreightService.queryObjectByGoodsId(info.getId(), getStoreId());
  202. //
  203. resultObj.put("info", info);
  204. resultObj.put("attribute", attribute);
  205. resultObj.put("userHasCollect", userHasCollect);
  206. resultObj.put("issue", issue);
  207. resultObj.put("comment", comment);
  208. resultObj.put("specificationList", specificationList);
  209. resultObj.put("productList", productEntityList);
  210. resultObj.put("stockNum", productEntityList.size()!=0? productEntityList.get(0).getStock_num():0);
  211. resultObj.put("cartNumber", cartVoList.size()!=0? cartVoList.get(0).getNumber():0);
  212. resultObj.put("defaultFreight", freightEntity != null? freightEntity.getDefaultFreight() :0);
  213. return toResponsSuccess(resultObj);
  214. }
  215. /*
  216. *  用户转发获取优惠券
  217. */
  218. @IgnoreAuth
  219. @GetMapping("transferCoupon")
  220. public Object transferCoupon(@LoginUser UserVo loginUser, Long goods_id, Integer send_type, Long referrer) {
  221. Map<String, Object> resultObj = Maps.newHashMap();
  222. // 记录推荐人是否可以领取红包,用户登录时校验
  223. Long userId = getUserId();
  224. try {
  225. // 是否已经有可用的转发红包
  226. Map params = Maps.newHashMap();
  227. params.put("user_id", userId);
  228. params.put("send_type", send_type);
  229. params.put("unUsed", true);
  230. List<CouponVo> enabledCouponVos = apiCouponService.queryUserCoupons(params);
  231. if ((null == enabledCouponVos || enabledCouponVos.size() == 0)
  232. && null != referrer && null != userId) {
  233. // 获取优惠信息提示
  234. Map couponParam = Maps.newHashMap();
  235. couponParam.put("enabled", true);
  236. Integer[] send_types = new Integer[]{CouponTypeEnum.COUPONTYPE2.getIndex()};
  237. couponParam.put("send_types", send_types);
  238. List<CouponVo> couponVos = apiCouponService.queryList(couponParam);
  239. if (null != couponVos && couponVos.size() > 0) {
  240. CouponVo couponVo = couponVos.get(0);
  241. Map footprintParam = Maps.newHashMap();
  242. footprintParam.put("goods_id", goods_id);
  243. footprintParam.put("referrer", referrer);
  244. Integer footprintNum = footprintService.queryTotal(footprintParam);
  245. if (null != footprintNum && null != couponVo.getMin_transmit_num()
  246. && footprintNum > couponVo.getMin_transmit_num()) {
  247. apiCouponService.takeCoupon(couponVo, userId, "", 0L, 1);
  248. }
  249. }
  250. }
  251. } catch (Exception e) {
  252. e.printStackTrace();
  253. }
  254. return toResponsSuccess("");
  255. }
  256. /**
  257. *  获取分类下的商品
  258. */
  259. @IgnoreAuth
  260. @GetMapping("category")
  261. public Object category(@LoginUser UserVo loginUser, Integer id) {
  262. Map<String, Object> resultObj = Maps.newHashMap();
  263. //
  264. CategoryVo currentCategory = categoryService.queryObject(id);
  265. //
  266. CategoryVo parentCategory = categoryService.queryObject(currentCategory.getParent_id());
  267. Map params = Maps.newHashMap();
  268. params.put("parent_id", currentCategory.getParent_id());
  269. List<CategoryVo> brotherCategory = categoryService.queryList(params);
  270. //
  271. resultObj.put("currentCategory", currentCategory);
  272. resultObj.put("parentCategory", parentCategory);
  273. resultObj.put("brotherCategory", brotherCategory);
  274. return toResponsSuccess(resultObj);
  275. }
  276. /**
  277. *  获取品牌下的商品
  278. */
  279. @IgnoreAuth
  280. @GetMapping("brand")
  281. public Object brand(@LoginUser UserVo loginUser, Integer id) {
  282. Map<String, Object> resultObj = Maps.newHashMap();
  283. //
  284. BrandVo currentBrand = apiBrandService.queryObject(id);
  285. //
  286. CategoryVo parentCategory = categoryService.queryObject(currentBrand.getCategoryId());
  287. Map params = Maps.newHashMap();
  288. params.put("categoryId", currentBrand.getCategoryId());
  289. List<BrandVo> brandList = apiBrandService.queryList(params);
  290. //
  291. resultObj.put("currentBrand", currentBrand);
  292. resultObj.put("brandList", brandList);
  293. return toResponsSuccess(resultObj);
  294. }
  295. /**
  296. *   获取商品列表
  297. */
  298. @GetMapping("list")
  299. public Object list(@LoginUser UserVo loginUser, Integer categoryId,
  300. Integer brandId, String keyword, Integer isNew, Integer isHot,String goodsBizType,
  301. @RequestParam(value = "page", defaultValue = "1") Integer
  302. page, @RequestParam(value = "size", defaultValue = "10") Integer size,
  303. String sort, String order) {
  304. Map params = Maps.newHashMap();
  305. if(org.apache.commons.lang3.StringUtils.isNotEmpty(keyword)){
  306. try{
  307. keyword = new String(keyword.getBytes("iso-8859-1"),"utf-8");
  308. }catch (Exception e){
  309. logger.error("error:", e);
  310. }
  311. params.put("keyword", keyword);
  312. }
  313. params.put("brand_id", brandId);
  314. params.put("is_new", isNew);
  315. params.put("is_hot", isHot);
  316. params.put("page", page);
  317. params.put("limit", size);
  318. params.put("order", sort);
  319. params.put("sidx", order);
  320. params.put("goodsBizType", goodsBizType);
  321. params.put("store_id", getStoreId());
  322. params.put("notGoodsBizType", true);
  323. //
  324. if (null != sort && sort.equals("price")) {
  325. params.put("sidx", "a.retail_price");
  326. params.put("order", order);
  327. } else {
  328. params.put("sidx", "a.id");
  329. params.put("order", "desc");
  330. }
  331. //添加到搜索历史
  332. if (!StringUtils.isNullOrEmpty(keyword)) {
  333. SearchHistoryVo searchHistoryVo = new SearchHistoryVo();
  334. searchHistoryVo.setAdd_time(System.currentTimeMillis() / 1000);
  335. searchHistoryVo.setKeyword(keyword);
  336. searchHistoryVo.setUser_id(null != loginUser ? loginUser.getId().toString() : "");
  337. searchHistoryVo.setFrom("");
  338. searchHistoryService.save(searchHistoryVo);
  339. }
  340. //筛选的分类
  341. List<CategoryVo> filterCategory = new ArrayList();
  342. CategoryVo rootCategory = new CategoryVo();
  343. rootCategory.setId(0);
  344. rootCategory.setName("全部");
  345. rootCategory.setChecked(false);
  346. filterCategory.add(rootCategory);
  347. //
  348. params.put("fields", "psr1.category_id");
  349. params.put("limit", 1);
  350. List<GoodsVo> goodsVoList = goodsService.queryList(params);
  351. params.put("limit", size);
  352. params.remove("fields");
  353. if (null != goodsVoList && goodsVoList.size() > 0) {
  354. List<Integer> categoryIds = new ArrayList();
  355. for (GoodsVo goodsVo : goodsVoList) {
  356. if (null != goodsVo && null != goodsVo.getCategory_id()) {
  357. categoryIds.add(goodsVo.getCategory_id());
  358. }
  359. }
  360. //查找二级分类的parent_id
  361. Map categoryParam = Maps.newHashMap();
  362. categoryParam.put("ids", categoryIds);
  363. categoryParam.put("fields", "parent_id");
  364. List<CategoryVo> parentCategoryList = categoryService.queryList(categoryParam);
  365. //
  366. List<Integer> parentIds = new ArrayList();
  367. for (CategoryVo categoryEntity : parentCategoryList) {
  368. parentIds.add(categoryEntity.getParent_id());
  369. }
  370. //一级分类
  371. categoryParam = Maps.newHashMap();
  372. categoryParam.put("fields", "id,name");
  373. categoryParam.put("order", "asc");
  374. categoryParam.put("sidx", "sort_order");
  375. categoryParam.put("ids", parentIds);
  376. List<CategoryVo> parentCategory = categoryService.queryList(categoryParam);
  377. if (null != parentCategory) {
  378. filterCategory.addAll(parentCategory);
  379. }
  380. }
  381. //查询列表数据
  382. params.put("fields", "distinct a.id, a.name, a.list_pic_url, psr1.market_price, psr1.retail_price, a.goods_brief");
  383. params.put("category_parent_id", categoryId);
  384. Query query = new Query(params);
  385. List<GoodsVo> goodsList = goodsService.queryList(query);
  386. int total = goodsService.queryTotal(query);
  387. ApiPageUtils goodsData = new ApiPageUtils(goodsList, total, query.getLimit(), query.getPage());
  388. //搜索到的商品
  389. for (CategoryVo categoryEntity : filterCategory) {
  390. if (null != categoryId && categoryEntity.getId() == 0 || categoryEntity.getId() == categoryId) {
  391. categoryEntity.setChecked(true);
  392. } else {
  393. categoryEntity.setChecked(false);
  394. }
  395. }
  396. goodsData.setFilterCategory(filterCategory);
  397. return toResponsSuccess(goodsData);
  398. }
  399. /**
  400. *   获取疯狂折扣商品列表
  401. */
  402. @IgnoreAuth
  403. @GetMapping("hotGoodsList")
  404. public Object hotGoodsList(@LoginUser UserVo loginUser, Integer categoryId,
  405. String sort, String order) {
  406. Map reusltObj = Maps.newHashMap();
  407. //
  408. Long store_id = getStoreId();
  409. Map params = Maps.newHashMap();
  410. //筛选的分类
  411. List<CategoryVo> filterCategory = new ArrayList();
  412. CategoryVo rootCategory = new CategoryVo();
  413. rootCategory.setId(0);
  414. rootCategory.setName("全部");
  415. rootCategory.setChecked(false);
  416. filterCategory.add(rootCategory);
  417. //
  418. params.put("fields", "psr1.category_id");
  419. params.put("is_hot", "1");
  420. params.put("store_id", store_id);
  421. List<GoodsVo> goodsEntityList = goodsService.queryList(params);
  422. params.remove("fields");
  423. if (null != goodsEntityList && goodsEntityList.size() > 0) {
  424. List<Integer> categoryIds = new ArrayList();
  425. for (GoodsVo goodsVo : goodsEntityList) {
  426. if (null == goodsVo || null == goodsVo.getCategory_id()) {
  427. continue;
  428. }
  429. categoryIds.add(goodsVo.getCategory_id());
  430. }
  431. //查找二级分类的parent_id
  432. Map categoryParam = Maps.newHashMap();
  433. categoryParam.put("ids", categoryIds);
  434. categoryParam.put("fields", "parent_id");
  435. List<CategoryVo> parentCategoryList = categoryService.queryList(categoryParam);
  436. //
  437. List<Integer> parentIds = new ArrayList();
  438. for (CategoryVo categoryEntity : parentCategoryList) {
  439. parentIds.add(categoryEntity.getParent_id());
  440. }
  441. //一级分类
  442. categoryParam = Maps.newHashMap();
  443. categoryParam.put("fields", "id,name");
  444. categoryParam.put("order", "asc");
  445. categoryParam.put("sidx", "sort_order");
  446. categoryParam.put("ids", parentIds);
  447. List<CategoryVo> parentCategory = categoryService.queryList(categoryParam);
  448. if (null != parentCategory) {
  449. filterCategory.addAll(parentCategory);
  450. }
  451. }
  452. params.put("category_parent_id", categoryId);
  453. params.put("fields", "a.id,a.name,a.goods_brief,a.list_pic_url,psr1.retail_price," +
  454. "psr1.market_price,b.id as product_id");
  455. params.put("is_hot", "1");
  456. params.put("is_delete", 0);
  457. params.put("store_id", store_id);
  458. if (!StringUtils.isNullOrEmpty(sort) && !StringUtils.isNullOrEmpty(order)) {
  459. if (sort.equals("price")) {
  460. params.put("sidx", "a.retail_price");
  461. params.put("order", order);
  462. }
  463. }
  464. List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(params);
  465. reusltObj.put("filterCategory", filterCategory);
  466. reusltObj.put("goodsList", hotGoods);
  467. return toResponsSuccess(reusltObj);
  468. }
  469. /**
  470. *   新品首发
  471. */
  472. @IgnoreAuth
  473. @GetMapping("new")
  474. public Object newAction(@LoginUser UserVo loginUser) {
  475. Map<String, Object> resultObj = Maps.newHashMap();
  476. Map bannerInfo = Maps.newHashMap();
  477. bannerInfo.put("url", "");
  478. bannerInfo.put("name", "坚持初心,为你寻觅世间好物");
  479. bannerInfo.put("img_url", "http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png");
  480. resultObj.put("bannerInfo", bannerInfo);
  481. return toResponsSuccess(resultObj);
  482. }
  483. /**
  484. *   人气推荐
  485. */
  486. @IgnoreAuth
  487. @GetMapping("hot")
  488. public Object hot(@LoginUser UserVo loginUser) {
  489. Map<String, Object> resultObj = Maps.newHashMap();
  490. //
  491. Map param = Maps.newHashMap();
  492. param.put("ad_position_id", 4);
  493. List<AdVo> adVoList = apiAdService.queryList(param);
  494. if (null != adVoList && adVoList.size() > 0) {
  495. Map bannerInfo = Maps.newHashMap();
  496. bannerInfo.put("url", "");
  497. bannerInfo.put("name", adVoList.get(0).getName());
  498. bannerInfo.put("img_url", adVoList.get(0).getImageUrl());
  499. resultObj.put("bannerInfo", bannerInfo);
  500. } else {
  501. Map bannerInfo = Maps.newHashMap();
  502. bannerInfo.put("url", "");
  503. bannerInfo.put("name", "大家都在商城选好物");
  504. bannerInfo.put("img_url", "https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180807/1057182334dfb2.png");
  505. resultObj.put("bannerInfo", bannerInfo);
  506. }
  507. return toResponsSuccess(resultObj);
  508. }
  509. /**
  510. *   商品详情页的大家都在看的商品
  511. */
  512. @IgnoreAuth
  513. @GetMapping("related")
  514. public Object related(@LoginUser UserVo loginUser, Long id) {
  515. Map<String, Object> resultObj = Maps.newHashMap();
  516. Map param = Maps.newHashMap();
  517. param.put("goods_id", id);
  518. param.put("fields", "related_goods_id");
  519. List<RelatedGoodsVo> relatedGoodsEntityList = relatedGoodsService.queryList(param);
  520. List<Integer> relatedGoodsIds = new ArrayList();
  521. for (RelatedGoodsVo relatedGoodsEntity : relatedGoodsEntityList) {
  522. relatedGoodsIds.add(relatedGoodsEntity.getRelated_goods_id());
  523. }
  524. List<Integer> relatedGoods = new ArrayList();
  525. if (null == relatedGoodsIds || relatedGoods.size() < 1) {
  526. //查找同分类下的商品
  527. GoodsVo goodsCategory = goodsService.queryObjectByStoreId(id, getStoreId());
  528. if (null != goodsCategory) {
  529. Map paramRelated = Maps.newHashMap();
  530. paramRelated.put("store_id", getStoreId());
  531. paramRelated.put("notGoodsBizType", true);
  532. paramRelated.put("fields", "a.id, a.name, a.list_pic_url, psr1.retail_price,b.id as product_id");
  533. paramRelated.put("category_id", goodsCategory.getCategory_id());
  534. relatedGoods = goodsService.queryList(paramRelated);
  535. }
  536. } else {
  537. Map paramRelated = Maps.newHashMap();
  538. paramRelated.put("store_id", getStoreId());
  539. paramRelated.put("notGoodsBizType", true);
  540. paramRelated.put("goods_ids", relatedGoodsIds);
  541. paramRelated.put("fields", "a.id, a.name, a.list_pic_url, psr1.retail_price,b.id as product_id");
  542. relatedGoods = goodsService.queryList(paramRelated);
  543. }
  544. resultObj.put("goodsList", relatedGoods);
  545. return toResponsSuccess(resultObj);
  546. }
  547. /**
  548. * 在售的商品总数
  549. */
  550. @IgnoreAuth
  551. @GetMapping("count")
  552. public Object count(@LoginUser UserVo loginUser) {
  553. Map<String, Object> resultObj = Maps.newHashMap();
  554. Map param = Maps.newHashMap();
  555. param.put("storeId", getStoreId());
  556. param.put("notGoodsBizType", true);
  557. param.put("is_delete", 0);
  558. param.put("is_on_sale", 1);
  559. Integer goodsCount = goodsService.queryTotal(param);
  560. resultObj.put("goodsCount", goodsCount);
  561. return toResponsSuccess(resultObj);
  562. }
  563. /**
  564. *   获取商品列表
  565. */
  566. @IgnoreAuth
  567. @GetMapping("productlist")
  568. public Object productlist(@LoginUser UserVo loginUser, Integer categoryId,
  569. Integer isNew, Integer discount,String goodsBizType,
  570. @RequestParam(value = "page", defaultValue = "1") Integer
  571. page, @RequestParam(value = "size", defaultValue = "10") Integer size,
  572. String sort, String order) {
  573. Map params = Maps.newHashMap();
  574. params.put("is_new", isNew);
  575. params.put("page", page);
  576. params.put("limit", size);
  577. params.put("order", sort);
  578. params.put("sidx", order);
  579. params.put("store_id", getStoreId());
  580. //
  581. if (null != sort && sort.equals("price")) {
  582. params.put("sidx", "retail_price");
  583. params.put("order", order);
  584. } else if (null != sort && sort.equals("sell")) {
  585. params.put("sidx", "psr1.sell_volume");
  586. params.put("order", order);
  587. } else {
  588. params.put("sidx", "id");
  589. params.put("order", "desc");
  590. }
  591. // 0不限 1特价 2团购
  592. if (null != discount && discount == 1) {
  593. params.put("is_hot", 1);
  594. } else if (null != discount && discount == 2) {
  595. params.put("is_group", true);
  596. }
  597. params.put("goodsBizType", goodsBizType);
  598. CategoryVo categoryVo = categoryService.queryObject(categoryId);
  599. if(categoryVo != null){
  600. if(categoryVo.getSort_order()!= null && categoryVo.getSort_order()== 0){
  601. }else{
  602. params.put("category_parent_id", categoryId);
  603. }
  604. }
  605. //查询列表数据
  606. Query query = new Query(params);
  607. List<GoodsVo> goodsList = goodsService.queryCatalogProductList(query);
  608. int total = goodsService.queryTotal(query);
  609. // 当前购物车中
  610. List<CartVo> cartList = new ArrayList();
  611. if (null != getUserId()) {
  612. Long storeId = getStoreId();
  613. //查询列表数据
  614. Map cartParam = Maps.newHashMap();
  615. cartParam.put("user_id", getUserId());
  616. cartParam.put("store_id", storeId);
  617. cartList = cartService.queryList(cartParam);
  618. }
  619. if (null != cartList && cartList.size() > 0 && null != goodsList && goodsList.size() > 0) {
  620. for (GoodsVo goodsVo : goodsList) {
  621. for (CartVo cartVo : cartList) {
  622. if (goodsVo.getId().equals(cartVo.getGoods_id())) {
  623. goodsVo.setCart_num(cartVo.getNumber());
  624. }
  625. }
  626. }
  627. }
  628. ApiPageUtils goodsData = new ApiPageUtils(goodsList, total, query.getLimit(), query.getPage());
  629. return toResponsSuccess(goodsData);
  630. }
  631. }