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.meterreplace.MeterReplacePageDto;
import com.zcxk.rmcp.api.dto.meterreplace.WaterMeterReplaceInputDto;
import com.zcxk.rmcp.api.enums.RmcpErrorEnum;
import com.zcxk.rmcp.api.vo.DeviceCommandVo;
import com.zcxk.rmcp.api.vo.MeterReplaceVo;
import com.zcxk.rmcp.web.service.MeterReplaceLogService;
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
 * @description: 换表管理
 * @date 2021/7/20
 **/
@Controller
@ResponseBody
@RequestMapping("/device/meter/replace")
@Api(tags = "设备维护-换表管理")
public class MeterReplaceController  extends BaseController {

    @Autowired
    private MeterReplaceLogService meterReplaceLogService;

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "查询换表数据")
    public AjaxMessage<MeterReplaceVo> findMeterReplace(@ApiParam(value = "换表ID", required = true) @RequestParam("id") Long id) {
        return AjaxMessage.success(meterReplaceLogService.findMeterReplace(id));
    }


    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询换表列表")
    public AjaxMessage<Pagination<MeterReplaceVo>> pageMeterReplace(@RequestBody @Valid MeterReplacePageDto pageDto) {
        return AjaxMessage.success(meterReplaceLogService.pageMeterReplace(pageDto));
    }

    @RequestMapping(value="export/excel" , method = RequestMethod.GET)
    @ApiOperation(value = "excel导出")
    public void exportExcel(MeterReplacePageDto pageDto, HttpServletResponse response) {
        List<MeterReplaceVo> list = meterReplaceLogService.listMeterReplace(pageDto);
        if (CollectionUtils.isEmpty(list)) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_NOT_FOUND);
        }
        try {
            EasyExcelUtil.excelWrite(response, MeterReplaceVo.class, "换表记录", list);
        } catch (Exception e) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_DOWNLOAD_ERROR);
        }
    }


    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "换表")
    public AjaxMessage submitWaterMeterReplace(@RequestBody @Valid WaterMeterReplaceInputDto waterMeterReplaceInputDto) {
        meterReplaceLogService.submitWaterMeterReplace(waterMeterReplaceInputDto);
        return AjaxMessage.success();
    }
}
