package com.tofly.xrtygis.custom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.xrtygis.custom.entity.dto.BigScreeRealIndexDto;
import com.tofly.xrtygis.custom.service.BigScreenService;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 大屏相关
 *
 * @author 陈浩
 * @date 2021/4/19
 */
@RestController
@RequestMapping("/bigScreen")
@AllArgsConstructor
@Api(tags = "大屏展示")
public class BigScreenController {

    private final BigScreenService bigScreenService;


    @GetMapping("/getParamInfo")
    @ApiOperation("获取水质参数配置信息")
    public ResultRespone getParamInfo(){
        return ResultRespone.success(bigScreenService.getInfo());
    }

    @GetMapping("/getBylistPcid")
    @ApiOperation("根据父级多个ID查看所有子级")
    public ResultRespone getBylistPcid(String pid){
        return ResultRespone.success(bigScreenService.getBylistPcid(pid));
    }

    @GetMapping("/deptAll")
    @ApiOperation("获取部门信息")
    public ResultRespone departmentListAll(){
        return ResultRespone.success(bigScreenService.departmentListAll());
    }

    @GetMapping("/getUserByDepts")
    @ApiOperation("根据部门获取用户")
    public ResultRespone getUserByDepts(String depts){
        return ResultRespone.success(bigScreenService.getUserByDepts(depts));
    }

    @GetMapping("/selectStation")
    @ApiOperation("获取测站信息")
    public ResultRespone selectStation(Long id,String stnm, String stcd,String categoryName){
        return ResultRespone.success(bigScreenService.selectStation(id,stnm,stcd,categoryName));
    }

    @GetMapping("/selectIndex")
    @ApiOperation("查看测站指标信息")
    @ApiImplicitParam(name = "id",value = "可传多个id",required = true,dataType = "string")
    public ResultRespone selectIndex(String ids){
        return ResultRespone.success(bigScreenService.selectIndex(ids));
    }

