package com.simuwang.daq.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.simuwang.base.mapper.FundAliasMapper;
import com.simuwang.base.mapper.FundInfoMapper;
import com.simuwang.base.pojo.dos.FundAliasDO;
import com.simuwang.base.pojo.dos.FundInfoDO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FundService {

    private final FundInfoMapper fundInfoMapper;
    private final FundAliasMapper fundAliasMapper;

    public FundService(FundInfoMapper fundInfoMapper, FundAliasMapper fundAliasMapper) {
        this.fundInfoMapper = fundInfoMapper;
        this.fundAliasMapper = fundAliasMapper;
    }

    /**
     * 根据基金名称和备案编码匹配基金信息
     *
     * @param fundName       基金名称
     * @param registerNumber 备案编码
     * @return 匹配上的基金id
     */
    public List<String> getFundIdByNamesAndCode(String fundName, String registerNumber) {
        List<FundInfoDO> fundInfoDOList = getFundInfoByNamesAndCode(fundName, registerNumber);
        return fundInfoDOList.stream().map(FundInfoDO::getFundId).distinct().collect(Collectors.toList());
    }

    /**
     * 根据基金名称和备案编码匹配基金信息
     *
     * @param fundName       基金名称
     * @param registerNumber 备案编码
     * @return 匹配上的基金信息(基金id, 基金名称, 备案编码)
     */
    public List<FundInfoDO> getFundInfoByNamesAndCode(String fundName, String registerNumber) {
        // 1.基金名称 + 备案编码 一起进行匹配
        List<FundInfoDO> fundInfoDOList = fundInfoMapper.queryFundByNameAndRegisterNumber(fundName, registerNumber);
        if (CollUtil.isNotEmpty(fundInfoDOList)) {
            return fundInfoDOList;
        }
        List<FundAliasDO> fundAliasDOList = fundAliasMapper.queryFundByNameAndRegisterNumber(fundName, registerNumber);
        if (CollUtil.isNotEmpty(fundAliasDOList)) {
            return fundAliasDOList.stream().map(this::convertToFundInfoDO).filter(Objects::nonNull).collect(Collectors.toList());
        }
        // 2.基金名称匹配
        if (StrUtil.isNotBlank(fundName) && StrUtil.isBlank(registerNumber)) {
            List<FundInfoDO> fundInfos = fundInfoMapper.queryFundByName(fundName);
            if (CollUtil.isNotEmpty(fundInfos)) {
                return fundInfos;
            }
            List<FundAliasDO> fundAliasList = fundAliasMapper.queryFundByName(fundName);
            if (CollUtil.isNotEmpty(fundAliasList)) {
                return fundAliasList.stream().map(this::convertToFundInfoDO).filter(Objects::nonNull).collect(Collectors.toList());
            }
        }
        // 3.备案编码匹配
        if (StrUtil.isBlank(fundName) && StrUtil.isNotBlank(registerNumber)) {
            List<FundInfoDO> fundInfos = fundInfoMapper.queryFundByRegisterNumber(registerNumber);
            if (CollUtil.isNotEmpty(fundInfos)) {
                return fundInfos;
            }
            List<FundAliasDO> fundAliasList = fundAliasMapper.queryFundByRegisterNumber(registerNumber);
            if (CollUtil.isNotEmpty(fundAliasList)) {
                return fundAliasList.stream().map(this::convertToFundInfoDO).filter(Objects::nonNull).collect(Collectors.toList());
            }
        }
        return CollUtil.newArrayList();
    }

    public FundInfoDO convertToFundInfoDO(FundAliasDO fundAliasDO) {
        if (fundAliasDO == null || StrUtil.isBlank(fundAliasDO.getTargetFundId())) {
            return null;
        }
        FundInfoDO fundInfoDO = new FundInfoDO();
        fundInfoDO.setFundId(fundAliasDO.getTargetFundId());
        fundInfoDO.setFundName(fundAliasDO.getTargetFundName());
        fundInfoDO.setRegisterNumber(fundAliasDO.getTargetRegisterNumber());
        return fundInfoDO;
    }

}
