| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package com.cyksj.web.controller.coupon;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.cyksj.common.annotation.NoSubmit;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.common.util.Jsons;
- import com.cyksj.dto.Result;
- import com.cyksj.enums.GatewayResponse;
- import com.cyksj.mapper.manage.coupon.CouponMapper;
- import com.cyksj.mapper.manage.coupon.CouponRecommendMapper;
- import com.cyksj.mapper.manage.coupon.CouponSkuMapper;
- import com.cyksj.mapper.manage.coupon.CouponUserMapper;
- import com.cyksj.model.dto.CouponPopularizeDto;
- import com.cyksj.model.entity.Coupon;
- import com.cyksj.model.entity.CouponSku;
- import com.cyksj.model.request.CouponCollectReq;
- import com.cyksj.model.views.*;
- import com.cyksj.redis.RedisService;
- import com.cyksj.service.coupon.CouponFontService;
- import com.cyksj.web.util.StpUserUtil;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapUtils;
- import lombok.RequiredArgsConstructor;
- import org.springframework.web.bind.annotation.*;
- import java.math.BigDecimal;
- import java.util.List;
- import java.util.stream.Collectors;
- /*
- *项目名: netflix
- *文件名: CouponFrontController
- *创建者: JavaZou
- *创建时间:2022/11/18 10:56
- */
- @RestController
- @RequestMapping("/applet/coupon")
- @RequiredArgsConstructor
- public class CouponFrontController {
- private final CouponUserMapper couponUserMapper;
- private final BeanSearcher beanSearcher;
- private final CouponFontService couponFontService;
- private final RedisService redisService;
- private final CouponSkuMapper couponSkuMapper;
- private final CouponMapper couponMapper;
- private final CouponRecommendMapper couponRecommendMapper;
- /**
- * 优惠券领取中心列表
- */
- @GetMapping("/center/list")
- public Result<Page<CouponActiveView>> couponCenterList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- Page<CouponActiveView> page = couponUserMapper.couponCenterList(userId, new Page<>(start, limit));
- page.getRecords().forEach(data -> {
- String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
- data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
- List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
- try {
- data.setGoodsSkuIds(Jsons.toJson(couponSkus));
- } catch (Exception e) {
- }
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(page);
- }
- /**
- * 个人优惠券列表
- */
- @GetMapping("/personal/list")
- public Result<Page<CouponUserView>> couponPersonalList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- Page<CouponUserView> page = couponUserMapper.couponPersonalList(userId, new Page<>(start, limit));
- page.getRecords().forEach(data -> {
- String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
- data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
- List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
- try {
- data.setGoodsSkuIds(Jsons.toJson(couponSkus));
- } catch (Exception e) {
- }
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(page);
- }
- /**
- * 可用优惠券展示数量
- */
- @GetMapping("/available/num")
- public Result<Integer> availableCouponNum(Long skuId, Boolean isRenew) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- Long goodsId = null;
- //实物还是虚物
- GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
- if (goodsDonSkuView == null) {
- throw BusinessRuntimeException.getInstance("规格不存在");
- }
- BigDecimal skuMoney = goodsDonSkuView.getMoney();
- if (goodsDonSkuView.getType() == 2) {
- goodsId = goodsDonSkuView.getGoodsId();
- }
- List<CouponUserView> couponUserViews = couponUserMapper.countAvailableCouponList(goodsId, skuId, userId, isRenew);
- Integer total = couponUserViews.size();
- if (goodsDonSkuView.getType() == 2) {
- List<CouponUserView> collect = couponUserViews.stream().filter(coupon -> {
- if (coupon.getType() == Coupon.Type.reduce && skuMoney.compareTo(coupon.getReduceCondition()) < 0) {
- return false;
- }
- return true;
- }).collect(Collectors.toList());
- total = collect.size();
- }
- return GatewayResponse.SUCCESS.newBuilder().toResult(total);
- }
- @GetMapping("/available/list")
- public Result<Page<CouponUserView>> availableCouponList(Long skuId, Long couponId, Boolean isRenew, @RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- Page<CouponUserView> couponActiveViewList = couponFontService.getAvailableCouponList(skuId, couponId, userId, isRenew, start, limit);
- return GatewayResponse.SUCCESS.newBuilder().toResult(couponActiveViewList);
- }
- /**
- * 推荐优惠券列表
- */
- @GetMapping("/recommend/list")
- public Result<Page<CouponRecommendView>> getRecommendList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
- Boolean isNewUser = true;
- if (couponNewUserView != null) {
- isNewUser = false;
- }
- Page<CouponRecommendView> page = couponRecommendMapper.getRecommendCouponViewList(userId, isNewUser, new Page<>(start, limit));
- page.getRecords().forEach(data -> {
- if (data.getCouponStatus() == CouponActiveView.CouponStatus.received) {
- data.setValidStartTime(data.getUserValidStartTime());
- data.setValidEndTime(data.getUserValidEndTime());
- }
- String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
- data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
- List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
- try {
- data.setGoodsSkuIds(Jsons.toJson(couponSkus));
- } catch (Exception e) {
- }
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(page);
- }
- /**
- * 一键领取推荐优惠券
- */
- @PostMapping("/collect/recommend/whole")
- @NoSubmit
- public Result<String> collectWholeRecommendCoupon() {
- Long userId = StpUserUtil.getLoginIdAsLong();
- couponFontService.collectWholeRecommendCoupon(userId);
- return GatewayResponse.SUCCESS.newBuilder().toResult("领取成功");
- }
- /**
- * 今日推荐优惠券弹窗已弹
- */
- @GetMapping("/get/recommend/appear")
- public Result<Boolean> recommendIsAppear() {
- Long userId = StpUserUtil.getLoginIdAsLong();
- String zero = DateUtil.beginOfDay(DateTime.now()).toString();
- Object exists = redisService.get(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)));
- if (exists == null) {
- redisService.set(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)), userId, RedisService.key.RECOMMEND_COUPON_DAY.getTimeout());
- }
- return GatewayResponse.SUCCESS.newBuilder().toResult(exists == null ? false : true);
- }
- /**
- * 领取优惠券
- */
- @PostMapping("/collect")
- public Result<String> collectCoupon(@RequestBody CouponCollectReq couponCollectReq) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- couponFontService.collectCoupon(couponCollectReq, userId);
- return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
- }
- /**
- * 领取推荐优惠券
- */
- @PostMapping("/recommend/collect")
- public Result<String> collectRecommendCoupon(@RequestBody CouponCollectReq couponCollectReq) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- couponFontService.collectRecommendCoupon(couponCollectReq, userId);
- return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
- }
- /**
- * 查询有可以领取的推荐优惠券
- */
- @GetMapping("/recommend/collect/num")
- public Result<Integer> collectRecommendCouponNum() {
- Long userId = StpUserUtil.getLoginIdAsLong();
- CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
- Boolean isNewUser = true;
- if (couponNewUserView != null) {
- isNewUser = false;
- }
- Integer num = couponFontService.collectRecommendCouponNum(userId, isNewUser);
- return GatewayResponse.SUCCESS.newBuilder().toResult(num);
- }
- /**
- * 优惠码详情
- */
- @GetMapping("/get/exCode/detail")
- public Result<CouponPopularizeDto> getExCodeDetail(Long skuId, String exCode, Boolean isRenew) {
- Long userId = StpUserUtil.getLoginIdAsLong();
- GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
- if (goodsDonSkuView == null) {
- throw BusinessRuntimeException.getInstance("该规格不存在");
- }
- BigDecimal skuMoney = goodsDonSkuView.getMoney();
- if (goodsDonSkuView.getType() == 2) {
- throw BusinessRuntimeException.getInstance("实物不支持使用优惠码");
- }
- CouponPopularizeDto dto = couponMapper.checkExCode(exCode);
- if (dto == null) {
- throw BusinessRuntimeException.getInstance("该优惠码不存在");
- }
- if (dto.getScope() == Coupon.Scope.newUser) {
- CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
- if (couponNewUserView != null) {
- throw BusinessRuntimeException.getInstance("该优惠码只适用于新用户");
- }
- }
- if (isRenew != null && isRenew) {
- Coupon.UseScope useScope = dto.getUseScope();
- if (useScope == Coupon.UseScope.orders) {
- throw BusinessRuntimeException.getInstance("该优惠码续费时不可使用");
- }
- }
- if (isRenew == null && !isRenew) {
- Coupon.UseScope useScope = dto.getUseScope();
- if (useScope == Coupon.UseScope.renew) {
- throw BusinessRuntimeException.getInstance("该优惠码下单时不可使用");
- }
- }
- if (dto.getIsValidTime() && dto.getValidStartTime().after(DateTime.now())) {
- throw BusinessRuntimeException.getInstance("该优惠码待生效");
- }
- if (dto.getIsValidTime() && dto.getValidEndTime().before(DateTime.now())) {
- throw BusinessRuntimeException.getInstance("该优惠码已失效");
- }
- CouponSku couponSku = couponSkuMapper.selectOne(Wrappers.lambdaQuery(CouponSku.class)
- .eq(CouponSku::getSkuId, skuId)
- .eq(CouponSku::getCouponId, dto.getCouponId())
- .select(CouponSku::getId).last("limit 1"));
- if (couponSku == null) {
- throw BusinessRuntimeException.getInstance("该优惠码不适用于该规格");
- }
- if (goodsDonSkuView.getType() == 2) {
- if (dto.getType() == Coupon.Type.reduce && skuMoney.compareTo(dto.getReduceCondition()) < 0) {
- throw BusinessRuntimeException.getInstance(String.format("满%s可用", dto.getReduceCondition().divide(BigDecimal.valueOf(100))));
- }
- }
- return GatewayResponse.SUCCESS.newBuilder().toResult(dto);
- }
- /**
- * 查询优惠券
- */
- @GetMapping("/get/couponList")
- public Result<List<CouponView>> getCouponList(@RequestParam(value = "couponIds") List<Long> couponIds) {
- if (couponIds.isEmpty()) {
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- List<CouponView> couponViews = beanSearcher.searchAll(CouponView.class, MapUtils.builder().field(CouponView::getId, couponIds).op(Operator.InList).build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(couponViews);
- }
- }
|