陶杰
2024-09-11 c1ff1058eeb0067c224ee53e1dc61ace051375c8
src/main/java/com/mzl/flower/service/impl/coupon/CouponTemplateServiceImpl2.java
@@ -29,6 +29,7 @@
import com.mzl.flower.service.coupon.CouponTemplateCustomerService;
import com.mzl.flower.service.coupon.CouponTemplateService2;
import com.mzl.flower.service.menber.MemberService;
import com.mzl.flower.service.payment.RedisLockService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
@@ -40,6 +41,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -76,6 +78,9 @@
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisLockService lockService;
@@ -225,7 +230,7 @@
        // 判断当前的优惠券的用户类型是指定用户还是全部用户,如果是指定用户的情况下需要发布优惠券
        if (StringUtils.isNotBlank(couponTemplateDO.getGetUserType())
                && StringUtils.isNotBlank(couponTemplateDO.getCategory())
                && couponTemplateDO.getGetUserType().equals(CouponGetUserTypeEnum.POINT.getType())
                && couponTemplateDO.getGetUserType().equals(CouponGetUserTypeEnum.TARGET.getType())
                && couponTemplateDO.getCategory().equals(CouponCategoryEnum.USER.getStatus())
        ) {
@@ -236,7 +241,7 @@
                // 根据发放后有效期来设置时间
                if (couponTemplateDO.getUsageTimeNum() == null || couponTemplateDO.getUsageTimeNum() <= 0) {
                    throw new IllegalArgumentException("使用时间数量必须为正整数");
                    throw new ValidationException("使用时间数量必须为正整数");
                }
                LocalDateTime currentTime = LocalDateTime.now();
                couponTemplateDO.setUsageStartDate(currentTime);
@@ -278,7 +283,7 @@
                    couponRecordDO.setCouponCode(couponTemplateDO.getCouponCode());
                    couponRecordDO.setCouponName(couponTemplateDO.getCouponName());
                    //  优惠券种类
                    couponRecordDO.setCategory(couponTemplateDO.getGetUserType());
                    couponRecordDO.setCategory(couponTemplateDO.getCategory());
                    couponRecordDO.setMinOrderAmount(couponTemplateDO.getMinOrderAmount());
                    couponRecordDO.setCouponDiscountValue(couponTemplateDO.getCouponDiscountValue());
                    couponRecordDO.setGetType(couponTemplateDO.getGetType());
@@ -290,8 +295,14 @@
                    // 创建相关信息
                    couponRecordDO.create(SecurityUtils.getUserId());
                    return couponRecordDO;
                }).collect(Collectors.toList());
                    // 查看当前优惠券是不是已经存在,存在的话则不能添加,防止同一人员重复下发同一张指定的优惠券
                    if(!couponRecordService.checkUserCouponExists(pointCustomRe.getCouponId(),pointCustomRe.getCustomId())){
                        return couponRecordDO;
                    }else{
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                // 批量保存优惠券信息
                couponRecordService.saveBatch(couponUsageDOList);
@@ -386,46 +397,58 @@
    }
    @Override
    public synchronized void exchangeCoupon(ExchangeCouponDTO dto) {
        dto.setNum(1);
    public  void exchangeCoupon(ExchangeCouponDTO dto) {
        final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId());
        if(couponTemplateDO==null){
            throw new ValidationException("优惠券不存在");
        String key="EXCHANGE_COUPON:"+dto.getCouponId()+":"+SecurityUtils.getUserId();
        boolean lock = lockService.getObjectLock(key, "");
        if(!lock){
            throw new ValidationException("系统操作频繁,请稍后重试");
        }
        try {
            dto.setNum(1);
            final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId());
            if(couponTemplateDO==null){
                throw new ValidationException("优惠券不存在");
            }
            CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
                    .eq(CustomerPoint::getUserId, SecurityUtils.getUserId()));
            if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()- cp.getExpiredPoint()) < couponTemplateDO.getPoint() * dto.getNum()){
                throw new ValidationException("积分不足");
            }
            CreateCouponRecordDTO recordDTO =new CreateCouponRecordDTO();
            recordDTO.setCouponId(dto.getCouponId());
            recordDTO.setUserId(SecurityUtils.getUserId());
            Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>()
                    .eq(Customer::getUserId, SecurityUtils.getUserId()));
            if(customer == null){
                throw new ValidationException("商户不存在");
            }
            recordDTO.setCustomerId(customer.getId());
            couponRecordService.createCouponRecord(recordDTO);
            //更新积分汇总
            cp.setUsedPoint(cp.getUsedPoint()+couponTemplateDO.getPoint());
            customerPointMapper.updateById(cp);
            //记录积分明细
            CustomerPointDetail detail = new CustomerPointDetail();
            detail.setUserId(customer.getUserId());
            detail.setCustomerId(customer.getId());
            detail.setPoint(couponTemplateDO.getPoint());
            detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
            detail.setType(Constants.POINT_TYPE.exchange.name());
            detail.setRecordDate(LocalDate.now());
            detail.setRemarks(couponTemplateDO.getCouponName());
            detail.create(SecurityUtils.getUserId());
            customerPointDetailMapper.insert(detail);
        }catch (Exception e){
            throw new ValidationException("兑换失败");
        }finally {
            lockService.releaseObjectLock(key,"");
        }
        CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
                .eq(CustomerPoint::getUserId, SecurityUtils.getUserId()));
        if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < couponTemplateDO.getPoint() * dto.getNum()){
            throw new ValidationException("积分不足");
        }
        CreateCouponRecordDTO recordDTO =new CreateCouponRecordDTO();
        recordDTO.setCouponId(dto.getCouponId());
        recordDTO.setUserId(SecurityUtils.getUserId());
        Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>()
                .eq(Customer::getUserId, SecurityUtils.getUserId()));
        if(customer == null){
            throw new ValidationException("商户不存在");
        }
        recordDTO.setCustomerId(customer.getId());
        couponRecordService.createCouponRecord(recordDTO);
        //更新积分汇总
        cp.setUsedPoint(cp.getUsedPoint()+couponTemplateDO.getPoint());
        customerPointMapper.updateById(cp);
        //记录积分明细
        CustomerPointDetail detail = new CustomerPointDetail();
        detail.setUserId(customer.getUserId());
        detail.setCustomerId(customer.getId());
        detail.setPoint(couponTemplateDO.getPoint());
        detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
        detail.setType(Constants.POINT_TYPE.exchange.name());
        detail.setRecordDate(LocalDate.now());
        detail.setRemarks(couponTemplateDO.getCouponName());
        detail.create(SecurityUtils.getUserId());
        customerPointDetailMapper.insert(detail);
    }
    @Override