package com.hzs.sale.wares.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.enums.EAwards;
import com.hzs.common.core.enums.ERangeType;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.sale.wares.BdWaresRange;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.sale.wares.mapper.BdWaresRangeMapper;
import com.hzs.sale.wares.service.IBdWaresRangeService;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IRangeServiceApi;
import com.hzs.system.config.dto.RangeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * 极差明细表 服务实现类
 */
@Service
public class BdWaresRangeServiceImpl extends ServiceImpl<BdWaresRangeMapper, BdWaresRange> implements IBdWaresRangeService {

    @DubboReference
    IRangeServiceApi iRangeServiceApi;
    @DubboReference
    IAwardsServiceApi iAwardsServiceApi;

    @Override
    public List<BdWaresRange> queryWaresRangeByCondition(CuMember cuMember, List<Integer> pkWareList) {
        int level = cuMember.getPkSettleGrade();
        int rangeType = ERangeType.GRADE.getValue();
        BdAwards bdAwards = iAwardsServiceApi.getAwardsOne(cuMember.getPkAwards()).getData();
        if (bdAwards.getAwardsValue() > EAwards.MEMBER.getValue()) {
            level = cuMember.getPkAwards();
            rangeType = ERangeType.AWARDS.getValue();
        }
        RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(cuMember.getPkSettleCountry(), rangeType, level).getData();
        if (rangeDTO == null) {
            return new ArrayList<>();
        }
        return baseMapper.queryWaresRangeByCondition(rangeDTO.getPkRange(), pkWareList);
    }

    @Override
    public List<BdWaresRange> querySkuWaresRangeByCondition(Integer pkCountry, Integer rangeType, Integer level, List<Integer> specsSkuList) {
        RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(pkCountry, rangeType, level).getData();
        LambdaQueryWrapper<BdWaresRange> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWaresRange::getPkRange, rangeDTO.getPkRange());
        queryWrapper.in(BdWaresRange::getPkWaresSpecsSku, specsSkuList);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdWaresRange> queryWaresList(Long pkWares, Integer pkCountry) {
        return baseMapper.queryWaresList(pkWares, pkCountry);
    }

    @Override
    public List<BdWaresRange> listRetailWaresRange(Integer pkGrade, List<Integer> pkWareList) {
        // 新零售只有等级级差，不需要处理奖衔
        RangeDTO rangeDTO = iRangeServiceApi.queryRangeDtoByCondition(CountryConstants.CHINA_COUNTRY, ERangeType.GRADE.getValue(), pkGrade).getData();
        if (rangeDTO == null) {
            return new ArrayList<>();
        }
        return baseMapper.queryWaresRangeByCondition(rangeDTO.getPkRange(), pkWareList);
    }

}
