| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- 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 com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.util.Jsons;
- import com.cyksj.dto.Result;
- import com.cyksj.enums.GatewayResponse;
- import com.cyksj.mapper.GoodsDonSkuMapper;
- import com.cyksj.mapper.OrderDonMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.views.*;
- import com.cyksj.redis.RedisService;
- 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;
- /**
- * @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 GoodsDonSkuMapper goodsDonSkuMapper;
- private final RedisService redisService;
- /**
- * 获取商品列表
- */
- @GetMapping("/get")
- public Result<SearchResult<GoodsDon>> get() {
- SearchResult<GoodsDon> 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<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
- SearchResult<GoodsDonPopularizeView> 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<List<GoodsFrontListView>> getGoodsList() {
- Object value = redisService.get(RedisService.key.PC_WEB_CACHE.getName());
- if (value != null) {
- try {
- List<GoodsFrontListView> list = Jsons.parseObject(value, List.class);
- if (CollUtil.isNotEmpty(list)) {
- return GatewayResponse.SUCCESS.newBuilder().toResult(list);
- }
- } catch (Exception e) {
- }
- }
- List<GoodsFrontListView> list = beanSearcher.searchAll(GoodsFrontListView.class, MapUtils.flatBuilder(request.getParameterMap())
- .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(GoodsDonFrontSkuView.class, MapUtils.builder().field(GoodsDonFrontSkuView::getGoodsId, data.getId()).field(GoodsDonFrontSkuView::getStatus, true).orderBy(GoodsDonFrontSkuView::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) {
- GoodsDonFrontSkuView donFrontSkuView = skuList.get(0);
- data.setPrice(donFrontSkuView.getPrice());
- data.setMonths(donFrontSkuView.getMonths());
- }
- });
- 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);
- 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());
- });
- redisService.setNx(RedisService.key.PC_WEB_CACHE.getName(), list, RedisService.key.PC_WEB_CACHE.getTimeout());
- return GatewayResponse.SUCCESS.newBuilder().toResult(list);
- }
- /**
- * h5 银河首页平台展示
- */
- @GetMapping("/get/homePage")
- public Result<List<GoodsHomePageView>> getGoodsHomePage() {
- Object value = redisService.get(RedisService.key.H5_WEB_CACHE.getName());
- if (value != null) {
- try {
- List<GoodsHomePageView> list = Jsons.parseObject(value, List.class);
- if (CollUtil.isNotEmpty(list)) {
- return GatewayResponse.SUCCESS.newBuilder().toResult(list);
- }
- } catch (Exception e) {
- }
- }
- List<GoodsHomePageView> dataList = beanSearcher.searchAll(GoodsHomePageView.class, MapUtils.flatBuilder(request.getParameterMap())
- .build());
- 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());
- GoodsDonFrontSkuView skuView = beanSearcher.searchFirst(GoodsDonFrontSkuView.class, MapUtils.builder().field(GoodsDonFrontSkuView::getGoodsId, data.getId()).field(GoodsDonFrontSkuView::getStatus, true).orderBy(GoodsDonFrontSkuView::getPrice).asc().build());
- if (skuView != null) {
- data.setPrice(skuView.getPrice());
- data.setMonths(skuView.getMonths());
- }
- });
- redisService.setNx(RedisService.key.H5_WEB_CACHE.getName(), dataList, RedisService.key.H5_WEB_CACHE.getTimeout());
- return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
- }
- /**
- * 获取商品详情
- */
- @GetMapping("/get/{id}")
- public Result<GoodsDonViews> getDetail(@PathVariable Long id) {
- GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, id).build());
- views.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, views.getId()).build()));
- MapBuilder builder = MapUtils.builder().field(GoodsDonSku::getGoodsId, views.getId()).field(GoodsDonSku::getStatus, true);
- GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getGoodsId, id).build());
- if (goodsDonSkuView != null && goodsDonSkuView.getType() == 1) {
- builder.orderBy(GoodsDonSku::getOrderBy).asc();
- }
- views.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, builder.build()));
- return GatewayResponse.SUCCESS.newBuilder().toResult(views);
- }
- /**
- * 获取商品多规格列表
- */
- @GetMapping("/get/sku")
- public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
- if (skuIds.isEmpty()) {
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- List<GoodsDonSkuView> 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<SearchResult<OrderCommentFrontView>> getComments() {
- SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
- .orderBy(OrderCommentFrontView::getCommentTime).desc()
- .orderBy(OrderCommentFrontView::getId).desc()
- .build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- }
|