GoodsDonController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.cyksj.web.controller.goods;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUnit;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.RandomUtil;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.cyksj.common.constant.Constant;
  8. import com.cyksj.dto.Result;
  9. import com.cyksj.enums.GatewayResponse;
  10. import com.cyksj.mapper.GoodsDonSkuMapper;
  11. import com.cyksj.mapper.OrderDonMapper;
  12. import com.cyksj.model.entity.*;
  13. import com.cyksj.model.views.GoodsDonPopularizeView;
  14. import com.cyksj.model.views.GoodsDonSkuView;
  15. import com.cyksj.model.views.GoodsDonViews;
  16. import com.ejlchina.searcher.BeanSearcher;
  17. import com.ejlchina.searcher.SearchResult;
  18. import com.ejlchina.searcher.param.Operator;
  19. import com.ejlchina.searcher.util.MapBuilder;
  20. import com.ejlchina.searcher.util.MapUtils;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.math.BigDecimal;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Optional;
  29. import java.util.stream.Collectors;
  30. /**
  31. * @author chan
  32. * @date 2022/9/26 5:33 PM
  33. */
  34. @Slf4j
  35. @RestController
  36. @RequestMapping("/applets/goods")
  37. @RequiredArgsConstructor
  38. public class GoodsDonController {
  39. private final HttpServletRequest request;
  40. private final BeanSearcher beanSearcher;
  41. private final OrderDonMapper orderDonMapper;
  42. private final GoodsDonSkuMapper goodsDonSkuMapper;
  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. Integer virtual = 2000;
  56. search.getDataList().forEach(goods->{
  57. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  58. .eq(GoodsDonSku::getStatus, true)
  59. .orderByAsc(GoodsDonSku::getPrice)
  60. .last("limit 1")
  61. .select(GoodsDonSku::getPrice));
  62. goods.setPrice(goodsDonSku.getPrice());
  63. if (goods.getType() != 3) {
  64. goods.setSoldNum(virtual + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  65. .eq(OrderDon::getGoodsId, goods.getId())
  66. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  67. }
  68. });
  69. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  70. }
  71. /**
  72. * 推广未登录,虚拟商品列表
  73. */
  74. @GetMapping("/get/popularize")
  75. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  76. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  77. .field(GoodsDonPopularizeView::getStatus, true)
  78. .field(GoodsDonPopularizeView::getDeleted, true)
  79. .field(GoodsDonPopularizeView::getType, 1)
  80. .build());
  81. DateTime now = DateTime.now();
  82. Integer randomNum = 2000;
  83. search.getDataList().forEach(data -> {
  84. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  85. .eq(GoodsDonSku::getGoodsId, data.getId())
  86. .eq(GoodsDonSku::getStatus, true)
  87. .orderByAsc(GoodsDonSku::getPrice)
  88. .last("limit 1"));
  89. Optional.ofNullable(goodsDonSku).ifPresent(sku -> {
  90. data.setPrice(sku.getPrice());
  91. data.setMonths(sku.getMonths());
  92. });
  93. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  94. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).build()));
  95. if (data.getId() != 17 && data.getId() != 23) {
  96. data.setSold(data.getSold() + randomNum);
  97. }
  98. SearchResult<OrderComment> orderComments = beanSearcher.search(OrderComment.class,
  99. MapUtils.builder().field(OrderComment::getGoodsId, data.getId())
  100. .orderBy(OrderComment::getId).desc()
  101. .build());
  102. if (orderComments != null) {
  103. List<OrderComment> comments = orderComments.getDataList();
  104. data.setOrderComments(comments);
  105. }
  106. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  107. .eq(OrderDon::getGoodsId, data.getId())
  108. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  109. .orderByDesc(OrderDon::getId)
  110. .last("limit 1"));
  111. StringBuilder notifyMsg = new StringBuilder();
  112. if (orderDon == null) {
  113. notifyMsg.append("2分钟");
  114. } else {
  115. Date payTime = orderDon.getPayTime();
  116. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  117. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  118. if (hour == 0) {
  119. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  120. if (minute == 0) {
  121. notifyMsg.append("1分钟");
  122. } else {
  123. notifyMsg.append(minute);
  124. notifyMsg.append("分钟");
  125. }
  126. } else {
  127. if (hour > 3) {
  128. int hours = RandomUtil.randomInt(3) + 1;
  129. notifyMsg.append(hours);
  130. } else {
  131. notifyMsg.append(hour);
  132. }
  133. notifyMsg.append("小时");
  134. }
  135. }
  136. notifyMsg.append("前有人购买");
  137. data.setNotifyMsg(notifyMsg.toString());
  138. });
  139. List<GoodsDonPopularizeView> collect = search.getDataList().stream().filter(data -> data.getPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
  140. search = new SearchResult<>(collect);
  141. search.setTotalCount(collect.size());
  142. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  143. }
  144. /**
  145. * 获取商品详情
  146. */
  147. @GetMapping("/get/{id}")
  148. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  149. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  150. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, views.getId()).build()));
  151. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  152. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getGoodsId, id).build());
  153. if (goodsDonSkuView != null && goodsDonSkuView.getType() == 1) {
  154. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  155. }
  156. views.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, builder.build()));
  157. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  158. }
  159. /**
  160. * 获取商品多规格列表
  161. */
  162. @GetMapping("/get/sku")
  163. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  164. if (skuIds.isEmpty()) {
  165. return GatewayResponse.SUCCESS.newBuilder().toResult();
  166. }
  167. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  168. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  169. .build());
  170. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  171. }
  172. }