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

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

import com.hzs.common.core.constant.SystemFieldConstants;
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;

/**
 * <p>
 * 极差明细表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-09-09
 */
@Service
public class BdWaresRangeServiceImpl extends ServiceImpl<BdWaresRangeMapper, BdWaresRange> implements IBdWaresRangeService {

    @DubboReference
    private IRangeServiceApi rangeServiceApi;
    @DubboReference
    private IAwardsServiceApi awardsServiceApi;

    @Override
    public List<BdWaresRange> queryWaresRangeByCondition(CuMember cuMember, List<Integer> pkWareList) {
        int level = cuMember.getPkSettleGrade();
        int rangeType = ERangeType.GRADE.getValue();
        BdAwards bdAwards = awardsServiceApi.getAwardsOne(cuMember.getPkAwards()).getData();
        if (bdAwards.getAwardsValue() > EAwards.MEMBER.getValue()) {
            level = cuMember.getPkAwards();
            rangeType = ERangeType.AWARDS.getValue();
        }
        RangeDTO rangeDTO = rangeServiceApi.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 = rangeServiceApi.queryRangeDtoByCondition(pkCountry, rangeType, level).getData();
        QueryWrapper<BdWaresRange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_RANGE, rangeDTO.getPkRange());
        queryWrapper.in(SystemFieldConstants.PK_WARES_SPECS_SKU, specsSkuList);
        return baseMapper.selectList(queryWrapper);
    }
    /**
     * 以range 为主表查询商品极差信息
     * @param pkWares
     * @return
     */
    @Override
    public List<BdWaresRange> queryWaresList(Long pkWares,Integer pkCountry) {
        return baseMapper.queryWaresList(pkWares,pkCountry);
    }
}
