package com.smppw.modaq.application.components.report.writer;

import cn.hutool.core.collection.CollUtil;
import com.smppw.modaq.domain.dto.report.*;
import com.smppw.modaq.domain.entity.report.ReportAssetAllocationDO;
import com.smppw.modaq.domain.entity.report.ReportFinancialIndicatorsDO;
import com.smppw.modaq.domain.entity.report.ReportInvestmentIndustryDO;
import com.smppw.modaq.domain.entity.report.ReportShareChangeDO;
import com.smppw.modaq.domain.mapper.report.*;
import org.springframework.stereotype.Component;

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

@Component(ReportWriterConstant.WRITER_QUARTERLY)
public class QuarterlyReportWriter<T extends QuarterlyReportData> extends AbstractReportWriter<T> {
    private final ReportShareChangeMapper shareChangeMapper;
    private final ReportAssetAllocationMapper assetAllocationMapper;
    private final ReportInvestmentIndustryMapper investmentIndustryMapper;
    private final ReportFinancialIndicatorMapper financialIndicatorMapper;

    public QuarterlyReportWriter(ReportBaseInfoMapper baseInfoMapper, ReportFundInfoMapper fundInfoMapper,
                                 ReportShareChangeMapper shareChangeMapper, ReportAssetAllocationMapper assetAllocationMapper,
                                 ReportInvestmentIndustryMapper investmentIndustryMapper, ReportFinancialIndicatorMapper financialIndicatorMapper) {
        super(baseInfoMapper, fundInfoMapper);
        this.shareChangeMapper = shareChangeMapper;
        this.assetAllocationMapper = assetAllocationMapper;
        this.investmentIndustryMapper = investmentIndustryMapper;
        this.financialIndicatorMapper = financialIndicatorMapper;
    }

    @Override
    protected void writeExtData(QuarterlyReportData reportData) {
        List<ReportShareChangeDTO> shareChange = reportData.getShareChange();
        if (CollUtil.isNotEmpty(shareChange)) {
            List<ReportShareChangeDO> entityList = shareChange.stream()
                    .map(ReportShareChangeDTO::toEntity).collect(Collectors.toList());
            this.shareChangeMapper.insert(entityList);
        }

        List<ReportAssetAllocationDTO> assetAllocation = reportData.getAssetAllocation();
        if (CollUtil.isNotEmpty(assetAllocation)) {
            List<ReportAssetAllocationDO> entityList = assetAllocation.stream()
                    .map(ReportAssetAllocationDTO::toEntity).collect(Collectors.toList());
            this.assetAllocationMapper.insert(entityList);
        }

        List<ReportFinancialIndicatorsDTO> financialIndicators = reportData.getFinancialIndicators();
        if (CollUtil.isNotEmpty(financialIndicators)) {
            List<ReportFinancialIndicatorsDO> entityList = financialIndicators.stream()
                    .map(ReportFinancialIndicatorsDTO::toEntity).collect(Collectors.toList());
            this.financialIndicatorMapper.insert(entityList);
        }

        List<ReportInvestmentIndustryDTO> investmentIndustry = reportData.getInvestmentIndustry();
        if (CollUtil.isNotEmpty(investmentIndustry)) {
            List<ReportInvestmentIndustryDO> entityList = investmentIndustry.stream()
                    .map(ReportInvestmentIndustryDTO::toEntity).collect(Collectors.toList());
            this.investmentIndustryMapper.insert(entityList);
        }
    }
}
