package com.shining.cloud.controller.information;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.SteadyMsgCodeEnum;
import com.shining.cloud.mapper.information.SteadyMsgMapper;
import com.shining.cloud.pojo.information.SteadyMsg;
import com.shining.cloud.pojo.information.SteadyMsgDetail;
import com.shining.cloud.pojo.information.SteadyTarget;
import com.shining.cloud.pojo.information.SteadyUrl;
import com.shining.cloud.service.information.SteadyAssService;

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

/**
 * @description: 稳态消息详情业务层
 * @author: gbl
 * @time: 2019-11-27 
 **/
@RestController
@RequestMapping("/steadymsg")
@Api(tags = "稳态消息接口")
public class SteadyAssController {
	@Autowired
	private SteadyAssService steadyAssService;
	@Autowired
	private SteadyMsgMapper steadyMsgMapper;
	
	/**
	 * @description: 稳态越限列表
	 * @author gbl
	 * @param userId 用户ID
	 * @param page 当前页数
	 * @param num 每页数量
	 */
	@PostMapping("steadyState")
	@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 = ResponseData.class)
    public ResponseMsgData steadyState(String userId, int page, int num, HttpServletRequest request) {
		ResponseMsgData responseData = new ResponseMsgData();
		
        try {
    		List<SteadyMsg> msgs = new ArrayList<>();
    		msgs = steadyAssService.getSteadyState(userId, page, num);
    		
    		Integer unread = steadyMsgMapper.getSteadyUnState(userId);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,SteadyMsgCodeEnum.GETSTATE_SUCCESS.getCode(), SteadyMsgCodeEnum.GETSTATE_SUCCESS.getMsg(),unread, msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,SteadyMsgCodeEnum.GETSTATE_FALL.getCode(), SteadyMsgCodeEnum.GETSTATE_FALL.getMsg(),0, null);
        }
		return responseData;
	}
	
	/**
	 * @description: 稳态越限列表详细信息
	 * @author gbl
	 * @param steadyIndex 稳态越限列表ID
	 */
	@PostMapping("steadyStateInfo")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "steadyIndex", value = "稳态越限列表ID", required = true, paramType = "query")
	})
    @ApiOperation(value = "稳态越限列表详细信息", notes = "稳态越限详细信息", response = ResponseData.class)
    public ResponseData steadyStateInfo(String steadyIndex, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();
		try {
    		List<SteadyMsgDetail> msgs = new ArrayList<>();
    		msgs = steadyAssService.getSteadyDetail(steadyIndex);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,SteadyMsgCodeEnum.GETSTATEINFO_SUCCESS.getCode(), SteadyMsgCodeEnum.GETSTATEINFO_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,SteadyMsgCodeEnum.GETSTATEINFO_FALL.getCode(), SteadyMsgCodeEnum.GETSTATEINFO_FALL.getMsg(), null);
        }
		
		return responseData;
	}

	/**
	 * @description: 稳态越限涉及指标
	 * @author gbl
	 * @param lineIndex 监测点ID
	 * @param timeID 统计时间
	 */
	@PostMapping("steadyTarget")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query")
	})
    @ApiOperation(value = "稳态越限涉及指标", notes = "稳态越限指标", response = ResponseData.class)
    public ResponseData steadyTarget(int lineIndex,Long timeID, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();
		
		try {
    		List<SteadyTarget> msgs = new ArrayList<>();
            String dateTime = PubUtils.timeStamp3Date(timeID,"");
    		Date time = PubUtils.string2Date(dateTime, "yyyy-MM-dd");
    		msgs = steadyAssService.getSteadyTarget(lineIndex,time);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,SteadyMsgCodeEnum.GETTARGET_SUCCESS.getCode(), SteadyMsgCodeEnum.GETTARGET_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,SteadyMsgCodeEnum.GETTARGET_FALL.getCode(), SteadyMsgCodeEnum.GETTARGET_FALL.getMsg(), null);
        }
		return responseData;
	}

	/**
	 * @description: 稳态越限指标图形
	 * @author gbl
	 * @param lineIndex 监测点ID
	 * @param timeID 统计时间
	 * @param typeCode
	 */
	@PostMapping("steadyTargetUrl")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query"),
        @ApiImplicitParam(name = "typeCode", value = "指标编码", required = true, paramType = "query")
	})
    @ApiOperation(value = "稳态越限指标图形", notes = "稳态越限图形", response = ResponseData.class)
    public ResponseData steadyTargetUrl(int lineIndex,Long timeID,int typeCode, HttpServletRequest request,HttpServletResponse response) {
		ResponseData responseData = new ResponseData();

		try {
			SteadyUrl msgs = new SteadyUrl();
            String dateTime = PubUtils.timeStamp3Date(timeID,"");
    		Date time = PubUtils.string2Date(dateTime, "yyyy-MM-dd");
    		msgs = steadyAssService.getSteadyTargetUrl(lineIndex,time,typeCode);
    		if(msgs == null){
    			request.getRequestDispatcher("steadyImage").forward(request,response);
        		msgs = steadyAssService.getSteadyTargetUrl(lineIndex,time,typeCode);
    		}

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,SteadyMsgCodeEnum.GETTARGET_SUCCESS.getCode(), SteadyMsgCodeEnum.GETTARGET_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,SteadyMsgCodeEnum.GETTARGET_FALL.getCode(), SteadyMsgCodeEnum.GETTARGET_FALL.getMsg(), null);
        }
		return responseData;
	}
	
	/**
	 * @description: 生成稳态越限指标图形
	 * @author gbl
	 * @param lineIndex 监测点ID
	 * @param timeID 统计时间
	 * @param typeCode
	 */
	@PostMapping("steadyImage")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "timeID", value = "统计时间", required = true, paramType = "query"),
        @ApiImplicitParam(name = "typeCode", value = "指标编码", required = true, paramType = "query")
	})
    @ApiOperation(value = "生成稳态越限指标图形", notes = "稳态越限图形", response = ResponseData.class)
    public ResponseData steadyImage(int lineIndex,Long timeID,int typeCode, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

		try {
			SteadyUrl msgs = new SteadyUrl();
            String dateTime = PubUtils.timeStamp3Date(timeID,"");
    		Date time = PubUtils.string2Date(dateTime, "yyyy-MM-dd");
    		msgs = steadyAssService.getUrl(request,lineIndex,time,typeCode);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,SteadyMsgCodeEnum.GETTARGET_SUCCESS.getCode(), SteadyMsgCodeEnum.GETTARGET_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,SteadyMsgCodeEnum.GETTARGET_FALL.getCode(), SteadyMsgCodeEnum.GETTARGET_FALL.getMsg(), null);
        }
		return responseData;
	}
}