CouponFrontController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.cyksj.web.controller.coupon;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.cyksj.common.annotation.NoSubmit;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.common.util.Jsons;
  9. import com.cyksj.dto.Result;
  10. import com.cyksj.enums.GatewayResponse;
  11. import com.cyksj.mapper.manage.coupon.CouponMapper;
  12. import com.cyksj.mapper.manage.coupon.CouponRecommendMapper;
  13. import com.cyksj.mapper.manage.coupon.CouponSkuMapper;
  14. import com.cyksj.mapper.manage.coupon.CouponUserMapper;
  15. import com.cyksj.model.dto.CouponPopularizeDto;
  16. import com.cyksj.model.entity.Coupon;
  17. import com.cyksj.model.entity.CouponSku;
  18. import com.cyksj.model.request.CouponCollectReq;
  19. import com.cyksj.model.views.*;
  20. import com.cyksj.redis.RedisService;
  21. import com.cyksj.service.coupon.CouponFontService;
  22. import com.cyksj.web.util.StpUserUtil;
  23. import com.ejlchina.searcher.BeanSearcher;
  24. import com.ejlchina.searcher.param.Operator;
  25. import com.ejlchina.searcher.util.MapUtils;
  26. import lombok.RequiredArgsConstructor;
  27. import org.springframework.web.bind.annotation.*;
  28. import java.math.BigDecimal;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. /*
  32. *项目名: netflix
  33. *文件名: CouponFrontController
  34. *创建者: JavaZou
  35. *创建时间:2022/11/18 10:56
  36. */
  37. @RestController
  38. @RequestMapping("/applet/coupon")
  39. @RequiredArgsConstructor
  40. public class CouponFrontController {
  41. private final CouponUserMapper couponUserMapper;
  42. private final BeanSearcher beanSearcher;
  43. private final CouponFontService couponFontService;
  44. private final RedisService redisService;
  45. private final CouponSkuMapper couponSkuMapper;
  46. private final CouponMapper couponMapper;
  47. private final CouponRecommendMapper couponRecommendMapper;
  48. /**
  49. * 优惠券领取中心列表
  50. */
  51. @GetMapping("/center/list")
  52. public Result<Page<CouponActiveView>> couponCenterList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  53. Long userId = StpUserUtil.getLoginIdAsLong();
  54. Page<CouponActiveView> page = couponUserMapper.couponCenterList(userId, new Page<>(start, limit));
  55. page.getRecords().forEach(data -> {
  56. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  57. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  58. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  59. try {
  60. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  61. } catch (Exception e) {
  62. }
  63. });
  64. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  65. }
  66. /**
  67. * 个人优惠券列表
  68. */
  69. @GetMapping("/personal/list")
  70. public Result<Page<CouponUserView>> couponPersonalList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  71. Long userId = StpUserUtil.getLoginIdAsLong();
  72. Page<CouponUserView> page = couponUserMapper.couponPersonalList(userId, new Page<>(start, limit));
  73. page.getRecords().forEach(data -> {
  74. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  75. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  76. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  77. try {
  78. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  79. } catch (Exception e) {
  80. }
  81. });
  82. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  83. }
  84. /**
  85. * 可用优惠券展示数量
  86. */
  87. @GetMapping("/available/num")
  88. public Result<Integer> availableCouponNum(Long skuId, Boolean isRenew) {
  89. Long userId = StpUserUtil.getLoginIdAsLong();
  90. Long goodsId = null;
  91. //实物还是虚物
  92. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
  93. if (goodsDonSkuView == null) {
  94. throw BusinessRuntimeException.getInstance("规格不存在");
  95. }
  96. BigDecimal skuMoney = goodsDonSkuView.getMoney();
  97. if (goodsDonSkuView.getType() == 2) {
  98. goodsId = goodsDonSkuView.getGoodsId();
  99. }
  100. List<CouponUserView> couponUserViews = couponUserMapper.countAvailableCouponList(goodsId, skuId, userId, isRenew);
  101. Integer total = couponUserViews.size();
  102. if (goodsDonSkuView.getType() == 2) {
  103. List<CouponUserView> collect = couponUserViews.stream().filter(coupon -> {
  104. if (coupon.getType() == Coupon.Type.reduce && skuMoney.compareTo(coupon.getReduceCondition()) < 0) {
  105. return false;
  106. }
  107. return true;
  108. }).collect(Collectors.toList());
  109. total = collect.size();
  110. }
  111. return GatewayResponse.SUCCESS.newBuilder().toResult(total);
  112. }
  113. @GetMapping("/available/list")
  114. public Result<Page<CouponUserView>> availableCouponList(Long skuId, Long couponId, Boolean isRenew, @RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  115. Long userId = StpUserUtil.getLoginIdAsLong();
  116. Page<CouponUserView> couponActiveViewList = couponFontService.getAvailableCouponList(skuId, couponId, userId, isRenew, start, limit);
  117. return GatewayResponse.SUCCESS.newBuilder().toResult(couponActiveViewList);
  118. }
  119. /**
  120. * 推荐优惠券列表
  121. */
  122. @GetMapping("/recommend/list")
  123. public Result<Page<CouponRecommendView>> getRecommendList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  124. Long userId = StpUserUtil.getLoginIdAsLong();
  125. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  126. Boolean isNewUser = true;
  127. if (couponNewUserView != null) {
  128. isNewUser = false;
  129. }
  130. Page<CouponRecommendView> page = couponRecommendMapper.getRecommendCouponViewList(userId, isNewUser, new Page<>(start, limit));
  131. page.getRecords().forEach(data -> {
  132. if (data.getCouponStatus() == CouponActiveView.CouponStatus.received) {
  133. data.setValidStartTime(data.getUserValidStartTime());
  134. data.setValidEndTime(data.getUserValidEndTime());
  135. }
  136. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  137. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  138. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  139. try {
  140. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  141. } catch (Exception e) {
  142. }
  143. });
  144. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  145. }
  146. /**
  147. * 一键领取推荐优惠券
  148. */
  149. @PostMapping("/collect/recommend/whole")
  150. @NoSubmit
  151. public Result<String> collectWholeRecommendCoupon() {
  152. Long userId = StpUserUtil.getLoginIdAsLong();
  153. couponFontService.collectWholeRecommendCoupon(userId);
  154. return GatewayResponse.SUCCESS.newBuilder().toResult("领取成功");
  155. }
  156. /**
  157. * 今日推荐优惠券弹窗已弹
  158. */
  159. @GetMapping("/get/recommend/appear")
  160. public Result<Boolean> recommendIsAppear() {
  161. Long userId = StpUserUtil.getLoginIdAsLong();
  162. String zero = DateUtil.beginOfDay(DateTime.now()).toString();
  163. Object exists = redisService.get(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)));
  164. if (exists == null) {
  165. redisService.set(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)), userId, RedisService.key.RECOMMEND_COUPON_DAY.getTimeout());
  166. }
  167. return GatewayResponse.SUCCESS.newBuilder().toResult(exists == null ? false : true);
  168. }
  169. /**
  170. * 领取优惠券
  171. */
  172. @PostMapping("/collect")
  173. public Result<String> collectCoupon(@RequestBody CouponCollectReq couponCollectReq) {
  174. Long userId = StpUserUtil.getLoginIdAsLong();
  175. couponFontService.collectCoupon(couponCollectReq, userId);
  176. return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
  177. }
  178. /**
  179. * 领取推荐优惠券
  180. */
  181. @PostMapping("/recommend/collect")
  182. public Result<String> collectRecommendCoupon(@RequestBody CouponCollectReq couponCollectReq) {
  183. Long userId = StpUserUtil.getLoginIdAsLong();
  184. couponFontService.collectRecommendCoupon(couponCollectReq, userId);
  185. return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
  186. }
  187. /**
  188. * 查询有可以领取的推荐优惠券
  189. */
  190. @GetMapping("/recommend/collect/num")
  191. public Result<Integer> collectRecommendCouponNum() {
  192. Long userId = StpUserUtil.getLoginIdAsLong();
  193. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  194. Boolean isNewUser = true;
  195. if (couponNewUserView != null) {
  196. isNewUser = false;
  197. }
  198. Integer num = couponFontService.collectRecommendCouponNum(userId, isNewUser);
  199. return GatewayResponse.SUCCESS.newBuilder().toResult(num);
  200. }
  201. /**
  202. * 优惠码详情
  203. */
  204. @GetMapping("/get/exCode/detail")
  205. public Result<CouponPopularizeDto> getExCodeDetail(Long skuId, String exCode, Boolean isRenew) {
  206. Long userId = StpUserUtil.getLoginIdAsLong();
  207. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
  208. if (goodsDonSkuView == null) {
  209. throw BusinessRuntimeException.getInstance("该规格不存在");
  210. }
  211. BigDecimal skuMoney = goodsDonSkuView.getMoney();
  212. if (goodsDonSkuView.getType() == 2) {
  213. throw BusinessRuntimeException.getInstance("实物不支持使用优惠码");
  214. }
  215. CouponPopularizeDto dto = couponMapper.checkExCode(exCode);
  216. if (dto == null) {
  217. throw BusinessRuntimeException.getInstance("该优惠码不存在");
  218. }
  219. if (dto.getScope() == Coupon.Scope.newUser) {
  220. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  221. if (couponNewUserView != null) {
  222. throw BusinessRuntimeException.getInstance("该优惠码只适用于新用户");
  223. }
  224. }
  225. if (isRenew != null && isRenew) {
  226. Coupon.UseScope useScope = dto.getUseScope();
  227. if (useScope == Coupon.UseScope.orders) {
  228. throw BusinessRuntimeException.getInstance("该优惠码续费时不可使用");
  229. }
  230. }
  231. if (isRenew == null && !isRenew) {
  232. Coupon.UseScope useScope = dto.getUseScope();
  233. if (useScope == Coupon.UseScope.renew) {
  234. throw BusinessRuntimeException.getInstance("该优惠码下单时不可使用");
  235. }
  236. }
  237. if (dto.getIsValidTime() && dto.getValidStartTime().after(DateTime.now())) {
  238. throw BusinessRuntimeException.getInstance("该优惠码待生效");
  239. }
  240. if (dto.getIsValidTime() && dto.getValidEndTime().before(DateTime.now())) {
  241. throw BusinessRuntimeException.getInstance("该优惠码已失效");
  242. }
  243. CouponSku couponSku = couponSkuMapper.selectOne(Wrappers.lambdaQuery(CouponSku.class)
  244. .eq(CouponSku::getSkuId, skuId)
  245. .eq(CouponSku::getCouponId, dto.getCouponId())
  246. .select(CouponSku::getId).last("limit 1"));
  247. if (couponSku == null) {
  248. throw BusinessRuntimeException.getInstance("该优惠码不适用于该规格");
  249. }
  250. if (goodsDonSkuView.getType() == 2) {
  251. if (dto.getType() == Coupon.Type.reduce && skuMoney.compareTo(dto.getReduceCondition()) < 0) {
  252. throw BusinessRuntimeException.getInstance(String.format("满%s可用", dto.getReduceCondition().divide(BigDecimal.valueOf(100))));
  253. }
  254. }
  255. return GatewayResponse.SUCCESS.newBuilder().toResult(dto);
  256. }
  257. /**
  258. * 查询优惠券
  259. */
  260. @GetMapping("/get/couponList")
  261. public Result<List<CouponView>> getCouponList(@RequestParam(value = "couponIds") List<Long> couponIds) {
  262. if (couponIds.isEmpty()) {
  263. return GatewayResponse.SUCCESS.newBuilder().toResult();
  264. }
  265. List<CouponView> couponViews = beanSearcher.searchAll(CouponView.class, MapUtils.builder().field(CouponView::getId, couponIds).op(Operator.InList).build());
  266. return GatewayResponse.SUCCESS.newBuilder().toResult(couponViews);
  267. }
  268. }