    @GetMapping("/stationHistory")
    @ApiOperation("实时指标信息-加压站电耗历史")
    public ResultRespone getStationHistory(Page page, String start, String end){
        return ResultRespone.success(bigScreenService.getStationHistory(page, start, end));
    }
    @GetMapping("/waterValHistory")
    @ApiOperation("实时指标信息-获取水量历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scid",value = "水厂id",required = true,dataType = "long"),
            @ApiImplicitParam(name = "param",value = "供水量/取水量 gsl/qsl ",required = true,dataType = "string")
    })
    public ResultRespone getWaterValHistory(String start, String end, String param, Long scid){
        return ResultRespone.success(bigScreenService.getWaterValHistory(start, end, param, scid));
    }
    @GetMapping("/consumeHistory")
    @ApiOperation("实时指标信息-获取药耗历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "一厂pac：pac_one，二厂pac：pac_two,一厂食盐：sy_one，二厂液氯：yl_two ", required = true, dataType = "string")
    })
    public ResultRespone getConsumeHistory( String start, String end, String param){
        return ResultRespone.success(bigScreenService.getConsumeHistory( start, end, param));
    }
    @GetMapping("/indexHistory")
    @ApiOperation("实时指标信息-获取瞬时指标历史")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "param",value = "指标编号",required = true,dataType = "string")
    )
    public ResultRespone getIndexHistory(Page page,String start, String end, String param){
        return ResultRespone.success(bigScreenService.getIndexHistory(page, start, end, param));
    }

    @GetMapping("/powerHistory")
    @ApiOperation("实时指标信息-获取电耗历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "0:一厂电耗  1：二厂电耗",required = true,dataType = "string"),
    })
    public ResultRespone getPowerHistory(String start,String end,String code){
        return ResultRespone.success(bigScreenService.getPowerHistory(start, end, code));
    }


    @PostMapping("/realTimeInfo")
    @ApiOperation("大屏-实时指标信息")
    public ResultRespone realTimeIndexInfo(@RequestBody BigScreeRealIndexDto dto){
        return ResultRespone.success(bigScreenService.realTimeIndexInfo(dto));
    }

    @GetMapping("/hourWaterHistory")
    @ApiOperation("实时指标信息--小时水量历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code" ,value = "0：一厂  1：二厂",required = true,dataType = "string"),
            @ApiImplicitParam(name = "param" ,value = "0：取水量  1：供水量",required = true,dataType = "string"),
    })
    public ResultRespone hourWaterHistory(Page page,String code,String param,String start,String end){
        return ResultRespone.success(bigScreenService.hourWaterHistory(page, code, param, start, end));
    }

    @GetMapping("/waterVolumeHistory")
    @ApiOperation("实时指标信息--查询供售水历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start" ,value = "开始时间 yyyy" ,dataType = "string"),
            @ApiImplicitParam(name = "end" ,value = "结束时间 yyyy" ,dataType = "string"),
            @ApiImplicitParam(name = "code" ,value = "0:查询供水量  1:查询售水量" , required = true,dataType = "string")
    })
    public ResultRespone  waterVolume(Page page, String start, String end, String code){
        return ResultRespone.success(bigScreenService.waterVolume(page, start, end, code));
    }


    @GetMapping("/getWarn")
    @ApiOperation("获取实时监控信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "code",value = "为空查询全部，0：待确认  1：确认")
    )
    public ResultRespone getWarn(String code){
        return ResultRespone.success(bigScreenService.getWarn(code));
    }

    @GetMapping("/getWarnType")
    @ApiOperation("获取报警类型")
    public ResultRespone getWarnType(){
        return ResultRespone.success(bigScreenService.getWarnType());
    }
    /**
     * 监控历史查询
     */
    @GetMapping("/warnHistory")
    @ApiOperation("查询监控历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "报警类型",dataType = "string"),
            @ApiImplicitParam(name = "start", value = "开始时间(精确到秒)不传默认当前时间的前三个月", dataType = "string"),
            @ApiImplicitParam(name = "end", value = "结束时间(精确到秒)",  dataType = "string"),
            @ApiImplicitParam(name = "isclear", value = "0:未消除 1：消除",  dataType = "string"),
            @ApiImplicitParam(name = "isconfirm", value = "0：未确认  1：确认",  dataType = "string")
    })
    public ResultRespone getWarnHistory(Page page, String type, String start, String end,String isclear,String isconfirm) {
        return ResultRespone.success(bigScreenService.getWarnHistory(page, type, start, end,isclear,isconfirm));
    }

    /**
     * 获取产销差
     */
    @GetMapping("/getNrw")
    @ApiOperation("获取产销差")
    public ResultRespone getNrw() {
        return ResultRespone.success(bigScreenService.getNrw());
    }

    /**
     * 更多产销差信息
     */
    @GetMapping("/nrwMore")
    @ApiOperation("更多产销差信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year",value = "年份",required = true,dataType = "integer"),
            @ApiImplicitParam(name = "code",value = "0:月份 1：季度 2:近三年",required = true,dataType = "integer")
    })
    public ResultRespone nrwMore(String year,Integer code){
        return ResultRespone.success(bigScreenService.nrwMore(year,code));
    }

    /**
     * 获取漏损率
     */
    @GetMapping("/getLeakage")
    @ApiOperation("获取漏损率")
    public ResultRespone getLeakage() {
        return ResultRespone.success(bigScreenService.getLeakage());
    }

    /**
     * 更多漏损率信息
     */
    @GetMapping("/leakageMore")
    @ApiOperation("更多漏损率信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year",value = "年份",required = true,dataType = "integer"),
            @ApiImplicitParam(name = "code",value = "0:月份 1：季度 2:近三年",required = true,dataType = "integer")
    })
    public ResultRespone leakageMore(String year,Integer code){
        return ResultRespone.success(bigScreenService.leakageMore(year,code));
    }

    /**
     * 获取供水量售水量
     */
    @GetMapping("/getSupSellWater")
    @ApiOperation("获取供水量售水量")
    public ResultRespone getSupSellWater() {
        return ResultRespone.success(bigScreenService.getSupSellWater());
    }

    /**
     * 供水量售水量更多信息
     */
    @GetMapping("/supSellWaterMore")
    @ApiOperation("供水量售水量更多信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year",value = "年份",required = true,dataType = "string"),
            @ApiImplicitParam(name = "code",value = "0:月份 1：季度 2:近三年",required = true,dataType = "integer")
    })
    public ResultRespone supSellWaterMore(String year,Integer code){
        return ResultRespone.success(bigScreenService.supSellWaterMore(year, code));
    }

    /**
     * 获取单变
     */
    @GetMapping("/getFixed")
    @ApiOperation("获取单变")
    public ResultRespone getFixed(){
        return ResultRespone.success(bigScreenService.getFixed());
    }

    /**
     * 单变更多信息
     */
    @GetMapping("/fixedMore")
    @ApiOperation("单变更多信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "years",value = "年份可传多个",required = true,dataType = "string"),
            @ApiImplicitParam(name = "code",value = "0:月份 1：季度 2:近三年",required = true,dataType = "integer"),
            @ApiImplicitParam(name = "type",value = "0:一厂  1:二厂  2:全部",required = true,dataType = "string")
    })
    public ResultRespone fixedMore(String years,Integer code,String type){
        return ResultRespone.success(bigScreenService.newfixedMore(years, code,type));
    }

    /**
     * 获取多个测站信息
     */
    @GetMapping("/getSiteInfos")
    @ApiOperation("获取多个测站信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "stids", value = "测站id，多个以','分隔", required = true, dataType = "string")
    )
    public ResultRespone getSiteInfos(String stids) {
        return ResultRespone.success(bigScreenService.getSiteInfos(stids));
    }

    /**
     * 获取水量信息
     */
    @GetMapping("/getWaterVolume")
    @ApiOperation("获取水量信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "itcds",value = "指标编号，多个‘,’分隔",required = true,dataType = "string")
    )
    public ResultRespone getWaterVolume(String itcds){
        return ResultRespone.success(bigScreenService.getWaterVolume(itcds));
    }

    /**
     * 获取耗量
     */
    @GetMapping("/getConsumption")
    @ApiOperation("获取耗量")
    public ResultRespone getConsumption(){
        return ResultRespone.success(bigScreenService.getConsumption());
    }

    /**
     * 获取指标实时信息
     */
    @GetMapping("/getRealTimeItVal")
    @ApiOperation("获取指标实时信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "itcds",value = "指标编号，多个‘,’分隔",required = true,dataType = "string")
    )
    public ResultRespone getRealTimeItVal(String itcds){
        return ResultRespone.success(bigScreenService.getRealTimeItVal(itcds));
    }

    @GetMapping("/getIndexInfo")
    @ApiOperation("获取历史指标信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stid", value = "测站id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "itnm", value = "指标项", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "0:分页  1:全部", required = true, dataType = "string")
    })
    public ResultRespone getIndexInfo(Page page,Long stid,String itnm,String start,String end,String code ){
        if ("0".equals(code)){
            return ResultRespone.success(bigScreenService.getIndexInfo(page,stid,itnm,start,end));
        }else {
            return ResultRespone.success(bigScreenService.getIndexInfo(stid,itnm,start,end));
        }

    }

    /**
     * 获取视频信息
     */
    @GetMapping("/getVideoInfo")
    @ApiOperation("获取视频信息")
    public ResultRespone getVideoInfo() {
        return ResultRespone.success(bigScreenService.getVideoInfo());
    }

    @GetMapping("/countWarnType")
    @ApiOperation(value = "报警类型统计")
    public ResultRespone countWarnType(){
        return ResultRespone.success(bigScreenService.countWarnType());
    }

    @PostMapping("/artwork")
    @ApiOperation(value = "工艺图接口")
    public ResultRespone artwork(@RequestBody Map<String,String> dto){
        return ResultRespone.success(bigScreenService.artwork(dto));
    }

    @GetMapping("/allRealIndex")
    @ApiOperation(value = "获取全部实时指标信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "scids",value = "测站id，多个‘，’分隔",required = true,dataType = "string")
    )
    public ResultRespone getAllRealIndex(String scids){
        return ResultRespone.success(bigScreenService.getAllRealIndex(scids));
    }

    @GetMapping("/handleWarn")
    @ApiOperation(value = "处理实时报警信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wanrIds",value = "实时表id",required = true,dataType = "long"),
            @ApiImplicitParam(name = "id",value = "确认人id",required = true,dataType = "long")
    })
    public ResultRespone handleWarn(String wanrIds,Long id){
        return ResultRespone.success(bigScreenService.handleWarn(wanrIds,id));
    }

    @GetMapping("/handleWarnHistroy")
    @ApiOperation(value = "处理历史报警信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wanrIds",value = "实时表id",required = true,dataType = "long"),
            @ApiImplicitParam(name = "id",value = "确认人id",required = true,dataType = "long")
    })
    public ResultRespone handleWarnHistroy(String wanrIds,Long id){
        return ResultRespone.success(bigScreenService.handleWarnHistory(wanrIds,id));
    }

    @GetMapping("/getWarnInfo")
    @ApiOperation("获取报警信息详情")
    public ResultRespone getWarnInfo(String ids){
        return ResultRespone.success(bigScreenService.getWarnInfo(ids));
    }

    /**
     * 返回图片
     * @param response
     * @return
     */
    @GetMapping(value = "/loadImg")
    @ApiOperation(value = "根据FTP文件地址获取图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "remotePath", value = "图片地址", required = true, dataType = "String"),
    })
    public byte[] loadFileImg(String remotePath,HttpServletResponse response){
        //String filename="/xnkd/demo/pic/QQ图片20191014133817.png";
        String fileType=remotePath.substring(remotePath.lastIndexOf("."));
        if (StringUtil.isNotEmpty(fileType)) {
            fileType = fileType.toLowerCase();
        }
        switch (fileType) {
            case ".jpg":
            case ".jpeg":
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                break;
            case ".png":
                response.setContentType(MediaType.IMAGE_PNG_VALUE);
                break;
            case ".gif":
                response.setContentType(MediaType.IMAGE_GIF_VALUE);
                break;
            default:
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                break;
        }
        byte[] b= FtpUtil.getByteDataforFile(remotePath);
        return b;
    }

    @GetMapping("/sumIndexInfo")
    @ApiOperation("获取部分实时指标合计值")
    @ApiImplicitParam(name = "index",value = "指标，多个‘,’分割",required = true,dataType = "string")
    public ResultRespone getSumIndexInfo(String index){
        return ResultRespone.success(bigScreenService.getSumIndexInfo(index));
    }

    @GetMapping("/warnInfo")
    @ApiOperation("获取报警信息-优化后")
    public ResultRespone getWarnInfo(Page page,String type,String start,String end){
        return ResultRespone.success(bigScreenService.getWarnInfo(page, type, start, end));
    }

    @GetMapping("/getItcdValeHistory")
    @ApiOperation("获取指标历史值")
    public ResultRespone getItcdValeHistory(Page page, String itcd, String start, String end, String state){
        return ResultRespone.success(bigScreenService.getItcdValeHistory(page, itcd, start, end, state));
    }

    @GetMapping("/getItcdInfo")
    @ApiOperation("指标编号获取指标信息")
    public ResultRespone getItcdInfo(String itcd){
        return ResultRespone.success(bigScreenService.getItcdInfo(itcd));
    }
}
