package com.pqs9900.controller.log;

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

import javax.servlet.http.HttpServletRequest;

import com.njcn.enums.LogTypeEnum;
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 com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.mapper.user.UserMapper;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.log.UserLogDetail;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;

@Controller
@RequestMapping(value = "log")
public class UserLogController {
    ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private UserMapper userMapper;

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(UserLogController.class);
    @Autowired
    private UserLogDetailService uls;

    @RequestMapping(value = "getUserLog", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getUserLog(HttpServletRequest request) {
        HttpResult result = new HttpResult();
        try {
            List<List> userLog = uls.getUserLog();
            if (userLog.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到日志信息列表");
            } else {
                result.setCode(200);
                result.setBody(userLog);
                uls.getLog("查询日志信息", "成功",PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(),0);
                result.setMessage("获取日志信息列表成功");
            }
        } catch (Exception e) {
            logger.error("查询日志信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询日志信息失败");
            uls.getLog("查询日志信息", "失败",PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(),0);
        }
        return result;
    }

    @RequestMapping(value = "getUserLogId", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getUserLogId(HttpServletRequest request,String username, String type, String name, String startTime, String endTime) throws JsonProcessingException {
        HttpResult result = new HttpResult();
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getUserLogId");
        try {
            if (!datePojo.getValidity()) {
                result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
                return result;
            }
            List<List> userLog = uls.getUserLogId(username,type, name, datePojo.getStartTime(), datePojo.getEndTime());
            if (userLog.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到日志信息列表");
            } else {
                result.setCode(200);
                result.setBody(userLog);
                uls.getLog("查询日志信息", "成功",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
                result.setMessage("获取日志信息列表成功");
            }
        } catch (Exception e) {
            logger.error("查询日志信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询日志信息失败");
            uls.getLog("查询日志信息", "失败",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }
        return result;
    }

    /***
     * 统计事件类型
     */
    @RequestMapping(value = "geteventtype", method = RequestMethod.POST)
    @ResponseBody
    public String geteventtype(HttpServletRequest request, String startTime, String endTime) throws JsonProcessingException {
        List<UserLogDetail> result = new ArrayList<UserLogDetail>();
        List<List> s = new ArrayList<List>();
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "geteventtype");
        try {
            result = uls.geteventtype(datePojo.getStartTime(), datePojo.getEndTime());
            int j = 0;
            for (int i = 0; i < result.size(); i++) {
                List<String> a = new ArrayList<String>();
                j = j + 1;
                a.add(result.get(i).getType());
                a.add(result.get(i).getNum().toString());
                s.add(a);
            }
        } catch (Exception e) {
            logger.error("查询统计事件异常，异常是" + e.toString());
        }
        return objectMapper.writeValueAsString(s);
    }

    /***
     * 统计详细事件
     */
    @RequestMapping(value = "getdetailtype", method = RequestMethod.POST)
    @ResponseBody
    public String getdetailtype(HttpServletRequest request, String startTime, String endTime) throws JsonProcessingException {
        List<UserLogDetail> result = new ArrayList<UserLogDetail>();
        List<List> s = new ArrayList<List>();
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "geteventtype");
        try {
            result = uls.getdetailtype(datePojo.getStartTime(), datePojo.getEndTime());
            int j = 0;
            for (int i = 0; i < result.size(); i++) {
                List<String> a = new ArrayList<String>();
                j = j + 1;
                a.add(result.get(i).getName());
                a.add(result.get(i).getNum().toString());
                s.add(a);
            }
        } catch (Exception e) {
            logger.error("统计详细事件异常，异常是" + e.toString());
        }
        return objectMapper.writeValueAsString(s);
    }

    /***
     * 统计用户
     */
    @RequestMapping(value = "getUserStatistics", method = RequestMethod.POST)
    @ResponseBody
    public String getUserStatistics(HttpServletRequest request, String startTime, String endTime) throws JsonProcessingException {
        List<UserLogDetail> result = new ArrayList<UserLogDetail>();
        List<List> s = new ArrayList<List>();
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "geteventtype");
        try {
            result = uls.getUserStatistics(datePojo.getStartTime(), datePojo.getEndTime());
            int j = 0;
            for (int i = 0; i < result.size(); i++) {
                List<String> a = new ArrayList<String>();
                j = j + 1;
                a.add(result.get(i).getName());
                a.add(result.get(i).getNum().toString());
                s.add(a);
            }
        } catch (Exception e) {
            logger.error("统计用户异常，异常是" + e.toString());
        }
        return objectMapper.writeValueAsString(s);
    }
    
    /***
     * 统计月份
     */
    @RequestMapping(value = "getdetailmonth", method = RequestMethod.POST)
    @ResponseBody
    public List<UserLogDetail> getdetailmonth(HttpServletRequest request, String startTime, String endTime) throws JsonProcessingException {
        List<UserLogDetail> result = new ArrayList<UserLogDetail>();
        List<List> s = new ArrayList<List>();
        DatePojo datePojo = PubUtils.validateDate(startTime, endTime, "getdetailmonth");
        try {
            result = uls.getdetailmonth(datePojo.getStartTime(), datePojo.getEndTime());
        } catch (Exception e) {
            logger.error("统计详细事件异常，异常是" + e.toString());
        }
        return result;
    }
    
    @ResponseBody
    @RequestMapping(value = "getUsername", method = RequestMethod.POST)
    public List<String> getUsername() {
        List<String> names = new ArrayList<>();
        try {
        	names = userMapper.getAllName();
        } catch (Exception e) {
            logger.error("查询日志异常，异常是" + e.toString());
        }
        return names;
    }
    
    @RequestMapping(value = "getWarnInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getWarnInfo(HttpServletRequest request,Long type) {
        HttpResult result = new HttpResult();
        try {
        	List<List> userLog = uls.getWarnInfo(type);
            if (userLog.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到日志信息列表");
            } else {
                result.setCode(200);
                result.setBody(userLog);
                result.setMessage("获取日志信息列表成功");
            }
            uls.getLog("查询日志信息", "成功",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            logger.error("查询日志信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询日志信息失败");
            uls.getLog("查询日志信息", "失败",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }
        return result;
    }

    @RequestMapping(value = "getWarnInfoCounts", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getWarnInfoCounts(Long type) {
        HttpResult result = new HttpResult();
        try {
            Integer counts = uls.getWarnInfoCounts(type);
            if (counts==0) {
                result.setCode(500);
                result.setMessage("日志列表数据为空");
            } else {
                result.setCode(200);
                result.setBody(counts);
                result.setMessage("存在日志列表数据展示");
            }
        } catch (Exception e) {
            logger.error("查询日志信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询日志信息失败");
        }
        return result;
    }
    
    @RequestMapping(value = "getMemoInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getMemoInfo(HttpServletRequest request) {
        HttpResult result = new HttpResult();
        try {
            Integer userLog = uls.getMemoInfo();
            if (userLog == null) {
                result.setCode(500);
                result.setMessage("");
            } else {
                result.setCode(200);
                result.setBody(userLog);
                result.setMessage("获取存储空间成功");
            }
            uls.getLog("查询存储空间", "成功",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            logger.error("查询存储空间异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询存储空间失败");
            uls.getLog("查询存储空间", "失败",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }
        return result;
    }
    
    @RequestMapping(value = "getSjtj", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getSjtj(HttpServletRequest request,String username, String type, String name, String startTime, String endTime) throws JsonProcessingException {
        HttpResult result = new HttpResult();
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getSjtj");
        try {
            if (!datePojo.getValidity()) {
                result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
                return result;
            }
            List<List> userLog = uls.getSjtj(username,type, name, datePojo.getStartTime(), datePojo.getEndTime());
            if (userLog.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到日志统计列表");
            } else {
                result.setCode(200);
                result.setBody(userLog);
                uls.getLog("查询日志统计", "成功",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
                result.setMessage("获取日志统计列表成功");
            }
        } catch (Exception e) {
            logger.error("查询日志统计异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "查询日志统计失败");
            uls.getLog("查询日志统计", "失败",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        }
        return result;
    }
    
    @RequestMapping(value = "expcsv", method = RequestMethod.POST)
    @ResponseBody
    public void expcsv(HttpServletRequest request,String message) {
    	uls.getLog(message, "成功",PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
    }
}
