GoodsDonController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package com.cyksj.web.controller.goods;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUnit;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.cyksj.common.constant.Constant;
  9. import com.cyksj.common.util.Jsons;
  10. import com.cyksj.dto.Result;
  11. import com.cyksj.enums.GatewayResponse;
  12. import com.cyksj.mapper.GoodsDonSkuMapper;
  13. import com.cyksj.mapper.OrderDonMapper;
  14. import com.cyksj.model.entity.*;
  15. import com.cyksj.model.views.*;
  16. import com.cyksj.redis.RedisService;
  17. import com.ejlchina.searcher.BeanSearcher;
  18. import com.ejlchina.searcher.SearchResult;
  19. import com.ejlchina.searcher.param.Operator;
  20. import com.ejlchina.searcher.util.MapBuilder;
  21. import com.ejlchina.searcher.util.MapUtils;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.web.bind.annotation.*;
  25. import javax.servlet.http.HttpServletRequest;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Optional;
  29. /**
  30. * @author chan
  31. * @date 2022/9/26 5:33 PM
  32. */
  33. @Slf4j
  34. @RestController
  35. @RequestMapping("/applets/goods")
  36. @RequiredArgsConstructor
  37. public class GoodsDonController {
  38. private final HttpServletRequest request;
  39. private final BeanSearcher beanSearcher;
  40. private final OrderDonMapper orderDonMapper;
  41. private final GoodsDonSkuMapper goodsDonSkuMapper;
  42. private final RedisService redisService;
  43. /**
  44. * 获取商品列表
  45. */
  46. @GetMapping("/get")
  47. public Result<SearchResult<GoodsDon>> get() {
  48. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  49. .field(GoodsDon::getStatus, true)
  50. .field(GoodsDon::getDeleted, true)
  51. .orderBy(GoodsDon::getSortBy).desc()
  52. .orderBy(GoodsDon::getId).asc()
  53. .build()
  54. );
  55. search.getDataList().forEach(goods -> {
  56. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  57. .eq(GoodsDonSku::getStatus, true)
  58. .orderByAsc(GoodsDonSku::getPrice)
  59. .last("limit 1")
  60. .select(GoodsDonSku::getPrice));
  61. Optional.ofNullable(goodsDonSku).ifPresent(sku->{
  62. goods.setPrice(goodsDonSku.getPrice());
  63. goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  64. .eq(OrderDon::getGoodsId, goods.getId())
  65. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  66. });
  67. });
  68. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  69. }
  70. /**
  71. * 推广未登录,虚拟商品列表
  72. */
  73. @GetMapping("/get/popularize")
  74. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  75. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  76. .field(GoodsDonPopularizeView::getStatus, true)
  77. .field(GoodsDonPopularizeView::getDeleted, true)
  78. .field(GoodsDonPopularizeView::getType, 1)
  79. .orderBy(GoodsDonPopularizeView::getSortBy).desc()
  80. .orderBy(GoodsDonPopularizeView::getId).asc()
  81. .build());
  82. DateTime now = DateTime.now();
  83. search.getDataList().forEach(data -> {
  84. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  85. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  86. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  87. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  88. data.setSold(sold + data.getVirtualSold());
  89. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  90. if (skuList.size() > 0) {
  91. GoodsDonSku goodsDonSku = skuList.get(0);
  92. data.setPrice(goodsDonSku.getPrice());
  93. data.setMonths(goodsDonSku.getMonths());
  94. }
  95. });
  96. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  97. .eq(OrderDon::getGoodsId, data.getId())
  98. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  99. .orderByDesc(OrderDon::getId)
  100. .last("limit 1"));
  101. StringBuilder notifyMsg = new StringBuilder();
  102. if (orderDon == null) {
  103. notifyMsg.append("点击购买立即上车");
  104. data.setNotifyMsg(notifyMsg.toString());
  105. return;
  106. } else {
  107. Date payTime = orderDon.getPayTime();
  108. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  109. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  110. if (hour == 0) {
  111. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  112. if (minute == 0) {
  113. notifyMsg.append("1分钟");
  114. } else {
  115. notifyMsg.append(minute);
  116. notifyMsg.append("分钟");
  117. }
  118. } else {
  119. if (hour > 3) {
  120. int hours = RandomUtil.randomInt(3) + 1;
  121. notifyMsg.append(hours);
  122. } else {
  123. notifyMsg.append(hour);
  124. }
  125. notifyMsg.append("小时");
  126. }
  127. }
  128. notifyMsg.append("前有人购买");
  129. data.setNotifyMsg(notifyMsg.toString());
  130. });
  131. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  132. }
  133. /**
  134. * 官网首页 平台列表
  135. */
  136. @GetMapping("/get/list")
  137. public Result<List<GoodsFrontListView>> getGoodsList() {
  138. Object value = redisService.get(RedisService.key.PC_WEB_CACHE.getName());
  139. if (value != null) {
  140. try {
  141. List<GoodsFrontListView> list = Jsons.parseObject(value, List.class);
  142. if (CollUtil.isNotEmpty(list)) {
  143. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  144. }
  145. } catch (Exception e) {
  146. }
  147. }
  148. List<GoodsFrontListView> list = beanSearcher.searchAll(GoodsFrontListView.class, MapUtils.flatBuilder(request.getParameterMap())
  149. .build());
  150. DateTime now = DateTime.now();
  151. list.forEach(data -> {
  152. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  153. data.setSkuList(beanSearcher.searchAll(GoodsDonFrontSkuView.class, MapUtils.builder().field(GoodsDonFrontSkuView::getGoodsId, data.getId()).field(GoodsDonFrontSkuView::getStatus, true).orderBy(GoodsDonFrontSkuView::getPrice).asc().build()));
  154. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  155. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  156. data.setSold(sold + data.getVirtualSold());
  157. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  158. if (skuList.size() > 0) {
  159. GoodsDonFrontSkuView donFrontSkuView = skuList.get(0);
  160. data.setPrice(donFrontSkuView.getPrice());
  161. data.setMonths(donFrontSkuView.getMonths());
  162. }
  163. });
  164. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  165. .field(OrderCommentFrontView::getGoodsId, data.getId())
  166. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  167. .orderBy(OrderCommentFrontView::getId).desc()
  168. .build());
  169. data.setComments(comments);
  170. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  171. .eq(OrderDon::getGoodsId, data.getId())
  172. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  173. .orderByDesc(OrderDon::getId)
  174. .last("limit 1"));
  175. StringBuilder notifyMsg = new StringBuilder();
  176. if (orderDon == null) {
  177. notifyMsg.append("点击购买立即上车");
  178. data.setNotifyMsg(notifyMsg.toString());
  179. return;
  180. } else {
  181. Date payTime = orderDon.getPayTime();
  182. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  183. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  184. if (hour == 0) {
  185. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  186. if (minute == 0) {
  187. notifyMsg.append("1分钟");
  188. } else {
  189. notifyMsg.append(minute);
  190. notifyMsg.append("分钟");
  191. }
  192. } else {
  193. if (hour > 3) {
  194. int hours = RandomUtil.randomInt(3) + 1;
  195. notifyMsg.append(hours);
  196. } else {
  197. notifyMsg.append(hour);
  198. }
  199. notifyMsg.append("小时");
  200. }
  201. }
  202. notifyMsg.append("前有人购买");
  203. data.setNotifyMsg(notifyMsg.toString());
  204. });
  205. redisService.setNx(RedisService.key.PC_WEB_CACHE.getName(), list, RedisService.key.PC_WEB_CACHE.getTimeout());
  206. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  207. }
  208. /**
  209. * h5 银河首页平台展示
  210. */
  211. @GetMapping("/get/homePage")
  212. public Result<List<GoodsHomePageView>> getGoodsHomePage() {
  213. Object value = redisService.get(RedisService.key.H5_WEB_CACHE.getName());
  214. if (value != null) {
  215. try {
  216. List<GoodsHomePageView> list = Jsons.parseObject(value, List.class);
  217. if (CollUtil.isNotEmpty(list)) {
  218. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  219. }
  220. } catch (Exception e) {
  221. }
  222. }
  223. List<GoodsHomePageView> dataList = beanSearcher.searchAll(GoodsHomePageView.class, MapUtils.flatBuilder(request.getParameterMap())
  224. .build());
  225. dataList.forEach(data -> {
  226. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  227. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  228. data.setSold(sold + data.getVirtualSold());
  229. GoodsDonFrontSkuView skuView = beanSearcher.searchFirst(GoodsDonFrontSkuView.class, MapUtils.builder().field(GoodsDonFrontSkuView::getGoodsId, data.getId()).field(GoodsDonFrontSkuView::getStatus, true).orderBy(GoodsDonFrontSkuView::getPrice).asc().build());
  230. if (skuView != null) {
  231. data.setPrice(skuView.getPrice());
  232. data.setMonths(skuView.getMonths());
  233. }
  234. });
  235. redisService.setNx(RedisService.key.H5_WEB_CACHE.getName(), dataList, RedisService.key.H5_WEB_CACHE.getTimeout());
  236. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  237. }
  238. /**
  239. * 获取商品详情
  240. */
  241. @GetMapping("/get/{id}")
  242. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  243. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  244. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, views.getId()).build()));
  245. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  246. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getGoodsId, id).build());
  247. if (goodsDonSkuView != null && goodsDonSkuView.getType() == 1) {
  248. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  249. }
  250. views.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, builder.build()));
  251. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  252. }
  253. /**
  254. * 获取商品多规格列表
  255. */
  256. @GetMapping("/get/sku")
  257. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  258. if (skuIds.isEmpty()) {
  259. return GatewayResponse.SUCCESS.newBuilder().toResult();
  260. }
  261. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  262. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  263. .build());
  264. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  265. }
  266. /**
  267. * 获取评价列表
  268. */
  269. @GetMapping("/get/comments")
  270. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  271. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  272. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  273. .orderBy(OrderCommentFrontView::getId).desc()
  274. .build());
  275. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  276. }
  277. }