RightsOperationServiceImpl.java 11.1 KB
package com.topdraw.business.process.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.coupon.service.dto.CouponDTO;
import com.topdraw.business.module.rights.history.domain.RightsHistory;
import com.topdraw.business.module.rights.history.service.RightsHistoryService;
import com.topdraw.business.module.rights.service.RightsService;
import com.topdraw.business.module.rights.service.dto.RightsDTO;
import com.topdraw.business.process.domian.*;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.ExpOperationService;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.util.TimestampUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 权益处理
 *
 * @author XiangHan
 * @date 2021.10.23
 */
@Service
@Slf4j
public class RightsOperationServiceImpl implements RightsOperationService {

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

    @Autowired
    RightsHistoryService rightsHistoryService;
    @Autowired
    CouponOperationService couponOperationService;
    @Autowired
    RightsService rightsService;
    @Autowired
    ExpOperationService expOperationService;
    @Autowired
    PointsOperationService pointsOperationService;
    @Autowired
    CouponService couponService;

//    @Autowired
//    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    private ExecutorService threadPoolTaskExecutor = Executors.newFixedThreadPool(10);

    /**
     * 系统手动发放
     * 实现步骤:
     *  1.当前权益只有实体券
     * @param rightsList
     */
    @Override
    public void grantRightsByManual(List<RightsHistory> rightsList) {
        // 1.权益区分
        Map<RightType,Object> tempRightsMap = this.distinguishRight(rightsList);
        // 2.权益下发
        this.refresh(tempRightsMap);
        // 3.保存权益历史
        this.doInsertTrRightHistory(rightsList);
    }

    /**
     * 权益发放
     * @param tempRightsMap 权益类型
     */
    @Override
    public void grantRights(Map<RightType, Object> tempRightsMap) {

//        this.threadPoolTaskExecutor.execute(()-> {
            // 2.创建权益历史对象
            List<RightsHistory> rightsList = this.getRightHistory(tempRightsMap);
            // 3.保存权益历史
            this.doInsertTrRightHistory(rightsList);
//        });

        // 1.权益下发
        this.refresh(tempRightsMap);
    }

    /**
     *
     * @param tempRightsMap
     * @return
     */
    private List<RightsHistory> getRightHistory(Map<RightType, Object> tempRightsMap) {
        List<TempRights> values = (List<TempRights>)tempRightsMap.get(RightType.RIGHTS);
        List<RightsHistory> rightsHistoryList = new ArrayList<>();

        values.forEach(value -> {
            RightsHistory rightsHistory = new RightsHistory();
            rightsHistory.setSendTime(LocalDateTime.now());
            rightsHistory.setRightsId(value.getId());
            rightsHistory.setMemberId(value.getMemberId());
            rightsHistory.setExpireTime(value.getExpireTime());
            String memberCode = value.getMemberCode();
            rightsHistory.setMemberCode(memberCode);
            rightsHistoryList.add(rightsHistory);
        });

        return rightsHistoryList;
    }

    /**
     * 成长值发放,基于已获得的权益
     * @param tempExpList 权益列表
     */
    private void grantExp(List<TempExp> tempExpList) {
        if (!CollectionUtils.isEmpty(tempExpList))
            this.expOperationService.grantPointsThroughTempExp(tempExpList);
    }

    /**
     * 发放积分,基于已获得的权益
     *
     * @param tempPointsList 权益列表
     */
    private void grantPoint(List<TempPoints> tempPointsList) {
        if (!CollectionUtils.isEmpty(tempPointsList))
            this.pointsOperationService.grantPointsThroughTempRightsList(tempPointsList);
    }

    /**
     * 发放优惠券,基于已获得的权益
     *
     * @param tempCouponList 优惠券
     */
    private void grantCoupon(List<TempCoupon> tempCouponList) {
        // 发放优惠券
        if (!CollectionUtils.isEmpty(tempCouponList))
            this.couponOperationService.grantCouponThroughTempCoupon(tempCouponList);
    }

