package com.ruoyi.web.controller.est;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Scheme;
import com.ruoyi.system.domain.SchemeInvestment;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.ISchemeInvestmentService;
import com.ruoyi.system.service.ISchemeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api("收益测算")
@RestController
@RequestMapping("api/investment")
public class InvestmentController extends BaseController {

    @Autowired
    public ISchemeService iSchemeService;
    @Autowired
    public ISchemeInvestmentService iSchemeInvestmentService;

    @PostMapping("selectinvestment")
    @ApiOperation("查询投资详情")
    public AjaxResult selectinvestment(Long id){
        //查询方案
        Scheme scheme=iSchemeService.selectSchemeById(id);
        //根据方案id查询对应的测算数据
        SchemeInvestmentVo schemeInvestmentVo=new SchemeInvestmentVo();
        schemeInvestmentVo.setId(scheme.getId());
        schemeInvestmentVo.setSchemeId(scheme.getSchemeId());
        schemeInvestmentVo.setName(scheme.getName());
        schemeInvestmentVo.setCombination(scheme.getCombination());
        schemeInvestmentVo.setEvaluationDimension(scheme.getEvaluationDimension());
        List<SchemeProductVo> schemeProductVoList=new ArrayList<>();
        schemeProductVoList.add(JSON.parseObject(scheme.getChargingCount(), SchemeProductVo.class));
        schemeProductVoList.add(JSON.parseObject(scheme.getDischargeCount(), SchemeProductVo.class));
        schemeProductVoList.add(JSON.parseObject(scheme.getDirectCurrentCount(), SchemeProductVo.class));
        schemeProductVoList.add(JSON.parseObject(scheme.getTractionHeadCount(), SchemeProductVo.class));
        schemeProductVoList.add(JSON.parseObject(scheme.getProductSemiTrailerCount(), SchemeProductVo.class));

        schemeInvestmentVo.setSchemeProductList(schemeProductVoList);
        SchemeInvestment schemeInvestment=iSchemeInvestmentService.selectSchemeInvestmentBySchemeId(scheme.getSchemeId());
        if (schemeInvestment!=null){
            InvestmentScaleVo in=JSON.parseObject(schemeInvestment.getInvestmentScale(), InvestmentScaleVo.class);
            schemeInvestmentVo.setInvestmentscale(in);
            //营收预测 - JSON字符串转为revenueForecastVo对象
            RevenueForecastVo rf=JSON.parseObject(schemeInvestment.getRevenueForecast(), RevenueForecastVo.class);
            schemeInvestmentVo.setRevenueForecast(rf);
            //收益模型 - JSON字符串转为RevenueModelVo对象
            RevenueModelVo rm=JSON.parseObject(schemeInvestment.getRevenueModel(),RevenueModelVo.class);
            schemeInvestmentVo.setRevenueModelVo(rm);
        }
        return AjaxResult.success(schemeInvestmentVo);
    }

    /**
     * 存储投资测算数据
     */
    @PostMapping("insertschemeinvestment")
    @ApiOperation("存储投资测算数据")
    public AjaxResult insertschemeinvestment(String schemeId,SchemeInvestment schemeInvestment){
        //获取方案id
        schemeInvestment.setSchemeId(schemeId);
        return AjaxResult.success(iSchemeInvestmentService.insertSchemeInvestment(schemeInvestment));
    }
}
