package com.hzs.bonus.bonus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.bonus.service.ICuMemberBonusRangeService;
import com.hzs.common.domain.member.bonus.CuMemberBonusRange;
import com.hzs.bonus.bonus.mapper.CuMemberBonusRangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 会员奖金-奖金收益明细表(复购极差) 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-12-30
 */
@Service
public class CuMemberBonusRangeServiceImpl extends ServiceImpl<CuMemberBonusRangeMapper, CuMemberBonusRange> implements ICuMemberBonusRangeService {

    private CuMemberBonusSettle cuMemberBonusSettle;

    @Autowired
    public void setCuMemberBonusSettle(CuMemberBonusSettle cuMemberBonusSettle) {
        this.cuMemberBonusSettle = cuMemberBonusSettle;
    }

    @Override
    public void deleteCuMemberBonusRange(Integer period) {
        baseMapper.deleteCuMemberBonusRange(period);
    }

    @Override
    public void batchInsertCuMemberBonusRange(List<CuMemberBonusRange> cuMemberBonusRangeList, Set<Long> memberIdSet) {
        if(cuMemberBonusRangeList.size() == 0){
            return;
        }
        List<CuMemberBonusRange> cuMemberBonusRanges = new ArrayList<>();
        for (CuMemberBonusRange cuMemberBonusRange : cuMemberBonusRangeList) {
            if(memberIdSet.contains(cuMemberBonusRange.getPkBonus())){
                cuMemberBonusRanges.add(cuMemberBonusRange);
            }
        }
        if(cuMemberBonusRanges.size()==0){
            return;
        }
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(cuMemberBonusRanges);
        lists.forEach(list ->
                baseMapper.batchInsertCuMemberBonusRange(list));
    }
}
