package com.njcn.cloud.controller.report;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.cloud.enums.app.EventMsgCodeEnum;
import com.njcn.cloud.pojo.dto.AppEventInfo;
import com.njcn.cloud.pojo.param.AppReportParam;
import com.njcn.cloud.pojo.vo.ReportVO;
import com.njcn.cloud.service.IAppReportService;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;


@RestController
@RequestMapping("/report")
@Api(tags = "报告查询接口")
@RequiredArgsConstructor
public class ShiningReportController extends BaseController {
    private final IAppReportService shiningReportService;

    @PostMapping("/reportList")
    @ApiOperation(value = "获取报告入口", notes = "获取报告列表")
    public HttpResult<Page> reportList(@RequestBody AppReportParam param) {
        String methodDescribe = getMethodDescribe("reportList");
        /********************************************************************************
         默认：
         月报，查前三个月；周报，查当前月；所有监测点
         自定义：
         没有监测点，查询符合条件所有监测点信息
         没有时间按默认的时间段查询
         开始时间或者结束时间落在报告起止时间内的都列举出来
         *******************************************************************************/
        Page<ReportVO> list = shiningReportService.reportList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/customReport")
    @ApiOperation(value = "自定义时间报告申请入口", notes = "自定义报告申请")
    public HttpResult<Integer> customReport(@RequestBody AppReportParam param) {
        String methodDescribe = getMethodDescribe("customReport");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTime startTime = DateUtil.parse(param.getStartTime());
        DateTime endTime = DateUtil.parse(param.getEndTime());
        if (param.getQuarter() != null) {
            param.setStartTime(sdf.format(DateUtil.beginOfQuarter(startTime)));
            param.setEndTime(sdf.format(DateUtil.endOfQuarter(endTime)));
        } else {
            param.setStartTime(sdf.format(DateUtil.beginOfYear(startTime)));
            param.setEndTime(sdf.format(DateUtil.endOfQuarter(endTime)));
        }
        int i = shiningReportService.customReport(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, i, methodDescribe);

    }

    @PostMapping("/eventDetailReportApply")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件ID", required = true, paramType = "query")})
    @ApiOperation(value = "暂降事件报告申请入口", notes = "暂降事件报告申请")
    public HttpResult<String> eventDetailReportApply(@RequestParam("userId") String userId, @RequestParam("eventDetailIndex") String eventDetailIndex) throws Exception {
        String methodDescribe = getMethodDescribe("eventDetailReportApply");
        if (StrUtil.isBlank(eventDetailIndex) && StrUtil.isBlank(userId)) {
            throw new BusinessException(EventMsgCodeEnum.eventDetailIndex_WRONG.getMsg());
        }
        String result = shiningReportService.eventDetailReportApply(userId, eventDetailIndex);
        if ("2".equals(result)) {
            throw new BusinessException(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_NODATA.getMsg());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @PostMapping("/eventDetailReportDownLoad")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件ID", required = true, paramType = "query")})
    @ApiOperation(value = "暂降事件报告下载入口", notes = "暂降事件报告下载")
    public HttpResult<String> eventDetailReportDownLoad(@RequestParam("userId") String userId, @RequestParam("eventDetailIndex") String eventDetailIndex) {
        String methodDescribe = getMethodDescribe("eventDetailReportDownLoad");
        AppEventInfo downLoadPath = shiningReportService.getDownLoadPath(userId, eventDetailIndex);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, downLoadPath.getReportPath(), methodDescribe);


    }

}
