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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.config.DeviceServerProperties;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.common.annotion.Permission;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dto.AlarmDeptStaticsDto;
import com.casic.missiles.modular.system.dto.DeviceMonitorDataDto;
import com.casic.missiles.modular.system.enums.DeviceExportEnum;
import com.casic.missiles.modular.system.service.IDeviceService;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class WellDataController extends ExportController {
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
    @Autowired
    private IDeviceService deviceService;
    @Resource
    private AbstractPermissionContext permissionService;
    @Autowired
    private DeviceServerProperties deviceServerProperties;

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

    @GetMapping(value = "/welldata/export")
    @Permission
    public void batchExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException {

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

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

        super.exportExcel(DeviceMonitorDataDto.class, list, DeviceExportEnum.WELLDATA_EXPORT.getFileName());
    }

    @GetMapping(value = "/liquiddata/list")
    @Permission
    public Object liquidDataList(String keywords, String deptid, String beginTime, String endTime) {
        Page<DeviceMonitorDataDto> page = PageFactory.defaultPage();
        if (CollectionUtil.isEmpty(page.orders())) {
            OrderItem orderItem = new OrderItem();
            orderItem.setColumn("UPTIME");
            orderItem.setAsc(false);
            page.addOrder(orderItem);
        }
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<DeviceMonitorDataDto> list = deviceService.selectLiquidDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        list.forEach(deviceDto -> {
            deviceDto.setDeptName(permissionService.getDeptService().getDeptName(deviceDto.getDeptid()));
        });
        page.setRecords(list);
        return ResponseData.success(super.packForBT(page));
    }

    @GetMapping(value = "/liquiddata/export")
    @Permission
    public void liquidDataExport(HttpServletResponse response, String keywords, String deptid, String beginTime, String endTime) throws IOException {

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

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

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

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


    /**
     * 管盯数据导出
     *
     * @param response
     * @param keywords
     * @param deptid
     * @param beginTime
     * @param endTime
     * @throws IOException
     */
    @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 = PageFactory.defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        //TODO 导出表头设置
        List<Map<String, Object>> mapList = deviceService.selectTubeDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        super.exportExcelMaps(new ArrayList<>(), mapList, DeviceExportEnum.TUBEDATA_EXPORT.getFileName());
//        super.exportExcel(DeviceExportEnum.TUBEDATA_EXPORT, mapList, response);
    }

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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectGasDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(), mapList, DeviceExportEnum.GASDATA_EXPORT.getFileName());
    }


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

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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectHarmfulDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.HARMFULDATA_EXPORT.getFileName());
    }

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


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


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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectTempPressDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.TEMPPRESSDATA_EXPORT.getFileName());
    }


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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectTempDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.TEMPDATA_EXPORT.getFileName());
    }

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

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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectDigDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.DIGDATA_EXPORT.getFileName());
    }

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

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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectWellLocDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.WELLLOCDATA_EXPORT.getFileName());
    }

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

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

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<Map<String, Object>> mapList = deviceService.selectNoiseDataScopePage(dataScope, page, deptid, beginTime, endTime, keywords);
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.NOISEDATA_EXPORT.getFileName());
    }


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

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

        return ResponseData.success(list);
    }


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

    /**
     * 燃气液位一体化数据导出
     * @param response
     * @param keywords
     * @param deptid
     * @param sensorType
     * @param beginTime
     * @param endTime
     * @throws IOException
     */
    @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 = PageFactory.defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);

        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        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");
                }
            }
        }
        //TODO 导出表头设置
        super.exportExcelMaps(new ArrayList<>(),mapList,DeviceExportEnum.LIQUIDGASDATA_EXPORT.getFileName());
    }

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

}
