package com.hzs.system.base.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.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 org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

@DubboService
public class SystemConfigServiceProvider implements ISystemConfigServiceApi {

    @Autowired
    private IBdSystemConfigService iBdSystemConfigService;
    @Autowired
    private IBdCountryService iBdCountryService;
    @Autowired
    private IBdCubasdocService iBdCubasdocService;

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

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

    @Override
    public R<Map<String, String>> getBonusSystemConfig() {
        List<BdCountry> countryList = iBdCountryService.getAllCountry();
        Map<String, String> systemConfigMap = new HashMap<>(ComputeUtil.mapInitCapacity(countryList.size()));
        countryList.forEach(bdCountry -> {
            Integer pkCountry = bdCountry.getPkId();
            Map<String, String> systemConfig = iBdSystemConfigService.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 = iBdCountryService.getAllCountry();
        Map<String, String> systemConfigMap = new HashMap<>(ComputeUtil.mapInitCapacity(countryList.size()));
        for (BdCountry bdCountry : countryList) {
            Integer pkCountry = bdCountry.getPkId();
            Map<String, String> systemConfig = iBdSystemConfigService.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 = iBdCubasdocService.queryCubasdocByCondition(bdCubasdocParam);
        if (CollectionUtil.isNotEmpty(cuBasDocList)) {
            BdCubasdocDTO bdCubasdocDTO = BeanUtil.copyProperties(cuBasDocList.get(0), BdCubasdocDTO.class);
            return R.ok(bdCubasdocDTO);
        }
        return R.fail();
    }

}
