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.readrate.ReadRateCountDto;
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.ReadRatePageVo;
import com.zcxk.rmcp.api.vo.ReadRateVo;
import com.zcxk.rmcp.api.vo.StatisticsReadRateVo;
import com.zcxk.rmcp.web.factory.ReadRateFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpResponse;
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("/read/rate")
@Api(tags = "抄表率统计")
public class ReadRateController extends BaseController{

    @Autowired
    private ReadRateFactory readRateFactory;

    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ApiOperation(value = "统计抄表率")
    public AjaxMessage<List> statistics(@ApiParam(value = "统计天数", required = true) @RequestParam("statDaysCode") Integer statDaysCode) {
        if (ReadRateEnum.getReadRateEnum(statDaysCode) == null) {
            return AjaxMessage.success(null);
        }
        return AjaxMessage.success(readRateFactory.getReadRateService(statDaysCode).statReadRateGroupCategory());
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询")
    public AjaxMessage<Pagination> page(@RequestBody @Valid ReadRateDto dto) {
        if (ReadRateEnum.getReadRateEnum(dto.getStatDaysCode()) == null) {
            return AjaxMessage.success(null);
        }
        return AjaxMessage.success(readRateFactory.getReadRateService(dto.getStatDaysCode()).pageReadRate(dto));
    }

    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation(value = "统计抄表总数")
    public AjaxMessage<StatisticsReadRateVo> countReadRate(@RequestBody @Valid ReadRateCountDto dto) {
        if (ReadRateEnum.getReadRateEnum(dto.getStatDaysCode()) == null) {
            return AjaxMessage.success(null);
        }
        return AjaxMessage.success(readRateFactory.getReadRateService(dto.getStatDaysCode()).countReadRate(dto));
    }

    @RequestMapping(value="export/excel" , method = RequestMethod.GET)
    @ApiOperation(value = "excel导出")
    public void exportExcel(ReadRateDto dto, HttpServletResponse response) {
        ReadRateEnum readRateEnum = ReadRateEnum.getReadRateEnum(dto.getStatDaysCode());
        List<ReadRateVo> list = readRateFactory.getReadRateService(dto.getStatDaysCode()).listReadRate(dto);
        if (CollectionUtils.isEmpty(list)) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_NOT_FOUND);
        }
        try {
            EasyExcelUtil.excelWrite(response, ReadRateVo.class, readRateEnum.getMessage() +"抄表率记录", list);
        } catch (Exception e) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_DOWNLOAD_ERROR);
        }
    }
}
