package com.hzs.system.base.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.ESystemConfig;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.base.ext.BdCubasdocExt;
import com.hzs.common.domain.system.config.BdBonusExpand;
import com.hzs.system.base.ISystemConfigServiceApi;
import com.hzs.system.base.dto.BdCubasdocDTO;
import com.hzs.system.base.param.BdCubasdocParam;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.service.IBdCubasdocService;
import com.hzs.system.base.service.IBdSystemConfigService;
import com.hzs.system.config.service.IBdBonusExpandService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class SystemConfigServiceProvider implements ISystemConfigServiceApi {

    @Autowired
    private IBdSystemConfigService systemConfigService;
    @Autowired
    private IBdCountryService bdCountryService;
    @Autowired
    private IBdCubasdocService bdCubasdocService;
    @Autowired
    private IBdBonusExpandService iBdBonusExpandService;

    @Override
    public R<String> getBonusSystemConfig(Integer pkCountry, String key) {
        return R.ok(systemConfigService.getSystemConfig(pkCountry, key));
    }

    @Override
    public R<Map<Integer, String>> getBonusSystemConfigBatch(Collection<Integer> pkCountryList, String key) {
        Map<Integer, String> resultMap = new HashMap<>();
        for (Integer pkCountry : pkCountryList) {
            resultMap.put(pkCountry, systemConfigService.getSystemConfig(pkCountry, key));
        }
        return R.ok(resultMap);
    }

    @Override
    public R<Map<String, String>> getBonusSystemConfig(Integer pkCountry) {
        return R.ok(systemConfigService.getSystemConfig(pkCountry));
    }

    @Override
    public R<Map<String, String>> getBonusSystemConfig() {
        List<BdCountry> countryList = bdCountryService.getAllCountry();
        Map<String, String> systemConfigMap = new HashMap<>(ComputeUtil.mapInitCapacity(countryList.size()));
        countryList.forEach(bdCountry -> {
            Integer pkCountry = bdCountry.getPkId();
            Map<String, String> systemConfig = systemConfigService.getSystemConfig(pkCountry);
            if (systemConfig.size() > 0) {
                systemConfig.forEach((key, value) -> {
                    if (ESystemConfig.BONUS_RECOMMEND_TYPE.getKey().equals(key) ||
                            ESystemConfig.BONUS_UPGRADE_INCOME_TYPE.getKey().equals(key) ||
                            ESystemConfig.UPGRADE_WAY.getKey().equals(key) ||
                            ESystemConfig.EXPAND_CAP_METHOD.getKey().equals(key)) {
                        systemConfigMap.put(pkCountry.toString() + key, value);
                    }
                });
            }
        });
        return R.ok(systemConfigMap);
    }

    /**
     * 批量查询所有国家的订单时间参数
     */
    @Override
    public R<Map<String, String>> getSystemConfig() {
        List<BdCountry> countryList = bdCountryService.getAllCountry();
        Map<String, String> systemConfigMap = new HashMap<>(ComputeUtil.mapInitCapacity(countryList.size()));
        for (BdCountry bdCountry : countryList) {
            Integer pkCountry = bdCountry.getPkId();
            Map<String, String> systemConfig = systemConfigService.getSystemConfig(pkCountry);
            if (systemConfig.size() > 0) {
                systemConfig.forEach((key, value) -> {
                    systemConfigMap.put(pkCountry.toString() + key, value);
                });
            }
        }
        return R.ok(systemConfigMap);
    }

    @Override
    public R<BdCubasdocDTO> queryBdCubasdocByUser(Long pkUser) {
        BdCubasdocParam bdCubasdocParam = BdCubasdocParam.builder().pkUser(pkUser).build();
        List<BdCubasdocExt> cuBasDocList = bdCubasdocService.queryCubasdocByCondition(bdCubasdocParam);
        if (CollectionUtil.isNotEmpty(cuBasDocList)) {
            BdCubasdocDTO bdCubasdocDTO = BeanUtil.copyProperties(cuBasDocList.get(0), BdCubasdocDTO.class);
            return R.ok(bdCubasdocDTO);
        }
        return R.fail();
    }

    @Override
    public R<BdBonusExpand> getBdBonusExpand() {
        LambdaQueryWrapper<BdBonusExpand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdBonusExpand::getPkCountry, 1);
        queryWrapper.orderByDesc(BdBonusExpand::getCreationTime);
        BdBonusExpand bdBonusExpand = iBdBonusExpandService.getOne(queryWrapper, false);
        return R.ok(bdBonusExpand);
    }

}
