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.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Scheme;
import com.ruoyi.system.domain.SchemeExploration;
import com.ruoyi.system.domain.vo.DrillingEngineeringVo;
import com.ruoyi.system.domain.vo.SchemeProduct;
import com.ruoyi.system.service.ISchemeInvestmentService;
import com.ruoyi.system.service.ISchemeExplorationService;
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.*;

import java.math.BigDecimal;

@Api("方案")
@RestController
@RequestMapping("api/current")
public class CurrentController extends BaseController {


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

    @PostMapping("selectexploration")
    @ApiOperation("查询勘探数据")
    public AjaxResult selectexploration(){
        //查询userid
        Long userid=getUserId();
        //根据userid查询对应的勘探数据
        SchemeExploration schemeExploration=iSchemeExplorationService.selectSchemeExplorationById(userid);
        return AjaxResult.success(schemeExploration);
    }


    @PostMapping("selectscheme")
    @ApiOperation("查询方案数据列表")
    public AjaxResult selectscheme(Scheme scheme,
                                   @RequestParam(name = "pageNum",required = true) Integer pageNum,
                                   @RequestParam(name = "pageSize",required = true) Integer pageSize){
        //这里需要根据对应的勘测数据进行查询
        TableDataInfo tableDataInfo=iSchemeService.selectSchemeListByPage(scheme,pageNum,pageSize);
        return AjaxResult.success(tableDataInfo);
    }

    @PostMapping("insertexploration")
    @ApiOperation("添加勘探数据")
    public AjaxResult insertexploration(@RequestBody DrillingEngineeringVo drillingEngineeringVo) {
        Long userid=getUserId();
        SchemeExploration schemeExploration = new SchemeExploration();
        // 对象转 JSON 字符串存入 LONGTEXT 字段
        schemeExploration.setExplorationData(JSON.toJSONString(drillingEngineeringVo));
        schemeExploration.setCreateBy(userid);
        return AjaxResult.success(iSchemeExplorationService.insertSchemeExploration(schemeExploration));
    }

    @PostMapping("insertscheme")
    @ApiOperation("添加方案数据")
    public AjaxResult insertscheme(//方案id
                                   String schemeId,
                                   //方案名称
                                   String name,
                                   //方案匹配度
                                   BigDecimal schemePmf,
                                   //设备组合
                                   String combination,
                                   //充电交流舱表
                                   SchemeProduct productCharging,
                                   //直流舱系统表
                                   SchemeProduct productDirectcurrent,
                                   //放电交流舱系统表
                                   SchemeProduct productDischarge,
                                   //半挂车表
                                   SchemeProduct productProductsemitrailer,
                                   //牵引头表
                                   SchemeProduct productTractionhead,
                                   //预计年收益
                                   String annualRevenue,
                                   //预计循环寿命
                                   String cycleLife,
                                   //预计总投资
                                   BigDecimal totalInvestment,
                                   //评估纬度
                                   String evaluationDimension){
        Scheme scheme=new Scheme();
        scheme.setSchemeId(schemeId);
        scheme.setName(name);
        scheme.setSchemePmf(schemePmf);
        scheme.setCombination(combination);
        scheme.setChargingCount(JSON.toJSONString(productCharging));
        scheme.setDischargeCount(JSON.toJSONString(productDischarge));
        scheme.setDirectCurrentCount(JSON.toJSONString(productDirectcurrent));
        scheme.setTractionHeadCount(JSON.toJSONString(productTractionhead));
        scheme.setProductSemiTrailerCount(JSON.toJSONString(productProductsemitrailer));
        scheme.setAnnualRevenue(annualRevenue);
        scheme.setCycleLife(cycleLife);
        scheme.setTotalInvestment(totalInvestment);
        scheme.setEvaluationDimension(evaluationDimension);
        return AjaxResult.success(iSchemeService.insertScheme(scheme));
    }

    @PostMapping("selectschemeid")
    @ApiOperation("查询方案详情")
    public AjaxResult selectschemeid(Long id){
        //查询方案
        Scheme scheme=iSchemeService.selectSchemeById(id);
        //根据对应的方案id查询出对应的值
        return AjaxResult.success(scheme);
    }


    @PostMapping("updateexploration")
    @ApiOperation("修改勘测数据")
    public AjaxResult updateexploration(@RequestBody DrillingEngineeringVo drillingEngineeringVo){
        Long userid=getUserId();
        SchemeExploration schemeExploration = new SchemeExploration();
        // 对象转 JSON 字符串存入 LONGTEXT 字段
        schemeExploration.setExplorationData(JSON.toJSONString(drillingEngineeringVo));
        schemeExploration.setUpdateBy(userid);
        return AjaxResult.success(iSchemeExplorationService.updateSchemeExploration(schemeExploration));
    }


    @PostMapping("updatescheme")
    @ApiOperation("修改方案数据")
    public AjaxResult updatescheme(//方案id
                                   String schemeId,
                                   //方案名称
                                   String name,
                                   //方案匹配度
                                   BigDecimal schemePmf,
                                   //设备组合
                                   String combination,
                                   //充电交流舱表
                                   SchemeProduct productCharging,
                                   //直流舱系统表
                                   SchemeProduct productDirectcurrent,
                                   //放电交流舱系统表
                                   SchemeProduct productDischarge,
                                   //半挂车表
                                   SchemeProduct productProductsemitrailer,
                                   //牵引头表
                                   SchemeProduct productTractionhead,
                                   //预计年收益
                                   String annualRevenue,
                                   //预计循环寿命
                                   String cycleLife,
                                   //预计总投资
                                   BigDecimal totalInvestment,
                                   //评估纬度
                                   String evaluationDimension){
        Scheme scheme=new Scheme();
        scheme.setSchemeId(schemeId);
        scheme.setName(name);
        scheme.setSchemePmf(schemePmf);
        scheme.setCombination(combination);
        scheme.setChargingCount(JSON.toJSONString(productCharging));
        scheme.setDischargeCount(JSON.toJSONString(productDischarge));
        scheme.setDirectCurrentCount(JSON.toJSONString(productDirectcurrent));
        scheme.setTractionHeadCount(JSON.toJSONString(productTractionhead));
        scheme.setProductSemiTrailerCount(JSON.toJSONString(productProductsemitrailer));
        scheme.setAnnualRevenue(annualRevenue);
        scheme.setCycleLife(cycleLife);
        scheme.setTotalInvestment(totalInvestment);
        scheme.setEvaluationDimension(evaluationDimension);
        return AjaxResult.success(iSchemeService.updateScheme(scheme));
    }

}
