package com.casic.missiles.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.config.DeviceServerProperties;
import com.casic.missiles.modular.system.enums.DeviceExportEnum;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.common.annotion.Permission;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.modular.system.dto.AlarmDeptStaticsDto;
import com.casic.missiles.modular.system.dto.DeviceMonitorDataDto;
import com.casic.missiles.modular.system.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "数据管理", description = "数据查询相关接口提供")
public class WellDataController extends ExportController {
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private ICommonPermissionService permissionService;
    @Autowired
    private DeviceServerProperties deviceServerProperties;

    @ApiOperation("井盖数据查询")
    @GetMapping(value = "/welldata/list")
    @Permission
    public Object wellDataList(String keywords, String deptid, String beginTime, String endTime) {
        Page<DeviceMonitorDataDto> page = new PageFactory<DeviceMonitorDataDto>().defaultPage();
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<DeviceMonitorDataDto> list = deviceService.selectWellDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        list.forEach(deviceDto -> {
            deviceDto.setDeptName(permissionService.getDeptName(deviceDto.getDeptid()));
        });
        page.setRecords(list);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("井盖查询数据导出")
    @GetMapping(value = "/welldata/export")
    @Permission
    public void batchExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException {

        Page<DeviceMonitorDataDto> page = new PageFactory<DeviceMonitorDataDto>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<DeviceMonitorDataDto> list = deviceService.selectWellDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);

        list.forEach(deviceDto -> {
            deviceDto.setDeptName(permissionService.getDeptName(deviceDto.getDeptid()));
        });

        super.exportExcel(DeviceExportEnum.WELLDATA_EXPORT, list, response);
    }

    @ApiOperation("液位数据查询")
    @GetMapping(value = "/liquiddata/list")
    @Permission
    public Object liquidDataList(String keywords, String deptid, String beginTime, String endTime) {
        Page<DeviceMonitorDataDto> page = new PageFactory<DeviceMonitorDataDto>().defaultPage();
        if (!page.isOpenSort()) {
            page.setOpenSort(true);
            page.setAsc(false);
            page.setOrderByField("UPTIME");
        }
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<DeviceMonitorDataDto> list = deviceService.selectLiquidDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        list.forEach(deviceDto -> {
            deviceDto.setDeptName(permissionService.getDeptName(deviceDto.getDeptid()));
        });
        page.setRecords(list);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("液位数据导出")
    @GetMapping(value = "/liquiddata/export")
    @Permission
    public void liquidDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException {

        Page<DeviceMonitorDataDto> page = new PageFactory<DeviceMonitorDataDto>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<DeviceMonitorDataDto> list = deviceService.selectLiquidDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);

        list.forEach(deviceDto -> {
            deviceDto.setDeptName(permissionService.getDeptName(deviceDto.getDeptid()));
        });
        super.exportExcel(DeviceExportEnum.LIQUIDDATA_EXPORT, list, response);
    }

