GoodsDonController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.util.MapBuilder;
  19. import com.ejlchina.searcher.util.MapUtils;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.web.bind.annotation.GetMapping;
  23. import org.springframework.web.bind.annotation.PathVariable;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import javax.servlet.http.HttpServletRequest;
  27. import java.math.BigDecimal;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. /**
  32. * @author chan
  33. * @date 2022/9/26 5:33 PM
  34. */
  35. @Slf4j
  36. @RestController
  37. @RequestMapping("/applets/goods")
  38. @RequiredArgsConstructor
  39. public class GoodsDonController {
  40. private final HttpServletRequest request;
  41. private final BeanSearcher beanSearcher;
  42. private final OrderDonMapper orderDonMapper;
  43. private final GoodsDonSkuMapper goodsDonSkuMapper;
  44. /**
  45. * 获取商品列表
  46. */
  47. @GetMapping("/get")
  48. public Result<SearchResult<GoodsDon>> get() {
  49. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  50. .field(GoodsDon::getStatus, true)
  51. .field(GoodsDon::getDeleted, true)
  52. .orderBy(GoodsDon::getSortBy).desc()
  53. .orderBy(GoodsDon::getId).asc()
  54. .build()
  55. );
  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. .select(GoodsDonSku::getPrice));
  61. goods.setPrice(goodsDonSku.getPrice());
  62. });
  63. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  64. }
  65. /**
  66. * 推广未登录,虚拟商品列表
  67. */
  68. @GetMapping("/get/popularize")
  69. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  70. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  71. .field(GoodsDonPopularizeView::getStatus, true)
  72. .field(GoodsDonPopularizeView::getDeleted, true)
  73. .field(GoodsDonPopularizeView::getType, 1)
  74. .build());
  75. DateTime now = DateTime.now();
  76. Integer randomNum = 2000;
  77. List<GoodsDonPopularizeView> collect = search.getDataList().stream().filter(data -> data.getPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
  78. collect.forEach(data -> {
  79. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  80. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).build()));
  81. data.setSold(data.getSold() + randomNum);
  82. SearchResult<OrderComment> orderComments = beanSearcher.search(OrderComment.class,
  83. MapUtils.builder().field(OrderComment::getGoodsId, data.getId())
  84. .orderBy(OrderComment::getId).desc()
  85. .build());
  86. if (orderComments != null) {
  87. List<OrderComment> comments = orderComments.getDataList();
  88. data.setOrderComments(comments);
  89. }
  90. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  91. .eq(OrderDon::getGoodsId, data.getId())
  92. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  93. .orderByDesc(OrderDon::getId)
  94. .last("limit 1"));
  95. StringBuilder notifyMsg = new StringBuilder();
  96. if (orderDon == null) {
  97. notifyMsg.append("2分钟");
  98. } else {
  99. Date payTime = orderDon.getPayTime();
  100. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  101. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  102. if (hour == 0) {
  103. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  104. if (minute == 0) {
  105. notifyMsg.append("1分钟");
  106. } else {
  107. notifyMsg.append(minute);
  108. notifyMsg.append("分钟");
  109. }
  110. } else {
  111. if (hour > 3) {
  112. int hours = RandomUtil.randomInt(3) + 1;
  113. notifyMsg.append(hours);
  114. } else {
  115. notifyMsg.append(hour);
  116. }
  117. notifyMsg.append("小时");
  118. }
  119. }
  120. notifyMsg.append("前有人购买");
  121. data.setNotifyMsg(notifyMsg.toString());
  122. });
  123. search = new SearchResult<>(collect);
  124. search.setTotalCount(collect.size());
  125. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  126. }
  127. /**
  128. * 获取商品详情
  129. */
  130. @GetMapping("/get/{id}")
  131. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  132. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  133. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, views.getId()).build()));
  134. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  135. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getGoodsId, id).build());
  136. if (goodsDonSkuView != null && goodsDonSkuView.getType() == 1) {
  137. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  138. }
  139. views.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, builder.build()));
  140. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  141. }
  142. }