package com.hzs.system.config.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.common.domain.system.ext.BdGradeScopeExt;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import com.hzs.system.config.service.IBdGradeScopeService;
import com.hzs.system.config.service.IBdGradeService;
import com.hzs.system.config.service.IBdStoreLevelService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 等级的dubbo服务类
 */
@DubboService
public class GradeServiceProvider implements IGradeServiceApi {

    @Autowired
    private IBdGradeService gradeService;
    @Autowired
    private IBdGradeScopeService gradeScopeService;

    @Override
    public R<GradeDTO> getGrade(Integer pkId) {
        return R.ok(BeanUtil.copyProperties(gradeService.getBdGrade(pkId), GradeDTO.class));
    }

    @Override
    public R<GradeDTO> getGradeByGradeValue(Integer gradeValue, Integer pkCountry) {
        return R.ok(BeanUtil.copyProperties(gradeService.getGradeByGradeValue(gradeValue, pkCountry), GradeDTO.class));
    }

    @Override
    public R<List<GradeDTO>> queryGradeConfigByCondition(Integer pkCountry) {
        List<BdGrade> gradeList = gradeService.queryGradeConfigByCondition(pkCountry);
        List<GradeDTO> gradeDTOList = getGradeDTOS(pkCountry, gradeList);
        return R.ok(gradeDTOList);
    }

    @Override
    public R<List<GradeDTO>> queryHaiFunGradeConfigByCondition(Integer pkCountry) {
        List<BdGrade> gradeList = gradeService.queryHaiFunGradeConfigByCondition(pkCountry);
        List<GradeDTO> gradeDTOList = getGradeDTOS(pkCountry, gradeList);
        return R.ok(gradeDTOList);
    }

    private List<GradeDTO> getGradeDTOS(Integer pkCountry, List<BdGrade> gradeList) {
        List<GradeDTO> gradeDTOList = new ArrayList<>();
        Map<Integer, List<BdGradeScopeExt>> gradeScopeMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(gradeList)) {
            BdGradeScopeExt gradeScopeExt = new BdGradeScopeExt();
            gradeScopeExt.setPkCountry(pkCountry);
            List<BdGradeScopeExt> bdGradeScopeExtList = gradeScopeService.selectGradeScopeList(gradeScopeExt);
            if (CollectionUtil.isNotEmpty(bdGradeScopeExtList)) {
                bdGradeScopeExtList.forEach(bdGradeScopeExt -> {
                    if (gradeScopeMap.containsKey(bdGradeScopeExt.getPkGrade())) {
                        gradeScopeMap.get(bdGradeScopeExt.getPkGrade()).add(bdGradeScopeExt);
                    } else {
                        List<BdGradeScopeExt> bdGradeScopeList = new ArrayList<>();
                        bdGradeScopeList.add(bdGradeScopeExt);
                        gradeScopeMap.put(bdGradeScopeExt.getPkGrade(), bdGradeScopeList);
                    }
                });
            }
            gradeList.forEach(bdGrade -> gradeDTOList.add(BeanUtil.copyProperties(bdGrade, GradeDTO.class)));
            gradeDTOList.forEach(gradeDTO -> {
                if (gradeScopeMap.containsKey(gradeDTO.getPkId())) {
                    gradeDTO.setBdGradeScopeExtList(gradeScopeMap.get(gradeDTO.getPkId()));
                }
            });
        }
        return gradeDTOList;
    }

    @Override
    public R<Map<Integer, List<GradeDTO>>> queryCountryGradeConfigByCondition(Set<Integer> pkCountrySet) {
        Map<Integer, List<GradeDTO>> gradeDtoMap = new HashMap<>();
        for (Integer pkCountry : pkCountrySet) {
            gradeDtoMap.put(pkCountry, queryGradeConfigByCondition(pkCountry).getData());
        }
        return R.ok(gradeDtoMap);
    }

    @Override
    public R<List<GradeDTO>> queryGradeList(Integer pkCountry) {
        List<BdGrade> gradeList = gradeService.queryGradeConfigByCondition(pkCountry);
        List<GradeDTO> gradeDTOList = new ArrayList<>();
        gradeList.forEach(bdGrade -> gradeDTOList.add(BeanUtil.copyProperties(bdGrade, GradeDTO.class)));
        return R.ok(gradeDTOList);
    }

    @Override
    public R<GradeDTO> queryGradeConfigByCondition(Integer pkCountry, Integer pkGrade) {
        BdGrade bdGrade = gradeService.getBdGrade(pkGrade);
        GradeDTO gradeDTO = BeanUtil.copyProperties(bdGrade, GradeDTO.class);
        BdGradeScopeExt gradeScopeExt = new BdGradeScopeExt();
        gradeScopeExt.setPkCountry(pkCountry);
        gradeScopeExt.setPkGrade(pkGrade);
        gradeDTO.setBdGradeScopeExtList(gradeScopeService.selectGradeScopeList(gradeScopeExt));
        return R.ok(gradeDTO);
    }

    @Override
    public R<List<GradeDTO>> selectGradeAll(Integer pkCountry) {
        List<BdGrade> gradeList = gradeService.selectGradeAll(pkCountry);
        List<GradeDTO> gradeDTOList = new ArrayList<>();
        gradeList.forEach(bdGrade -> gradeDTOList.add(BeanUtil.copyProperties(bdGrade, GradeDTO.class)));
        return R.ok(gradeDTOList);
    }

    @Override
    public R<List<BdGrade>> queryGrowUpFootmarkGrade(Integer pkCountry) {
        return R.ok(gradeService.queryGrowUpFootmarkGrade(pkCountry));
    }

    @Override
    public R<List<GradeDTO>> findAll() {
        List<BdGrade> gradeList = gradeService.list();
        List<GradeDTO> gradeDtoList = new ArrayList<>();
        for (BdGrade bdGrade : gradeList) {
            GradeDTO gradeDTO = BeanUtil.copyProperties(bdGrade, GradeDTO.class);
            gradeDtoList.add(gradeDTO);
        }
        return R.ok(gradeDtoList);
    }

    @Override
    public R<List<GradeDTO>> queryNextGrade(Integer pkCountry) {
        List<BdGrade> gradeList = gradeService.queryNextGrade(pkCountry);
        List<GradeDTO> gradeDtoList = new ArrayList<>();
        for (BdGrade bdGrade : gradeList) {
            GradeDTO gradeDTO = BeanUtil.copyProperties(bdGrade, GradeDTO.class);
            gradeDtoList.add(gradeDTO);
        }
        return R.ok(gradeDtoList);
    }

}
