package com.dayou.report.html.builder;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.stream.CollectorUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.entity.ReportSection;
import com.dayou.exception.ErrorCode;
import com.dayou.report.calculation.cityPrice.bo.*;
import com.dayou.report.calculation.cityPrice.process.CityPriceCalculateProcess;
import com.dayou.report.dto.InputTableData;
import com.dayou.report.dto.Params;
import com.dayou.report.enums.TableNoEnum;
import com.dayou.report.html.DataToHtmlBO;
import com.dayou.report.html.DataToHtmlConvertor;
import com.dayou.service.IReportSectionService;
import com.dayou.service.impl.ReportSectionServiceImpl;
import com.dayou.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/9/15
 * created with IntelliJ IDEA.
 */
@Component
public class CityBaseLandPriceBuilder  {

    @Autowired
    private CityPriceCalculateProcess cityPriceCalculateProcess;

    @Autowired
    private IReportSectionService reportSectionService;

    @Autowired
    private DataToHtmlConvertor dataToHtmlConvertor;

    public String businessCenter54(List<InputTableData> inputTableData, Long sectionId, List<Params> params) {
        Map<String, List<? extends DataToHtmlBO>> collect = inputTableData.stream().collect(Collectors.toMap(InputTableData::getTableNo, InputTableData::getTableData));
        List<? extends DataToHtmlBO> baseBO = collect.get(TableNoEnum.BUSINESS_CENTER_BASE.getTableNo());
        List<? extends DataToHtmlBO> weightBO = collect.get(TableNoEnum.BUSINESS_CENTER_INDEX_WEIGHT.getTableNo());
        List<BusinessCenterBaseBO> baseBOS = null;
        List<BusinessCenterIndexWeightBO> weightBOS = null;
        if (CollectionUtil.isNotEmpty(baseBO)){
            baseBOS = (List<BusinessCenterBaseBO>) baseBO;
        }
        if (CollectionUtil.isNotEmpty(weightBO)){
            weightBOS = (List<BusinessCenterIndexWeightBO>) weightBO;
        }
        //计算 表542
        List<BusinessCenterIndexWeightBO> businessCenterIndexWeightBOS = cityPriceCalculateProcess.businessCenterIndexWeight(weightBOS);

        //计算 表543
        Map<String, BigDecimal> weights = businessCenterIndexWeightBOS.stream().collect(Collectors.toMap(BusinessCenterIndexWeightBO::getName, BusinessCenterIndexWeightBO::getWeight));
        List<BusinessCenterFormatBO> businessCenterFormatBOS = cityPriceCalculateProcess.businessCenterFormat(baseBOS, weights);

        //计算 表544
        Map<String, BigDecimal> comprehensive = businessCenterFormatBOS.stream().collect(Collectors.toMap(BusinessCenterFormatBO::getName, BusinessCenterFormatBO::getComprehensive));
        List<BusinessCenterLevelBO> businessCenterLevelBOS = cityPriceCalculateProcess.businessCenterLeve(comprehensive);

        //计算 表545
        //Map<String, BigDecimal> scaleMap = businessCenterLevelBOS.stream().collect(Collectors.toMap(BusinessCenterLevelBO::getLevel, BusinessCenterLevelBO::getAvgComprehensiveFormat));
        List<BusinessCenterScaleBO> businessCenterScaleBOS = cityPriceCalculateProcess.businessCenterScale(businessCenterLevelBOS);

        //计算 表546
        List<BusinessCenterCapacityScoreBO> businessCenterCapacityScoreBOS = cityPriceCalculateProcess.businessCenterCapacityScore(businessCenterScaleBOS);

        //计算 表547
        List<BusinessCenterEffectDistanceBo> businessCenterEffectDistanceBos = cityPriceCalculateProcess.businessCenterEffectDistance(businessCenterCapacityScoreBOS);

        //把对象转换成HTML
        String t541 = dataToHtmlConvertor.toHtml(baseBOS, TableNoEnum.BUSINESS_CENTER_BASE.getTableNo());
        String t542 = dataToHtmlConvertor.toHtml(businessCenterIndexWeightBOS, TableNoEnum.BUSINESS_CENTER_INDEX_WEIGHT.getTableNo());
        String t543 =dataToHtmlConvertor.toHtml(businessCenterFormatBOS,TableNoEnum.BUSINESS_CENTER_FORMAT.getTableNo());
        String t544 = dataToHtmlConvertor.toHtml(businessCenterLevelBOS, TableNoEnum.BUSINESS_CENTER_LEVEL.getTableNo());
        String t545 = dataToHtmlConvertor.toHtml(businessCenterScaleBOS, TableNoEnum.BUSINESS_CENTER_SCALE.getTableNo());
        String t546 = dataToHtmlConvertor.toHtml(businessCenterCapacityScoreBOS, TableNoEnum.BUSINESS_CENTER_CAPACITY_SCORE.getTableNo());
        String t547 = dataToHtmlConvertor.toHtml(businessCenterEffectDistanceBos, TableNoEnum.BUSINESS_CENTER_EFFECT_DISTANCE.getTableNo());
        //todo 5-4-8
        String section = reportSectionService.getById(sectionId).getSection();
        if (StrUtil.isNotBlank(section)){
            section = section.replace(TableNoEnum.BUSINESS_CENTER_BASE.getPlaceHolder(), t541)
                    .replace(TableNoEnum.BUSINESS_CENTER_INDEX_WEIGHT.getPlaceHolder(), t542)
                    .replace(TableNoEnum.BUSINESS_CENTER_FORMAT.getPlaceHolder(), t543)
                    .replace(TableNoEnum.BUSINESS_CENTER_LEVEL.getPlaceHolder(), t544)
                    .replace(TableNoEnum.BUSINESS_CENTER_SCALE.getPlaceHolder(), t545)
                    .replace(TableNoEnum.BUSINESS_CENTER_CAPACITY_SCORE.getPlaceHolder(), t546)
                    .replace(TableNoEnum.BUSINESS_CENTER_EFFECT_DISTANCE.getPlaceHolder(), t547);
            return section;
        }else {
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"段落模板中无计算结果占位符，请先检查段落模板再计算。");
            return null;
        }
    }
}
