package com.shining.cloud.controller.message;

import com.njcn.enums.app.ReturnCode;
import com.njcn.enums.app.UserCodeEnum;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.utils.PubUtils;
import com.shining.cloud.service.information.EventMsgService;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author hongawen
 * @date: 2019/11/11 14:51
 */
@RestController
@RequestMapping("/msg")
@Api(tags = "内部消息推送")
public class EventMsgPushController {

    private static final Logger logger = LoggerFactory.getLogger(EventMsgPushController.class);

    @Autowired
    private EventMsgService eventMsgService;


    /**
     * 暂态消息推送
     */
    @PostMapping("sendEventMsg")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventDetailIndex", value = "暂降索引", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lineId", value = "监测点索引", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态消息推送", notes = "暂态消息推送", response = ResponseData.class)
    public ResponseData sendEventMsg(String eventDetailIndex, int lineId, HttpServletRequest request) {
        logger.error("推送暂降事件：事件索引：{},监测点ID为：{}",eventDetailIndex,lineId);
        ResponseData responseData;
        if (StringUtils.isEmpty(eventDetailIndex) || StringUtils.isEmpty(lineId)) {
            return PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.INVALID_PARAMETER.getCode(), UserCodeEnum.INVALID_PARAMETER.getMsg(), null);
        }
        try {
            eventMsgService.sendEventMsg(eventDetailIndex, lineId,request);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, UserCodeEnum.EVENT_MSG_SUCCESS.getCode(), UserCodeEnum.EVENT_MSG_SUCCESS.getMsg(), null);
        } catch (Exception e) {
            logger.error("暂降消息推送，异常为：" + e.getMessage());
            if (e.getMessage().length() < 10) {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.getCodeByMsg(e.getMessage()), e.getMessage(), null);
            } else {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.EVENT_MSG_FAIL.getCode(), UserCodeEnum.EVENT_MSG_FAIL.getMsg(), null);
            }
        }
        return responseData;
    }

    /**
     * 稳态消息推送
     */
    @PostMapping("sendSteadyMsg")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIndex", value = "用户索引", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lineNum", value = "监测点个数", required = true, paramType = "query"),
    })
    @ApiOperation(value = "稳态消息推送", notes = "稳态消息推送", response = ResponseData.class)
    public ResponseData sendSteadyMsg(String userIndex, Long timeID, Integer lineNum) {
        ResponseData responseData;
        logger.error("稳态消息推送：用户索引：{},统计时间：{}，监测点个数：{}",userIndex,timeID,lineNum);
        if (StringUtils.isEmpty(userIndex) || StringUtils.isEmpty(timeID) || StringUtils.isEmpty(lineNum)) {
            return PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.INVALID_PARAMETER.getCode(), UserCodeEnum.INVALID_PARAMETER.getMsg(), null);
        }
        try {
            String msgIndex = eventMsgService.sendSteadyMsg(userIndex, timeID, lineNum);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msgIndex", msgIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, UserCodeEnum.STEADY_MSG_SUCCESS.getCode(), UserCodeEnum.STEADY_MSG_SUCCESS.getMsg(), jsonObject);
        } catch (Exception e) {
            logger.error("稳态消息推送，异常为：" + e.getMessage());
            if (e.getMessage().length() < 10) {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.getCodeByMsg(e.getMessage()), e.getMessage(), null);
            } else {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.STEADY_MSG_FAIL.getCode(), UserCodeEnum.STEADY_MSG_FAIL.getMsg(), null);
            }
        }
        return responseData;
    }


    /**
     * 终端消息推送
     */
    @PostMapping("sendTargetMsg")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIndex", value = "用户索引", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devNum", value = "终端个数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "alarmNum", value = "终端警告次数",  paramType = "query"),
            @ApiImplicitParam(name = "comOutNum", value = "通讯状态",  paramType = "query"),
            @ApiImplicitParam(name = "flowNum", value = "流量百分比", paramType = "query"),
            @ApiImplicitParam(name = "flag", value = "强制推送标志",required = true,  paramType = "query"),
    })
    @ApiOperation(value = "终端消息推送", notes = "终端消息推送", response = ResponseData.class)
    public ResponseData sendTargetMsg(String userIndex, Long timeID,Integer devNum, Integer alarmNum, Integer comOutNum, Float flowNum, int flag) {
        logger.error("稳态消息推送：用户索引：{},统计时间：{}，终端个数：{}",userIndex,timeID,devNum);
        ResponseData responseData;
        if (StringUtils.isEmpty(userIndex) || StringUtils.isEmpty(timeID) || StringUtils.isEmpty(flag)) {
            return PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.INVALID_PARAMETER.getCode(), UserCodeEnum.INVALID_PARAMETER.getMsg(), null);
        }
        try {
            String msgIndex = eventMsgService.sendTargetMsg(userIndex, timeID, devNum,alarmNum,comOutNum,flowNum,flag);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msgIndex", msgIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, UserCodeEnum.DEVICE_MSG_SUCCESS.getCode(), UserCodeEnum.DEVICE_MSG_SUCCESS.getMsg(), jsonObject);
        } catch (Exception e) {
            logger.error("终端消息推送，异常为：" + e.getMessage());
            if (e.getMessage().length() < 10) {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.getCodeByMsg(e.getMessage()), e.getMessage(), null);
            } else {
                responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, UserCodeEnum.DEVICE_MSG_FAIL.getCode(), UserCodeEnum.DEVICE_MSG_FAIL.getMsg(), null);
            }
        }
        return responseData;
    }

}
