GoodsDonController.java 16 KB

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