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.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.views.*; import com.cyksj.redis.RedisService; 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.util.Date; import java.util.List; import java.util.Optional; import java.util.Set; /** * @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; /** * 获取商品列表 */ @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).desc() .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") 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).desc() .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(Boolean isLogin) { List filter_goodsIds = getFilterGoodsIds(); String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "PC"; if (CollUtil.isNotEmpty(filter_goodsIds)) { pcCacheKey += ":hz-ip"; } Long userId = null; if (isLogin != null && isLogin) { userId = StpUserUtil.getLoginIdAsLong(); } final Long fUid = userId; Object value = redisService.get(pcCacheKey); if (value != null) { try { List list = Jsons.parseObject(value, List.class); if (CollUtil.isNotEmpty(list)) { if (fUid != null) { list.forEach(data -> { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } }); } return GatewayResponse.SUCCESS.newBuilder().toResult(list); } } catch (Exception e) { } } MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); if (CollUtil.isNotEmpty(filter_goodsIds)) { builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn); } List list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build()); DateTime now = DateTime.now(); list.forEach(data -> { data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build())); data.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, data.getId()).field(GoodsDonSkuFrontView::getStatus, true).orderBy(GoodsDonSkuFrontView::getPrice).asc().build())); data.getSkuList().forEach(sku -> { if (sku.getIsDp()) { List dpSkuViews = beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder().field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId()).build()); sku.setDpSkuViews(dpSkuViews); } }); Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getVirtualSold()); OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder() .field(OrderCommentFrontView::getGoodsId, data.getId()) .orderBy(OrderCommentFrontView::getCommentTime).desc() .orderBy(OrderCommentFrontView::getId).desc() .build()); data.setComments(comments); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1)); if (fUid != null) { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } } }); redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); return GatewayResponse.SUCCESS.newBuilder().toResult(list); } /** * h5 银河首页平台展示 */ @GetMapping("/get/homePage") public Result> getGoodsHomePage(Boolean isLogin) { List filter_goodsIds = getFilterGoodsIds(); String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "H5"; if (CollUtil.isNotEmpty(filter_goodsIds)) { h5CacheKey += ":hz-ip"; } Long userId = null; if (isLogin != null && isLogin) { userId = StpUserUtil.getLoginIdAsLong(); } final Long fUid = userId; Object value = redisService.get(h5CacheKey); if (value != null) { try { List list = Jsons.parseObject(value, List.class); if (CollUtil.isNotEmpty(list)) { if (fUid != null) { list.forEach(data -> { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } }); } return GatewayResponse.SUCCESS.newBuilder().toResult(list); } } catch (Exception e) { } } MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()); if (CollUtil.isNotEmpty(filter_goodsIds)) { builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn); } List dataList = beanSearcher.searchAll(GoodsHomePageView.class, builder.build()); DateTime now = DateTime.now(); dataList.forEach(data -> { Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class) .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getVirtualSold()); data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1)); if (fUid != null) { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } } }); redisService.setNx(h5CacheKey, dataList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout()); return GatewayResponse.SUCCESS.newBuilder().toResult(dataList); } /** * 注册首页平台展示 */ @GetMapping("/get/register") public Result> getRegisterGoodsDon() { String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getName(); 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)); }); 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) { GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::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())); views.getSkuList().forEach(sku -> { if (sku.getIsDp()) { sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder() .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId()) .build())); } }); return GatewayResponse.SUCCESS.newBuilder().toResult(views); } /** * 获取商品推荐平台 */ @GetMapping("/get/recommend/{id}") public Result> getRecommendGoodsByGid(@PathVariable Long id, Boolean isLogin) throws Exception { String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "recommend:" + id; Object value = redisService.get(key); Long userId = null; if (isLogin) { userId = StpUserUtil.getLoginIdAsLong(); } final Long fUid = userId; if (value != null) { List goodsRecommendViews = Jsons.parseList(value.toString(), GoodsRecommendViews.class); if (fUid != null) { goodsRecommendViews.forEach(data -> { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } }); } return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews); } if (value == 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); List goodsRecommendViews = beanSearcher.searchAll(GoodsRecommendViews.class, MapUtils.builder().field(GoodsRecommendViews::getGoodsId, recommend_gidList).op(Operator.InList).build()); goodsRecommendViews.forEach(data -> { Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class) .eq(RegisterOrderDon::getGoodsId, data.getGoodsId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus)); data.setSold(sold + data.getVirtualSold()); if (fUid != null) { String specificGoodsIdsStr = data.getSpecificGoodsIds(); if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) { data.setSpecificGoodsIds(null); data.setSpecificPrice(null); } } }); redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l); return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews); } } return GatewayResponse.SUCCESS.newBuilder().toResult(); } /** * 获取商品多规格列表 */ @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); } 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、注册平台 * @return */ public String getPayMsgTime(Long goodsId, DateTime now, Integer type) { 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) .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; } }