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 currencyService;

    @Autowired
    private IBdStorehouseAreaMiddleService storehouseAreaMiddleService;

    @Autowired
    private IBdCubasdocService bdCubasdocService;

    @Autowired
    private ISysCorpService sysCorpService;

    @Autowired
    private IBdStorehouseService storehouseService;

    @Override
    public R<CurrencyDTO> getCurrency(Integer pkCountry) {
        BdCurrency bdCurrency = currencyService.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 = currencyService.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 = currencyService.getByPkid(currencyId);
            currencyDTOList.add(BeanUtil.copyProperties(bdCurrency, CurrencyDTO.class));
        }
        return R.ok(currencyDTOList);
    }

    @Override
    public R<CurrencyDTO> getCurrencyByPkId(Integer currencyId) {
        BdCurrency bdCurrency = currencyService.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(storehouseAreaMiddleService.queryBdStorehouseByProvince(province, storeType));
    }

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

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

    @Override
    public R<List<CurrencyDTO>> findAll() {
        List<BdCurrency> currencyList = currencyService.list();
        List<CurrencyDTO> currencyDtoList = currencyList.stream().map(currency -> {
            return 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 = bdCubasdocService.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 = bdCubasdocService.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(sysCorpService.getSysCorp(pkId, pkCountry));
    }

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

}
