package com.zcxk.rmcp.web.controller;

import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.rmcp.api.dto.BasePageDto;
import com.zcxk.rmcp.api.dto.device.DeviceDataDto;
import com.zcxk.rmcp.api.dto.device.DeviceInputDto;
import com.zcxk.rmcp.api.dto.device.DeviceQueryDto;
import com.zcxk.rmcp.api.vo.*;
import com.zcxk.rmcp.core.mongo.DeviceDataItem;
import com.zcxk.rmcp.web.service.DeviceDataService;
import com.zcxk.rmcp.web.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *
 * @author linqingwei
 * @date 2021-07-19 14:31
 *
 */
@RestController
@RequestMapping("/device")
@Api(tags = "设备管理")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceDataService dataServiceService;


    @ResponseBody
    @GetMapping("/getPage")
    @ApiOperation(value = "获取设备分页")
    public AjaxMessage<Pagination<DeviceVo>> devicePageList(
            @Valid DeviceQueryDto dto, BasePageDto page
    ) {
        Pagination<DeviceVo> pageInfo = deviceService.getPage(dto, page);
        return AjaxMessage.success(pageInfo);
    }


    @ResponseBody
    @GetMapping("/getDeviceListExcel")
    @ApiOperation(value = "导出设备列表excel")
    public void getDeviceListExcel(
            @Valid DeviceQueryDto dto,
            HttpServletResponse httpServletResponse
    ) {
        deviceService.getDeviceListExcel(dto, httpServletResponse);
        //return  AjaxMessage.success("导出执行成功，请稍后在消息查看导出文件并下载。");
    }


    @GetMapping("/detail")
    @ApiOperation(value = "设备详情")
    public AjaxMessage<DeviceDetailVo> deviceDetail(
            @ApiParam(value = "设备id", required = true) @RequestParam(required = true) Long deviceId
    ) {
        DeviceDetailVo device = deviceService.deviceDetail(deviceId);
        return AjaxMessage.success(device);
    }



    @PostMapping("add")
    @ApiOperation(value = "添加设备")
    public AjaxMessage<Void> add(
            @ApiParam(value = "设备", required = true) @RequestBody(required = true) @Valid DeviceInputDto device
    ) {
        deviceService.add(device);
        return AjaxMessage.success();
    }


    @ResponseBody
    @PostMapping("edit")
    @ApiOperation(value = "编辑设备")
    public AjaxMessage<Void> edit(
            @ApiParam(value = "设备", required = true) @RequestBody(required = true) @Valid DeviceInputDto device
    ) {
        deviceService.edit(device);
        return AjaxMessage.success();
    }

    @ResponseBody
    @PostMapping("batchDelete")
    @ApiOperation(value = "批量删除")
    public AjaxMessage<Void> batchDelete(
            @ApiParam(value = "设备id", required = true) @RequestBody(required = true) List<Long> ids
    ) {
        deviceService.batchDelele(ids);
        return AjaxMessage.success();
    }

    @ResponseBody
    @PostMapping("allDelete")
    @ApiOperation(value = "全部删除")
    public AjaxMessage<Void> allDelete(
            @Valid DeviceQueryDto dto
    ) {
        deviceService.allDelele(dto);
        return AjaxMessage.success();
    }

    @GetMapping("/downTemplate")
    @ApiOperation(value = "下载Execl模板", notes = "下载Execl模板")
    public void downTemplate(
            @ApiParam(value = "分类id", required = true) @RequestParam(required = true) Integer categoryId,
            @ApiParam(value = "access_token", required = true) @RequestParam(required = true) String accessToken,
            HttpServletRequest request, HttpServletResponse response
    ) {
        deviceService.downTemplate(categoryId, response);
    }


    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ApiOperation(value = "设备统计")
    public AjaxMessage<DeviceStatisticsVo> statisticsDevice() {
        return AjaxMessage.success(deviceService.statisticsDevice());
    }

    @RequestMapping(value = "/map/data", method = RequestMethod.GET)
    @ApiOperation(value = "地图数据")
    public AjaxMessage<DeviceMapDataVo> mapData(@ApiParam(value = "分类id", required = false) @RequestParam(required = false) Integer categoryId) {
        return AjaxMessage.success(deviceService.listDeviceAndCommunityInfo(categoryId));
    }

    @ResponseBody
    @PostMapping("batchValve")
    @ApiOperation(value = "批量开关阀")
    public AjaxMessage<Void> batchValve(
            @ApiParam(value = "设备id", required = true) @RequestBody(required = true) List<Long> ids,
            @ApiParam(value = "0:关阀 1:开阀", required = true) @RequestParam(required = true) Integer valveStatus
    ) {
        deviceService.batchValve(ids,valveStatus);
        return AjaxMessage.success();
    }

    @PostMapping("/getData")
    @ApiOperation(value = "获取设备数据")
    public AjaxMessage<List<DeviceDataItem>> getData(@RequestBody @Valid DeviceDataDto deviceDataDto) {
        return AjaxMessage.success(dataServiceService.getDataItem(deviceDataDto));
    }

    @GetMapping("/getDataExcel")
    @ApiOperation(value = "导出设备数据excel")
    public void getDataExcel(
            @Valid DeviceDataDto deviceDataDto,
            HttpServletResponse httpServletResponse
    ) {
        dataServiceService.getDataExcel(deviceDataDto, httpServletResponse);

    }



    @ResponseBody
    @GetMapping("/queryDeviceInfoList")
    @ApiOperation(value = "设备查询接口")
    public AjaxMessage<List<DeviceVo>> queryDeviceInfoList(
            @ApiParam(value = "设备编号", required = false) @RequestParam(required = false) String deviceNo,
            @ApiParam(value = "水表电子号", required = false) @RequestParam(required = false) String meterNo
    ){
        List<DeviceVo> list = deviceService.queryDeviceInfoList(deviceNo, meterNo);
        return AjaxMessage.success(list);
    }

}
