GoodsDonController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. List<GoodsDonSkuFrontView> skuList = beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, data.getId()).field(GoodsDonSkuFrontView::getStatus, true).orderBy(GoodsDonSkuFrontView::getPrice).asc().build());
  142. data.setSkuList(skuList);
  143. data.getSkuList().forEach(sku -> {
  144. if (sku.getIsDp()) {
  145. List<GoodsDiscountPlusPurchaseRelationFrontView> dpSkuViews = beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder().field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId()).build());
  146. sku.setDpSkuViews(dpSkuViews);
  147. }
  148. });
  149. if (!skuList.isEmpty()) {
  150. data.setTwPrice(skuList.get(0).getTwPrice());
  151. data.setHkPrice(skuList.get(0).getHkPrice());
  152. }
  153. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  154. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  155. data.setSold(sold + data.getVirtualSold());
  156. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  157. .field(OrderCommentFrontView::getGoodsId, data.getId())
  158. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  159. .orderBy(OrderCommentFrontView::getId).desc()
  160. .build());
  161. data.setComments(comments);
  162. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1));
  163. });
  164. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  165. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  166. }
  167. /**
  168. * h5 银河首页平台展示
  169. */
  170. @GetMapping("/get/homePage")
  171. public Result<List<GoodsHomePageView>> getGoodsHomePage() {
  172. List<Long> filter_goodsIds = getFilterGoodsIds(false);
  173. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getNameLanguagePrefix() + "H5";
  174. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  175. h5CacheKey += ":hz-ip";
  176. }
  177. Object value = redisService.get(h5CacheKey);
  178. if (value != null) {
  179. try {
  180. List<GoodsHomePageView> list = Jsons.parseList(value, GoodsHomePageView.class);
  181. if (CollUtil.isNotEmpty(list)) {
  182. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  183. }
  184. } catch (Exception e) {
  185. }
  186. }
  187. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  188. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  189. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  190. }
  191. List<GoodsHomePageView> dataList = beanSearcher.searchAll(GoodsHomePageView.class, builder.build());
  192. DateTime now = DateTime.now();
  193. dataList.forEach(data -> {
  194. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  195. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  196. data.setSold(sold + data.getVirtualSold());
  197. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1));
  198. List<GoodsDonSku> skuList = beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build());
  199. if (!skuList.isEmpty()) {
  200. data.setTwPrice(skuList.get(0).getTwPrice());
  201. data.setHkPrice(skuList.get(0).getHkPrice());
  202. }
  203. });
  204. redisService.setNx(h5CacheKey, dataList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  205. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  206. }
  207. /**
  208. * 注册首页平台展示
  209. */
  210. @GetMapping("/get/register")
  211. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  212. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getNameLanguagePrefix();
  213. Object value = redisService.get(registerCacheKey);
  214. if (value != null) {
  215. try {
  216. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  217. if (CollUtil.isNotEmpty(list)) {
  218. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  219. }
  220. } catch (Exception e) {
  221. }
  222. }
  223. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  224. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  225. DateTime now = DateTime.now();
  226. dataList.forEach(data->{
  227. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  228. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  229. data.setSold(sold + data.getVirtualSold());
  230. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2));
  231. });
  232. redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  233. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  234. }
  235. /**
  236. * 获取商品详情
  237. */
  238. @GetMapping("/get/{id}")
  239. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  240. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  241. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  242. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  243. if (views.getType() == 1) {
  244. builder.orderBy(GoodsDonSku::getSorted).asc();
  245. }
  246. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  247. views.getSkuList().forEach(sku -> {
  248. if (sku.getIsDp()) {
  249. sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder()
  250. .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId())
  251. .build()));
  252. }
  253. });
  254. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  255. }
  256. /**
  257. * 获取商品推荐平台
  258. */
  259. @GetMapping("/get/recommend/{id}")
  260. public Result<List<GoodsRecommendViews>> getRecommendGoodsByGid(@PathVariable Long id) throws Exception {
  261. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getNameLanguagePrefix() + "recommend:" + id;
  262. Object value = redisService.get(key);
  263. if (value != null) {
  264. List<GoodsRecommendViews> goodsRecommendViews = Jsons.parseList(value.toString(), GoodsRecommendViews.class);
  265. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  266. }
  267. if (value == null) {
  268. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  269. .field(GoodsDon::getId, id)
  270. .field(GoodsDon::getStatus, true)
  271. .field(GoodsDon::getDeleted, true)
  272. .build());
  273. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  274. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  275. List<GoodsRecommendViews> goodsRecommendViews = beanSearcher.searchAll(GoodsRecommendViews.class, MapUtils.builder().field(GoodsRecommendViews::getGoodsId, recommend_gidList).op(Operator.InList).build());
  276. goodsRecommendViews.forEach(data->{
  277. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  278. .eq(RegisterOrderDon::getGoodsId, data.getGoodsId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  279. data.setSold(sold + data.getVirtualSold());
  280. });
  281. redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l);
  282. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  283. }
  284. }
  285. return GatewayResponse.SUCCESS.newBuilder().toResult();
  286. }
  287. /**
  288. * 获取商品多规格列表
  289. */
  290. @GetMapping("/get/sku")
  291. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  292. if (skuIds.isEmpty()) {
  293. return GatewayResponse.SUCCESS.newBuilder().toResult();
  294. }
  295. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  296. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  297. .build());
  298. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  299. }
  300. /**
  301. * 获取评价列表
  302. */
  303. @GetMapping("/get/comments")
  304. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  305. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  306. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  307. .orderBy(OrderCommentFrontView::getId).desc()
  308. .build());
  309. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  310. }
  311. public List<Long> getFilterGoodsIds(Boolean isOpen) {
  312. if (!isOpen) {
  313. return null;
  314. }
  315. //过滤杭州Ai ChatGPT平台
  316. //获取当前ip地址
  317. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  318. .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  319. List<Long> filter_goodsIds = null;
  320. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  321. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  322. try {
  323. filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  324. } catch (Exception e) {
  325. }
  326. }
  327. }
  328. return filter_goodsIds;
  329. }
  330. /**
  331. *
  332. * @param goodsId
  333. * @param type 1、普通,2、注册平台
  334. * @return
  335. */
  336. public String getPayMsgTime(Long goodsId, DateTime now, Integer type) {
  337. Date startTime = null;
  338. StringBuilder notifyMsg = new StringBuilder();
  339. if (type == 1) {
  340. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  341. .eq(OrderDon::getGoodsId, goodsId)
  342. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  343. .orderByDesc(OrderDon::getId)
  344. .last("limit 1"));
  345. if (orderDon == null) {
  346. notifyMsg.append(I18nMessageUtil.getI18nMsg("order_buy_now_notify"));
  347. return notifyMsg.toString();
  348. }
  349. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  350. } else if (type == 2) {
  351. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  352. .eq(RegisterOrderDon::getGoodsId, goodsId)
  353. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  354. .orderByDesc(RegisterOrderDon::getId)
  355. .last("limit 1"));
  356. if (orderDon == null) {
  357. return null;
  358. }
  359. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  360. }
  361. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  362. if (hour == 0) {
  363. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  364. if (minute == 0) {
  365. notifyMsg.append("1");
  366. notifyMsg.append(I18nMessageUtil.getI18nMsg("minute"));
  367. } else {
  368. notifyMsg.append(minute);
  369. if (minute > 1) {
  370. notifyMsg.append(I18nMessageUtil.getI18nMsg("minutes"));
  371. } else {
  372. notifyMsg.append(I18nMessageUtil.getI18nMsg("minute"));
  373. }
  374. }
  375. } else {
  376. if (hour > 3) {
  377. int hours = RandomUtil.randomInt(3) + 1;
  378. notifyMsg.append(hours);
  379. } else {
  380. notifyMsg.append(hour);
  381. }
  382. if (hour > 1) {
  383. notifyMsg.append(I18nMessageUtil.getI18nMsg("hours"));
  384. } else {
  385. notifyMsg.append(I18nMessageUtil.getI18nMsg("hour"));
  386. }
  387. }
  388. notifyMsg.append(I18nMessageUtil.getI18nMsg("order_pre_buy_it"));
  389. return notifyMsg.toString();
  390. }
  391. }