package cn.ezhizao.project.system.controller;

import cn.ezhizao.common.constant.Constants;
import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.framework.security.LoginBody;
import cn.ezhizao.framework.security.service.SysLoginService;
import cn.ezhizao.framework.security.service.SysPermissionService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.project.system.domain.SysMenu;
import cn.ezhizao.project.system.domain.SysUser;
import cn.ezhizao.project.system.service.ISysMenuService;
import cn.ezhizao.project.tenant.domain.Tenant;
import cn.ezhizao.project.tenant.service.ITenantService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController extends BaseController
{
    @Resource
    private SysLoginService loginService;

    @Resource
    private ITenantService tenantService;

    @Resource
    private ISysMenuService menuService;

    @Resource
    private SysPermissionService permissionService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @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;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @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();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

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

    /**
     * 根据用户名获取租户列表
     * @param userName
     * @return
     */
    @GetMapping("/snms/getTenantList/{userName}")
    public AjaxResult getTenantList(@PathVariable("userName") String userName){
        List<Tenant> tenantList = loginService.getTenantList(userName);
        if(tenantList==null||tenantList.size()==0){
            return error("未找到企业信息！请先注册并认证企业信息");
        }else{
            return success(tenantList);
        }
    }

    /**
     * 根据企业名称 模糊查询租户
     * @param orgName
     * @return
     */
    @GetMapping("/snms/getTenantListByOrgName/{orgName}")
    public AjaxResult getTenantListByOrgName(@PathVariable("orgName") String orgName){
        return success(tenantService.query().like("org_name",orgName).list());
    }


}
