GoodsDonController.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.RegisterOrderDonMapper;
  18. import com.cyksj.mapper.SysConfigMapper;
  19. import com.cyksj.model.entity.*;
  20. import com.cyksj.model.views.*;
  21. import com.cyksj.redis.RedisService;
  22. import com.cyksj.web.util.StpUserUtil;
  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.math.BigDecimal;
  33. import java.util.Date;
  34. import java.util.List;
  35. import java.util.Optional;
  36. import java.util.Set;
  37. /**
  38. * @author chan
  39. * @date 2022/9/26 5:33 PM
  40. */
  41. @Slf4j
  42. @RestController
  43. @RequestMapping("/applets/goods")
  44. @RequiredArgsConstructor
  45. public class GoodsDonController {
  46. private final HttpServletRequest request;
  47. private final BeanSearcher beanSearcher;
  48. private final OrderDonMapper orderDonMapper;
  49. private final RegisterOrderDonMapper registerOrderDonMapper;
  50. private final GoodsDonSkuMapper goodsDonSkuMapper;
  51. private final RedisService redisService;
  52. private final SysConfigMapper sysConfigMapper;
  53. /**
  54. * 获取商品列表
  55. */
  56. @GetMapping("/get")
  57. public Result<SearchResult<GoodsDon>> get() {
  58. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  59. .field(GoodsDon::getStatus, true)
  60. .field(GoodsDon::getDeleted, true)
  61. .orderBy(GoodsDon::getSortBy).desc()
  62. .orderBy(GoodsDon::getId).asc()
  63. .build()
  64. );
  65. search.getDataList().forEach(goods -> {
  66. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  67. .eq(GoodsDonSku::getStatus, true)
  68. .orderByAsc(GoodsDonSku::getPrice)
  69. .last("limit 1")
  70. .select(GoodsDonSku::getPrice));
  71. Optional.ofNullable(goodsDonSku).ifPresent(sku->{
  72. goods.setPrice(goodsDonSku.getPrice());
  73. goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  74. .eq(OrderDon::getGoodsId, goods.getId())
  75. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  76. });
  77. });
  78. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  79. }
  80. /**
  81. * 推广未登录,虚拟商品列表
  82. */
  83. @GetMapping("/get/popularize")
  84. @Deprecated
  85. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  86. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  87. .field(GoodsDonPopularizeView::getStatus, true)
  88. .field(GoodsDonPopularizeView::getDeleted, true)
  89. .field(GoodsDonPopularizeView::getType, 1)
  90. .orderBy(GoodsDonPopularizeView::getSortBy).desc()
  91. .orderBy(GoodsDonPopularizeView::getId).asc()
  92. .build());
  93. DateTime now = DateTime.now();
  94. search.getDataList().forEach(data -> {
  95. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  96. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  97. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  98. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  99. data.setSold(sold + data.getVirtualSold());
  100. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  101. if (skuList.size() > 0) {
  102. GoodsDonSku goodsDonSku = skuList.get(0);
  103. data.setPrice(goodsDonSku.getPrice());
  104. data.setMonths(goodsDonSku.getMonths());
  105. }
  106. });
  107. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  108. .eq(OrderDon::getGoodsId, data.getId())
  109. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  110. .orderByDesc(OrderDon::getId)
  111. .last("limit 1"));
  112. StringBuilder notifyMsg = new StringBuilder();
  113. if (orderDon == null) {
  114. notifyMsg.append("点击购买立即上车");
  115. data.setNotifyMsg(notifyMsg.toString());
  116. return;
  117. } else {
  118. Date payTime = orderDon.getPayTime();
  119. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  120. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  121. if (hour == 0) {
  122. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  123. if (minute == 0) {
  124. notifyMsg.append("1分钟");
  125. } else {
  126. notifyMsg.append(minute);
  127. notifyMsg.append("分钟");
  128. }
  129. } else {
  130. if (hour > 3) {
  131. int hours = RandomUtil.randomInt(3) + 1;
  132. notifyMsg.append(hours);
  133. } else {
  134. notifyMsg.append(hour);
  135. }
  136. notifyMsg.append("小时");
  137. }
  138. }
  139. notifyMsg.append("前有人购买");
  140. data.setNotifyMsg(notifyMsg.toString());
  141. });
  142. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  143. }
  144. /**
  145. * 官网首页 平台列表
  146. */
  147. @GetMapping("/get/list")
  148. public Result<List<GoodsFrontListView>> getGoodsList() {
  149. List<Long> filter_goodsIds = getFilterGoodsIds();
  150. String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "PC";
  151. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  152. pcCacheKey += ":hz-ip";
  153. }
  154. Long fUid = StpUserUtil.getUserIdAfterLogin();
  155. Object value = redisService.get(pcCacheKey);
  156. if (value != null) {
  157. try {
  158. List<GoodsFrontListView> list = Jsons.parseList(value, GoodsFrontListView.class);
  159. if (CollUtil.isNotEmpty(list)) {
  160. //设置特定价格
  161. setSpecificPrice(list, fUid);
  162. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  163. }
  164. } catch (Exception e) {
  165. }
  166. }
  167. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  168. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  169. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  170. }
  171. List<GoodsFrontListView> list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  172. DateTime now = DateTime.now();
  173. list.forEach(data -> {
  174. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  175. data.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, data.getId()).field(GoodsDonSkuFrontView::getStatus, true).orderBy(GoodsDonSkuFrontView::getPrice).asc().build()));
  176. data.getSkuList().forEach(sku -> {
  177. if (sku.getIsDp()) {
  178. List<GoodsDiscountPlusPurchaseRelationFrontView> dpSkuViews = beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder().field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId()).build());
  179. sku.setDpSkuViews(dpSkuViews);
  180. }
  181. });
  182. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  183. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  184. data.setSold(sold + data.getVirtualSold());
  185. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  186. });
  187. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  188. //设置特定价格
  189. setSpecificPrice(list, fUid);
  190. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  191. }
  192. /**
  193. * h5 银河首页平台展示
  194. */
  195. @GetMapping("/get/homePage")
  196. @Deprecated
  197. public Result<List<GoodsFrontListView>> getGoodsHomePage() {
  198. List<Long> filter_goodsIds = getFilterGoodsIds();
  199. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "H5";
  200. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  201. h5CacheKey += ":hz-ip";
  202. }
  203. Long fUid = StpUserUtil.getUserIdAfterLogin();
  204. Object value = redisService.get(h5CacheKey);
  205. if (value != null) {
  206. try {
  207. List<GoodsFrontListView> list = Jsons.parseList(value, GoodsFrontListView.class);
  208. if (CollUtil.isNotEmpty(list)) {
  209. //设置特定价格
  210. setSpecificPrice(list, fUid);
  211. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  212. }
  213. } catch (Exception e) {
  214. }
  215. }
  216. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  217. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  218. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  219. }
  220. List<GoodsFrontListView> dataList = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  221. DateTime now = DateTime.now();
  222. dataList.forEach(data -> {
  223. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  224. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  225. data.setSold(sold + data.getVirtualSold());
  226. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  227. });
  228. redisService.setNx(h5CacheKey, dataList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  229. //设置特定价格
  230. setSpecificPrice(dataList, fUid);
  231. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  232. }
  233. /**
  234. * 注册首页平台展示
  235. */
  236. @GetMapping("/get/register")
  237. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  238. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getName();
  239. Object value = redisService.get(registerCacheKey);
  240. if (value != null) {
  241. try {
  242. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  243. if (CollUtil.isNotEmpty(list)) {
  244. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  245. }
  246. } catch (Exception e) {
  247. }
  248. }
  249. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  250. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  251. DateTime now = DateTime.now();
  252. dataList.forEach(data->{
  253. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  254. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  255. data.setSold(sold + data.getVirtualSold());
  256. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2, null));
  257. });
  258. redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  259. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  260. }
  261. /**
  262. * 获取商品详情
  263. */
  264. @GetMapping("/get/{id}")
  265. public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
  266. Long fUid = StpUserUtil.getUserIdAfterLogin();
  267. GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
  268. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  269. MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
  270. if (views.getType() == 1) {
  271. builder.orderBy(GoodsDonSku::getOrderBy).asc();
  272. }
  273. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  274. views.getSkuList().forEach(sku -> {
  275. if (sku.getIsDp()) {
  276. sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder()
  277. .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId())
  278. .build()));
  279. }
  280. if (views.getIsSp()) {
  281. if (fUid == null) {
  282. sku.setSpecificPrice(null);
  283. sku.setSpecificGoodsIds(null);
  284. return;
  285. }
  286. //设置特定价格
  287. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  288. sku.setSpecificGoodsIds(null);
  289. sku.setSpecificPrice(null);
  290. }
  291. }
  292. });
  293. //评论
  294. // OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  295. // .field(OrderCommentFrontView::getGoodsId, views.getId())
  296. // .orderBy(OrderCommentFrontView::getCommentTime).desc()
  297. // .orderBy(OrderCommentFrontView::getId).desc()
  298. // .build());
  299. // views.setComments(comments);
  300. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  301. }
  302. /**
  303. * 获取商品推荐平台
  304. */
  305. @GetMapping("/get/recommend/{id}")
  306. public Result<List<GoodsFrontListView>> getRecommendGoodsByGid(@PathVariable Long id) throws Exception {
  307. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "recommend:" + id;
  308. Object value = redisService.get(key);
  309. Long fUid = StpUserUtil.getUserIdAfterLogin();
  310. if (value != null) {
  311. List<GoodsFrontListView> goodsRecommendViews = Jsons.parseList(value.toString(), GoodsFrontListView.class);
  312. setSpecificPrice(goodsRecommendViews, fUid);
  313. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  314. }
  315. if (value == null) {
  316. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  317. .field(GoodsDon::getId, id)
  318. .field(GoodsDon::getStatus, true)
  319. .field(GoodsDon::getDeleted, true)
  320. .build());
  321. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  322. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  323. List<GoodsFrontListView> goodsRecommendViews = beanSearcher.searchAll(GoodsFrontListView.class, MapUtils.builder().field(GoodsRecommendViews::getGoodsId, recommend_gidList).op(Operator.InList).build());
  324. goodsRecommendViews.forEach(data -> {
  325. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  326. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  327. data.setSold(sold + data.getVirtualSold());
  328. });
  329. redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l);
  330. //设置特定价格
  331. setSpecificPrice(goodsRecommendViews, fUid);
  332. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  333. }
  334. }
  335. return GatewayResponse.SUCCESS.newBuilder().toResult();
  336. }
  337. /**
  338. * 获取商品多规格列表
  339. */
  340. @GetMapping("/get/sku")
  341. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  342. if (skuIds.isEmpty()) {
  343. return GatewayResponse.SUCCESS.newBuilder().toResult();
  344. }
  345. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  346. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  347. .build());
  348. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  349. }
  350. /**
  351. * 获取评价列表
  352. */
  353. @GetMapping("/get/comments")
  354. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  355. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  356. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  357. .orderBy(OrderCommentFrontView::getId).desc()
  358. .build());
  359. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  360. }
  361. public List<Long> getFilterGoodsIds() {
  362. //过滤杭州Ai ChatGPT平台
  363. //获取当前ip地址
  364. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  365. .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  366. List<Long> filter_goodsIds = null;
  367. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  368. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  369. try {
  370. filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  371. } catch (Exception e) {
  372. }
  373. }
  374. }
  375. return filter_goodsIds;
  376. }
  377. /**
  378. * @param goodsId
  379. * @param type 1、普通,2、注册平台
  380. * @param yhsId 店铺id
  381. * @return
  382. */
  383. public String getPayMsgTime(Long goodsId, DateTime now, Integer type, Long yhsId) {
  384. Date startTime = null;
  385. StringBuilder notifyMsg = new StringBuilder();
  386. if (type == 1) {
  387. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  388. .eq(OrderDon::getGoodsId, goodsId)
  389. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  390. .eq(yhsId != null, OrderDon::getYhsId, yhsId)
  391. .orderByDesc(OrderDon::getId)
  392. .last("limit 1"));
  393. if (orderDon == null) {
  394. notifyMsg.append("点击购买立即上车");
  395. return notifyMsg.toString();
  396. }
  397. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  398. } else if (type == 2) {
  399. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  400. .eq(RegisterOrderDon::getGoodsId, goodsId)
  401. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  402. .orderByDesc(RegisterOrderDon::getId)
  403. .last("limit 1"));
  404. if (orderDon == null) {
  405. return null;
  406. }
  407. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  408. }
  409. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  410. if (hour == 0) {
  411. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  412. if (minute == 0) {
  413. notifyMsg.append("1分钟");
  414. } else {
  415. notifyMsg.append(minute);
  416. notifyMsg.append("分钟");
  417. }
  418. } else {
  419. if (hour > 3) {
  420. int hours = RandomUtil.randomInt(3) + 1;
  421. notifyMsg.append(hours);
  422. } else {
  423. notifyMsg.append(hour);
  424. }
  425. notifyMsg.append("小时");
  426. }
  427. notifyMsg.append("前有人购买");
  428. return notifyMsg.toString();
  429. }
  430. /**
  431. * 是否购买过特定平台
  432. */
  433. public Boolean isPaySpecificGoodsIds(String specificGoodsIdsStr, Long userId) {
  434. if (StrUtil.isNotBlank(specificGoodsIdsStr)) {
  435. try {
  436. Set<Long> specificGoodsIds = Jsons.parseSet(specificGoodsIdsStr, Long.class);
  437. Number number = beanSearcher.searchCount(OrderDon.class, MapUtils.builder().field(OrderDon::getGoodsId, specificGoodsIds).op(Operator.InList)
  438. .field(OrderDon::getUserId, userId)
  439. .field(OrderDon::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
  440. .build());
  441. if (number.intValue() == 0) {
  442. return false;
  443. }
  444. return true;
  445. } catch (Exception e) {
  446. }
  447. }
  448. return false;
  449. }
  450. /**
  451. * 设置平台 特定价格
  452. */
  453. public void setSpecificPrice(List<GoodsFrontListView> list, Long fUid) {
  454. if (fUid != null) {
  455. list.forEach(data -> {
  456. if (data.getIsSp()) {
  457. //查找所有特定规格最小价格 满足条件
  458. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "specific:" + data.getId();
  459. Object speciObj = redisService.get(specificGoodsKey);
  460. List<GoodsDonSku> specificSkus = null;
  461. if (speciObj != null) {
  462. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  463. } else {
  464. specificSkus = goodsDonSkuMapper.selectList(
  465. Wrappers.lambdaQuery(GoodsDonSku.class)
  466. .eq(GoodsDonSku::getGoodsId, data.getId())
  467. .eq(GoodsDonSku::getStatus, true)
  468. .gt(GoodsDonSku::getSpecificPrice, 0));
  469. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  470. }
  471. specificSkus.forEach(specificSku -> {
  472. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  473. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  474. BigDecimal specificPrice = data.getSpecificPrice();
  475. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  476. Long specificSkuId = specificSku.getId();
  477. if (specificPrice == null) {
  478. data.setSpecificPrice(skuSpecificPrice);
  479. data.setSpecificSkuId(specificSkuId);
  480. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  481. data.setSpecificPrice(skuSpecificPrice);
  482. data.setSpecificSkuId(specificSkuId);
  483. }
  484. }
  485. });
  486. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  487. if (CollUtil.isNotEmpty(skuList)) {
  488. skuList.forEach(sku->{
  489. //设置特定价格
  490. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  491. sku.setSpecificGoodsIds(null);
  492. sku.setSpecificPrice(null);
  493. }
  494. });
  495. }
  496. }
  497. });
  498. }
  499. }
  500. /**
  501. * 店铺平台列表
  502. */
  503. @GetMapping("/get/shop/goods/{yhsId}")
  504. public Result<List<YhShopGoodsFrontView>> getShopGoods(@PathVariable Long yhsId) {
  505. String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "yhShop:" + yhsId;
  506. Object value = redisService.get(yhShopCacheKey);
  507. List<YhShopGoodsFrontView> list = null;
  508. if (value != null) {
  509. try {
  510. list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  511. } catch (Exception e) {
  512. }
  513. }
  514. if (CollUtil.isEmpty(list)) {
  515. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  516. builder.put("condition", String.format("and yhs_id = %s", yhsId));
  517. list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build());
  518. redisService.setNx(yhShopCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  519. }
  520. DateTime now = DateTime.now();
  521. list.forEach(data->{
  522. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  523. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  524. data.setSold(sold + data.getVs());
  525. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  526. });
  527. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  528. }
  529. /**
  530. * 店铺平台详情
  531. */
  532. @GetMapping("/get/shop/goods/detail/{yhsId}/{goodsId}")
  533. public Result<YhShopGoodsFrontView> getGoodsDetailByYhsId(@PathVariable Long yhsId, @PathVariable Long goodsId) {
  534. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  535. String condition = String.format("and yhs_id = %s and goods_id = %s", yhsId, goodsId);
  536. builder.put("condition", condition);
  537. YhShopGoodsFrontView goodsDetailView = beanSearcher.searchFirst(YhShopGoodsFrontView.class, builder.build());
  538. goodsDetailView.setSkuList(beanSearcher.searchAll(YhShopGoodsSkuFrontView.class, MapUtils.builder().put("condition", condition).orderBy(YhShopGoodsSkuFrontView::getOrderBy).asc().build()));
  539. goodsDetailView.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, goodsDetailView.getId()).build()));
  540. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  541. .field(OrderCommentFrontView::getGoodsId, goodsDetailView.getId())
  542. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  543. .orderBy(OrderCommentFrontView::getId).desc()
  544. .build());
  545. goodsDetailView.setComments(comments);
  546. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDetailView);
  547. }
  548. }