package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.common.util.FileUtils;
import com.huatek.busi.entity.AssessInputData;
import com.huatek.busi.entity.BusiParamTransformer;
import com.huatek.busi.entity.SimulateResult;
import com.huatek.busi.service.ISimulateResultService;
import com.huatek.busi.vo.SimulateResultVo;
import com.huatek.busi.vo.TransformerInputDateVo;
import com.huatek.busi.where.SimulateResultSelectVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.io.File;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags={"仿真结果管理接口"})
@RestController
@RequestMapping("/api/simulateResult")
public class SimulateResultController {

    @Reference(check = false)
    ISimulateResultService simulateResultService;

    @ApiOperation(value = "查询仿真结果列表",httpMethod="POST", notes= "查询仿真结果列表", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/list", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> list(@RequestBody SimulateResultSelectVo simulateResultSelectVo){
        return simulateResultService.list(simulateResultSelectVo);
    }


    @ApiOperation(value = "查询仿真结果数据并用来绘制echarts图",httpMethod="POST", notes= "查询仿真结果数据并用来绘制echarts图", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/resultData", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> resultData(@RequestBody SimulateResultSelectVo simulateResultSelectVo){
        return simulateResultService.resultData(simulateResultSelectVo);
    }



    @ApiOperation(value = "查询套管计算结果列表",httpMethod="POST", notes= "查询套管计算结果列表", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/drivepipeList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> drivepipeList(@RequestBody SimulateResultSelectVo simulateResultSelectVo){
        return simulateResultService.drivepipeList(simulateResultSelectVo);
    }

    @ApiOperation(value = "根据编号查询仿真结果",httpMethod="POST", notes= "根据编号查询仿真结果", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findById", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findById(@RequestParam String id){
        SimulateResult simulateResult = simulateResultService.findById(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", simulateResult);
    }

    @ApiOperation(value = "根据输入数据项计算仿真结果数据",httpMethod="POST", notes= "根据输入数据项计算仿真结果数据", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/calculateByInputItemDate", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> calculateByInputItemDate(@RequestHeader String token, @RequestBody SimulateResultVo simulateResultVo){
        return simulateResultService.calculateByInputItemDate(token, simulateResultVo);
    }

    @ApiOperation(value = "获取CSV文件数据",httpMethod="POST", notes= "获取CSV文件数据", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findCsvData", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findCsvData(@RequestHeader String token, @RequestBody SimulateResultVo simulateResultVo){
        return simulateResultService.findCsvData(token, simulateResultVo);
    }

    @ApiOperation(value = "根据输入数据项计算变压器运行数据",httpMethod="POST", notes= "根据输入数据项计算变压器运行数据", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/calculatetransformerByInputItemDate", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> calculatetransformerByInputItemDate(@RequestHeader String token, @RequestBody TransformerInputDateVo transformerInputDateVo){
        return simulateResultService.calculatetransformerByInputItemDate(token, transformerInputDateVo);
    }

    @ApiOperation(value = "查询主变数据生成结果列表",httpMethod="POST", notes= "查询主变数据生成结果列表", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findOilList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findOilList(@RequestBody SimulateResultSelectVo simulateResultSelectVo){
        return simulateResultService.findOilList(simulateResultSelectVo);
    }

    @ApiOperation(value = "查询断路器输入项",httpMethod="POST", notes= "查询断路器输入项", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findAssessList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findAssessList(){
        return simulateResultService.findAssessList();
    }

    @ApiOperation(value = "查询所有断路器输入项",httpMethod="POST", notes= "查询断路器输入项", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findAssessAllList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findAssessAllList(){
        return simulateResultService.findAssessAllList();
    }

    @ApiOperation(value = "修改断路器输入项权重")
    @PostMapping(value = "/updateAssessList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> updateAssessList(@RequestHeader("token")String token, @RequestBody List<AssessInputData> inputData) {
        int count = simulateResultService.updateAssessList(token, inputData);
        log.debug("busi param gis update: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "根据判断依据查询断路器输入项",httpMethod="POST", notes= "根据判断依据查询断路器输入项", produces = "application/json;charset=utf-8", response= Map.class)
    @GetMapping(value = "/findListByJudgment")
    public Map<String, Object> findListByJudgment(@RequestParam("nameId")  String nameId,
                                                  @RequestParam("num") String num){
        return simulateResultService.findListByJudgment(nameId,num);
    }

    @ApiOperation("下载csv文件")
    @GetMapping(value = "/downloadExcel")
    public Map<String, Object> downloadExcel(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        try {
            String id = params.get("id").toString();
            SimulateResult simulateResult = simulateResultService.findById(id);
            if(simulateResult != null){
                String filePath = simulateResult.getFilePath();
                String fileName = simulateResult.getFileName();

                filePath = simulateResultService.getFilePath(filePath);
                filePath = FileUtils.fileDownload(new File(filePath), fileName, response);
                return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.DOWNLOAD.SUCCESS", filePath);
            }
        }catch (Exception e){
            log.error("下载csv文件异常：{}", e.getMessage());
        }
        return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.DOWNLOAD.FAIL", "");
    }
}
