package com.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.user.service.UserLoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/loginLog")
@Api(value = "登录日志")
public class LoginLogController {

    private static Logger log = LoggerFactory.getLogger(LoginLogController.class);

    @Resource
    private UserCenter userCenter;

    @Resource
    private UserLoginLogService userLoginLogService;

    @RequestMapping(value = "/getUserLoginLogList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户登录日志列表")
    public ResponseData getUserLoginLogList(@RequestParam(value = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                log.error("CoresController --> outLogin() error!", "用户身份错误或已过期");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            Map<String, Object> param = new HashMap<>();
            param.put("userId", userInfoModel.getHighUser().getId());

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(userLoginLogService.getUserLoginLogList(param)));

        } catch (Exception e) {
            log.error("loginLog --> getUserLoginLogList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}

