CouponOperationServiceImpl.java 7.02 KB
package com.topdraw.business.process.service.impl;

import com.topdraw.business.module.coupon.history.domain.CouponHistory;
import com.topdraw.business.module.coupon.history.service.CouponHistoryService;
import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.member.domain.Member;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.process.domian.TempCoupon;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.MemberOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.util.RedissonUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
public class CouponOperationServiceImpl implements CouponOperationService {

    private static final Logger LOG = LoggerFactory.getLogger(CouponOperationServiceImpl.class);

    @Autowired
    CouponService couponService;
    @Autowired
    CouponHistoryService couponHistoryService;
    @Autowired
    MemberOperationService memberOperationService;
    @Autowired
    RightsOperationService rightsOperationService;
    @Autowired
    MemberService memberService;
    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    // 过期阀值(默认一个月)
    private static final Integer EXPIRE_FACTOR_DAY = 30;

    @Override
    public void grantCouponThroughTempCoupon(List<TempCoupon> tempCouponList) {
        // 优惠券领取、使用历史记录表
        for (TempCoupon tempCoupon : tempCouponList) {
            this.refresh(tempCoupon);
        }
    }

    /**
     * 手动发放优惠券
     * @param tempCouponList
     */
    @Override
    public void grantCouponByManual(List<TempCoupon> tempCouponList) {
        // 优惠券领取、使用历史记录表
        for (TempCoupon tempCoupon : tempCouponList) {
            this.refresh(tempCoupon);
        }
    }


    /**
     * 优惠券领取历史记录表
     *
     * @param tempCoupon 领取的优惠券
     */
    private void refresh(TempCoupon tempCoupon) {
        // 1.更新会员优惠券数量
        this.refreshMemberCoupon(tempCoupon);
        // 2.保存优惠券领取、使用历史记录表
        this.doInsertCouponHistory(tempCoupon);
    }


    /**
     * 更新会员优惠券信息
     * @param tempCoupon 账号id
     */
    private void refreshMemberCoupon(TempCoupon tempCoupon) {
//        Long userId = tempCoupon.getUserId();
        Long memberId = tempCoupon.getMemberId();
        Integer rightsAmount = tempCoupon.getRightsAmount();
        try {
            // 1.历史总优惠券数量
            Long historyCouponCount = this.getTotalHistoryCoupon(memberId);
            // 1.当前总优惠券数量
            Long totalCouponCount = this.getTotalCoupon(historyCouponCount,rightsAmount);
            // 2.获取已过期的优惠券数量
            Long expireCouponCount = this.getTotalExpireCoupon(memberId);
            // 3.即将过期的优惠券数量
            Long expireSoonCouponCount = this.getTotalExpireSoonCoupon(memberId,EXPIRE_FACTOR_DAY);
            // 4.当前优惠券数量 = 总优惠券-已过期的优惠券
            Long currentCoupon = this.getCurrentCoupon(totalCouponCount,expireCouponCount);
            // 5.更新用户信息(优惠券数量、即将过期的优惠券数量)
            this.doUpdateMemberInfo(memberId,currentCoupon,expireSoonCouponCount);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private Long getTotalCoupon(Long historyCouponCount, Integer rightsAmount) {
        return (Objects.nonNull(historyCouponCount) ? historyCouponCount: 0L) + (Objects.nonNull(rightsAmount) ? rightsAmount: 0L);
    }


    /**
     * 更新当前用户优惠券信息
     * @param memberId
     * @param currentCoupon
     * @param expireSoonCouponCount
     */
    private void doUpdateMemberInfo(Long memberId, Long currentCoupon, Long expireSoonCouponCount) {
        MemberDTO memberDTO = this.findMemberByMemberId(memberId);

        Member member = new Member();
        BeanUtils.copyProperties(memberDTO,member);

        member.setCouponAmount(currentCoupon);
        member.setDueCouponAmount(expireSoonCouponCount);
        member.setUpdateTime(LocalDateTime.now());
        this.memberOperationService.doUpdateMemberInfo(member);
    }

    private MemberDTO findMemberByMemberId(Long memberId) {
        MemberDTO memberDTO = this.memberService.findById(memberId);
        return memberDTO;
    }



    /**
     * 当前优惠券数量 = 总优惠券-已过期的优惠券
     * @param totalCouponCount 总数
     * @param expireCouponCount 已过期总数
     * @return
     */
    private Long getCurrentCoupon(Long totalCouponCount, Long expireCouponCount) {
        return (Objects.nonNull(totalCouponCount)?totalCouponCount:0L)-(Objects.nonNull(expireCouponCount)?expireCouponCount:0L);
    }


    /**
     * 即将过期的优惠券数量
     * @param expireFactor
     * @return
     */
    private Long getTotalExpireSoonCoupon(Long userId, Integer expireFactor) {
        LocalDateTime expireTime = LocalDateTime.now().plusDays(expireFactor);
        return this.couponHistoryService.countByUserIdAndExpireTimeBetween(userId,LocalDateTime.now(),expireTime);
    }


    /**
     * 获取已过期的优惠券数量
     * @param userId
     * @return
     */
    private Long getTotalExpireCoupon(Long userId) {
        return this.couponHistoryService.countByUserIdAndExpireTimeBefore(userId,LocalDateTime.now());
    }


    /**
     * 获取用户领取的总优惠券
     * @param userId
     * @return
     */
    private Long getTotalHistoryCoupon(Long userId) {
        return this.couponHistoryService.countByUserId(userId);
    }


    /**
     * 优惠券领取、使用历史记录表
     * @param tempCoupon 优惠券
     */
    private void doInsertCouponHistory(TempCoupon tempCoupon) {
        CouponHistory couponHistory = new CouponHistory();
        BeanUtils.copyProperties(tempCoupon,couponHistory);
        couponHistory.setId(null);
        couponHistory.setCouponId(tempCoupon.getId());
        couponHistory.setUserId(tempCoupon.getMemberId());
        couponHistory.setCouponCode(tempCoupon.getCode());
        couponHistory.setUserNickname(tempCoupon.getUserNickname());
        couponHistory.setOrderDetailId(tempCoupon.getOrderId());
        couponHistory.setReceiveTime(LocalDateTime.now());
        couponHistory.setUseStatus(Objects.nonNull(couponHistory.getUseStatus()) ? couponHistory.getUseStatus():0);
        this.couponHistoryService.create(couponHistory);
    }


}