package com.shining.cloud.controller.report;

import cn.hutool.core.date.DateUtil;
import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.sso.pojo.user.User;
import com.shining.cloud.enums.information.EventMsgCodeEnum;
import com.shining.cloud.pojo.information.EventInfo;
import com.shining.cloud.pojo.report.ReportData;
import com.shining.cloud.service.report.ShiningReportService;
import com.shining.cloud.serviceImpl.report.ShiningReportServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/report")
@Api(tags = "报告查询接口")
public class ShiningReportController {
    @Autowired
    private ShiningReportService shiningReportService;

    @PostMapping("reportList")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "systemType", value = "系统类型，1-暂态，2-稳态", required = true, paramType = "query")
            , @ApiImplicitParam(name = "reportType", value = "报告类型，1-周报，2-月报，3-季报，4-年报，5-自定义报告", required = true, paramType = "query")
            , @ApiImplicitParam(name = "lineIndex", value = "监测点ID", paramType = "query")
            , @ApiImplicitParam(name = "startTime", value = "查询的开始时间", paramType = "query")
            , @ApiImplicitParam(name = "endTime", value = "查询的结束时间", paramType = "query")
            , @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query")
            , @ApiImplicitParam(name = "num", value = "每页数量", required = true, paramType = "query")})
    @ApiOperation(value = "获取报告入口", notes = "获取报告列表", response = ResponseData.class)
    public ResponseData reportList(@RequestParam("userId") String userId, @RequestParam("systemType") Integer systemType
            , @RequestParam("reportType") Integer reportType, @RequestParam("page") Integer page, @RequestParam("num") Integer num
            , @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("lineIndex") Long lineIndex) {
        ResponseData responseData = new ResponseData();

        /********************************************************************************
         默认：
         月报，查前三个月；周报，查当前月；所有监测点
         自定义：
         没有监测点，查询符合条件所有监测点信息
         没有时间按默认的时间段查询
         开始时间或者结束时间落在报告起止时间内的都列举出来
         *******************************************************************************/

        try {
            List<ReportData> list = shiningReportService.reportList(userId, systemType, reportType, page, num, lineIndex, startTime, endTime);
            responseData.setResultCode(ReturnCode.RETURN_SUCCESS);
            responseData.setMsgCode(EventMsgCodeEnum.REPORTLIST_SUCCESS.getCode());
            responseData.setData(list);
            responseData.setMsg(EventMsgCodeEnum.REPORTLIST_SUCCESS.getMsg());
        } catch (Exception e) {
            responseData.setResultCode(ReturnCode.RETURN_FAIL);
            responseData.setMsgCode(EventMsgCodeEnum.REPORTLIST_FAIL.getCode());
            responseData.setData(new ArrayList<>());
            responseData.setMsg(EventMsgCodeEnum.REPORTLIST_FAIL.getMsg());
        }

        return responseData;
    }

    @PostMapping("customReport")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "lineIndex", value = "监测点ID", paramType = "query")
            , @ApiImplicitParam(name = "systemType", value = "系统类型，1-暂态，2-稳态", required = true, paramType = "query")
            , @ApiImplicitParam(name = "startTime", value = "查询的开始时间", paramType = "query")
            , @ApiImplicitParam(name = "endTime", value = "查询的结束时间", paramType = "query")
            , @ApiImplicitParam(name = "year", value = "年份", paramType = "query")
            , @ApiImplicitParam(name = "quarter", value = "季度", paramType = "query")})
    @ApiOperation(value = "自定义时间报告申请入口", notes = "自定义报告申请", response = ResponseData.class)
    public ResponseData customReport(@RequestParam("userId") String userId, @RequestParam("systemType") Integer systemType
            , @Param("lineIndex") Long lineIndex, @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("year") Integer year, @Param("quarter") Integer quarter, HttpServletRequest request) {
        ResponseData responseData = new ResponseData();
        Date start;
        Date end;

        try {
            if (year == null) {
                start = new Date(startTime);
                end = new Date(endTime);
            } else if (quarter == 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, year);
                start = DateUtil.beginOfYear(calendar.getTime());

                if (calendar.get(Calendar.YEAR) != Calendar.getInstance().get(Calendar.YEAR)) {
                    end = DateUtil.endOfYear(calendar.getTime());
                } else {
                    end = new Date();
                }
            } else {
                Calendar starttime = Calendar.getInstance();
                starttime.set(Calendar.YEAR, year);
                Calendar endtime = Calendar.getInstance();
                endtime.set(Calendar.YEAR, year);
                switch (quarter) {
                    case 1:
                        starttime.set(starttime.get(Calendar.YEAR), 0, 1, 0, 0, 0);
                        start = starttime.getTime();
                        endtime.set(starttime.get(Calendar.YEAR), 3, 1, 0, 0, 0);
                        end = new Date(endtime.getTime().getTime() - 1000);
                        break;
                    case 2:
                        starttime.set(starttime.get(Calendar.YEAR), 3, 1, 0, 0, 0);
                        start = starttime.getTime();
                        endtime.set(starttime.get(Calendar.YEAR), 6, 1, 0, 0, 0);
                        end = new Date(endtime.getTime().getTime() - 1000);
                        break;
                    case 3:
                        starttime.set(starttime.get(Calendar.YEAR), 6, 1, 0, 0, 0);
                        start = starttime.getTime();
                        endtime.set(starttime.get(Calendar.YEAR), 9, 1, 0, 0, 0);
                        end = new Date(endtime.getTime().getTime() - 1000);
                        break;
                    case 4:
                        starttime.set(starttime.get(Calendar.YEAR), 9, 1, 0, 0, 0);
                        start = starttime.getTime();
                        endtime.set(starttime.get(Calendar.YEAR) + 1, 0, 1, 0, 0, 0);
                        end = new Date(endtime.getTime().getTime() - 1000);
                        break;
                    default:
                        starttime.set(starttime.get(Calendar.YEAR), 0, 1, 0, 0, 0);
                        start = starttime.getTime();
                        endtime.set(starttime.get(Calendar.YEAR), 3, 1, 0, 0, 0);
                        end = new Date(endtime.getTime().getTime() - 1000);
                        break;
                }
            }

            start = getDate(start); //去除毫秒，数据库date类型没有保存毫秒
            end = getDate(end);
            int result = shiningReportService.customReport(userId, lineIndex, systemType, start, end, 5, request);

            if (result == ShiningReportServiceImpl.RESULTCODE.RESULT_SUCCESS.getCode()) {
                responseData.setResultCode(ReturnCode.RETURN_SUCCESS);
                responseData.setMsgCode(EventMsgCodeEnum.CUSTOMREPORT_SUCCESS.getCode());
                responseData.setMsg(EventMsgCodeEnum.CUSTOMREPORT_SUCCESS.getMsg());

                return responseData;
            }
        } catch (Exception e) {
            //do something
        }

        responseData.setResultCode(ReturnCode.RETURN_FAIL);
        responseData.setMsgCode(EventMsgCodeEnum.CUSTOMREPORT_FAIL.getCode());
        responseData.setMsg(EventMsgCodeEnum.CUSTOMREPORT_FAIL.getMsg());

        return responseData;
    }

    private Date getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MILLISECOND, 0);
        date = calendar.getTime();
        return date;
    }


    @PostMapping("eventDetailReportApply")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件ID", required = true, paramType = "query")})
    @ApiOperation(value = "暂降事件报告申请入口", notes = "暂降事件报告申请", response = ResponseData.class)
    public ResponseData eventDetailReportApply(@RequestParam("userId") String userId, @RequestParam("eventDetailIndex") String eventDetailIndex, HttpServletRequest request) {
        ResponseData responseData = new ResponseData();

        try {
            int result = shiningReportService.eventDetailReportApply(userId, eventDetailIndex, request);

            if (result == ShiningReportServiceImpl.RESULTCODE.RESULT_NODATA.getCode()) {
                responseData.setResultCode(ReturnCode.RETURN_FAIL);
                responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_NODATA.getCode());
                responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_NODATA.getMsg());

                return responseData;
            }

            if (result == ShiningReportServiceImpl.RESULTCODE.RESULT_SUCCESS.getCode()) {
                responseData.setResultCode(ReturnCode.RETURN_SUCCESS);
                responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_SUCCESS.getCode());
                responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_SUCCESS.getMsg());

                return responseData;
            }
        } catch (Exception e) {
            //do something
        }

        responseData.setResultCode(ReturnCode.RETURN_FAIL);
        responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_FAIL.getCode());
        responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTAPPLY_FAIL.getMsg());

        return responseData;
    }

    @PostMapping("eventDetailReportDownLoad")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
            , @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件ID", required = true, paramType = "query")})
    @ApiOperation(value = "暂降事件报告下载入口", notes = "暂降事件报告下载", response = ResponseData.class)
    public ResponseData eventDetailReportDownLoad(@RequestParam("userId") String userId, @RequestParam("eventDetailIndex") String eventDetailIndex) {
        ResponseData responseData = new ResponseData();
        JSONObject jsonObject = new JSONObject();

        try {
            EventInfo reportData = shiningReportService.getDownLoadPath(userId, eventDetailIndex);

            if (reportData == null) {
                responseData.setResultCode(ReturnCode.RETURN_FAIL);
                responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_FAIL.getCode());
                responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_FAIL.getMsg());
                jsonObject.put("reportUrl", null);
            } else if (reportData.getReportState() == 3) {
                responseData.setResultCode(ReturnCode.RETURN_FAIL);
                responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_ERROR.getCode());
                responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_ERROR.getMsg());
                jsonObject.put("reportUrl", null);
            } else {
                responseData.setResultCode(ReturnCode.RETURN_SUCCESS);
                responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_SUCCESS.getCode());
                responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_SUCCESS.getMsg());
                jsonObject.put("reportUrl", reportData.getReportPath());
            }
        } catch (Exception e) {
            responseData.setResultCode(ReturnCode.RETURN_FAIL);
            responseData.setMsgCode(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_FAIL.getCode());
            responseData.setMsg(EventMsgCodeEnum.EVENTDETAILREPORTDOWNLOAD_FAIL.getMsg());
            jsonObject.put("reportUrl", null);
        }

        responseData.setData(jsonObject);

        return responseData;
    }

    @PostMapping("marketingUserInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")})
    @ApiOperation(value = "获取营销人员信息", notes = "获取营销人员信息", response = ResponseData.class)
    public ResponseData getmarketingUserInfo(@RequestParam("userId") String userId) {
        ResponseData responseData = new ResponseData();
        JSONObject jsonObject = new JSONObject();

        try {
            User user = shiningReportService.getmarketingUserInfo(userId);
            responseData.setResultCode(ReturnCode.RETURN_SUCCESS);
            responseData.setMsgCode(EventMsgCodeEnum.MARKETINGUSERINFO_SUCCESS.getCode());
            responseData.setMsg(EventMsgCodeEnum.MARKETINGUSERINFO_SUCCESS.getMsg());
            jsonObject.put("name", user.getName());
            jsonObject.put("phone", user.getPhone());
            responseData.setData(jsonObject);
        } catch (Exception e) {
            responseData.setResultCode(ReturnCode.RETURN_FAIL);
            responseData.setMsgCode(EventMsgCodeEnum.MARKETINGUSERINFO_FAIL.getCode());
            responseData.setMsg(EventMsgCodeEnum.MARKETINGUSERINFO_FAIL.getMsg());
            responseData.setData(jsonObject);
        }

        return responseData;
    }
}
