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.domain.system.base.BdCurrency;
import com.hzs.common.domain.system.base.BdStorehouse;
import com.hzs.common.domain.system.base.ext.BdCubasdocExt;
import com.hzs.common.domain.system.sys.SysCorp;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.base.param.BdCubasdocParam;
import com.hzs.system.base.service.IBdCubasdocService;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.base.service.IBdStorehouseAreaMiddleService;
import com.hzs.system.base.service.IBdStorehouseService;
import com.hzs.system.sys.service.ISysCorpService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

@DubboService
public class CurrencyServiceProvider implements ICurrencyServiceApi {

    @Autowired
    private IBdCurrencyService iBdCurrencyService;
    @Autowired
    private IBdStorehouseAreaMiddleService iBdStorehouseAreaMiddleService;
    @Autowired
    private IBdCubasdocService iBdCubasdocService;
    @Autowired
    private ISysCorpService iSysCorpService;
    @Autowired
    private IBdStorehouseService iBdStorehouseService;

    @Override
    public R<CurrencyDTO> getCurrency(Integer pkCountry) {
        BdCurrency bdCurrency = iBdCurrencyService.getCurrency(pkCountry);
        return R.ok(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
    }

    @Override
    public R<List<CurrencyDTO>> getCurrencyByPkCountryList(Set<Integer> pkCountryList) {
        List<CurrencyDTO> currencyDTOList = new ArrayList<>();
        for (Integer pkCountry : pkCountryList) {
            BdCurrency bdCurrency = iBdCurrencyService.getCurrency(pkCountry);
            currencyDTOList.add(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
        }
        return R.ok(currencyDTOList);
    }

    @Override
    public R<List<CurrencyDTO>> getCurrencyById(Set<Integer> currencyIdSet) {
        List<CurrencyDTO> currencyDTOList = new ArrayList<>();
        for (Integer currencyId : currencyIdSet) {
            BdCurrency bdCurrency = iBdCurrencyService.getByPkId(currencyId);
            currencyDTOList.add(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
        }
        return R.ok(currencyDTOList);
    }

    @Override
    public R<CurrencyDTO> getCurrencyByPkId(Integer currencyId) {
        BdCurrency bdCurrency = iBdCurrencyService.getByPkId(currencyId);
        return R.ok(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
    }

    @Override
    public R<BdStorehouse> queryBdStorehouseByProvince(Integer province, Integer... type) {
        int storeType = 1;
        if (type != null && type.length > 0) {
            storeType = type[0];
        }
        return R.ok(iBdStorehouseAreaMiddleService.queryBdStorehouseByProvince(province, storeType));
    }

    @Override
    public R<List<BdStorehouse>> queryBdStorehouseMapByProvince(Collection<Integer> provinceList) {
        return R.ok(iBdStorehouseAreaMiddleService.queryStorehouseListByProvince(provinceList));
    }

    @Override
    public R<CurrencyDTO> getByHistoryPkId(Integer currencyId) {
        BdCurrency bdCurrency = iBdCurrencyService.getByHistoryPkid(currencyId);
        return R.ok(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
    }

    /**
     * 查询所有的币种
     */
    @Override
    public R<List<CurrencyDTO>> findAll() {
        List<BdCurrency> currencyList = iBdCurrencyService.list();
        List<CurrencyDTO> currencyDtoList = currencyList.stream().map(currency -> BeanUtil.copyProperties(currency, CurrencyDTO.class)).collect(Collectors.toList());
        return R.ok(currencyDtoList);
    }

    @Override
    public R<BdCubasdocExt> queryBdCubasdocById(Integer pkId, Integer pkCountry) {
        BdCubasdocParam cubasdocParam = BdCubasdocParam.builder()
                .pkCountry(pkCountry)
                .pkId(pkId)
                .build();
        List<BdCubasdocExt> bdCubasdocList = iBdCubasdocService.querySupplierByCondition(cubasdocParam);
        if (CollectionUtil.isNotEmpty(bdCubasdocList)) {
            return R.ok(bdCubasdocList.get(0));
        }
        return R.ok(null);
    }

    @Override
    public R<BdCubasdocExt> queryBdCustomerById(Integer pkId, Integer pkCountry) {
        BdCubasdocParam customerParam = BdCubasdocParam.builder()
                .pkCountry(pkCountry)
                .pkId(pkId)
                .build();
        List<BdCubasdocExt> bdCubasdocList = iBdCubasdocService.queryCustomerByCondition(customerParam);
        if (CollectionUtil.isNotEmpty(bdCubasdocList)) {
            return R.ok(bdCubasdocList.get(0));
        }
        return R.ok(null);
    }

    @Override
    public R<SysCorp> querySysCorp(Integer pkId, Integer pkCountry) {
        return R.ok(iSysCorpService.getSysCorp(pkId, pkCountry));
    }

    @Override
    public R<List<BdStorehouse>> queryStorehouseByIds(Set<Integer> storehouseIdSet, Integer pkCountry, Integer pkCorp) {
        return R.ok(iBdStorehouseService.queryStorehouseByIds(storehouseIdSet, pkCountry, pkCorp));
    }
}
