package com.shining.cloud.controller.information;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.RestController;

import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.pojo.commons.ResponseMsgData;
import com.njcn.utils.PubUtils;
import com.shining.cloud.enums.information.DevMsgCodeEnum;
import com.shining.cloud.pojo.information.DevComInfo;
import com.shining.cloud.pojo.information.DevComTJ;
import com.shining.cloud.pojo.information.DevMsgAssInfo;
import com.shining.cloud.pojo.information.DevMsgDetail;
import com.shining.cloud.pojo.information.DevMsgDetailInfo;
import com.shining.cloud.service.information.DevMsgAssService;
import com.shining.cloud.service.information.DevMsgService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @description: 终端消息业务层
 * @author: denghuajun
 * @time: 2019-10-17 14:33:55
 **/
@RestController
@RequestMapping("/deviceMsg")
@Api(tags = "终端状态信息接口")
public class DevMsgController {

    @Autowired
    private DevMsgService devMsgService;

    @Autowired
    private DevMsgAssService devMsgAssService;

    /**
     * 终端消息列表获取
     */
    @PostMapping("deviceMsgList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "num", value = "每页数量", required = true, paramType = "query"),
    })
    @ApiOperation(value = "终端消息列表入口", notes = "终端消息列表", response = ResponseMsgData.class)
    public ResponseMsgData deviceMsgList(String userId, int page, int num, HttpServletRequest request) {
        ResponseMsgData responseData = null;
        if (PubUtils.isBlank(userId)) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.USERID_WRONG.getCode(), DevMsgCodeEnum.USERID_WRONG.getMsg(), 0, null);
            return responseData;
        }

        try {
            List<DevMsgDetail> devMsgDetails = devMsgService.deviceMsgList(userId, page, num);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETDEVMSG_SUCCESS.getCode(), DevMsgCodeEnum.GETDEVMSG_SUCCESS.getMsg(), devMsgService.unState(userId), devMsgDetails);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETDEVMSG_FALL.getCode(), DevMsgCodeEnum.GETDEVMSG_FALL.getMsg(), 0, null);
        }
        return responseData;
    }

    /**
     * 终端列表获取
     */
    @PostMapping("deviceList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devMsgIndex", value = "终端ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "num", value = "每页数量", required = true, paramType = "query"),
    })
    @ApiOperation(value = "终端列表入口", notes = "终端列表", response = ResponseMsgData.class)
    public ResponseMsgData deviceList(String devMsgIndex, int page, int num, HttpServletRequest request) {
        ResponseMsgData responseData = null;

        if (PubUtils.isBlank(devMsgIndex)) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.DEVINDEX_WRONG.getCode(), DevMsgCodeEnum.DEVINDEX_WRONG.getMsg(), 0, null);
            return responseData;
        }

        try {
            List<DevMsgDetailInfo> devMsgDetailInfos = devMsgAssService.deviceList(devMsgIndex, page, num);
            int state = devMsgAssService.state(devMsgIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETDEVLIST_SUCCESS.getCode(), DevMsgCodeEnum.GETDEVLIST_SUCCESS.getMsg(), state, devMsgDetailInfos);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETDEVLIST_FALL.getCode(), DevMsgCodeEnum.GETDEVLIST_FALL.getMsg(), 0, null);
        }
        return responseData;
    }

    /**
     * 终端列表获取
     */
    @PostMapping("devMsgInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devIndex", value = "终端ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query",dataType = "Long"),
    })
    @ApiOperation(value = "终端消息详情列表入口", notes = "终端消息详情", response = ResponseMsgData.class)
    public ResponseData devMsgInfo(int devIndex, Long timeID, HttpServletRequest request) {
        ResponseData responseData = null;
        try {
            String dateTime = PubUtils.timeStamp3Date(timeID,"");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateTime);
            DevMsgAssInfo devMsgAssInfos = devMsgAssService.devMsgInfo(devIndex,date);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETDEVINFO_SUCCESS.getCode(), DevMsgCodeEnum.GETDEVINFO_SUCCESS.getMsg(), devMsgAssInfos);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETDEVINFO_FALL.getCode(), DevMsgCodeEnum.GETDEVINFO_FALL.getMsg(), null);
        }
        return responseData;
    }
    
    /**
     * 终端通讯状态统计
     */
    @PostMapping("devCountTJ")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
    })
    @ApiOperation(value = "终端通讯状态统计入口", notes = "终端通讯状态统计", response = ResponseMsgData.class)
    public ResponseData devCountTJ(String userId, HttpServletRequest request) {
        ResponseData responseData = null;
        try {
        	DevComTJ devMsgAssInfos = devMsgAssService.getDevComTJ(userId);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETDEVCOMTJ_SUCCESS.getCode(), DevMsgCodeEnum.GETDEVCOMTJ_SUCCESS.getMsg(), devMsgAssInfos);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETDEVCOMTJ_FALL.getCode(), DevMsgCodeEnum.GETDEVCOMTJ_FALL.getMsg(), null);
        }
        return responseData;
    }
    
    /**
     * 终端通讯信息
     */
    @PostMapping("devComInfo")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
    })
    @ApiOperation(value = "终端通讯信息入口", notes = "终端通讯", response = ResponseMsgData.class)
    public ResponseData devComnfo(String userId, HttpServletRequest request) {
        ResponseData responseData = null;
        try {
        	List<DevComInfo> devMsgAssInfos = devMsgAssService.getDevComInfo(userId);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,DevMsgCodeEnum.GETDEVCOMINFO_SUCCESS.getCode(), DevMsgCodeEnum.GETDEVCOMINFO_SUCCESS.getMsg(), devMsgAssInfos);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,DevMsgCodeEnum.GETDEVCOMINFO_FALL.getCode(), DevMsgCodeEnum.GETDEVCOMINFO_FALL.getMsg(), null);
        }
        return responseData;
    }
}