GoodsDonController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 cn.hutool.core.util.StrUtil;
  8. import cn.hutool.extra.servlet.ServletUtil;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.cyksj.common.constant.Constant;
  11. import com.cyksj.common.util.Jsons;
  12. import com.cyksj.common.util.StringUtil;
  13. import com.cyksj.dto.Result;
  14. import com.cyksj.enums.GatewayResponse;
  15. import com.cyksj.mapper.GoodsDonSkuMapper;
  16. import com.cyksj.mapper.OrderDonMapper;
  17. import com.cyksj.mapper.SysConfigMapper;
  18. import com.cyksj.mapper.RegisterOrderDonMapper;
  19. import com.cyksj.model.entity.*;
  20. import com.cyksj.model.views.*;
  21. import com.cyksj.redis.RedisService;
  22. import com.ejlchina.searcher.BeanSearcher;
  23. import com.ejlchina.searcher.SearchResult;
  24. import com.ejlchina.searcher.param.Operator;
  25. import com.ejlchina.searcher.util.MapBuilder;
  26. import com.ejlchina.searcher.util.MapUtils;
  27. import lombok.RequiredArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.web.bind.annotation.*;
  30. import javax.servlet.http.HttpServletRequest;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Optional;
  34. /**
  35. * @author chan
  36. * @date 2022/9/26 5:33 PM
  37. */
  38. @Slf4j
  39. @RestController
  40. @RequestMapping("/applets/goods")
  41. @RequiredArgsConstructor
  42. public class GoodsDonController {
  43. private final HttpServletRequest request;
  44. private final BeanSearcher beanSearcher;
  45. private final OrderDonMapper orderDonMapper;
  46. private final RegisterOrderDonMapper registerOrderDonMapper;
  47. private final GoodsDonSkuMapper goodsDonSkuMapper;
  48. private final RedisService redisService;
  49. private final SysConfigMapper sysConfigMapper;
  50. /**
  51. * 获取商品列表
  52. */
  53. @GetMapping("/get")
  54. public Result<SearchResult<GoodsDon>> get() {
  55. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  56. .field(GoodsDon::getStatus, true)
  57. .field(GoodsDon::getDeleted, true)
  58. .orderBy(GoodsDon::getSortBy).desc()
  59. .orderBy(GoodsDon::getId).asc()
  60. .build()
  61. );
  62. search.getDataList().forEach(goods -> {
  63. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  64. .eq(GoodsDonSku::getStatus, true)
  65. .orderByAsc(GoodsDonSku::getPrice)
  66. .last("limit 1")
  67. .select(GoodsDonSku::getPrice));
  68. Optional.ofNullable(goodsDonSku).ifPresent(sku->{
  69. goods.setPrice(goodsDonSku.getPrice());
  70. goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  71. .eq(OrderDon::getGoodsId, goods.getId())
  72. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  73. });
  74. });
  75. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  76. }
  77. /**
  78. * 推广未登录,虚拟商品列表
  79. */
  80. @GetMapping("/get/popularize")
  81. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  82. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  83. .field(GoodsDonPopularizeView::getStatus, true)
  84. .field(GoodsDonPopularizeView::getDeleted, true)
  85. .field(GoodsDonPopularizeView::getType, 1)
  86. .orderBy(GoodsDonPopularizeView::getSortBy).desc()
  87. .orderBy(GoodsDonPopularizeView::getId).asc()
  88. .build());
  89. DateTime now = DateTime.now();
  90. search.getDataList().forEach(data -> {
  91. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  92. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  93. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  94. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  95. data.setSold(sold + data.getVirtualSold());
  96. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  97. if (skuList.size() > 0) {
  98. GoodsDonSku goodsDonSku = skuList.get(0);
  99. data.setPrice(goodsDonSku.getPrice());
  100. data.setMonths(goodsDonSku.getMonths());
  101. }
  102. });
  103. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  104. .eq(OrderDon::getGoodsId, data.getId())
  105. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  106. .orderByDesc(OrderDon::getId)
  107. .last("limit 1"));
  108. StringBuilder notifyMsg = new StringBuilder();
  109. if (orderDon == null) {
  110. notifyMsg.append("点击购买立即上车");
  111. data.setNotifyMsg(notifyMsg.toString());
  112. return;
  113. } else {
  114. Date payTime = orderDon.getPayTime();
  115. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  116. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  117. if (hour == 0) {
  118. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  119. if (minute == 0) {
  120. notifyMsg.append("1分钟");
  121. } else {
  122. notifyMsg.append(minute);
  123. notifyMsg.append("分钟");
  124. }
  125. } else {
  126. if (hour > 3) {
  127. int hours = RandomUtil.randomInt(3) + 1;
  128. notifyMsg.append(hours);
  129. } else {
  130. notifyMsg.append(hour);
  131. }
  132. notifyMsg.append("小时");
  133. }
  134. }
  135. notifyMsg.append("前有人购买");
  136. data.setNotifyMsg(notifyMsg.toString());
  137. });
  138. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  139. }
  140. /**
  141. * 官网首页 平台列表
  142. */
  143. @GetMapping("/get/list")
  144. public Result<List<GoodsFrontListView>> getGoodsList() {
  145. List<Long> filter_goodsIds = getFilterGoodsIds();
  146. String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "PC";
  147. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  148. pcCacheKey += ":hz-ip";
  149. }
  150. Object value = redisService.get(pcCacheKey);
  151. if (value != null) {
  152. try {
  153. List<GoodsFrontListView> list = Jsons.parseObject(value, List.class);
  154. if (CollUtil.isNotEmpty(list)) {
  155. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  156. }
  157. } catch (Exception e) {
  158. }
  159. }
  160. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  161. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  162. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  163. }
  164. List<GoodsFrontListView> list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  165. DateTime now = DateTime.now();
  166. list.forEach(data -> {
  167. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  168. data.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, data.getId()).field(GoodsDonSkuFrontView::getStatus, true).orderBy(GoodsDonSkuFrontView::getPrice).asc().build()));
  169. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  170. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  171. data.setSold(sold + data.getVirtualSold());
  172. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  173. .field(OrderCommentFrontView::getGoodsId, data.getId())
  174. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  175. .orderBy(OrderCommentFrontView::getId).desc()
  176. .build());
  177. data.setComments(comments);
  178. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1));
  179. });
  180. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  181. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  182. }
  183. /**
  184. * h5 银河首页平台展示
  185. */
  186. @GetMapping("/get/homePage")
  187. public Result<List<GoodsHomePageView>> getGoodsHomePage() {
  188. List<Long> filter_goodsIds = getFilterGoodsIds();
  189. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "H5";
  190. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  191. h5CacheKey += ":hz-ip";
  192. }
  193. Object value = redisService.get(h5CacheKey);
  194. if (value != null) {
  195. try {
  196. List<GoodsHomePageView> list = Jsons.parseObject(value, List.class);
  197. if (CollUtil.isNotEmpty(list)) {
  198. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  199. }
  200. } catch (Exception e) {
  201. }
  202. }
  203. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  204. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  205. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  206. }
  207. List<GoodsHomePageView> dataList = beanSearcher.searchAll(GoodsHomePageView.class, builder.build());
  208. DateTime now = DateTime.now();
  209. dataList.forEach(data -> {
  210. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  211. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  212. data.setSold(sold + data.getVirtualSold());
  213. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1));
  214. });
  215. redisService.setNx(h5CacheKey, dataList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  216. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  217. }
  218. /**
  219. * 注册首页平台展示
  220. */
  221. @GetMapping("/get/register")
  222. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  223. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getName();
  224. Object value = redisService.get(registerCacheKey);
  225. if (value != null) {
  226. try {
  227. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  228. if (CollUtil.isNotEmpty(list)) {
  229. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  230. }
  231. } catch (Exception e) {
  232. }
  233. }
  234. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  235. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  236. DateTime now = DateTime.now();
  237. dataList.forEach(data->{
  238. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  239. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  240. data.setSold(sold + data.getVirtualSold());
  241. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2));
  242. });
  243. redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  244. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  245. }
  246. /**
  247. * 获取商品详情
  248. */
  249. @GetMapping("/get/{id}")
  250. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  251. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  252. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  253. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  254. if (views.getType() == 1) {
  255. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  256. }
  257. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  258. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  259. }
  260. /**
  261. * 获取商品推荐平台
  262. */
  263. @GetMapping("/get/recommend/{id}")
  264. public Result<List<GoodsRecommendViews>> getRecommendGoodsByGid(@PathVariable Long id) throws Exception {
  265. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "recommend:" + id;
  266. Object value = redisService.get(key);
  267. if (value != null) {
  268. List<GoodsRecommendViews> goodsRecommendViews = Jsons.parseList(value.toString(), GoodsRecommendViews.class);
  269. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  270. }
  271. if (value == null) {
  272. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  273. .field(GoodsDon::getId, id)
  274. .field(GoodsDon::getStatus, true)
  275. .field(GoodsDon::getDeleted, true)
  276. .build());
  277. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  278. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  279. List<GoodsRecommendViews> goodsRecommendViews = beanSearcher.searchAll(GoodsRecommendViews.class, MapUtils.builder().field(GoodsRecommendViews::getGoodsId, recommend_gidList).op(Operator.InList).build());
  280. goodsRecommendViews.forEach(data->{
  281. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  282. .eq(RegisterOrderDon::getGoodsId, data.getGoodsId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  283. data.setSold(sold + data.getVirtualSold());
  284. });
  285. redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l);
  286. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  287. }
  288. }
  289. return GatewayResponse.SUCCESS.newBuilder().toResult();
  290. }
  291. /**
  292. * 获取商品多规格列表
  293. */
  294. @GetMapping("/get/sku")
  295. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  296. if (skuIds.isEmpty()) {
  297. return GatewayResponse.SUCCESS.newBuilder().toResult();
  298. }
  299. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  300. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  301. .build());
  302. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  303. }
  304. /**
  305. * 获取评价列表
  306. */
  307. @GetMapping("/get/comments")
  308. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  309. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  310. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  311. .orderBy(OrderCommentFrontView::getId).desc()
  312. .build());
  313. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  314. }
  315. public List<Long> getFilterGoodsIds() {
  316. //过滤杭州Ai ChatGPT平台
  317. //获取当前ip地址
  318. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  319. .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  320. List<Long> filter_goodsIds = null;
  321. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  322. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  323. try {
  324. filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  325. } catch (Exception e) {
  326. }
  327. }
  328. }
  329. return filter_goodsIds;
  330. }
  331. /**
  332. *
  333. * @param goodsId
  334. * @param type 1、普通,2、注册平台
  335. * @return
  336. */
  337. public String getPayMsgTime(Long goodsId, DateTime now, Integer type) {
  338. Date startTime = null;
  339. StringBuilder notifyMsg = new StringBuilder();
  340. if (type == 1) {
  341. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  342. .eq(OrderDon::getGoodsId, goodsId)
  343. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  344. .orderByDesc(OrderDon::getId)
  345. .last("limit 1"));
  346. if (orderDon == null) {
  347. notifyMsg.append("点击购买立即上车");
  348. return notifyMsg.toString();
  349. }
  350. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  351. } else if (type == 2) {
  352. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  353. .eq(RegisterOrderDon::getGoodsId, goodsId)
  354. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  355. .orderByDesc(RegisterOrderDon::getId)
  356. .last("limit 1"));
  357. if (orderDon == null) {
  358. return null;
  359. }
  360. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  361. }
  362. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  363. if (hour == 0) {
  364. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  365. if (minute == 0) {
  366. notifyMsg.append("1分钟");
  367. } else {
  368. notifyMsg.append(minute);
  369. notifyMsg.append("分钟");
  370. }
  371. } else {
  372. if (hour > 3) {
  373. int hours = RandomUtil.randomInt(3) + 1;
  374. notifyMsg.append(hours);
  375. } else {
  376. notifyMsg.append(hour);
  377. }
  378. notifyMsg.append("小时");
  379. }
  380. notifyMsg.append("前有人购买");
  381. return notifyMsg.toString();
  382. }
  383. }