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.dto.WellDeptStaticsDto;
import com.casic.missiles.modular.system.dto.WellParentDeptStaticsDto;
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.util.List;

@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 = "/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 = "/statics/wellStaticsByDept")
    @Permission
    public Object wellStaticsByDept(Long pid) {
        DataScope dataScope = permissionService.getCurrUserDataScope();

        //根据所属派出所统计闸井数量
        List<WellDeptStaticsDto> list = deviceService.wellStaticsByDept(pid, dataScope);

        return ResponseData.success(list);
    }

    @ApiOperation("按分局统计井数量")
    @GetMapping(value = "/statics/wellStaticsByPDept")
    @Permission
    public Object wellStaticsByParentDept() {
        //根据分局统计闸井数量
        List<WellParentDeptStaticsDto> list = deviceService.wellStaticsByParentDept();

        return ResponseData.success(list);
    }

}
