package com.zcxk.rmcp.web.controller;

import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.common.pojo.PageResult;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.utils.DateUtil;
import com.zcxk.core.utils.export.EasyExcelUtil;
import com.zcxk.rmcp.api.dto.meterreadrecord.MeterReadRecordDeviceDto;
import com.zcxk.rmcp.api.dto.meterreadrecord.MeterReadRecordDto;
import com.zcxk.rmcp.api.dto.meterreadrecord.MeterReadRecordUpdateDto;
import com.zcxk.rmcp.api.enums.RmcpErrorEnum;
import com.zcxk.rmcp.api.vo.MeterReadRecordExcelVo;
import com.zcxk.rmcp.api.vo.MeterReadRecordVo;
import com.zcxk.rmcp.api.vo.ReadRateVo;
import com.zcxk.rmcp.core.mongo.MeterReadRecord;
import com.zcxk.rmcp.web.service.MeterReadRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @author Andy
 * @version V1.0
 * @description: 抄表记录
 * @date 2021/7/22
 **/
@Controller
@ResponseBody
@RequestMapping("/meter/read/record")
@Api(tags = "抄表记录")
public class MeterReadRecordController extends BaseController {

    @Resource
    private MeterReadRecordService meterReadRecordService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "抄表记录添加")
    public AjaxMessage<Void> addMeterReadRecord(@RequestBody @Valid MeterReadRecord readRecord) {
        meterReadRecordService.saveMeterReadRecord(readRecord);
        return AjaxMessage.success();
    }

    @RequestMapping(value = "/update/reading", method = RequestMethod.POST)
    @ApiOperation(value = "更新读数")
    public AjaxMessage<Long> updateReadData(@RequestBody @Valid MeterReadRecordUpdateDto readRecord) {
        return AjaxMessage.success(meterReadRecordService.updateReadData(readRecord));
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询抄表记录")
    public AjaxMessage<PageResult> pageMeterReadRecord(@RequestBody @Valid MeterReadRecordDto dto) {
        return AjaxMessage.success(meterReadRecordService.pageMeterReadRecord(dto));
    }

    @RequestMapping(value = "/list/water/consumption", method = RequestMethod.POST)
    @ApiOperation(value = "查询单个设备某段时间的用水量")
    public AjaxMessage<List> listSingleDeviceTimeWaterConsumption(@RequestBody @Valid MeterReadRecordDeviceDto dto) {
        return AjaxMessage.success(meterReadRecordService.listSingleDeviceTimeWaterConsumption(dto));
    }

    @RequestMapping(value="/export/excel" , method = RequestMethod.GET)
    @ApiOperation(value = "excel导出", produces = "application/octet-stream")
    public void exportExcel(MeterReadRecordDto dto, HttpServletResponse response) {
        List<MeterReadRecordVo> list = meterReadRecordService.listMeterReadRecord(dto);
        if (CollectionUtils.isEmpty(list)) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_NOT_FOUND);
        }
        List<MeterReadRecordExcelVo> dataList = new ArrayList<>();
        for (MeterReadRecordVo meterReadRecordVo : list) {
            MeterReadRecordExcelVo excelVo = new MeterReadRecordExcelVo();
            BeanCopyUtils.copyProperties(meterReadRecordVo, excelVo,MeterReadRecordExcelVo.class);
            excelVo.setReadTime(DateUtil.format(meterReadRecordVo.getData().getReadTime(), "yyyy-MM-dd HH:mm:ss"));
            excelVo.setReadStatus(meterReadRecordVo.getData().getReadStatus());
            excelVo.setReadData(meterReadRecordVo.getData().getReadData());
            dataList.add(excelVo);
        }
        try {
            EasyExcelUtil.excelWrite(response, MeterReadRecordExcelVo.class, dto.getCategoryName() +"抄表记录", dataList);
        } catch (Exception e) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_DOWNLOAD_ERROR);
        }
    }

}
