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.BdMemberSpecialDetailExt;
import com.hzs.common.domain.system.config.ext.BdSpecialCurrencyDetailExt;
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.IBdMemberSpecialCurrencyService;
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.Collections;
import java.util.List;
import java.util.Set;

/**
 * @Description: 专区币种配置提供的公共api服务
 * @Author: sui q
 * @Time: 2022/9/6 10:41
 * @Classname: AreaCurrencyServiceProvider
 * @PackageName: com.hzs.system.config.provider
 */
@DubboService
public class AreaCurrencyServiceProvider implements IAreaCurrencyServiceApi {

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

    @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);
    }

    /**
     * 根据条件查找对应的币种配置
     *
     * @param specialArea 所属专区
     * @param pkCountry   国家
     * @return: R<List < AreaCurrencyDTO>>
     * @Author: sui q
     * @Date: 2022/9/6 10:37
     */
    @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>> queryMemberCurrency(Integer specialArea, Long userId) {
        List<BdMemberSpecialDetailExt> list = iBdMemberSpecialCurrencyService.listMemberDetail(userId, specialArea);
        if (CollectionUtil.isNotEmpty(list)) {
            return R.ok(BeanUtil.copyToList(list, AreaCurrencyDTO.class));
        }
        return R.ok(Collections.emptyList());
    }

    @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())) {
            List<AreaCurrencyDTO> resultList = BeanUtil.copyToList(bdSpecialCurrencyExtList.get(0).getSpecialCurrencyDetailList(), AreaCurrencyDTO.class);
            // 2023.11.17 需求添加，记录模板名称，用于后续写死判断逻辑使用
            resultList.get(0).setTempName(bdSpecialCurrencyExtList.get(0).getTempName());
            return R.ok(resultList);
        }
        return R.ok(null);
    }

    /**
     * 批量查询重销的扣除比例
     *
     * @param pkCurrencySet
     * @param account
     * @return
     */
    @Override
    public List<AreaCurrencyDTO> queryAreaCurrencyBySpecialArea(Set<Integer> pkCurrencySet, Integer account) {
        List<AreaCurrencyDTO> resultList = new ArrayList<>();

        List<BdSpecialCurrencyDetailExt> bdSpecialCurrencyDetailListList = specialCurrencyService.queryAreaCurrencyBySpecialArea(pkCurrencySet, account);
        if (CollectionUtil.isNotEmpty(bdSpecialCurrencyDetailListList)) {
            for (BdSpecialCurrencyDetailExt bdSpecialCurrencyDetail : bdSpecialCurrencyDetailListList) {
                resultList.add(AreaCurrencyDTO.builder()
                        .pkSpecialCurrency(bdSpecialCurrencyDetail.getPkSpecialCurrency())
                        .pkAccount(bdSpecialCurrencyDetail.getPkAccount())
                        .fieldValue(bdSpecialCurrencyDetail.getFieldValue())
                        .deductMaxRatio(bdSpecialCurrencyDetail.getDeductMaxRatio())
                        .build());
            }
        }

        return resultList;
    }
}
