| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package com.cyksj.web.controller.goods;
- 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.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.GoodsDonPopularizeView;
- import com.cyksj.model.views.GoodsDonSkuView;
- import com.cyksj.model.views.GoodsDonViews;
- 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.Date;
- import java.util.List;
- import java.util.Optional;
- import java.util.stream.Collectors;
- /**
- * @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;
- /**
- * 获取商品列表
- */
- @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()
- );
- Integer virtual = 2000;
- 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));
- goods.setPrice(goodsDonSku.getPrice());
- if (goods.getType() != 3) {
- goods.setSoldNum(virtual + 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)
- .build());
- DateTime now = DateTime.now();
- Integer randomNum = 2000;
- search.getDataList().forEach(data -> {
- GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
- .eq(GoodsDonSku::getGoodsId, data.getId())
- .eq(GoodsDonSku::getStatus, true)
- .orderByAsc(GoodsDonSku::getPrice)
- .last("limit 1"));
- Optional.ofNullable(goodsDonSku).ifPresent(sku -> {
- data.setPrice(sku.getPrice());
- data.setMonths(sku.getMonths());
- });
- 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).build()));
- if (data.getId() != 17 && data.getId() != 23) {
- data.setSold(data.getSold() + randomNum);
- }
- SearchResult<OrderComment> orderComments = beanSearcher.search(OrderComment.class,
- MapUtils.builder().field(OrderComment::getGoodsId, data.getId())
- .orderBy(OrderComment::getId).desc()
- .build());
- if (orderComments != null) {
- List<OrderComment> comments = orderComments.getDataList();
- data.setOrderComments(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("2分钟");
- } 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());
- });
- List<GoodsDonPopularizeView> collect = search.getDataList().stream().filter(data -> data.getPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
- search = new SearchResult<>(collect);
- search.setTotalCount(collect.size());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 获取商品详情
- */
- @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);
- }
- }
|