package com.pqs9900.controller.log;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.service.log.UserLoginService;
import com.njcn.utils.PubUtils;
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;

@Controller
@RequestMapping(value = "login")
public class UserLoginController {
    @Autowired
    UserLoginService loginService;
    @Autowired
    UserLogDetailService userLogDetailService;
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(UserLoginController.class);

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