package com.zksy.web.controller.system;

import com.zksy.common.constant.Constants;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.core.domain.entity.SysMenu;
import com.zksy.common.core.domain.entity.SysUser;
import com.zksy.common.core.domain.model.LoginBody;
import com.zksy.common.core.domain.model.LoginUser;
import com.zksy.common.utils.SecurityUtils;
import com.zksy.common.utils.result.Response;
import com.zksy.framework.security.moblie.LoginBean;
import com.zksy.framework.web.service.SysLoginService;
import com.zksy.framework.web.service.SysPermissionService;
import com.zksy.system.service.ISysMenuService;
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 java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "用户登录", description = "用户登录desc")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation(value = "用户通过验证码登录")
    @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);
        return ajax;
    }

    @ApiOperation(value = "用户不需要验证码登录")
    @PostMapping("/loginNoCode")
    public AjaxResult loginNoCode(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginNoCode(loginBody.getUsername(), loginBody.getPassword(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

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

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

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation(value = "用户通过手机登录")
    @PostMapping("/loginMobile")
    public Response loginMobile(@RequestBody LoginBean loginBody) {
        SysUser user = new SysUser();
        user.setPhonenumber(loginBody.getMobile());
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        //生成令牌
        String token = loginService.loginMobile(loginUser);
        return Response.success(token);
    }
}
