package com.zcxk.rmcp.web.controller;

import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.core.utils.export.EasyExcelUtil;
import com.zcxk.rmcp.api.dto.devicecommand.DeviceCommandPageDto;
import com.zcxk.rmcp.api.dto.readrate.ReadRateDto;
import com.zcxk.rmcp.api.enums.ReadRateEnum;
import com.zcxk.rmcp.api.enums.RmcpErrorEnum;
import com.zcxk.rmcp.api.vo.DeviceCommandVo;
import com.zcxk.rmcp.api.vo.ReadRateVo;
import com.zcxk.rmcp.web.service.DeviceCommandService;
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.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author Andy
 * @version V1.0
 **/
@Controller
@ResponseBody
@RequestMapping("/device/command")
@Api(tags = "设备维护-阀控管理")
public class DeviceCommandController extends BaseController{


    @Autowired
    private DeviceCommandService deviceCommandService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询阀控列表")
    public AjaxMessage<Pagination<DeviceCommandVo>> pageDeviceCommand(@RequestBody @Valid DeviceCommandPageDto deviceCommandPageDto) {
        return AjaxMessage.success(deviceCommandService.pageDeviceCommand(deviceCommandPageDto));
    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询阀控列表")
    public AjaxMessage<DeviceCommandVo> findDeviceCommand(@ApiParam(value = "阀控ID", required = true) @RequestParam("id") Long id) {
        return AjaxMessage.success(deviceCommandService.findDeviceCommand(id));
    }


    @RequestMapping(value="export/excel" , method = RequestMethod.GET)
    @ApiOperation(value = "excel导出")
    public void exportExcel(DeviceCommandPageDto deviceCommandPageDto, HttpServletResponse response) {
        List<DeviceCommandVo> list = deviceCommandService.listDeviceCommand(deviceCommandPageDto);
        if (CollectionUtils.isEmpty(list)) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_NOT_FOUND);
        }
        try {
            EasyExcelUtil.excelWrite(response, DeviceCommandVo.class, "阀控管理记录", list);
        } catch (Exception e) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_DOWNLOAD_ERROR);
        }
    }
}
