package com.hzs.system.config.provider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.system.config.IAccountServiceApi;
import com.hzs.system.config.service.IBdAccountService;
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;

/**
 * 币种配置的dubbo服务类
 */
@DubboService
public class AccountServiceProvider implements IAccountServiceApi {

    @Autowired
    private IBdAccountService accountService;

    @Override
    public R<List<BdAccount>> queryAccountByType(Integer accountProperty, Integer pkCountry) {
        return R.ok(accountService.queryAccountByType(accountProperty, pkCountry));
    }

    @Override
    public R<List<BdAccount>> queryAccountByType(Integer accountType, Collection<Integer> pkCountryList) {
        return R.ok(accountService.queryAccountByType(accountType, pkCountryList));
    }

    @Override
    public R<BdAccount> getAccount(Integer pkId) {
        return R.ok(accountService.getBdAccount(pkId));
    }

    @Override
    public R<BdAccount> getBdAccountByFieldValue(Integer pkCountry, Integer fieldValue) {
        return R.ok(accountService.getBdAccountByFieldValue(pkCountry, fieldValue));
    }

    @Override
    public R<Map<Integer, BdAccount>> getAccountMap(List<Integer> pkIdList) {
        Map<Integer, BdAccount> resultMap = new HashMap<>();
        pkIdList.parallelStream().forEach((pkId) -> {
            resultMap.put(pkId, accountService.getBdAccount(pkId));
        });
        return R.ok(resultMap);
    }

    @Override
    public R<Map<Integer, BdAccount>> getAccountListByCountry(Integer pkCountry) {
        Map<Integer, BdAccount> resultMap = new HashMap<>();
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        List<BdAccount> bdAccountList = accountService.list(queryWrapper);
        for (BdAccount bdAccount : bdAccountList) {
            resultMap.put(bdAccount.getPkId(), bdAccount);
        }
        return R.ok(resultMap);
    }

    @Override
    public R<Map<Integer, BdAccount>> getAccountMapByCountry(Integer pkCountry) {
        Map<Integer, BdAccount> resultMap = new HashMap<>();
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        List<BdAccount> bdAccountList = accountService.list(queryWrapper);
        for (BdAccount bdAccount : bdAccountList) {
            resultMap.put(bdAccount.getFieldValue(), bdAccount);
        }
        return R.ok(resultMap);
    }

    @Override
    public R<BdAccount> queryAccountByCondition(Integer pkCountry) {
        return R.ok(accountService.queryAccountByCondition(pkCountry));
    }

    @Override
    public R<Integer> getPkAccount(Integer pkId) {
        return R.ok(accountService.getPkAccount(pkId));
    }

    @Override
    public R<List<BdAccount>> memberAccountByType(Integer pkCountry) {
        return R.ok(accountService.memberAccountByType(pkCountry));
    }

}
