package com.cpop.oam.business.controller.backstage;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.cpop.core.base.entity.LoginSuccess;
import com.cpop.core.base.entity.R;
import com.cpop.core.base.enums.OperationLogEnum;
import com.cpop.core.constant.Constants;
import com.cpop.core.service.OperationLogService;
import com.cpop.core.utils.MessageUtils;
import com.cpop.core.utils.ServletUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.ip.IpUtils;
import com.cpop.oam.business.bo.LoginForm;
import com.cpop.oam.business.service.LoginService;
import com.cpop.oam.business.vo.loginInfo.SysStaffLoginInfo;
import com.cpop.oam.business.vo.LoginUserInfoVo;
import com.cpop.oam.business.vo.MenuRouteVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author DB
 * @createTime 2023/09/10 11:13
 * @description 系统登陆
 */
@Tag(name = "系统登录模块")
@RestController
@RequestMapping("/backstage")
public class BackstageLoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 系统登录
     * @author DB
     * @since 2023/12/2
     * @return R<LoginSuccess>
     */
    @Operation(summary="系统登录")
    @PostMapping("/login")
    public R<LoginSuccess> login(@RequestBody LoginForm loginForm) {
        SysStaffLoginInfo login = loginService.login(loginForm);
        // 第1步，先登录上
        StpUtil.login(login.getId(),"PC");
        SaSession session = StpUtil.getSession();
        session.set("id", login.getId())
                .set("loginIp", IpUtils.getIpAddr(ServletUtils.getRequest()))
                .set("username", login.getUsername())
                .set("userId", login.getUserId())
                .set("name", login.getName())
                .set("avatar", login.getAvatar() == null ? "" : login.getAvatar())
                .set("userType", login.getUserType())
                .set("phone", login.getPhone())
                .set("roleId", login.getRoleId())
                .set("isSuper", login.getIsSuper());
        //添加操作日志
        SpringUtils.getBean(OperationLogService.class).insertOperationLog(200, OperationLogEnum.SYSTEM_LOGIN, session, null);
        // 第2步，获取 Token  相关参数
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return R.ok(new LoginSuccess(tokenInfo.getLoginId().toString(), login.getUserId(), tokenInfo.getTokenValue()));
    }

    /**
     * 系统注销
     * @author DB
     * @since 2023/12/2
     * @return SaResult
     */
    @Operation(summary="系统注销")
    @GetMapping("/logout")
    public R<Void> logout() {
        SpringUtils.getBean(OperationLogService.class).insertOperationLog(Constants.SUCCESS, OperationLogEnum.SYSTEM_LOGOUT, StpUtil.getSession(), MessageUtils.message("i18n_operationLog_systemLogout"));
        StpUtil.logout();
        return R.ok();
    }

    /**
     * @author LOST.yuan
     * @Description 获取登录用户详情
     * @date 14:52 2022/9/7
     * @return {@link R<LoginUserInfoVo>}
     **/
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取登录用户详情")
    public R<LoginUserInfoVo> getUserInfo() {
        return R.ok(loginService.getUserInfo());
    }

    /**
     * @author LOST.yuan
     * 获取权限码
     * @since 14:52 2022/9/7
     * @return {@link List<String>}
     **/
    @Operation(summary = "获取权限码")
    @GetMapping("/getPermCode")
    public R<Set<String>> getPermCode(){
        Set<String> list = loginService.getPermCode();
        return R.ok(list);
    }

    /**
     * @author LOST.yuan
     * 获取菜单列表
     * @since 14:52 2022/9/7
     **/
    @Operation(summary = "获取菜单列表")
    @GetMapping("/getMenuList")
    public R<List<MenuRouteVo>> getMenuList(){
        List<MenuRouteVo> list = loginService.getSysMenuList();
        return R.ok(list);
    }
}
