CouponReceiveService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.dk.oauth.service.integral;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.annotaiton.Pagination;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.response.ResponseResultUtil;
  8. import com.dk.common.response.ResponseResultVO;
  9. import com.dk.oauth.entity.UserLogin;
  10. import com.dk.oauth.infrastructure.convert.integral.CouponReceiveConvert;
  11. import com.dk.oauth.mapper.UserMapper;
  12. import com.dk.oauth.mapper.integral.CouponMapper;
  13. import com.dk.oauth.mapper.integral.CouponSendMapper;
  14. import com.dk.oauth.mapper.integral.CouponUseMapper;
  15. import com.dk.oauth.model.VO.integral.CouponReceiveVO;
  16. import com.dk.oauth.model.VO.integral.CouponSendVO;
  17. import com.dk.oauth.model.pojo.integral.CouponReceive;
  18. import com.dk.oauth.mapper.integral.CouponReceiveMapper;
  19. import com.dk.common.service.BaseService;
  20. import com.dk.common.mapper.BaseMapper;
  21. import com.dk.oauth.model.pojo.integral.CouponUse;
  22. import com.dk.oauth.model.query.integral.CouponReceiveQuery;
  23. import com.dk.oauth.model.query.integral.CouponSendQuery;
  24. import com.dk.oauth.model.response.integral.CouponReceiveResponse;
  25. import com.dk.oauth.model.response.integral.CouponResponse;
  26. import com.dk.oauth.model.response.integral.CouponSendResponse;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import java.time.LocalDate;
  31. import java.time.LocalDateTime;
  32. import java.util.List;
  33. @Service
  34. @Transactional
  35. public class CouponReceiveService extends BaseService<CouponReceive> {
  36. @Override
  37. public BaseMapper<CouponReceive> getRepository() {
  38. return couponReceiveMapper;
  39. }
  40. @Autowired
  41. private CouponReceiveMapper couponReceiveMapper;
  42. @Autowired
  43. //优惠券获取转换类
  44. private CouponReceiveConvert couponReceiveConvert;
  45. @Autowired
  46. //优惠券发出
  47. private CouponSendMapper couponSendMapper;
  48. @Autowired
  49. //优惠券使用
  50. private CouponUseMapper couponUseMapper;
  51. @Autowired
  52. //优惠券
  53. private CouponMapper couponMapper;
  54. @Autowired
  55. private UserMapper userMapper;
  56. /**
  57. * @desc : 条件查询
  58. * @date : 2024/7/26 11:07
  59. * @author : 寇珊珊
  60. */
  61. @Pagination
  62. public ResponseResultVO<PageList<CouponReceiveResponse>> selectByCond(CouponReceiveQuery couponReceiveQuery) {
  63. return super.mergeListWithCount(couponReceiveQuery, couponReceiveMapper.selectByCond(couponReceiveQuery),
  64. couponReceiveMapper.countByCond(couponReceiveQuery));
  65. }
  66. /**
  67. * @desc : 新建
  68. * @date : 2024/7/26 11:08
  69. * @author : 寇珊珊
  70. */
  71. @Transactional(rollbackFor = {Exception.class})
  72. public ResponseResultVO<?> insert(CouponReceiveVO couponReceiveVO) {
  73. // 限制助力链接发出本人不可给自己助力 2024/08/01 刘尧
  74. UserLogin userLogin = userMapper.selectById(couponReceiveVO.getUserIdForm());
  75. if (userLogin.getUserWxid().equals(couponReceiveVO.getUserWxid())){
  76. //助力链接发出者不能为自己助力
  77. throw new BaseBusinessException(ErrorCodeEnum.USER_POWER_OWN.getCode(), ErrorCodeEnum.USER_POWER_OWN.getMessage());
  78. }
  79. //region 查询7日内已进行过优惠券助力
  80. List<CouponReceiveResponse> couponReceiveResponses = couponReceiveMapper.selectByCond(new CouponReceiveQuery().setUserWxid(couponReceiveVO.getUserWxid()).setAssistFlag(true));
  81. if (couponReceiveResponses != null && couponReceiveResponses.size() > 0) {
  82. //近期己助力过
  83. throw new BaseBusinessException(ErrorCodeEnum.I_HAVE_RECENTLY_PROVIDED_ASSISTANCE.getCode(), ErrorCodeEnum.I_HAVE_RECENTLY_PROVIDED_ASSISTANCE.getMessage());
  84. }
  85. //endregion
  86. //region 7日内没有进行过助力
  87. else {
  88. //region 查询发出优惠券所需助力人数
  89. CouponSendResponse couponSendResponse = couponSendMapper.selectById(couponReceiveVO.getSendId());
  90. Integer userNum = couponSendResponse.getUserNum();
  91. //endregion
  92. //region 查询优惠券获取对应的优惠券发出已有多少人助力
  93. Long count = couponReceiveMapper.countByCond(new CouponReceiveQuery().setSendId(couponReceiveVO.getSendId()));
  94. //endregion
  95. //region 当前优惠券获取个数 小于 优惠券发出所需助力人数
  96. if (count < Long.valueOf(userNum)) {
  97. //region 新建
  98. couponReceiveVO.setAssistDate(LocalDateTime.now());
  99. CouponReceive couponReceive = couponReceiveConvert.convertToPo(couponReceiveVO);
  100. couponReceiveMapper.insert(couponReceive);
  101. //endregion
  102. //region 是否新建优惠券使用
  103. //新建之后再查一遍 查询优惠券获取对应的优惠券发出已有多少人助力
  104. Long addAfterCount = couponReceiveMapper.countByCond(new CouponReceiveQuery().setSendId(couponReceiveVO.getSendId()));
  105. //当前优惠券获取个数等于优惠券发出所需助力人数
  106. if (addAfterCount == Long.valueOf(userNum)) {
  107. //region 新建优惠券使用 状态未使用
  108. CouponUse couponUse = new CouponUse();
  109. //公司id
  110. couponUse.setCpId(couponReceiveVO.getCpId());
  111. //优惠券发出id
  112. couponUse.setSendId(couponSendResponse.getSendId());
  113. //优惠券id
  114. couponUse.setCouponId(couponSendResponse.getCouponId());
  115. //使用有效期
  116. CouponResponse couponResponse = couponMapper.selectById(couponSendResponse.getCouponId());
  117. couponUse.setUseValidDays(couponResponse.getUseValidDays());
  118. //获取日期
  119. couponUse.setReceiveDate(LocalDate.now());
  120. //优惠券状态
  121. couponUse.setCouponStatus(Constant.couponStatus.WEI_SHI_YONG.getName());
  122. //获取用户
  123. couponUse.setReceiveUser(couponReceiveVO.getReceiveUser());
  124. couponUseMapper.insert(couponUse);
  125. //endregion
  126. }
  127. //endregion
  128. }
  129. //endregion
  130. //region 当前优惠券获取个数 大于等于 优惠券发出所需助力人数
  131. else if (count >= Long.valueOf(userNum)) {
  132. //所需助力人数已满
  133. throw new BaseBusinessException(ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getCode(), ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getMessage());
  134. }
  135. //endregion
  136. }
  137. //endregion
  138. return ResponseResultUtil.success();
  139. }
  140. }