package com.dayou.report;

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.dto.*;
import com.dayou.report.format.CalculateDataFormatUtil;
import com.dayou.report.html.builder.CityBaseLandPriceBuilder;
import com.dayou.service.IReportSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 报告中的逻辑计算
 *
 * @author wucl
 * @since 2023-09-05
 */
@Component
public class ActionConvertHandler {

    @Autowired
    private IReportSectionService reportSectionService;

    @Autowired
    private CityBaseLandPriceBuilder cityBaseLandPriceBuilder;


    public String businessCenter54(String dataString){

        try {
            InputData inputData = CalculateDataFormatUtil.calculateJsonStringFormat(dataString);
            List<Params> params = inputData.getParams();
            List<InputTableData> inputTableData = inputData.getInputTableData();
            Long sectionId = inputData.getSectionId();
            String result = cityBaseLandPriceBuilder.businessCenter54(inputTableData, sectionId, params);
            if (StrUtil.isNotBlank(result)){
                reportSectionService.update(new LambdaUpdateWrapper<ReportSection>().eq(BaseEntity::getId,sectionId).set(ReportSection::getCalculateData,dataString));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"计算输出存在错误，请检查后再试。");
            return null;
        }
    }
}

