GoodsDonController.java 19 KB

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