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.BdAccount;
import com.hzs.common.domain.system.config.ext.BdSpecialCurrencyExt;
import com.hzs.common.domain.system.ext.AreaCurrencyExt;
import com.hzs.system.config.IAreaCurrencyServiceApi;
import com.hzs.system.config.dto.AreaCurrencyDTO;
import com.hzs.system.config.param.SpecialCurrencyParam;
import com.hzs.system.config.service.IBdAccountService;
import com.hzs.system.config.service.IBdSpecialCurrencyService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * 专区币种配置提供的公共api服务
 */
@DubboService
public class AreaCurrencyServiceProvider implements IAreaCurrencyServiceApi {

    @Autowired
    private IBdAccountService bdAccountService;
    @Autowired
    private IBdSpecialCurrencyService specialCurrencyService;

    @Override
    public R<List<AreaCurrencyDTO>> queryAreaCurrencyByPkCountry(Integer pkCountry) {
        List<BdAccount> bdAccountList = bdAccountService.queryBdAccount(pkCountry);
        List<AreaCurrencyDTO> areaCurrencyDTOList = new ArrayList<>();
        for (BdAccount bdAccount : bdAccountList) {
            AreaCurrencyDTO areaCurrencyDTO = new AreaCurrencyDTO();
            areaCurrencyDTO.setPkAccount(bdAccount.getPkId());
            areaCurrencyDTO.setFieldValue(bdAccount.getFieldValue());
            areaCurrencyDTOList.add(areaCurrencyDTO);
        }
        return R.ok(areaCurrencyDTOList);
    }

    @Override
    public R<List<AreaCurrencyDTO>> queryAreaCurrencyByCondition(Integer specialArea, Integer pkCountry) {
        List<AreaCurrencyExt> areaCurrencyExtList = bdAccountService.queryAreaCurrencyByCondition(specialArea, pkCountry);
        List<AreaCurrencyDTO> areaCurrencyDTOList = new ArrayList<>();
        areaCurrencyExtList.forEach(areaCurrencyExt -> areaCurrencyDTOList.add(BeanUtil.copyProperties(areaCurrencyExt, AreaCurrencyDTO.class)));
        return R.ok(areaCurrencyDTOList);
    }

    @Override
    public R<List<AreaCurrencyDTO>> queryAreaCurrencyByCondition(Integer pkSpecialAccount) {
        SpecialCurrencyParam specialCurrencyParam = new SpecialCurrencyParam();
        specialCurrencyParam.setPkId(pkSpecialAccount);
        List<BdSpecialCurrencyExt> bdSpecialCurrencyExtList = specialCurrencyService.querySpecialCurrencyList(specialCurrencyParam);
        if (CollectionUtil.isNotEmpty(bdSpecialCurrencyExtList) && CollectionUtil.isNotEmpty(bdSpecialCurrencyExtList.get(0).getSpecialCurrencyDetailList())) {
            return R.ok(BeanUtil.copyToList(bdSpecialCurrencyExtList.get(0).getSpecialCurrencyDetailList(), AreaCurrencyDTO.class));
        }
        return R.ok(null);
    }

}
