/*
 * Copyright (c) 2019, PANNK and/or its affiliates. All rights reserved.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package com.pannk.mms.modules.sys.controller;

import com.pannk.mms.common.base.BaseController;
import com.pannk.mms.common.base.Result;
import com.pannk.mms.modules.sys.service.SysUserService;
import com.pannk.mms.modules.sys.vo.LoginForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author：wolf
 * @date：2020/11/2 23:13
 **/
@Api(tags = "系统登录")
@RequestMapping("/sys")
@RestController
public class SysLoginController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户登录
     *
     * @param loginForm 登录对象
     * @return
     */
    @ApiOperation(nickname = "登录", value = "登录", notes = "用户名+密码登录")
    @ApiImplicitParams(@ApiImplicitParam(value = "登录对象", name = "loginForm", required = true, dataTypeClass = LoginForm
            .class))
    @PostMapping("/login")
    public Result login(@RequestBody LoginForm loginForm) {
        return sysUserService.login(loginForm);
    }


    /**
     * 登出
     *
     * @param request 请求
     * @return
     */
    @ApiOperation(nickname = "登出", value = "登出", notes = "用户退出系统")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        sysUserService.logout(getUserId(), request.getHeader("token"));
        return Result.success();
    }
}