    @ApiOperation("燃气数据查询")
    @GetMapping(value = "/gasdata/list")
    //@Permission
    public Object gasDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectGasDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("管盯数据查询")
    @GetMapping(value = "/tubedata/list")
    //@Permission
    public Object tubeDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTubeDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }


    @ApiOperation("管盯数据导出")
    @GetMapping(value = "/tubedata/export")
    //@Permission
    public void tubeDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTubeDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.TUBEDATA_EXPORT, mapList, response);
    }

    @ApiOperation("燃气数据导出")
    @GetMapping(value = "/gasdata/export")
    //@Permission
    public void gasDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectGasDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.GASDATA_EXPORT, mapList, response);
    }

    @ApiOperation("有害气体数据查询")
    @GetMapping(value = "/harmfuldata/list")
    //@Permission
    public Object harmfulDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectHarmfulDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("有害气体数据导出")
    @GetMapping(value = "/harmfuldata/export")
    //@Permission
    public void harmfulDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectHarmfulDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.HARMFULDATA_EXPORT, mapList, response);
    }

    @ApiOperation("温湿度数据查询")
    @GetMapping(value = "/tempdata/list")
    //@Permission
    public Object tempDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTempDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }



    @ApiOperation("温湿度数据查询")
    @GetMapping(value = "/temppressdata/list")
    //@Permission
    public Object tempPressdataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTempPressDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }


    @ApiOperation("温度压力数据导出")
    @GetMapping(value = "/temppressdata/export")
    //@Permission
    public void temppressDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTempPressDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.TEMPPRESSDATA_EXPORT, mapList, response);
    }


    @ApiOperation("温湿度数据导出")
    @GetMapping(value = "/tempdata/export")
    //@Permission
    public void tempDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectTempDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.TEMPDATA_EXPORT, mapList, response);
    }

    @ApiOperation("开挖监测仪数据查询")
    @GetMapping(value = "/digdata/list")
    //@Permission
    public Object digDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectDigDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("开挖监测仪数据导出")
    @GetMapping(value = "/digdata/export")
    //@Permission
    public void digDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectDigDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.DIGDATA_EXPORT, mapList, response);
    }

    @ApiOperation("井盖定位监测仪数据查询")
    @GetMapping(value = "/welllocadata/list")
    //@Permission
    public Object wellLocDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectWellLocDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("井盖定位监测仪数据导出")
    @GetMapping(value = "/welllocadata/export")
    //@Permission
    public void wellLocDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectWellLocDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.WELLLOCDATA_EXPORT, mapList, response);
    }

    @ApiOperation("噪声记录仪数据查询")
    @GetMapping(value = "/noisedata/list")
    //@Permission
    public Object noiseDataList(String keywords, String deptid, String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectNoiseDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("噪声记录仪数据导出")
    @GetMapping(value = "/noisedata/export")
    //@Permission
    public void noiseDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectNoiseDataScopePage(dataScope,page,deptid,beginTime,endTime,keywords);
        super.exportExcel(DeviceExportEnum.NOISEDATA_EXPORT, mapList, response);
    }


    @ApiOperation("根据权属单位统计报警次数")
    @GetMapping(value = "/statics/alarmsByDept")
    @Permission
    public Object alarmsByDept(Long deviceType, String beginTime, String endTime) {
        DataScope dataScope = permissionService.getCurrUserDataScope();

        //根据权属单位查询告警次数及告警设备数
        List<AlarmDeptStaticsDto> list = deviceService.alarmsStaticsByDept(deviceType, beginTime, endTime, dataScope, permissionService.getCurrLoginUser().getDeptId());

        return ResponseData.success(list);
    }


    @ApiOperation("燃气液位一体化数据查询")
    @GetMapping(value = "/liquidGasData/list")
    public Object liquidGasDataList(String keywords, String deptid, String sensorType,String beginTime, String endTime){
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectGasLiquidDataScopePage(dataScope,page,sensorType,deptid,beginTime,endTime,keywords);
        page.setRecords(mapList);
        return ResponseData.success(super.packForBT(page));
    }

    @ApiOperation("燃气液位一体化数据导出")
    @GetMapping(value = "/liquidGasData/export")
    public void liquidGasDataExport(HttpServletResponse response, String keywords, String deptid, String sensorType,String beginTime, String endTime) throws IOException{
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectGasLiquidDataScopePage(dataScope,page,sensorType,deptid,beginTime,endTime,keywords);

        for (Map<String, Object> map : mapList) {

            if ("gas".equals(sensorType)) {
                Double dValue = Double.valueOf(String.valueOf(map.get("data")));
                if(dValue <= 20.0){
                    map.put("data", dValue+ "%LEL");
                }else{
                    DecimalFormat df = new DecimalFormat("0.00");
                    dValue = dValue / 20.0;
                    map.put("data",df.format(dValue) + "%VOL");
                    map.put("data",df.format(dValue) + "%VOL");
                }
            }
        }

        super.exportExcel(DeviceExportEnum.LIQUIDGASDATA_EXPORT, mapList, response);
    }

    @ApiOperation("数据列表查询")
    @GetMapping(value = "/data/list")
    //@Permission
    public Object dataList(String wellCode, String watchType, String beginTime, String endTime){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<Map<String,Object>> mapList = deviceService.selectDataList(dataScope,wellCode,watchType,beginTime,endTime);
        return ResponseData.success(mapList);
    }

}
