GoodsDonController.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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.exception.BusinessRuntimeException;
  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.request.ClickRecordReq;
  22. import com.cyksj.model.views.*;
  23. import com.cyksj.redis.RedisService;
  24. import com.cyksj.service.home.ClickRecordFrontService;
  25. import com.cyksj.service.shop.YhShopFrontService;
  26. import com.cyksj.web.util.StpUserUtil;
  27. import com.ejlchina.searcher.BeanSearcher;
  28. import com.ejlchina.searcher.SearchResult;
  29. import com.ejlchina.searcher.param.Operator;
  30. import com.ejlchina.searcher.util.MapBuilder;
  31. import com.ejlchina.searcher.util.MapUtils;
  32. import lombok.RequiredArgsConstructor;
  33. import lombok.extern.slf4j.Slf4j;
  34. import org.springframework.web.bind.annotation.*;
  35. import javax.servlet.http.HttpServletRequest;
  36. import java.math.BigDecimal;
  37. import java.util.*;
  38. import java.util.concurrent.ConcurrentHashMap;
  39. import java.util.stream.Collectors;
  40. import java.util.stream.Stream;
  41. /**
  42. * @author chan
  43. * @date 2022/9/26 5:33 PM
  44. */
  45. @Slf4j
  46. @RestController
  47. @RequestMapping("/applets/goods")
  48. @RequiredArgsConstructor
  49. public class GoodsDonController {
  50. private final HttpServletRequest request;
  51. private final BeanSearcher beanSearcher;
  52. private final OrderDonMapper orderDonMapper;
  53. private final RegisterOrderDonMapper registerOrderDonMapper;
  54. private final GoodsDonSkuMapper goodsDonSkuMapper;
  55. private final RedisService redisService;
  56. private final SysConfigMapper sysConfigMapper;
  57. private final ClickRecordFrontService clickRecordFrontService;
  58. private final YhShopFrontService yhShopFrontService;
  59. /**
  60. * 获取平台商品分类
  61. */
  62. @GetMapping("/get/category")
  63. public Result<List<GoodsDonCategoryFrontView>> getCategory(String customId) {
  64. Long yhsId = null;
  65. if (StrUtil.isNotBlank(customId)) {
  66. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  67. }
  68. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category";
  69. Object o = redisService.get(key);
  70. List<GoodsDonCategoryFrontView> list;
  71. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  72. String condition = " and (select count(*) from goods_don_category_relate gcr inner join goods_don g on g.id = gcr.goods_id %s where gcr.category = gc.id) > 0";
  73. if (yhsId != null) {
  74. condition = String.format(condition, String.format(" inner join yh_shop_goods_sku ys on ys.goods_id = g.id and ys.status is true and ys.deleted is true and g.is_distribute is true and yhs_id = %s", yhsId));
  75. } else {
  76. condition = String.format(condition, StrUtil.EMPTY);
  77. }
  78. mapBuilder.put("condition", condition);
  79. if (o == null) {
  80. list = beanSearcher.searchAll(GoodsDonCategoryFrontView.class, mapBuilder
  81. .build());
  82. redisService.setNx(key, list, 30 * 60l);
  83. o = redisService.get(key);
  84. }
  85. list = Jsons.parseList(o, GoodsDonCategoryFrontView.class);
  86. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  87. }
  88. /**
  89. * 商品搜索
  90. */
  91. @GetMapping("/search")
  92. public Result<SearchResult<GoodsDonSearchView>> search() {
  93. SearchResult<GoodsDonSearchView> search = beanSearcher.search(GoodsDonSearchView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
  94. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  95. }
  96. /**
  97. * 获取商品列表
  98. */
  99. @GetMapping("/get")
  100. public Result<SearchResult<GoodsDon>> get() {
  101. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  102. .field(GoodsDon::getStatus, true)
  103. .field(GoodsDon::getDeleted, true)
  104. .orderBy(GoodsDon::getSortBy).asc()
  105. .orderBy(GoodsDon::getId).asc()
  106. .build()
  107. );
  108. search.getDataList().forEach(goods -> {
  109. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  110. .eq(GoodsDonSku::getStatus, true)
  111. .orderByAsc(GoodsDonSku::getPrice)
  112. .last("limit 1")
  113. .select(GoodsDonSku::getPrice));
  114. Optional.ofNullable(goodsDonSku).ifPresent(sku -> {
  115. goods.setPrice(goodsDonSku.getPrice());
  116. goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  117. .eq(OrderDon::getGoodsId, goods.getId())
  118. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  119. });
  120. });
  121. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  122. }
  123. /**
  124. * 推广未登录,虚拟商品列表
  125. */
  126. @GetMapping("/get/popularize")
  127. @Deprecated
  128. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  129. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  130. .field(GoodsDonPopularizeView::getStatus, true)
  131. .field(GoodsDonPopularizeView::getDeleted, true)
  132. .field(GoodsDonPopularizeView::getType, 1)
  133. .orderBy(GoodsDonPopularizeView::getSortBy).asc()
  134. .orderBy(GoodsDonPopularizeView::getId).asc()
  135. .build());
  136. DateTime now = DateTime.now();
  137. search.getDataList().forEach(data -> {
  138. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  139. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  140. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  141. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  142. data.setSold(sold + data.getVirtualSold());
  143. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  144. if (skuList.size() > 0) {
  145. GoodsDonSku goodsDonSku = skuList.get(0);
  146. data.setPrice(goodsDonSku.getPrice());
  147. data.setMonths(goodsDonSku.getMonths());
  148. }
  149. });
  150. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  151. .eq(OrderDon::getGoodsId, data.getId())
  152. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  153. .orderByDesc(OrderDon::getId)
  154. .last("limit 1"));
  155. StringBuilder notifyMsg = new StringBuilder();
  156. if (orderDon == null) {
  157. notifyMsg.append("点击购买立即上车");
  158. data.setNotifyMsg(notifyMsg.toString());
  159. return;
  160. } else {
  161. Date payTime = orderDon.getPayTime();
  162. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  163. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  164. if (hour == 0) {
  165. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  166. if (minute == 0) {
  167. notifyMsg.append("1分钟");
  168. } else {
  169. notifyMsg.append(minute);
  170. notifyMsg.append("分钟");
  171. }
  172. } else {
  173. if (hour > 3) {
  174. int hours = RandomUtil.randomInt(3) + 1;
  175. notifyMsg.append(hours);
  176. } else {
  177. notifyMsg.append(hour);
  178. }
  179. notifyMsg.append("小时");
  180. }
  181. }
  182. notifyMsg.append("前有人购买");
  183. data.setNotifyMsg(notifyMsg.toString());
  184. });
  185. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  186. }
  187. /**
  188. * 官网首页 平台列表
  189. */
  190. @GetMapping("/get/list")
  191. public Result<? extends Object> getGoodsList(String customId) {
  192. if (StrUtil.isNotBlank(customId)) {
  193. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  194. return shopGoods;
  195. }
  196. List<Long> filter_goodsIds = getFilterGoodsIds();
  197. String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "PC";
  198. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  199. pcCacheKey += ":hz-ip";
  200. }
  201. Long fUid = StpUserUtil.getUserIdAfterLogin();
  202. Object o = redisService.get(pcCacheKey);
  203. List<GoodsFrontListView> list = null;
  204. if (o == null) {
  205. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  206. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  207. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  208. }
  209. builder.field(GoodsFrontListView::getType, 1);
  210. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  211. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  212. o = redisService.get(pcCacheKey);
  213. }
  214. if (list == null) {
  215. list = Jsons.parseList(o, GoodsFrontListView.class);
  216. }
  217. DateTime now = DateTime.now();
  218. list.forEach(data -> {
  219. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  220. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  221. data.setSold(sold + data.getSold());
  222. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  223. //设置特定价格
  224. setSpecificPrice(data, fUid);
  225. });
  226. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  227. }
  228. /**
  229. * h5 银河首页平台展示
  230. */
  231. @GetMapping("/get/homePage")
  232. @Deprecated
  233. public Result<? extends Object> getGoodsHomePage(String customId) {
  234. if (StrUtil.isNotBlank(customId)) {
  235. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  236. return shopGoods;
  237. }
  238. List<Long> filter_goodsIds = getFilterGoodsIds();
  239. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "H5";
  240. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  241. h5CacheKey += ":hz-ip";
  242. }
  243. Long fUid = StpUserUtil.getUserIdAfterLogin();
  244. Object o = redisService.get(h5CacheKey);
  245. List<GoodsFrontListView> list = null;
  246. if (o == null) {
  247. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  248. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  249. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  250. }
  251. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  252. redisService.setNx(h5CacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  253. o = redisService.get(h5CacheKey);
  254. }
  255. if (list == null) {
  256. list = Jsons.parseList(o, GoodsFrontListView.class);
  257. }
  258. DateTime now = DateTime.now();
  259. list.forEach(data -> {
  260. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  261. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  262. data.setSold(sold + data.getSold());
  263. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  264. //设置特定价格
  265. setSpecificPrice(data, fUid);
  266. });
  267. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  268. }
  269. /**
  270. * 注册首页平台展示
  271. */
  272. @GetMapping("/get/register")
  273. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  274. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getEnvName();
  275. Object value = redisService.get(registerCacheKey);
  276. if (value != null) {
  277. try {
  278. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  279. if (CollUtil.isNotEmpty(list)) {
  280. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  281. }
  282. } catch (Exception e) {
  283. }
  284. }
  285. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  286. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  287. DateTime now = DateTime.now();
  288. dataList.forEach(data -> {
  289. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  290. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  291. data.setSold(sold + data.getVirtualSold());
  292. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2, null));
  293. });
  294. redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  295. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  296. }
  297. /**
  298. * 获取商品详情
  299. */
  300. @GetMapping("/get/{id}")
  301. public Result<? extends Object> getDetail(@PathVariable Long id, String customId) {
  302. if (StrUtil.isNotBlank(customId) && id != 15) {
  303. Result<YhShopGoodsFrontView> yhShopGoodsDetail = getGoodsDetailByYhsId(yhShopFrontService.getYhsIdByCustomId(customId), id);
  304. return yhShopGoodsDetail;
  305. }
  306. String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "goods_detail:" + id;
  307. Object o = redisService.get(goodsDetailKey);
  308. GoodsFrontListView views = null;
  309. if (o == null) {
  310. views = beanSearcher.searchFirst(GoodsFrontListView.class, MapUtils.builder().field(GoodsFrontListView::getId, id)
  311. .build());
  312. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  313. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  314. if (views.getType() == 1) {
  315. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  316. }
  317. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  318. redisService.setNx(goodsDetailKey, views, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  319. o = redisService.get(goodsDetailKey);
  320. }
  321. Long fUid = StpUserUtil.getUserIdAfterLogin();
  322. if (views == null) {
  323. views = Jsons.parseObject(o, GoodsFrontListView.class);
  324. }
  325. for (GoodsDonSkuFrontView sku : views.getSkuList()) {
  326. if (sku.getIsDp()) {
  327. sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder()
  328. .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId())
  329. .build()));
  330. }
  331. if (views.getIsSp()) {
  332. if (fUid == null) {
  333. sku.setSpecificPrice(null);
  334. sku.setSpecificGoodsIds(null);
  335. continue;
  336. }
  337. //设置特定价格
  338. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  339. sku.setSpecificGoodsIds(null);
  340. sku.setSpecificPrice(null);
  341. } else {
  342. BigDecimal specificPrice = views.getSpecificPrice();
  343. BigDecimal skuSpecificPrice = sku.getSpecificPrice();
  344. Long specificSkuId = sku.getId();
  345. if (specificPrice == null) {
  346. views.setSpecificPrice(skuSpecificPrice);
  347. views.setSpecificSkuId(specificSkuId);
  348. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  349. views.setSpecificPrice(skuSpecificPrice);
  350. views.setSpecificSkuId(specificSkuId);
  351. }
  352. }
  353. }
  354. }
  355. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  356. .eq(OrderDon::getGoodsId, views.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  357. views.setSold(sold + views.getSold());
  358. //评论
  359. // OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  360. // .field(OrderCommentFrontView::getGoodsId, views.getId())
  361. // .orderBy(OrderCommentFrontView::getCommentTime).desc()
  362. // .orderBy(OrderCommentFrontView::getId).desc()
  363. // .build());
  364. // views.setComments(comments);
  365. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  366. }
  367. /**
  368. * 获取商品推荐平台
  369. */
  370. @GetMapping("/get/recommend/{id}")
  371. public Result<List<GoodsFrontListView>> getRecommendGoodsByGid(@PathVariable Long id, String customId) throws Exception {
  372. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "recommend:" + id;
  373. Long yhsId = null;
  374. if (StrUtil.isNotBlank(customId)) {
  375. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  376. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":recommend:" + id;
  377. }
  378. Object o = redisService.get(key);
  379. Long fUid = StpUserUtil.getUserIdAfterLogin();
  380. List<GoodsFrontListView> goodsRecommendViews = null;
  381. if (o == null) {
  382. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  383. .field(GoodsDon::getId, id).field(GoodsDon::getStatus, true)
  384. .field(GoodsDon::getDeleted, true)
  385. .build());
  386. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  387. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  388. MapBuilder mapBuilder = MapUtils.builder();
  389. if (yhsId != null) {
  390. mapBuilder.put("condition", String.format(" and g.id in (select distinct goods_id from yh_shop_goods_sku where yhs_id = %s and status is true and deleted is true) and g.is_distribute is true", yhsId));
  391. }
  392. mapBuilder.field(GoodsFrontListView::getId, recommend_gidList).op(Operator.InList);
  393. goodsRecommendViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build());
  394. redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l);
  395. o = redisService.get(key);
  396. } else {
  397. return GatewayResponse.SUCCESS.newBuilder().toResult();
  398. }
  399. }
  400. if (goodsRecommendViews == null) {
  401. goodsRecommendViews = Jsons.parseList(o.toString(), GoodsFrontListView.class);
  402. }
  403. goodsRecommendViews.forEach(data -> {
  404. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  405. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  406. data.setSold(data.getSold() + sold);
  407. //设置特定价格
  408. setSpecificPrice(data, fUid);
  409. });
  410. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  411. }
  412. /**
  413. * 获取商品多规格列表
  414. */
  415. @GetMapping("/get/sku")
  416. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  417. if (skuIds.isEmpty()) {
  418. return GatewayResponse.SUCCESS.newBuilder().toResult();
  419. }
  420. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  421. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  422. .build());
  423. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  424. }
  425. /**
  426. * 获取评价列表
  427. */
  428. @GetMapping("/get/comments")
  429. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  430. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  431. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  432. .orderBy(OrderCommentFrontView::getId).desc()
  433. .build());
  434. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  435. }
  436. /**
  437. * 获取礼品卡平台性情
  438. */
  439. @GetMapping("/get/card/{id}")
  440. public Result<Map<Integer, List<GiftCardGoodsSkuFrontView>>> getGiftCardDetail(@PathVariable Long id) {
  441. List<GiftCardGoodsSkuFrontView> giftCardGoodsSkuFrontViews = beanSearcher.searchAll(GiftCardGoodsSkuFrontView.class, MapUtils.builder().field(GiftCardGoodsSkuFrontView::getGoodsId, id).build());
  442. Map<Integer, List<GiftCardGoodsSkuFrontView>> map = giftCardGoodsSkuFrontViews.stream().collect(Collectors.groupingBy(GiftCardGoodsSkuFrontView::getGcType));
  443. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  444. }
  445. /**
  446. * 获取首页配置列表
  447. */
  448. @GetMapping("/get/homeManage")
  449. public Result<Map<String, List<HomeManagementFrontView>>> getHomeManagementFrontPage(String customId) {
  450. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "homeManage";
  451. Long yhsId = null;
  452. if (StrUtil.isNotBlank(customId)) {
  453. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  454. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":homeManage";
  455. }
  456. Long fUid = StpUserUtil.getUserIdAfterLogin();
  457. Object o = redisService.get(key);
  458. if (o == null) {
  459. String condition = "g.deleted is true and g.status is true";
  460. if (yhsId != null) {
  461. condition += String.format(" and g.id in (select distinct goods_id from yh_shop_goods_sku where yhs_id = %s and status is true and deleted is true) and g.is_distribute is true", yhsId);
  462. }
  463. Map<String, List<HomeManagementFrontView>> map = new ConcurrentHashMap<>();
  464. final String conditionSql = condition;
  465. Stream.of(HomeManagementConfig.Type.values())
  466. .forEach(type -> {
  467. MapBuilder builder = MapUtils.builder()
  468. .field(HomeManagementFrontView::getType, type.name());
  469. if (type == HomeManagementConfig.Type.ex || type == HomeManagementConfig.Type.vg) {
  470. builder.put("condition", conditionSql);
  471. }
  472. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  473. map.putIfAbsent(type.name(), homeManagementFrontViews);
  474. });
  475. redisService.setNx(key, map, 50 * 60l);
  476. o = redisService.get(key);
  477. }
  478. Map<String, List<HomeManagementFrontView>> map = Jsons.parseObject(o, Map.class);
  479. if (map.containsKey(HomeManagementConfig.Type.ex.name()) || map.containsKey(HomeManagementConfig.Type.vg.name())) {
  480. try {
  481. //设置特定价格
  482. List<HomeManagementFrontView> exList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.ex.name())), HomeManagementFrontView.class);
  483. if (CollUtil.isNotEmpty(exList)) {
  484. setHomeManageSpecific(exList, fUid);
  485. map.put(HomeManagementConfig.Type.ex.name(), exList);
  486. }
  487. //设置特定价格
  488. List<HomeManagementFrontView> vgList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.vg.name())), HomeManagementFrontView.class);
  489. if (CollUtil.isNotEmpty(vgList)) {
  490. setHomeManageSpecific(vgList, fUid);
  491. map.put(HomeManagementConfig.Type.vg.name(), vgList);
  492. }
  493. } catch (Exception e) {
  494. }
  495. }
  496. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  497. }
  498. /**
  499. * 首页点击 记录
  500. */
  501. @PostMapping("/homePage/click")
  502. public Result<String> homeManageClick(@RequestBody ClickRecordReq request) {
  503. Long userId = StpUserUtil.getUserIdAfterLogin();
  504. clickRecordFrontService.click(userId, request);
  505. return GatewayResponse.SUCCESS.newBuilder().toResult();
  506. }
  507. public List<Long> getFilterGoodsIds() {
  508. //过滤杭州Ai ChatGPT平台
  509. //获取当前ip地址
  510. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  511. .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  512. List<Long> filter_goodsIds = null;
  513. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  514. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  515. try {
  516. filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  517. } catch (Exception e) {
  518. }
  519. }
  520. }
  521. return filter_goodsIds;
  522. }
  523. /**
  524. * @param goodsId
  525. * @param type 1、普通,2、注册平台
  526. * @param yhsId 店铺id
  527. * @return
  528. */
  529. public String getPayMsgTime(Long goodsId, DateTime now, Integer type, Long yhsId) {
  530. Date startTime = null;
  531. StringBuilder notifyMsg = new StringBuilder();
  532. if (type == 1) {
  533. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  534. .eq(OrderDon::getGoodsId, goodsId)
  535. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  536. .eq(yhsId != null, OrderDon::getYhsId, yhsId)
  537. .orderByDesc(OrderDon::getId)
  538. .last("limit 1"));
  539. if (orderDon == null) {
  540. notifyMsg.append("点击购买立即上车");
  541. return notifyMsg.toString();
  542. }
  543. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  544. } else if (type == 2) {
  545. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  546. .eq(RegisterOrderDon::getGoodsId, goodsId)
  547. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  548. .orderByDesc(RegisterOrderDon::getId)
  549. .last("limit 1"));
  550. if (orderDon == null) {
  551. return null;
  552. }
  553. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  554. }
  555. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  556. if (hour == 0) {
  557. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  558. if (minute == 0) {
  559. notifyMsg.append("1分钟");
  560. } else {
  561. notifyMsg.append(minute);
  562. notifyMsg.append("分钟");
  563. }
  564. } else {
  565. if (hour > 3) {
  566. int hours = RandomUtil.randomInt(3) + 1;
  567. notifyMsg.append(hours);
  568. } else {
  569. notifyMsg.append(hour);
  570. }
  571. notifyMsg.append("小时");
  572. }
  573. notifyMsg.append("前有人购买");
  574. return notifyMsg.toString();
  575. }
  576. /**
  577. * 是否购买过特定平台
  578. */
  579. public Boolean isPaySpecificGoodsIds(String specificGoodsIdsStr, Long userId) {
  580. if (StrUtil.isNotBlank(specificGoodsIdsStr)) {
  581. try {
  582. Set<Long> specificGoodsIds = Jsons.parseSet(specificGoodsIdsStr, Long.class);
  583. Number number = beanSearcher.searchCount(OrderDon.class, MapUtils.builder().field(OrderDon::getGoodsId, specificGoodsIds).op(Operator.InList)
  584. .field(OrderDon::getUserId, userId)
  585. .field(OrderDon::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
  586. .build());
  587. if (number.intValue() == 0) {
  588. return false;
  589. }
  590. return true;
  591. } catch (Exception e) {
  592. }
  593. }
  594. return false;
  595. }
  596. /**
  597. * 设置平台 特定价格
  598. */
  599. public void setSpecificPrice(GoodsFrontListView data, Long fUid) {
  600. if (fUid != null) {
  601. if (data.getIsSp()) {
  602. //查找所有特定规格最小价格 满足条件
  603. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + data.getId();
  604. Object speciObj = redisService.get(specificGoodsKey);
  605. List<GoodsDonSku> specificSkus = null;
  606. if (speciObj != null) {
  607. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  608. } else {
  609. specificSkus = goodsDonSkuMapper.selectList(
  610. Wrappers.lambdaQuery(GoodsDonSku.class)
  611. .eq(GoodsDonSku::getGoodsId, data.getId())
  612. .eq(GoodsDonSku::getStatus, true)
  613. .gt(GoodsDonSku::getSpecificPrice, 0));
  614. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  615. }
  616. specificSkus.forEach(specificSku -> {
  617. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  618. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  619. BigDecimal specificPrice = data.getSpecificPrice();
  620. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  621. Long specificSkuId = specificSku.getId();
  622. if (specificPrice == null) {
  623. data.setSpecificPrice(skuSpecificPrice);
  624. data.setSpecificSkuId(specificSkuId);
  625. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  626. data.setSpecificPrice(skuSpecificPrice);
  627. data.setSpecificSkuId(specificSkuId);
  628. }
  629. }
  630. });
  631. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  632. if (CollUtil.isNotEmpty(skuList)) {
  633. skuList.forEach(sku -> {
  634. //设置特定价格
  635. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  636. sku.setSpecificGoodsIds(null);
  637. sku.setSpecificPrice(null);
  638. }
  639. });
  640. }
  641. }
  642. }
  643. }
  644. /**
  645. * 获取特定规格
  646. */
  647. public List<GoodsDonSku> getSpecificSku(Long goodsId) {
  648. //查找所有特定规格最小价格 满足条件
  649. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + goodsId;
  650. Object speciObj = redisService.get(specificGoodsKey);
  651. List<GoodsDonSku> specificSkus = null;
  652. if (speciObj != null) {
  653. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  654. } else {
  655. specificSkus = goodsDonSkuMapper.selectList(
  656. Wrappers.lambdaQuery(GoodsDonSku.class)
  657. .eq(GoodsDonSku::getGoodsId, goodsId)
  658. .eq(GoodsDonSku::getStatus, true)
  659. .gt(GoodsDonSku::getSpecificPrice, 0));
  660. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  661. }
  662. specificSkus = Optional.ofNullable(specificSkus).orElse(new ArrayList<>());
  663. return specificSkus;
  664. }
  665. /**
  666. * 设置首页配置 涉及平台特定价格
  667. */
  668. public void setHomeManageSpecific(List<HomeManagementFrontView> list, Long fUid) {
  669. DateTime now = DateTime.now();
  670. list.forEach(data -> {
  671. if (data.getIsSp() != null && data.getIsSp() && fUid != null) {
  672. //查找所有特定规格最小价格 满足条件
  673. List<GoodsDonSku> specificSkus = getSpecificSku(data.getGoodsId());
  674. specificSkus.forEach(specificSku -> {
  675. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  676. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  677. BigDecimal specificPrice = data.getSpecificPrice();
  678. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  679. Long specificSkuId = specificSku.getId();
  680. if (specificPrice == null) {
  681. data.setSpecificPrice(skuSpecificPrice);
  682. data.setSpecificSkuId(specificSkuId);
  683. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  684. data.setSpecificPrice(skuSpecificPrice);
  685. data.setSpecificSkuId(specificSkuId);
  686. }
  687. }
  688. });
  689. }
  690. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  691. .eq(OrderDon::getGoodsId, data.getGoodsId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  692. data.setSold(sold + data.getSold());
  693. data.setNotifyMsg(getPayMsgTime(data.getGoodsId(), now, 1, null));
  694. });
  695. }
  696. /**
  697. * 店铺平台列表
  698. */
  699. @GetMapping("/get/shop/goods/{yhsId}")
  700. public Result<List<YhShopGoodsFrontView>> getShopGoods(@PathVariable Long yhsId) {
  701. String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId;
  702. Object value = redisService.get(yhShopCacheKey);
  703. List<YhShopGoodsFrontView> list = null;
  704. if (value != null) {
  705. try {
  706. list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  707. } catch (Exception e) {
  708. }
  709. }
  710. if (CollUtil.isEmpty(list)) {
  711. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  712. builder.put("condition", String.format("and yhs_id = %s", yhsId));
  713. list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build());
  714. redisService.setNx(yhShopCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  715. }
  716. DateTime now = DateTime.now();
  717. list.forEach(data->{
  718. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  719. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  720. data.setSold(sold + data.getSold());
  721. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  722. });
  723. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  724. }
  725. /**
  726. * 店铺平台详情
  727. */
  728. @GetMapping("/get/shop/goods/detail/{yhsId}/{goodsId}")
  729. public Result<YhShopGoodsFrontView> getGoodsDetailByYhsId(@PathVariable Long yhsId, @PathVariable Long goodsId) {
  730. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  731. String condition = String.format("and yhs_id = %s and ys.goods_id = %s", yhsId, goodsId);
  732. builder.put("condition", condition);
  733. YhShopGoodsFrontView goodsDetailView = beanSearcher.searchFirst(YhShopGoodsFrontView.class, builder.build());
  734. if (goodsDetailView == null) {
  735. throw BusinessRuntimeException.getInstance("该平台不存在或已下架,请刷新页面");
  736. }
  737. goodsDetailView.setSkuList(beanSearcher.searchAll(YhShopGoodsSkuFrontView.class, MapUtils.builder().put("condition", condition).orderBy(YhShopGoodsSkuFrontView::getOrderBy).asc().build()));
  738. goodsDetailView.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, goodsDetailView.getId()).build()));
  739. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  740. .field(OrderCommentFrontView::getGoodsId, goodsDetailView.getId())
  741. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  742. .orderBy(OrderCommentFrontView::getId).desc()
  743. .build());
  744. goodsDetailView.setComments(comments);
  745. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDetailView);
  746. }
  747. /**
  748. * 根据分类获取对应平台
  749. */
  750. @GetMapping("/get/categoryGoods")
  751. public Result<? extends Object> getGoodsByCategory(Long cgy, String customId) {
  752. if (StrUtil.isNotBlank(customId)) {
  753. Result<List<YhShopGoodsFrontView>> shopGoods = getShopCategoryGoods(yhShopFrontService.getYhsIdByCustomId(customId), cgy);
  754. return shopGoods;
  755. }
  756. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category:goods";
  757. if (cgy != null) {
  758. categoryGoodsKey += ":" + cgy;
  759. }
  760. Object o = redisService.get(categoryGoodsKey);
  761. if (o == null) {
  762. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  763. if (cgy != null) {
  764. String condition = String.format(" and id in (select goods_id from goods_don_category_relate where category = %s)", cgy);
  765. mapBuilder.put("condition", condition);
  766. }
  767. List<GoodsFrontListView> goodsFrontListViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build());
  768. redisService.setNx(categoryGoodsKey, goodsFrontListViews, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 10 * 60);
  769. o = redisService.get(categoryGoodsKey);
  770. }
  771. List<GoodsFrontListView> goodsFrontListViews = Jsons.parseList(o, GoodsFrontListView.class);
  772. DateTime now = DateTime.now();
  773. goodsFrontListViews.forEach(data -> {
  774. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  775. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  776. data.setSold(sold + data.getSold());
  777. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  778. });
  779. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsFrontListViews);
  780. }
  781. @GetMapping("/get/shop/categoryGoods/{yhsId}")
  782. public Result<List<YhShopGoodsFrontView>> getShopCategoryGoods(@PathVariable Long yhsId, Long category) {
  783. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":category";
  784. if (category != null) {
  785. categoryGoodsKey += ":" + category;
  786. }
  787. Object value = redisService.get(categoryGoodsKey);
  788. if (value == null) {
  789. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  790. String condition = String.format("and yhs_id = %s", yhsId);
  791. if (category != null) {
  792. condition += String.format(" and ys.goods_id in (select goods_id from goods_don_category_relate where category = %s)", category);
  793. }
  794. builder.put("condition", condition);
  795. List<YhShopGoodsFrontView> list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build());
  796. redisService.setNx(categoryGoodsKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 5 * 60);
  797. value = redisService.get(categoryGoodsKey);
  798. }
  799. List<YhShopGoodsFrontView> list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  800. DateTime now = DateTime.now();
  801. list.forEach(data -> {
  802. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  803. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  804. data.setSold(sold + data.getSold());
  805. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  806. });
  807. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  808. }
  809. }