package com.tofly.xrtygis.custom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.xrtygis.custom.entity.dto.StanderdDto;
import com.tofly.xrtygis.custom.service.CustomScadaAboutService;
import com.tofly.xrtygis.entity.Scadastaitem;
import com.tofly.xrtygis.entity.Scadastation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@AllArgsConstructor
@RequestMapping("/scadaAbout")
@Api(tags = "自定义Scada相关接口")
public class CustomScadaAboutController {

    private final CustomScadaAboutService customScadaAboutService;

    /**
     * 模糊查询站点
     */
    @ApiOperation("模糊查询站点")
    @GetMapping("/likeStnm")
    public ResultRespone likeStnm(String stnm) {
        return ResultRespone.success(customScadaAboutService.likeStnm(stnm));
    }

    /**
     * scada监控历史
     */
    @GetMapping(value = "/viewHistory")
    @ApiOperation("scada监控历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stid", value = "测站id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "zbx", value = "指标项", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "0:分页  1：全部数据", required = true, dataType = "string")
    })
    public ResultRespone viewHistory(Page page, Long stid, String zbx, String startTime, String endTime, String code) {
        if ("0".equals(code)) {
            return ResultRespone.success(customScadaAboutService.viewHistory(page, stid, zbx, startTime, endTime));
        } else {
            return ResultRespone.success(customScadaAboutService.viewHistory(stid, zbx, startTime, endTime));
        }

    }

    /**
     * 获取合格率
     */
    @PostMapping(value = "/getPassRate")
    @ApiOperation("获取合格率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itcd", value = "指标编号", required = true, dataType = "long"),
            @ApiImplicitParam(name = "duration", value = "时长", required = true, dataType = "double")
    })
    public ResultRespone calItemValue(String itcd, String startTime, String endTime, Double duration) {
        return ResultRespone.success(customScadaAboutService.calItemValue(itcd, startTime, endTime, duration));
    }

    /**
     * scada添加测站信息
     */
    @PostMapping("/insertStation")
    @ApiOperation("scada添加测站信息")
    public ResultRespone insertStation(@RequestBody Scadastation scadastation) {
        return ResultRespone.success(customScadaAboutService.insertStation(scadastation));
    }

    /**
     * scada修改测站信息
     */
    @PutMapping("/updateStation")
    @ApiOperation("scada修改测站信息")
    public ResultRespone updateStation(@RequestBody Scadastation scadastation) {
        return ResultRespone.success(customScadaAboutService.updateStation(scadastation));
    }

    /**
     * scada删除测站信息
     */
    @DeleteMapping("/deleteStation")
    @ApiOperation("scada删除测站信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "测站id,多个‘,’拼接", required = true, dataType = "string")
    )
    public ResultRespone deleteStation(String id) {
        return ResultRespone.success(customScadaAboutService.deleteStation(id));
    }

    /**
     * scada查看测站信息
     */
    @ApiOperation("scada查看测站信息")
    @GetMapping("/selectStation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stnm", value = "测站名称", dataType = "string"),
            @ApiImplicitParam(name = "stcd", value = "测站编号", dataType = "string"),
            @ApiImplicitParam(name = "code", value = "0:分页查看  1:查看全部", required = true, dataType = "string")
    })
    public ResultRespone selectStation(Page page, Long id, String stnm, String stcd, String code, String categoryName) {
        if ("0".equals(code)) {
            return ResultRespone.success(customScadaAboutService.selectStation(page, id, stnm, stcd, categoryName));
        } else {
            return ResultRespone.success(customScadaAboutService.selectStation(id, stnm, stcd, categoryName));
        }

    }

    /**
     * scada添加指标信息
     */
    @PostMapping(value = "/insertIndex")
    @ApiOperation("scada添加指标信息")
    public ResultRespone insertIndex(@RequestBody Scadastaitem scadastaitem) {
        return ResultRespone.success(customScadaAboutService.insertIndex(scadastaitem));
    }

    /**
     * scada修改指标信息
     */
    @PutMapping("/updateIndex")
    @ApiOperation("scada修改指标信息")
    public ResultRespone updateIndex(@RequestBody Scadastaitem scadastaitem) {
        return ResultRespone.success(customScadaAboutService.updateIndex(scadastaitem));
    }

    /**
     * scada查看指标信息
     */
    @GetMapping("/selectIndex")
    @ApiOperation("scada查看指标信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "测站id,多个使用','分隔", required = true, dataType = "string")
    )
    public ResultRespone selectIndex(String ids) {
        return ResultRespone.success(customScadaAboutService.selectIndex(ids));
    }

    /**
     * 查看未被绑定指标
     */
    @GetMapping("/viewIndex")
    @ApiOperation("查看未被绑定指标")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "itcd", value = "指标编号", required = true, dataType = "string")
    )
    public ResultRespone viewIndex(String itcd) {
        return ResultRespone.success(customScadaAboutService.viewIndex(itcd));
    }

    /**
     * scada删除指标信息
     */
    @ApiOperation("scada删除指标信息")
    @DeleteMapping("/deleteIndex")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "多个id使用,分隔", required = true, dataType = "string")
    )
    public ResultRespone deleteIndex(String ids) {
        return ResultRespone.success(customScadaAboutService.deleteIndex(ids));
    }

    /**
     * 查询指标名称
     */
    @ApiOperation("查询指标名称")
    @DeleteMapping("/getItnm")
    public ResultRespone getItnm(String stnm) {
        return ResultRespone.success(customScadaAboutService.getItnm(stnm));
    }

    /**
     * 查询指标名称
     */
    @ApiOperation("查询指标名称")
    @GetMapping("/getStnm")
    public ResultRespone getStnm() {
        return ResultRespone.success(customScadaAboutService.getStnm());
    }

    @PostMapping("/stationExport")
    @ApiOperation("测站信息批量导入")
    public ResultRespone stationExport(String dto) {
        return ResultRespone.success(customScadaAboutService.stationExport(dto));
    }

    @GetMapping("/getIndexType")
    @ApiOperation("获取测站指标类别")
    public ResultRespone getIndexType() {
        return ResultRespone.success(customScadaAboutService.getIndexType());
    }

    @PostMapping("/insertStandard")
    @ApiOperation("为测站指标批量添加标准")
    public ResultRespone insertStandard(StanderdDto dto) {
        return ResultRespone.success(customScadaAboutService.insertStandard(dto));
    }

    @GetMapping("/getStItInfo")
    @ApiOperation("获取已配置合格上下限/优质上下限测站和指标")
    public ResultRespone getStationItemInfo() {
        return ResultRespone.success(customScadaAboutService.getStationItemInfo());
    }

    @PostMapping("/trendAnalys")
    @ApiOperation("SCADA信息-趋势分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itcd", value = "指标编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "start", value = "开始时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
            @ApiImplicitParam(name = "end", value = "结束时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
            @ApiImplicitParam(name = "time",value = "超时时间",required = true,dataType = "double"),
            @ApiImplicitParam(name = "itid",value = "需排除id",dataType = "string"),

    })
    public ResultRespone trendAnalys(String itcd, String start, String end, Double time, String itid) {
        return ResultRespone.success(customScadaAboutService.trendAnalys(itcd, start, end, time, itid));
    }

    @PostMapping("/itValHistory")
    @ApiOperation("趋势分析-指标历史值查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itcd", value = "指标编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "start", value = "开始时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itcd", value = "结束时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code",value = "0:不分页  1：分页",required = true,dataType = "string")
    })
    public ResultRespone getValHistory(Page page, String itcd, String start, String end,String code){
        if ("0".equals(code)){
            return ResultRespone.success(customScadaAboutService.getValHistory(itcd, start, end));
        }else {
            return ResultRespone.success(customScadaAboutService.getValHistory(page,itcd, start, end));
        }
    }

    @GetMapping("/itemValExport")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itcd", value = "指标编号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "start", value = "开始时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
            @ApiImplicitParam(name = "itcd", value = "结束时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "string"),
    })
    @ApiOperation("Scada指标历史数据导出")
    public ResultRespone itemValExport(HttpServletResponse response, String itcd, String start, String end){
        return ResultRespone.success(customScadaAboutService.itemValExport(response, itcd, start, end));
    }

    @GetMapping("/itvalQuery")
    @ApiOperation("单个指标历史分页查询")
    public ResultRespone itvalPageQuery(Page page, String itcd, String start, String end){
        return ResultRespone.success(customScadaAboutService.itvalPageQuery(page, itcd, start, end));
    }

    @PutMapping("/setCategoryName")
    @ApiOperation("批量修改测站归类名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stid", value = "测站id,多个‘,’分隔", required = true, dataType = "string"),
            @ApiImplicitParam(name = "name", value = "归类名称", required = true, dataType = "string")
    })
    public ResultRespone setCategoryName(String stid, String name){
        return ResultRespone.success(customScadaAboutService.setCategoryName(stid, name));
    }

    @GetMapping("/getIndexTypeInfo")
    @ApiOperation("获取相应指标类型测站信息")
    public ResultRespone getIndexTypeInfo(Long typeId){
        return ResultRespone.success(customScadaAboutService.getIndexTypeInfo(typeId));
    }
}
