package com.ktg.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ktg.common.constant.Constants;
import com.ktg.common.core.domain.AjaxResult;
import com.ktg.common.core.domain.entity.SysMenu;
import com.ktg.common.core.domain.entity.SysUser;
import com.ktg.common.core.domain.model.LoginBody;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.framework.web.service.IscsService;
import com.ktg.framework.web.service.SysLoginService;
import com.ktg.framework.web.service.SysPermissionService;
import com.ktg.system.service.ISysMenuService;
import com.ktg.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IscsService iscsService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("系统用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        SysUser user = iSysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, loginBody.getUsername()));
        ajax.put("nickName", user.getNickName());
        return ajax;
    }

    /**
     * 机柜登录，防止多机柜操作，只允许同时在线一个
     *
     * @param loginBody
     * @return
     */
    @PostMapping("/machineLogin")
    public AjaxResult machineLogin(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.machineLogin(loginBody);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        // 用户卡nfc
        Set<String> userCardList = iscsService.getUserCardList(user.getUserId());
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        ajax.put("userCardList", userCardList);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /*@ApiOperation("系统用户登录-指纹验证登录")
    @PostMapping("/loginByFingerprint")
    public AjaxResult loginByFingerprint(@RequestBody FingerprintLoginBody loginBody) {
        AjaxResult ajaxResult = loginService.loginByFingerprint(loginBody);
        return ajaxResult;
    }*/

    @ApiOperation("系统用户登录-指纹验证登录dat")
    @PostMapping("/loginByFingerprintDat")
    public AjaxResult loginByFingerprintDat(MultipartFile file) throws IOException {
        AjaxResult ajaxResult = loginService.loginByFingerprintDat(file);
        // AjaxResult ajaxResult = loginService.loginByArcFace(file);
        return ajaxResult;
    }

    @ApiOperation("系统用户登录-face登录-opencv")
    @PostMapping("/loginByFace")
    public AjaxResult loginByFace(MultipartFile file) throws IOException {
        AjaxResult ajaxResult = loginService.loginByFace(file);
        return ajaxResult;
    }

    @ApiOperation("系统用户登录-face登录-arcsoft")
    @PostMapping("/loginByArcFace")
    public AjaxResult loginByArcFace(MultipartFile file) throws IOException {
        AjaxResult ajaxResult = loginService.loginByArcFace(file);
        return ajaxResult;
    }
}
