package com.pqs9900.controller.DataInfo;
/**
 * @Author denghuajun
 * @Description //TODO 终端流量
 * @Date 2019/3/15 16:28
 **/

import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.PatternRegex;
import com.njcn.utils.PubUtils;
import com.pqs9900.mapper.DataInfo.FlowDetailMapper;
import com.pqs9900.pojo.DataInfo.FlowDetail;
import com.pqs9900.pojo.State.MonthYearDayFlow;
import com.pqs9900.service.DataInfo.FlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("flow")
public class FlowController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(FlowController.class);

    @Autowired
    private FlowDetailMapper flowDetailMapper;

    @Autowired
    private FlowService flowService;

    @RequestMapping(value = "getFlowDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getFlowDetail(HttpServletRequest request, HttpSession session, String startTime, String
            endTime) {
        HttpResult result;
        List<FlowDetail> flowDetails=new ArrayList<>();
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getShow");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "解析前台传递的时间有误");
            return result;
        }
        try {
            flowDetails = flowDetailMapper.getFlowDetail(datePojo.getStartTime(),datePojo.getEndTime());
            result = PubUtils.assignmentResult(flowDetails, 200, "获取终端流量成功");
        } catch (Exception e) {
            logger.error("查询所有终端流量异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取中终端流量错误");
        }

        return result;
    }

    @RequestMapping(value = "getFlowDayDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getFlowDayDetail(HttpServletRequest request, HttpSession session, String startTime, Long
            devIndex) {
        HttpResult result;
        List<MonthYearDayFlow> monthYearDayFlows=new ArrayList<>();
        try {
            monthYearDayFlows = flowService.getFlowDayDetail(devIndex,startTime);
            result = PubUtils.assignmentResult(monthYearDayFlows, 200, "获取月份日均流量成功");
        } catch (Exception e) {
            logger.error("查询所有终端流量异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取中终端流量错误");
        }

        return result;
    }

}
