CouponFrontController.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. package com.cyksj.web.controller.coupon;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.cyksj.common.annotation.NoSubmit;
  9. import com.cyksj.common.constant.Constant;
  10. import com.cyksj.common.exception.BusinessRuntimeException;
  11. import com.cyksj.common.util.Jsons;
  12. import com.cyksj.dto.Result;
  13. import com.cyksj.enums.GatewayResponse;
  14. import com.cyksj.mapper.CouponUserPopupsRecordMapper;
  15. import com.cyksj.mapper.UserMapper;
  16. import com.cyksj.mapper.manage.coupon.CouponMapper;
  17. import com.cyksj.mapper.manage.coupon.CouponRecommendMapper;
  18. import com.cyksj.mapper.manage.coupon.CouponSkuMapper;
  19. import com.cyksj.mapper.manage.coupon.CouponUserMapper;
  20. import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
  21. import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
  22. import com.cyksj.model.dto.CouponPopularizeDto;
  23. import com.cyksj.model.entity.*;
  24. import com.cyksj.model.request.ChannelPopularizeCouponReq;
  25. import com.cyksj.model.request.CouponCollectReq;
  26. import com.cyksj.model.views.*;
  27. import com.cyksj.redis.RedisService;
  28. import com.cyksj.service.coupon.CouponFontService;
  29. import com.cyksj.service.sys.SysConfigService;
  30. import com.cyksj.web.util.StpUserUtil;
  31. import com.ejlchina.searcher.BeanSearcher;
  32. import com.ejlchina.searcher.param.Operator;
  33. import com.ejlchina.searcher.util.MapUtils;
  34. import lombok.RequiredArgsConstructor;
  35. import org.springframework.validation.annotation.Validated;
  36. import org.springframework.web.bind.annotation.*;
  37. import javax.servlet.http.HttpServletRequest;
  38. import java.math.BigDecimal;
  39. import java.util.List;
  40. import java.util.stream.Collectors;
  41. /*
  42. *项目名: netflix
  43. *文件名: CouponFrontController
  44. *创建者: JavaZou
  45. *创建时间:2022/11/18 10:56
  46. */
  47. @RestController
  48. @RequestMapping("/applet/coupon")
  49. @RequiredArgsConstructor
  50. public class CouponFrontController {
  51. private final CouponUserMapper couponUserMapper;
  52. private final BeanSearcher beanSearcher;
  53. private final CouponFontService couponFontService;
  54. private final RedisService redisService;
  55. private final CouponSkuMapper couponSkuMapper;
  56. private final CouponMapper couponMapper;
  57. private final UserDistributeMapper userDistributeMapper;
  58. private final CouponRecommendMapper couponRecommendMapper;
  59. private final UserDistributeSharedMapper userDistributeSharedMapper;
  60. private final UserMapper userMapper;
  61. private final HttpServletRequest request;
  62. private final SysConfigService sysConfigService;
  63. private final CouponUserPopupsRecordMapper couponUserPopupsRecordMapper;
  64. /**
  65. * 优惠券领取中心列表
  66. */
  67. @GetMapping("/center/list")
  68. public Result<Page<CouponActiveView>> couponCenterList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  69. Long userId = StpUserUtil.getLoginIdAsLong();
  70. Page<CouponActiveView> page = couponUserMapper.couponCenterList(userId, new Page<>(start, limit));
  71. page.getRecords().forEach(data -> {
  72. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  73. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  74. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  75. try {
  76. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  77. } catch (Exception e) {
  78. }
  79. });
  80. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  81. }
  82. /**
  83. * 个人优惠券列表
  84. */
  85. @GetMapping("/personal/list")
  86. public Result<Page<CouponUserView>> couponPersonalList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit, Long couponId) {
  87. Long userId = StpUserUtil.getLoginIdAsLong();
  88. Page<CouponUserView> page = couponUserMapper.couponPersonalList(userId, new Page<>(start, limit), couponId);
  89. page.getRecords().forEach(data -> {
  90. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  91. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  92. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  93. try {
  94. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  95. } catch (Exception e) {
  96. }
  97. });
  98. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  99. }
  100. /**
  101. * 可用优惠券展示数量
  102. */
  103. @GetMapping("/available/num")
  104. public Result<Integer> availableCouponNum(Long skuId, Boolean isRenew) {
  105. Long userId = StpUserUtil.getLoginIdAsLong();
  106. Long goodsId = null;
  107. //实物还是虚物
  108. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
  109. if (goodsDonSkuView == null) {
  110. throw BusinessRuntimeException.getInstance("规格不存在");
  111. }
  112. BigDecimal skuMoney = goodsDonSkuView.getMoney();
  113. if (goodsDonSkuView.getType() == 2) {
  114. goodsId = goodsDonSkuView.getGoodsId();
  115. }
  116. List<CouponUserView> couponUserViews = couponUserMapper.countAvailableCouponList(goodsId, skuId, userId, isRenew);
  117. Integer total = couponUserViews.size();
  118. if (goodsDonSkuView.getType() == 2) {
  119. List<CouponUserView> collect = couponUserViews.stream().filter(coupon -> {
  120. if (coupon.getType() == Coupon.Type.reduce && skuMoney.compareTo(coupon.getReduceCondition()) < 0) {
  121. return false;
  122. }
  123. return true;
  124. }).collect(Collectors.toList());
  125. total = collect.size();
  126. }
  127. return GatewayResponse.SUCCESS.newBuilder().toResult(total);
  128. }
  129. @GetMapping("/available/list")
  130. public Result<Page<CouponUserView>> availableCouponList(Long skuId, Long couponId, Boolean isRenew, @RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit) {
  131. Long userId = StpUserUtil.getLoginIdAsLong();
  132. Page<CouponUserView> couponActiveViewList = couponFontService.getAvailableCouponList(skuId, couponId, userId, isRenew, start, limit);
  133. return GatewayResponse.SUCCESS.newBuilder().toResult(couponActiveViewList);
  134. }
  135. /**
  136. * 推荐优惠券列表
  137. */
  138. @GetMapping("/recommend/list")
  139. public Result<Page<CouponRecommendView>> getRecommendList(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit, Long couponId) {
  140. Long userId = StpUserUtil.getLoginIdAsLong();
  141. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  142. Boolean isNewUser = true;
  143. if (couponNewUserView != null) {
  144. isNewUser = false;
  145. }
  146. Page<CouponRecommendView> page = couponRecommendMapper.getRecommendCouponViewList(userId, isNewUser, new Page<>(start, limit), couponId);
  147. page.getRecords().forEach(data -> {
  148. if (data.getCouponStatus() == CouponActiveView.CouponStatus.received) {
  149. data.setValidStartTime(data.getUserValidStartTime());
  150. data.setValidEndTime(data.getUserValidEndTime());
  151. }
  152. String str = couponSkuMapper.selectGoodsSkuStr(data.getCouponId());
  153. data.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  154. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(data.getCouponId());
  155. try {
  156. data.setGoodsSkuIds(Jsons.toJson(couponSkus));
  157. } catch (Exception e) {
  158. }
  159. });
  160. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  161. }
  162. /**
  163. * 一键领取推荐优惠券
  164. */
  165. @PostMapping("/collect/recommend/whole")
  166. @NoSubmit
  167. public Result<String> collectWholeRecommendCoupon() {
  168. Long userId = StpUserUtil.getLoginIdAsLong();
  169. couponFontService.collectWholeRecommendCoupon(userId);
  170. return GatewayResponse.SUCCESS.newBuilder().toResult("领取成功");
  171. }
  172. /**
  173. * 今日推荐优惠券弹窗已弹
  174. */
  175. @GetMapping("/get/recommend/appear")
  176. public Result<Boolean> recommendIsAppear() {
  177. Long userId = StpUserUtil.getLoginIdAsLong();
  178. String zero = DateUtil.beginOfDay(DateTime.now()).toString();
  179. Object exists = redisService.get(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)));
  180. if (exists == null) {
  181. redisService.set(RedisService.key.RECOMMEND_COUPON_DAY.getNameFormat(String.format("%s-%s", zero, userId)), userId, RedisService.key.RECOMMEND_COUPON_DAY.getTimeout());
  182. }
  183. return GatewayResponse.SUCCESS.newBuilder().toResult(exists == null ? false : true);
  184. }
  185. /**
  186. * 领取优惠券
  187. */
  188. @PostMapping("/collect")
  189. public Result<String> collectCoupon(@RequestBody CouponCollectReq couponCollectReq) {
  190. Long userId = StpUserUtil.getLoginIdAsLong();
  191. couponFontService.collectCoupon(couponCollectReq, userId);
  192. return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
  193. }
  194. /**
  195. * 领取推荐优惠券
  196. */
  197. @PostMapping("/recommend/collect")
  198. public Result<String> collectRecommendCoupon(@RequestBody CouponCollectReq couponCollectReq) {
  199. Long userId = StpUserUtil.getLoginIdAsLong();
  200. couponFontService.collectRecommendCoupon(couponCollectReq, userId);
  201. return GatewayResponse.SUCCESS.newBuilder().toResult("领取优惠券成功");
  202. }
  203. /**
  204. * 查询有可以领取的推荐优惠券
  205. */
  206. @GetMapping("/recommend/collect/num")
  207. public Result<Integer> collectRecommendCouponNum() {
  208. Long userId = StpUserUtil.getLoginIdAsLong();
  209. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  210. Boolean isNewUser = true;
  211. if (couponNewUserView != null) {
  212. isNewUser = false;
  213. }
  214. Integer num = couponFontService.collectRecommendCouponNum(userId, isNewUser);
  215. return GatewayResponse.SUCCESS.newBuilder().toResult(num);
  216. }
  217. /**
  218. * 优惠码详情
  219. */
  220. @GetMapping("/get/exCode/detail")
  221. public Result<CouponPopularizeDto> getExCodeDetail(Long skuId, String exCode, Boolean isRenew) {
  222. Long userId = StpUserUtil.getLoginIdAsLong();
  223. GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, skuId).op(Operator.Equal).build());
  224. if (goodsDonSkuView == null) {
  225. throw BusinessRuntimeException.getInstance("该规格不存在");
  226. }
  227. BigDecimal skuMoney = goodsDonSkuView.getMoney();
  228. if (goodsDonSkuView.getType() == 2) {
  229. throw BusinessRuntimeException.getInstance("实物不支持使用优惠码");
  230. }
  231. CouponPopularizeDto dto = couponMapper.checkExCode(exCode);
  232. if (dto == null) {
  233. throw BusinessRuntimeException.getInstance("该优惠码不存在");
  234. }
  235. if (dto.getIsGeneral() != null) {
  236. if (dto.getIsGeneral() && userId.equals(dto.getUserId())) {
  237. throw BusinessRuntimeException.getInstance("不可以使用自己的优惠码");
  238. }
  239. if (!dto.getIsGeneral()) {
  240. UserDistribute userDistribute = userDistributeMapper.selectById(dto.getDistributeId());
  241. if (userDistribute != null && userDistribute.getUserId().equals(userId)) {
  242. throw BusinessRuntimeException.getInstance("不可以使用自己的优惠码");
  243. }
  244. }
  245. //是否有上下级关系
  246. if (dto.getIsGeneral()) {
  247. UserDistributeShared userDistributeShared = userDistributeSharedMapper.selectOne(Wrappers.lambdaQuery(UserDistributeShared.class)
  248. .eq(UserDistributeShared::getUserId, userId).last("limit 1"));
  249. if (userDistributeShared == null) {
  250. //是否是 当天注册新用户 且下过单
  251. User user = userMapper.selectById(userId);
  252. if (user.getCreatedTime().before(DateUtil.beginOfDay(DateTime.now()))) {
  253. throw BusinessRuntimeException.getInstance("该优惠码只适用于新用户");
  254. }
  255. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  256. if (couponNewUserView != null) throw BusinessRuntimeException.getInstance("该优惠码只适用于新用户");
  257. }
  258. if (userDistributeShared != null && !userDistributeShared.getSharedId().equals(dto.getUserId())) {
  259. throw BusinessRuntimeException.getInstance("您已有推广,无法使用其他人推广码");
  260. }
  261. }
  262. }
  263. if (dto.getScope() == Coupon.Scope.newUser) {
  264. CouponNewUserView couponNewUserView = beanSearcher.searchFirst(CouponNewUserView.class, MapUtils.builder().field(CouponNewUserView::getUserId, userId).op(Operator.Equal).build());
  265. if (couponNewUserView != null) {
  266. throw BusinessRuntimeException.getInstance("该优惠码只适用于新用户");
  267. }
  268. }
  269. if (isRenew != null && isRenew) {
  270. Coupon.UseScope useScope = dto.getUseScope();
  271. if (useScope == Coupon.UseScope.orders) {
  272. throw BusinessRuntimeException.getInstance("该优惠码续费时不可使用");
  273. }
  274. }
  275. if (isRenew == null || !isRenew) {
  276. Coupon.UseScope useScope = dto.getUseScope();
  277. if (useScope == Coupon.UseScope.renew) {
  278. throw BusinessRuntimeException.getInstance("该优惠码下单时不可使用");
  279. }
  280. }
  281. if (dto.getIsValidTime() && dto.getValidStartTime().after(DateTime.now())) {
  282. throw BusinessRuntimeException.getInstance("该优惠码待生效");
  283. }
  284. if (dto.getIsValidTime() && dto.getValidEndTime().before(DateTime.now())) {
  285. throw BusinessRuntimeException.getInstance("该优惠码已失效");
  286. }
  287. CouponSku couponSku = couponSkuMapper.selectOne(Wrappers.lambdaQuery(CouponSku.class)
  288. .eq(CouponSku::getSkuId, skuId)
  289. .eq(CouponSku::getCouponId, dto.getCouponId())
  290. .select(CouponSku::getId).last("limit 1"));
  291. if (couponSku == null) {
  292. throw BusinessRuntimeException.getInstance("该优惠码不适用于该规格");
  293. }
  294. if (goodsDonSkuView.getType() == 2) {
  295. if (dto.getType() == Coupon.Type.reduce && skuMoney.compareTo(dto.getReduceCondition()) < 0) {
  296. throw BusinessRuntimeException.getInstance(String.format("满%s可用", dto.getReduceCondition().divide(BigDecimal.valueOf(100))));
  297. }
  298. }
  299. return GatewayResponse.SUCCESS.newBuilder().toResult(dto);
  300. }
  301. /**
  302. * 查询优惠券
  303. */
  304. @GetMapping("/get/couponList")
  305. public Result<List<CouponView>> getCouponList(@RequestParam(value = "couponIds") List<Long> couponIds) {
  306. if (couponIds.isEmpty()) {
  307. return GatewayResponse.SUCCESS.newBuilder().toResult();
  308. }
  309. List<CouponView> couponViews = beanSearcher.searchAll(CouponView.class, MapUtils.builder()
  310. .field(CouponView::getId, couponIds).op(Operator.InList)
  311. .orderBy(CouponView::getId).desc()
  312. .build());
  313. return GatewayResponse.SUCCESS.newBuilder().toResult(couponViews);
  314. }
  315. /**
  316. * 渠道推广用户领取优惠券
  317. */
  318. @PostMapping("/channel/popularize/receive")
  319. public Result<String> receiveChannelPopularizeCoupon(@RequestBody @Validated ChannelPopularizeCouponReq req) {
  320. long userId = StpUserUtil.getLoginIdAsLong();
  321. req.setUserId(userId);
  322. couponFontService.receiveChannelPopularizeCoupon(req);
  323. return GatewayResponse.SUCCESS.newBuilder().toResult("领取成功");
  324. }
  325. /**
  326. * 渠道推广一键领取优惠券
  327. */
  328. @PostMapping("/channel/popularize/collectAll/{popularizeId}")
  329. public Result<String> collectAllChannelCoupons(@PathVariable Long popularizeId) {
  330. long userId = StpUserUtil.getLoginIdAsLong();
  331. couponFontService.collectAllChannelCoupons(userId, popularizeId);
  332. return GatewayResponse.SUCCESS.newBuilder().toResult("领取成功");
  333. }
  334. /**
  335. * 新用户福利 优惠券详情展示
  336. */
  337. @GetMapping("/get/newUser/welfare")
  338. public Result<Object> getNewUserWelfare() {
  339. long userId = StpUserUtil.getLoginIdAsLong();
  340. SysConfig one = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  341. .eq(SysConfig::getSysKey, Constant.YL_NEW_WELFARE_COUPON_ID)
  342. .last("limit 1"));
  343. if (one == null || StrUtil.isEmpty(one.getSysValue())) {
  344. return GatewayResponse.SUCCESS.newBuilder().toResult();
  345. }
  346. String couponId = one.getSysValue();
  347. CouponUserView couponUserView = couponFontService.getNewUserWelfareCoupons(userId, Long.valueOf(couponId));
  348. if (couponUserView == null) {
  349. CouponDetailView couponDetailView = beanSearcher.searchFirst(CouponDetailView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CouponDetailView::getCouponId, couponId).build());
  350. return GatewayResponse.SUCCESS.newBuilder().toResult(couponDetailView);
  351. }
  352. return GatewayResponse.SUCCESS.newBuilder().toResult(couponUserView);
  353. }
  354. @GetMapping("/get/coupon/detail/{couponId}")
  355. public Result<CouponUserView> getCouponDetailById(@PathVariable Long couponId) {
  356. long userId = StpUserUtil.getLoginIdAsLong();
  357. CouponUserView couponUserView = couponFontService.getCouponDetailById(userId, couponId);
  358. return GatewayResponse.SUCCESS.newBuilder().toResult(couponUserView);
  359. }
  360. /**
  361. * 领取优惠券
  362. */
  363. @PostMapping("/receive/coupon/{couponId}")
  364. public Result<CouponUserView> receiveCoupon(@PathVariable Long couponId) {
  365. long userId = StpUserUtil.getLoginIdAsLong();
  366. CouponUserView couponUserView = couponFontService.receiveCoupon(userId, couponId, CouponUser.Channel.manual);
  367. return GatewayResponse.SUCCESS.newBuilder().toResult(couponUserView);
  368. }
  369. /**
  370. * 优惠券详情
  371. */
  372. @GetMapping("/get/detail/{couponId}")
  373. public Result<CouponDetailView> getCouponDetail(@PathVariable Long couponId) {
  374. CouponDetailView couponDetailView = beanSearcher.searchFirst(CouponDetailView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CouponDetailView::getCouponId, couponId).build());
  375. String str = couponSkuMapper.selectGoodsSkuStr(couponDetailView.getCouponId());
  376. couponDetailView.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  377. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(couponDetailView.getCouponId());
  378. try {
  379. couponDetailView.setGoodsSkuIds(Jsons.toJson(couponSkus));
  380. } catch (Exception e) {
  381. }
  382. return GatewayResponse.SUCCESS.newBuilder().toResult(couponDetailView);
  383. }
  384. /**
  385. * 引流新人优惠券详情
  386. */
  387. @GetMapping("/get/yl/welfare/coupon/detail")
  388. @Deprecated
  389. public Result<CouponDetailView> getYlWelfareCouponDetail() {
  390. SysConfig one = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  391. .eq(SysConfig::getSysKey, Constant.YL_NEW_WELFARE_COUPON_ID)
  392. .last("limit 1"));
  393. if (one == null || StrUtil.isEmpty(one.getSysValue())) {
  394. return GatewayResponse.SUCCESS.newBuilder().toResult();
  395. }
  396. String couponId = one.getSysValue();
  397. CouponDetailView couponDetailView = beanSearcher.searchFirst(CouponDetailView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CouponDetailView::getCouponId, couponId).build());
  398. return GatewayResponse.SUCCESS.newBuilder().toResult(couponDetailView);
  399. }
  400. /**
  401. * 领取新人优惠券
  402. */
  403. @PostMapping("/receive/newWelfare")
  404. public Result<CouponUserView> receiveNewWelfareCoupon() {
  405. long userId = StpUserUtil.getLoginIdAsLong();
  406. SysConfig one = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  407. .eq(SysConfig::getSysKey, Constant.YL_NEW_WELFARE_COUPON_ID)
  408. .last("limit 1"));
  409. if (one == null || StrUtil.isEmpty(one.getSysValue())) {
  410. throw BusinessRuntimeException.getInstance("暂无新人优惠券活动");
  411. }
  412. String couponId = one.getSysValue();
  413. CouponUserView couponUserView = couponFontService.receiveCoupon(userId, Long.valueOf(couponId), CouponUser.Channel.new_welfare);
  414. if (couponUserView == null) {
  415. couponUserView = beanSearcher.searchFirst(CouponUserView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CouponNewUserView::getUserId, userId).field(CouponUserView::getCouponId, couponId).build());
  416. }
  417. if (couponUserView != null) {
  418. String str = couponSkuMapper.selectGoodsSkuStr(couponUserView.getCouponId());
  419. couponUserView.setGoodsSkuStr(str.replaceAll(",", "/").replaceAll(";", ""));
  420. List<CouponSkuView> couponSkus = couponSkuMapper.getGoodsSkuByCouponId(couponUserView.getCouponId());
  421. try {
  422. couponUserView.setGoodsSkuIds(Jsons.toJson(couponSkus));
  423. } catch (Exception e) {
  424. }
  425. }
  426. return GatewayResponse.SUCCESS.newBuilder().toResult(couponUserView);
  427. }
  428. //==================================================优惠券弹窗======================================================================================
  429. /**
  430. * 用户存在弹窗列表
  431. * @return
  432. */
  433. @GetMapping("/get/popups")
  434. public Result<List<CouponUserPopupsView>> getCouponPopups() {
  435. long userId = StpUserUtil.getLoginIdAsLong();
  436. DateTime now = DateTime.now();
  437. List<CouponUserPopupsView> couponUserPopups = beanSearcher.searchAll(CouponUserPopupsView.class, MapUtils.flatBuilder(request.getParameterMap())
  438. .field(CouponUserPopupsView::getStartTime, now).op(Operator.LessEqual)
  439. .field(CouponUserPopupsView::getEndTime, now).op(Operator.GreaterThan)
  440. .build());
  441. couponUserPopups = couponUserPopups.stream().filter(cu -> {
  442. try {
  443. List<Long> couponIds = Jsons.parseList(cu.getCouponIds(), Long.class);
  444. Integer selectCount = couponUserMapper.selectCount(Wrappers.lambdaQuery(CouponUser.class)
  445. .eq(CouponUser::getUserId, userId)
  446. .in(CouponUser::getCouponId, couponIds)
  447. .le(CouponUser::getValidStartTime, now)
  448. .gt(CouponUser::getValidEndTime, now)
  449. .eq(CouponUser::getStatus, CouponUser.Status.unused));
  450. //是否符合条件
  451. if (selectCount > 0) {
  452. Integer frequency = cu.getFrequency();
  453. LambdaQueryWrapper<CouponUserPopupsRecord> wrapper = Wrappers.lambdaQuery(CouponUserPopupsRecord.class);
  454. if (frequency != 0) {
  455. wrapper.between(CouponUserPopupsRecord::getCreatedTime, DateUtil.beginOfDay(now), DateUtil.endOfDay(now));
  456. }
  457. Integer popupsRecordCount = couponUserPopupsRecordMapper.selectCount(wrapper.eq(CouponUserPopupsRecord::getUserId, userId).eq(CouponUserPopupsRecord::getPopupsId, cu.getPopupsId()));
  458. //未有弹窗记录
  459. if (popupsRecordCount == 0) {
  460. List<CouponUserView> couponUserViews = beanSearcher.searchAll(CouponUserView.class, MapUtils.builder().field(CouponUserView::getUserId, userId)
  461. .field(CouponUserView::getCouponId, couponIds).op(Operator.InList)
  462. .orderBy(CouponUserView::getDiscount).asc()
  463. .build());
  464. cu.setCouponUserViews(couponUserViews);
  465. return true;
  466. }
  467. }
  468. return false;
  469. } catch (Exception e) {
  470. }
  471. return false;
  472. }).collect(Collectors.toList());
  473. return GatewayResponse.SUCCESS.newBuilder().toResult(couponUserPopups);
  474. }
  475. /**
  476. * 优惠弹窗记录
  477. */
  478. @PostMapping("/record/popups/{popupsId}")
  479. public Result<String> recordPopups(@PathVariable Long popupsId) {
  480. long userId = StpUserUtil.getLoginIdAsLong();
  481. CouponUserPopupsRecord couponUserPopupsRecord = new CouponUserPopupsRecord();
  482. couponUserPopupsRecord.setPopupsId(popupsId);
  483. couponUserPopupsRecord.setUserId(userId);
  484. couponUserPopupsRecordMapper.insert(couponUserPopupsRecord);
  485. return GatewayResponse.SUCCESS.newBuilder().toResult();
  486. }
  487. }