package com.shining.cloud.controller.statistics;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
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.utils.PubUtils;
import com.shining.cloud.enums.information.StatisticsEnum;
import com.shining.cloud.enums.information.SteadyMsgCodeEnum;
import com.shining.cloud.pojo.statistics.StatisticsData;
import com.shining.cloud.pojo.statistics.StatisticsDetail;
import com.shining.cloud.service.statistics.StatisticsService;

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("/statistics")
@Api(tags = "统计数据接口")
public class StatisticsController {
	@Autowired
	private StatisticsService statisticsService;
	
	/**
	 * @description: 统计数据
	 * @author gbl
	 * @param lineIndex 监测点ID
	 * @param page 当前页数
	 * @param num 每页数量
	 */
	@PostMapping("statistics")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "lineIndex", value = "监测点ID", required = true, 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 statistics(int lineIndex,Long startTime,Long endTime, int page, int num, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();
		
		try {
			String start="";
			String end="";
			if(startTime == null || endTime == null){
		        Calendar calendar1 = Calendar.getInstance();
		        calendar1.setTime(new Date());

		        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
		        String month = sdf1.format(calendar1.getTime());
		        start = month + "-01";
		        end = month + "-31";
			}else{
	            start = PubUtils.timeStamp3Date(startTime,"");
	            end = PubUtils.timeStamp3Date(endTime,"");
			}
    		List<StatisticsData> msgs = new ArrayList<>();
    		msgs = statisticsService.queryData(lineIndex,PubUtils.string2Date(start, "yyyy-MM-dd"),
    				PubUtils.string2Date(end, "yyyy-MM-dd"),page,num);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,StatisticsEnum.GETSTATISTICSDATA_SUCCESS.getCode(), StatisticsEnum.GETSTATISTICSDATA_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,StatisticsEnum.GETSTATISTICSDATA_FALL.getCode(), StatisticsEnum.GETSTATISTICSDATA_FALL.getMsg(), null);
        }
		return responseData;
	}
	
	/**
	 * @description: 统计数据详情
	 * @author gbl
	 * @param lineIndex 监测点ID
	 * @param timeID 统计时间
	 */
	@PostMapping("statisticsDetail")
	@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 statisticsDetail(int lineIndex,Long timeID, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();
		
		try {
    		StatisticsDetail msgs = new StatisticsDetail();
            String dateTime = PubUtils.timeStamp3Date(timeID,"");
    		Date time = PubUtils.string2Date(dateTime, "yyyy-MM-dd");
    		msgs = statisticsService.queryDetail(lineIndex,time);

            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS,StatisticsEnum.GETSTATISTICSLIST_SUCCESS.getCode(), StatisticsEnum.GETSTATISTICSLIST_SUCCESS.getMsg(), msgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL,StatisticsEnum.GETSTATISTICSLIST_FALL.getCode(), StatisticsEnum.GETSTATISTICSLIST_FALL.getMsg(), null);
        }
		
		return responseData;
	}
}