    /**
     * 权益发放
     * @param tempRightsMap
     */
    private void refresh(Map<RightType, Object> tempRightsMap) {
        /*FutureTask<Map<Long,Long>> futureTask1 = new FutureTask(()->{
            log.info(Thread.currentThread().getName() + "=========>> start");
            // 积分
            this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
            log.info(Thread.currentThread().getName() + "=========>>grantPoint end");
            // 成长值
//            this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
            // 优惠券
//            this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
            return null;
        });
        FutureTask<Map<Long,Long>> futureTask2 = new FutureTask(()->{
            // 积分
//            this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
            // 成长值
            this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
            // 优惠券
//            this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
            return null;
        });
        FutureTask<Map<Long,Long>> futureTask3 = new FutureTask(()->{
            // 积分
//            this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
            // 成长值
//            this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
            // 优惠券
            this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
            return null;
        });
        this.threadPoolTaskExecutor.execute(futureTask1);
        this.threadPoolTaskExecutor.execute(futureTask2);
        this.threadPoolTaskExecutor.execute(futureTask3);*/
        /*this.threadPoolTaskExecutor.execute(() -> {
          // 积分
          this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
          // 成长值
          this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
          // 优惠券
          this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
        });*/


        /*this.threadPoolTaskExecutor.execute(() -> {
            log.info(Thread.currentThread().getName() + "=========>> start");
            // 积分
            this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
            log.info(Thread.currentThread().getName() + "=========>> end");
        });*/

        List<TempPoints> tempPointsList = (List<TempPoints>) tempRightsMap.get(RightType.POINTS);
        if (!CollectionUtils.isEmpty(tempPointsList)) {
            // 积分
            this.grantPoint(tempPointsList);
        }

        List<TempExp> tempExpList = (List<TempExp>) tempRightsMap.get(RightType.EXP);
        if (!CollectionUtils.isEmpty(tempExpList)) {
            // 成长值
            this.grantExp(tempExpList);
        }

        List<TempCoupon> tempCouponList = (List<TempCoupon>) tempRightsMap.get(RightType.COUPON);
        if (!CollectionUtils.isEmpty(tempCouponList)) {
            // 优惠券
            this.grantCoupon(tempCouponList);
        }

    }

    /**
     * 区分权益
     * @param rightsList
     * @return
     */
    private Map<RightType, Object> distinguishRight(List<RightsHistory> rightsList) {

        Map<RightType,Object> map = new HashMap<>();

        // 优惠券
        List<TempCoupon> tempCouponList = new ArrayList<>();

        for (RightsHistory right : rightsList) {
            Long rightId = right.getRightsId();
            Long memberId = right.getMemberId();
            Long userId = right.getUserId();
            // 权益类型
            RightsDTO rightsDTO = this.getRights(rightId);
            // 权益的实体类型  1:积分;2成长值;3优惠券
            String type = rightsDTO.getEntityType();
            Long expireTime = rightsDTO.getExpireTime();

            switch (type) {
               // 优惠券
               case "1":
                   Long entityId = rightsDTO.getEntityId();
                   CouponDTO couponDTO = this.findCouponById(entityId);
                   if (Objects.nonNull(couponDTO)) {
                       TempCoupon tempCoupon = new TempCoupon();
                       tempCoupon.setId(couponDTO.getId());
                       tempCoupon.setMemberId(memberId);
                       tempCoupon.setUserId(userId);
                       tempCoupon.setRightsAmount(1);
                       tempCoupon.setRightsSendStrategy(0);
                       tempCoupon.setCode(couponDTO.getCode());
                       if (Objects.nonNull(expireTime))
                           tempCoupon.setExpireTime(LocalDateTimeUtil.of(expireTime));
                       tempCouponList.add(tempCoupon);
                   }
                   break;
                // 观影券
               case "2":
                   break;
               // 活动参与机会
               case "3":
                   break;

                default:
                    break;
            }
        }

        // 优惠券
        if (!CollectionUtils.isEmpty(tempCouponList))
        map.put(RightType.COUPON,tempCouponList);
        return map;
    }

    /**
     * 获取优惠券信息
     * @param id
     * @return
     */
    private CouponDTO findCouponById(Long id) {
        CouponDTO couponDTO = this.couponService.findById(id);
        return couponDTO;
    }

    /**
     * 权益详情
     * @param rightsId
     * @return
     */
    private RightsDTO getRights(Long rightsId) {
        RightsDTO rightsDTO = this.rightsService.findById(rightsId);
        return rightsDTO;
    }


    /**
     * 添加权益领取记录
     * @param rightsHistories
     */
    private void doInsertTrRightHistory(List<RightsHistory> rightsHistories) {
        if (!CollectionUtils.isEmpty(rightsHistories)) {

            for (RightsHistory rightsHistory : rightsHistories) {
                Long operatorId = rightsHistory.getOperatorId();
                String operatorName = rightsHistory.getOperatorName();
                rightsHistory.setSendTime(LocalDateTime.now());
                rightsHistory.setOperatorId(Objects.nonNull(operatorId)?operatorId:0);
                rightsHistory.setOperatorName(!StringUtils.isEmpty(operatorName)?operatorName:"系统发放");
                this.rightsHistoryService.create(rightsHistory);
            }

        }

    }

}