|
|
@@ -1,23 +1,154 @@
|
|
|
package com.dk.oauth.service.integral;
|
|
|
|
|
|
+import com.dk.common.exception.BaseBusinessException;
|
|
|
+import com.dk.common.infrastructure.annotaiton.Pagination;
|
|
|
+import com.dk.common.infrastructure.constant.Constant;
|
|
|
+import com.dk.common.infrastructure.enums.ErrorCodeEnum;
|
|
|
+import com.dk.common.model.pojo.PageList;
|
|
|
+import com.dk.common.response.ResponseResultUtil;
|
|
|
+import com.dk.common.response.ResponseResultVO;
|
|
|
+import com.dk.oauth.infrastructure.convert.integral.CouponReceiveConvert;
|
|
|
+import com.dk.oauth.mapper.integral.CouponMapper;
|
|
|
+import com.dk.oauth.mapper.integral.CouponSendMapper;
|
|
|
+import com.dk.oauth.mapper.integral.CouponUseMapper;
|
|
|
+import com.dk.oauth.model.VO.integral.CouponReceiveVO;
|
|
|
+import com.dk.oauth.model.VO.integral.CouponSendVO;
|
|
|
import com.dk.oauth.model.pojo.integral.CouponReceive;
|
|
|
import com.dk.oauth.mapper.integral.CouponReceiveMapper;
|
|
|
import com.dk.common.service.BaseService;
|
|
|
import com.dk.common.mapper.BaseMapper;
|
|
|
+import com.dk.oauth.model.pojo.integral.CouponUse;
|
|
|
+import com.dk.oauth.model.query.integral.CouponReceiveQuery;
|
|
|
+import com.dk.oauth.model.query.integral.CouponSendQuery;
|
|
|
+import com.dk.oauth.model.response.integral.CouponReceiveResponse;
|
|
|
+import com.dk.oauth.model.response.integral.CouponResponse;
|
|
|
+import com.dk.oauth.model.response.integral.CouponSendResponse;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class CouponReceiveService extends BaseService<CouponReceive> {
|
|
|
|
|
|
- @Override
|
|
|
- public BaseMapper<CouponReceive> getRepository() {
|
|
|
- return couponReceiveMapper;
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public BaseMapper<CouponReceive> getRepository() {
|
|
|
+ return couponReceiveMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CouponReceiveMapper couponReceiveMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ //优惠券获取转换类
|
|
|
+ private CouponReceiveConvert couponReceiveConvert;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ //优惠券发出
|
|
|
+ private CouponSendMapper couponSendMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ //优惠券使用
|
|
|
+ private CouponUseMapper couponUseMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ //优惠券
|
|
|
+ private CouponMapper couponMapper;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 条件查询
|
|
|
+ * @date : 2024/7/26 11:07
|
|
|
+ * @author : 寇珊珊
|
|
|
+ */
|
|
|
+ @Pagination
|
|
|
+ public ResponseResultVO<PageList<CouponReceiveResponse>> selectByCond(CouponReceiveQuery couponReceiveQuery) {
|
|
|
+ return super.mergeListWithCount(couponReceiveQuery, couponReceiveMapper.selectByCond(couponReceiveQuery),
|
|
|
+ couponReceiveMapper.countByCond(couponReceiveQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 新建
|
|
|
+ * @date : 2024/7/26 11:08
|
|
|
+ * @author : 寇珊珊
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = {Exception.class})
|
|
|
+ public ResponseResultVO<?> insert(CouponReceiveVO couponReceiveVO) {
|
|
|
+ //region 查询7日内已进行过优惠券助力
|
|
|
+ List<CouponReceiveResponse> couponReceiveResponses = couponReceiveMapper.selectByCond(new CouponReceiveQuery().setUserWxid(couponReceiveVO.getUserWxid()).setAssistFlag(true));
|
|
|
+ if (couponReceiveResponses != null && couponReceiveResponses.size() > 0) {
|
|
|
+ //近期己助力过
|
|
|
+ throw new BaseBusinessException(ErrorCodeEnum.I_HAVE_RECENTLY_PROVIDED_ASSISTANCE.getCode(), ErrorCodeEnum.I_HAVE_RECENTLY_PROVIDED_ASSISTANCE.getMessage());
|
|
|
+ }
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ //region 7日内没有进行过助力
|
|
|
+ else {
|
|
|
+
|
|
|
+ //region 查询发出优惠券所需助力人数
|
|
|
+ CouponSendResponse couponSendResponse = couponSendMapper.selectById(couponReceiveVO.getSendId());
|
|
|
+ Integer userNum = couponSendResponse.getUserNum();
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ //region 查询优惠券获取对应的优惠券发出已有多少人助力
|
|
|
+ Long count = couponReceiveMapper.countByCond(new CouponReceiveQuery().setSendId(couponReceiveVO.getSendId()));
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ //region 当前优惠券获取个数 小于 优惠券发出所需助力人数
|
|
|
+ if (count < Long.valueOf(userNum)) {
|
|
|
+ //region 新建
|
|
|
+ couponReceiveVO.setAssistDate(LocalDateTime.now());
|
|
|
+ CouponReceive couponReceive = couponReceiveConvert.convertToPo(couponReceiveVO);
|
|
|
+ couponReceiveMapper.insert(couponReceive);
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ //region 是否新建优惠券使用
|
|
|
+ //新建之后再查一遍 查询优惠券获取对应的优惠券发出已有多少人助力
|
|
|
+ Long addAfterCount = couponReceiveMapper.countByCond(new CouponReceiveQuery().setSendId(couponReceiveVO.getSendId()));
|
|
|
+ //当前优惠券获取个数等于优惠券发出所需助力人数
|
|
|
+ if (addAfterCount == Long.valueOf(userNum)) {
|
|
|
+
|
|
|
+ //region 新建优惠券使用 状态未使用
|
|
|
+ CouponUse couponUse = new CouponUse();
|
|
|
+ //公司id
|
|
|
+ couponUse.setCpId(couponReceiveVO.getCpId());
|
|
|
+ //优惠券发出id
|
|
|
+ couponUse.setSendId(couponSendResponse.getSendId());
|
|
|
+ //优惠券id
|
|
|
+ couponUse.setCouponId(couponSendResponse.getCouponId());
|
|
|
+ //使用有效期
|
|
|
+ CouponResponse couponResponse = couponMapper.selectById(couponSendResponse.getCouponId());
|
|
|
+ couponUse.setUseValidDays(couponResponse.getUseValidDays());
|
|
|
+ //获取日期
|
|
|
+ couponUse.setReceiveDate(LocalDateTime.now());
|
|
|
+ //优惠券状态
|
|
|
+ couponUse.setCouponStatus(Constant.couponStatus.WEI_SHI_YONG.getName());
|
|
|
+ //获取用户
|
|
|
+ couponUse.setReceiveUser(couponReceiveVO.getReceiveUser());
|
|
|
+ couponUseMapper.insert(couponUse);
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ }
|
|
|
+ //endregion
|
|
|
+ }
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ //region 当前优惠券获取个数 大于等于 优惠券发出所需助力人数
|
|
|
+ else if (count >= Long.valueOf(userNum)) {
|
|
|
+ //所需助力人数已满
|
|
|
+ throw new BaseBusinessException(ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getCode(), ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getMessage());
|
|
|
+ }
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ }
|
|
|
+ //endregion
|
|
|
+
|
|
|
+ return ResponseResultUtil.success();
|
|
|
+ }
|
|
|
|
|
|
- @Autowired
|
|
|
- private CouponReceiveMapper couponReceiveMapper;
|
|
|
|
|
|
}
|