package com.cyksj.web.controller.goods; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.servlet.ServletUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.cyksj.common.constant.Constant; import com.cyksj.common.exception.BusinessRuntimeException; import com.cyksj.common.util.Jsons; import com.cyksj.common.util.StringUtil; import com.cyksj.dto.Result; import com.cyksj.enums.GatewayResponse; import com.cyksj.mapper.GoodsDonSkuMapper; import com.cyksj.mapper.OrderDonMapper; import com.cyksj.mapper.RegisterOrderDonMapper; import com.cyksj.mapper.SysConfigMapper; import com.cyksj.model.entity.*; import com.cyksj.model.request.ClickRecordReq; import com.cyksj.model.views.*; import com.cyksj.redis.RedisService; import com.cyksj.service.home.ClickRecordFrontService; import com.cyksj.service.shop.YhShopFrontService; import com.cyksj.web.util.StpUserUtil; import com.ejlchina.searcher.BeanSearcher; import com.ejlchina.searcher.SearchResult; import com.ejlchina.searcher.param.Operator; import com.ejlchina.searcher.util.MapBuilder; import com.ejlchina.searcher.util.MapUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author chan * @date 2022/9/26 5:33 PM */ @Slf4j @RestController @RequestMapping("/applets/goods") @RequiredArgsConstructor public class GoodsDonController { private final HttpServletRequest request; private final BeanSearcher beanSearcher; private final OrderDonMapper orderDonMapper; private final RegisterOrderDonMapper registerOrderDonMapper; private final GoodsDonSkuMapper goodsDonSkuMapper; private final RedisService redisService; private final SysConfigMapper sysConfigMapper; private final ClickRecordFrontService clickRecordFrontService; private final YhShopFrontService yhShopFrontService; /** * 获取平台商品分类 */ @GetMapping("/get/category") public Result> getCategory(String customId) { Long yhsId = null; if (StrUtil.isNotBlank(customId)) { yhsId = yhShopFrontService.getYhsIdByCustomId(customId); } String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category"; Object o = redisService.get(key); List list; MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap()); 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"; if (yhsId != null) { 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)); } else { condition = String.format(condition, StrUtil.EMPTY); } mapBuilder.put("condition", condition); if (o == null) { list = beanSearcher.searchAll(GoodsDonCategoryFrontView.class, mapBuilder .build()); redisService.setNx(key, list, 30 * 60l); o = redisService.get(key); } list = Jsons.parseList(o, GoodsDonCategoryFrontView.class); return GatewayResponse.SUCCESS.newBuilder().toResult(list); } /** * 商品搜索 */ @GetMapping("/search") public Result> search() { SearchResult search = beanSearcher.search(GoodsDonSearchView.class, MapUtils.flatBuilder(request.getParameterMap()).build()); return GatewayResponse.SUCCESS.newBuilder().toResult(search); } /** * 获取商品列表 */ @GetMapping("/get") public Result> get() { SearchResult search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap()) .field(GoodsDon::getStatus, true) .field(GoodsDon::getDeleted, true) .orderBy(GoodsDon::getSortBy).asc() .orderBy(GoodsDon::getId).asc() .build() ); search.getDataList().forEach(goods -> { GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId()) .eq(GoodsDonSku::getStatus, true) .orderByAsc(GoodsDonSku::getPrice) .last("limit 1") .select(GoodsDonSku::getPrice)); Optional.ofNullable(goodsDonSku).ifPresent(sku -> { goods.setPrice(goodsDonSku.getPrice()); goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, goods.getId()) .notIn(OrderDon::getStatus, Constant.noOrderStatus))); }); }); return GatewayResponse.SUCCESS.newBuilder().toResult(search); } /** * 推广未登录,虚拟商品列表 */ @GetMapping("/get/popularize") @Deprecated public Result> getPopularizeVipGoods() { SearchResult search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap()) .field(GoodsDonPopularizeView::getStatus, true) .field(GoodsDonPopularizeView::getDeleted, true) .field(GoodsDonPopularizeView::getType, 1) .orderBy(GoodsDonPopularizeView::getSortBy).asc() .orderBy(GoodsDonPopularizeView::getId).asc() .build()); DateTime now = DateTime.now(); search.getDataList().forEach(data -> { data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build())); data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build())); Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getVirtualSold()); Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> { if (skuList.size() > 0) { GoodsDonSku goodsDonSku = skuList.get(0); data.setPrice(goodsDonSku.getPrice()); data.setMonths(goodsDonSku.getMonths()); } }); OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()) .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId) .orderByDesc(OrderDon::getId) .last("limit 1")); StringBuilder notifyMsg = new StringBuilder(); if (orderDon == null) { notifyMsg.append("点击购买立即上车"); data.setNotifyMsg(notifyMsg.toString()); return; } else { Date payTime = orderDon.getPayTime(); Date startTime = payTime != null ? payTime : orderDon.getCreatedTime(); long hour = DateUtil.between(startTime, now, DateUnit.HOUR); if (hour == 0) { long minute = DateUtil.between(startTime, now, DateUnit.MINUTE); if (minute == 0) { notifyMsg.append("1分钟"); } else { notifyMsg.append(minute); notifyMsg.append("分钟"); } } else { if (hour > 3) { int hours = RandomUtil.randomInt(3) + 1; notifyMsg.append(hours); } else { notifyMsg.append(hour); } notifyMsg.append("小时"); } } notifyMsg.append("前有人购买"); data.setNotifyMsg(notifyMsg.toString()); }); return GatewayResponse.SUCCESS.newBuilder().toResult(search); } /** * 官网首页 平台列表 */ @GetMapping("/get/list") public Result getGoodsList(String customId) { if (StrUtil.isNotBlank(customId)) { Result> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId)); return shopGoods; } List filter_goodsIds = getFilterGoodsIds(); String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "PC"; if (CollUtil.isNotEmpty(filter_goodsIds)) { pcCacheKey += ":hz-ip"; } Long fUid = StpUserUtil.getUserIdAfterLogin(); Object o = redisService.get(pcCacheKey); List list = null; if (o == null) { MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); if (CollUtil.isNotEmpty(filter_goodsIds)) { builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn); } builder.field(GoodsFrontListView::getType, 1); list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build()); redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); o = redisService.get(pcCacheKey); } if (list == null) { list = Jsons.parseList(o, GoodsFrontListView.class); } DateTime now = DateTime.now(); list.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null)); //设置特定价格 setSpecificPrice(data, fUid); }); return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list)); } /** * h5 银河首页平台展示 */ @GetMapping("/get/homePage") @Deprecated public Result getGoodsHomePage(String customId) { if (StrUtil.isNotBlank(customId)) { Result> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId)); return shopGoods; } List filter_goodsIds = getFilterGoodsIds(); String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "H5"; if (CollUtil.isNotEmpty(filter_goodsIds)) { h5CacheKey += ":hz-ip"; } Long fUid = StpUserUtil.getUserIdAfterLogin(); Object o = redisService.get(h5CacheKey); List list = null; if (o == null) { MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); if (CollUtil.isNotEmpty(filter_goodsIds)) { builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn); } list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build()); redisService.setNx(h5CacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); o = redisService.get(h5CacheKey); } if (list == null) { list = Jsons.parseList(o, GoodsFrontListView.class); } DateTime now = DateTime.now(); list.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null)); //设置特定价格 setSpecificPrice(data, fUid); }); return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list)); } /** * 注册首页平台展示 */ @GetMapping("/get/register") public Result> getRegisterGoodsDon() { String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getEnvName(); Object value = redisService.get(registerCacheKey); if (value != null) { try { List list = Jsons.parseList(value, RegisterGoodsDonView.class); if (CollUtil.isNotEmpty(list)) { return GatewayResponse.SUCCESS.newBuilder().toResult(list); } } catch (Exception e) { } } MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); List dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build()); DateTime now = DateTime.now(); dataList.forEach(data -> { Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class) .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getVirtualSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2, null)); }); redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout()); return GatewayResponse.SUCCESS.newBuilder().toResult(dataList); } /** * 获取商品详情 */ @GetMapping("/get/{id}") public Result getDetail(@PathVariable Long id, String customId) { if (StrUtil.isNotBlank(customId) && id != 15) { Result yhShopGoodsDetail = getGoodsDetailByYhsId(yhShopFrontService.getYhsIdByCustomId(customId), id); return yhShopGoodsDetail; } String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "goods_detail:" + id; Object o = redisService.get(goodsDetailKey); GoodsFrontListView views = null; if (o == null) { views = beanSearcher.searchFirst(GoodsFrontListView.class, MapUtils.builder().field(GoodsFrontListView::getId, id) .build()); views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build())); MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true); if (views.getType() == 1) { builder.orderBy(GoodsDonSku::getOrderBy).asc(); } views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build())); redisService.setNx(goodsDetailKey, views, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10)); o = redisService.get(goodsDetailKey); } Long fUid = StpUserUtil.getUserIdAfterLogin(); if (views == null) { views = Jsons.parseObject(o, GoodsFrontListView.class); } for (GoodsDonSkuFrontView sku : views.getSkuList()) { if (sku.getIsDp()) { sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder() .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId()) .build())); } if (views.getIsSp()) { if (fUid == null) { sku.setSpecificPrice(null); sku.setSpecificGoodsIds(null); continue; } //设置特定价格 if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) { sku.setSpecificGoodsIds(null); sku.setSpecificPrice(null); } else { BigDecimal specificPrice = views.getSpecificPrice(); BigDecimal skuSpecificPrice = sku.getSpecificPrice(); Long specificSkuId = sku.getId(); if (specificPrice == null) { views.setSpecificPrice(skuSpecificPrice); views.setSpecificSkuId(specificSkuId); } else if (specificPrice.compareTo(skuSpecificPrice) > 0) { views.setSpecificPrice(skuSpecificPrice); views.setSpecificSkuId(specificSkuId); } } } } Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, views.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); views.setSold(sold + views.getSold()); //评论 // OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder() // .field(OrderCommentFrontView::getGoodsId, views.getId()) // .orderBy(OrderCommentFrontView::getCommentTime).desc() // .orderBy(OrderCommentFrontView::getId).desc() // .build()); // views.setComments(comments); return GatewayResponse.SUCCESS.newBuilder().toResult(views); } /** * 获取商品推荐平台 */ @GetMapping("/get/recommend/{id}") public Result> getRecommendGoodsByGid(@PathVariable Long id, String customId) throws Exception { String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "recommend:" + id; Long yhsId = null; if (StrUtil.isNotBlank(customId)) { yhsId = yhShopFrontService.getYhsIdByCustomId(customId); key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":recommend:" + id; } Object o = redisService.get(key); Long fUid = StpUserUtil.getUserIdAfterLogin(); List goodsRecommendViews = null; if (o == null) { GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder() .field(GoodsDon::getId, id).field(GoodsDon::getStatus, true) .field(GoodsDon::getDeleted, true) .build()); if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) { List recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class); MapBuilder mapBuilder = MapUtils.builder(); if (yhsId != null) { 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)); } mapBuilder.field(GoodsFrontListView::getId, recommend_gidList).op(Operator.InList); goodsRecommendViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build()); redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l); o = redisService.get(key); } else { return GatewayResponse.SUCCESS.newBuilder().toResult(); } } if (goodsRecommendViews == null) { goodsRecommendViews = Jsons.parseList(o.toString(), GoodsFrontListView.class); } goodsRecommendViews.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(data.getSold() + sold); //设置特定价格 setSpecificPrice(data, fUid); }); return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews); } /** * 获取商品多规格列表 */ @GetMapping("/get/sku") public Result> getGoodsDonSkuView(@RequestParam(value = "skuIds") List skuIds) { if (skuIds.isEmpty()) { return GatewayResponse.SUCCESS.newBuilder().toResult(); } List views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder() .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList) .build()); return GatewayResponse.SUCCESS.newBuilder().toResult(views); } /** * 获取评价列表 */ @GetMapping("/get/comments") public Result> getComments() { SearchResult search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap()) .orderBy(OrderCommentFrontView::getCommentTime).desc() .orderBy(OrderCommentFrontView::getId).desc() .build()); return GatewayResponse.SUCCESS.newBuilder().toResult(search); } /** * 获取礼品卡平台性情 */ @GetMapping("/get/card/{id}") public Result>> getGiftCardDetail(@PathVariable Long id) { List giftCardGoodsSkuFrontViews = beanSearcher.searchAll(GiftCardGoodsSkuFrontView.class, MapUtils.builder().field(GiftCardGoodsSkuFrontView::getGoodsId, id).build()); Map> map = giftCardGoodsSkuFrontViews.stream().collect(Collectors.groupingBy(GiftCardGoodsSkuFrontView::getGcType)); return GatewayResponse.SUCCESS.newBuilder().toResult(map); } /** * 获取首页配置列表 */ @GetMapping("/get/homeManage") public Result>> getHomeManagementFrontPage(String customId) { String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "homeManage"; Long yhsId = null; if (StrUtil.isNotBlank(customId)) { yhsId = yhShopFrontService.getYhsIdByCustomId(customId); key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":homeManage"; } Long fUid = StpUserUtil.getUserIdAfterLogin(); Object o = redisService.get(key); if (o == null) { String condition = "g.deleted is true and g.status is true"; if (yhsId != null) { 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); } Map> map = new ConcurrentHashMap<>(); final String conditionSql = condition; Stream.of(HomeManagementConfig.Type.values()) .forEach(type -> { MapBuilder builder = MapUtils.builder() .field(HomeManagementFrontView::getType, type.name()); if (type == HomeManagementConfig.Type.ex || type == HomeManagementConfig.Type.vg) { builder.put("condition", conditionSql); } List homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build()); map.putIfAbsent(type.name(), homeManagementFrontViews); }); redisService.setNx(key, map, 50 * 60l); o = redisService.get(key); } Map> map = Jsons.parseObject(o, Map.class); if (map.containsKey(HomeManagementConfig.Type.ex.name()) || map.containsKey(HomeManagementConfig.Type.vg.name())) { try { //设置特定价格 List exList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.ex.name())), HomeManagementFrontView.class); if (CollUtil.isNotEmpty(exList)) { setHomeManageSpecific(exList, fUid); map.put(HomeManagementConfig.Type.ex.name(), exList); } //设置特定价格 List vgList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.vg.name())), HomeManagementFrontView.class); if (CollUtil.isNotEmpty(vgList)) { setHomeManageSpecific(vgList, fUid); map.put(HomeManagementConfig.Type.vg.name(), vgList); } } catch (Exception e) { } } return GatewayResponse.SUCCESS.newBuilder().toResult(map); } /** * 首页点击 记录 */ @PostMapping("/homePage/click") public Result homeManageClick(@RequestBody ClickRecordReq request) { Long userId = StpUserUtil.getUserIdAfterLogin(); clickRecordFrontService.click(userId, request); return GatewayResponse.SUCCESS.newBuilder().toResult(); } public List getFilterGoodsIds() { //过滤杭州Ai ChatGPT平台 //获取当前ip地址 SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class) .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1")); List filter_goodsIds = null; if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) { if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) { try { filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class); } catch (Exception e) { } } } return filter_goodsIds; } /** * @param goodsId * @param type 1、普通,2、注册平台 * @param yhsId 店铺id * @return */ public String getPayMsgTime(Long goodsId, DateTime now, Integer type, Long yhsId) { Date startTime = null; StringBuilder notifyMsg = new StringBuilder(); if (type == 1) { OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, goodsId) .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId) .eq(yhsId != null, OrderDon::getYhsId, yhsId) .orderByDesc(OrderDon::getId) .last("limit 1")); if (orderDon == null) { notifyMsg.append("点击购买立即上车"); return notifyMsg.toString(); } startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime(); } else if (type == 2) { RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class) .eq(RegisterOrderDon::getGoodsId, goodsId) .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId) .orderByDesc(RegisterOrderDon::getId) .last("limit 1")); if (orderDon == null) { return null; } startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime(); } long hour = DateUtil.between(startTime, now, DateUnit.HOUR); if (hour == 0) { long minute = DateUtil.between(startTime, now, DateUnit.MINUTE); if (minute == 0) { notifyMsg.append("1分钟"); } else { notifyMsg.append(minute); notifyMsg.append("分钟"); } } else { if (hour > 3) { int hours = RandomUtil.randomInt(3) + 1; notifyMsg.append(hours); } else { notifyMsg.append(hour); } notifyMsg.append("小时"); } notifyMsg.append("前有人购买"); return notifyMsg.toString(); } /** * 是否购买过特定平台 */ public Boolean isPaySpecificGoodsIds(String specificGoodsIdsStr, Long userId) { if (StrUtil.isNotBlank(specificGoodsIdsStr)) { try { Set specificGoodsIds = Jsons.parseSet(specificGoodsIdsStr, Long.class); Number number = beanSearcher.searchCount(OrderDon.class, MapUtils.builder().field(OrderDon::getGoodsId, specificGoodsIds).op(Operator.InList) .field(OrderDon::getUserId, userId) .field(OrderDon::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn) .build()); if (number.intValue() == 0) { return false; } return true; } catch (Exception e) { } } return false; } /** * 设置平台 特定价格 */ public void setSpecificPrice(GoodsFrontListView data, Long fUid) { if (fUid != null) { if (data.getIsSp()) { //查找所有特定规格最小价格 满足条件 String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + data.getId(); Object speciObj = redisService.get(specificGoodsKey); List specificSkus = null; if (speciObj != null) { specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class); } else { specificSkus = goodsDonSkuMapper.selectList( Wrappers.lambdaQuery(GoodsDonSku.class) .eq(GoodsDonSku::getGoodsId, data.getId()) .eq(GoodsDonSku::getStatus, true) .gt(GoodsDonSku::getSpecificPrice, 0)); redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); } specificSkus.forEach(specificSku -> { String specificGoodsIdsStr = specificSku.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { BigDecimal specificPrice = data.getSpecificPrice(); BigDecimal skuSpecificPrice = specificSku.getSpecificPrice(); Long specificSkuId = specificSku.getId(); if (specificPrice == null) { data.setSpecificPrice(skuSpecificPrice); data.setSpecificSkuId(specificSkuId); } else if (specificPrice.compareTo(skuSpecificPrice) > 0) { data.setSpecificPrice(skuSpecificPrice); data.setSpecificSkuId(specificSkuId); } } }); List skuList = data.getSkuList(); if (CollUtil.isNotEmpty(skuList)) { skuList.forEach(sku -> { //设置特定价格 if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) { sku.setSpecificGoodsIds(null); sku.setSpecificPrice(null); } }); } } } } /** * 获取特定规格 */ public List getSpecificSku(Long goodsId) { //查找所有特定规格最小价格 满足条件 String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + goodsId; Object speciObj = redisService.get(specificGoodsKey); List specificSkus = null; if (speciObj != null) { specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class); } else { specificSkus = goodsDonSkuMapper.selectList( Wrappers.lambdaQuery(GoodsDonSku.class) .eq(GoodsDonSku::getGoodsId, goodsId) .eq(GoodsDonSku::getStatus, true) .gt(GoodsDonSku::getSpecificPrice, 0)); redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); } specificSkus = Optional.ofNullable(specificSkus).orElse(new ArrayList<>()); return specificSkus; } /** * 设置首页配置 涉及平台特定价格 */ public void setHomeManageSpecific(List list, Long fUid) { DateTime now = DateTime.now(); list.forEach(data -> { if (data.getIsSp() != null && data.getIsSp() && fUid != null) { //查找所有特定规格最小价格 满足条件 List specificSkus = getSpecificSku(data.getGoodsId()); specificSkus.forEach(specificSku -> { String specificGoodsIdsStr = specificSku.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { BigDecimal specificPrice = data.getSpecificPrice(); BigDecimal skuSpecificPrice = specificSku.getSpecificPrice(); Long specificSkuId = specificSku.getId(); if (specificPrice == null) { data.setSpecificPrice(skuSpecificPrice); data.setSpecificSkuId(specificSkuId); } else if (specificPrice.compareTo(skuSpecificPrice) > 0) { data.setSpecificPrice(skuSpecificPrice); data.setSpecificSkuId(specificSkuId); } } }); } Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getGoodsId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getGoodsId(), now, 1, null)); }); } /** * 店铺平台列表 */ @GetMapping("/get/shop/goods/{yhsId}") public Result> getShopGoods(@PathVariable Long yhsId) { String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId; Object value = redisService.get(yhShopCacheKey); List list = null; if (value != null) { try { list = Jsons.parseList(value, YhShopGoodsFrontView.class); } catch (Exception e) { } } if (CollUtil.isEmpty(list)) { MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); builder.put("condition", String.format("and yhs_id = %s", yhsId)); list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build()); redisService.setNx(yhShopCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); } DateTime now = DateTime.now(); list.forEach(data->{ Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId)); }); return GatewayResponse.SUCCESS.newBuilder().toResult(list); } /** * 店铺平台详情 */ @GetMapping("/get/shop/goods/detail/{yhsId}/{goodsId}") public Result getGoodsDetailByYhsId(@PathVariable Long yhsId, @PathVariable Long goodsId) { MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); String condition = String.format("and yhs_id = %s and ys.goods_id = %s", yhsId, goodsId); builder.put("condition", condition); YhShopGoodsFrontView goodsDetailView = beanSearcher.searchFirst(YhShopGoodsFrontView.class, builder.build()); if (goodsDetailView == null) { throw BusinessRuntimeException.getInstance("该平台不存在或已下架,请刷新页面"); } goodsDetailView.setSkuList(beanSearcher.searchAll(YhShopGoodsSkuFrontView.class, MapUtils.builder().put("condition", condition).orderBy(YhShopGoodsSkuFrontView::getOrderBy).asc().build())); goodsDetailView.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, goodsDetailView.getId()).build())); OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder() .field(OrderCommentFrontView::getGoodsId, goodsDetailView.getId()) .orderBy(OrderCommentFrontView::getCommentTime).desc() .orderBy(OrderCommentFrontView::getId).desc() .build()); goodsDetailView.setComments(comments); return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDetailView); } /** * 根据分类获取对应平台 */ @GetMapping("/get/categoryGoods") public Result getGoodsByCategory(Long cgy, String customId) { if (StrUtil.isNotBlank(customId)) { Result> shopGoods = getShopCategoryGoods(yhShopFrontService.getYhsIdByCustomId(customId), cgy); return shopGoods; } String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category:goods"; if (cgy != null) { categoryGoodsKey += ":" + cgy; } Object o = redisService.get(categoryGoodsKey); if (o == null) { MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap()); if (cgy != null) { String condition = String.format(" and id in (select goods_id from goods_don_category_relate where category = %s)", cgy); mapBuilder.put("condition", condition); } List goodsFrontListViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build()); redisService.setNx(categoryGoodsKey, goodsFrontListViews, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 10 * 60); o = redisService.get(categoryGoodsKey); } List goodsFrontListViews = Jsons.parseList(o, GoodsFrontListView.class); DateTime now = DateTime.now(); goodsFrontListViews.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null)); }); return GatewayResponse.SUCCESS.newBuilder().toResult(goodsFrontListViews); } @GetMapping("/get/shop/categoryGoods/{yhsId}") public Result> getShopCategoryGoods(@PathVariable Long yhsId, Long category) { String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":category"; if (category != null) { categoryGoodsKey += ":" + category; } Object value = redisService.get(categoryGoodsKey); if (value == null) { MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); String condition = String.format("and yhs_id = %s", yhsId); if (category != null) { condition += String.format(" and ys.goods_id in (select goods_id from goods_don_category_relate where category = %s)", category); } builder.put("condition", condition); List list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build()); redisService.setNx(categoryGoodsKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 5 * 60); value = redisService.get(categoryGoodsKey); } List list = Jsons.parseList(value, YhShopGoodsFrontView.class); DateTime now = DateTime.now(); list.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId)); }); return GatewayResponse.SUCCESS.newBuilder().toResult(list); } }