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.*;
import cn.ezhizao.project.system.service.ISysMenuService;
import cn.ezhizao.project.system.service.ISysModuleService;
import cn.ezhizao.project.system.service.ISysVersionMenuService;
import cn.ezhizao.project.system.service.ISysVersionService;
import cn.ezhizao.project.tenant.domain.Tenant;
import cn.ezhizao.project.tenant.service.IBizUserTenantService;
import cn.ezhizao.project.tenant.service.ITenantService;
//import netscape.security.UserTarget; 找不到包 没用到 暂时注释
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;

    @Resource
    private IBizUserTenantService userTenantService;
    @Resource
    private ISysVersionService versionService;
    @Resource
    private ISysVersionMenuService versionMenuService;
    @Resource
    private ISysModuleService moduleService;

    /**
     * 登录方法
     *
     * @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);
        Long tenantId =  getTenantId();
        Tenant tenant = tenantService.getById(tenantId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        ajax.put("tenant", tenant);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        List<SysModule> modules = menuService.selectModuleByUserId(userId);
        modules.forEach(v -> v.setChildren(menuService.buildMenus(v.getMenus())));
        Map<String, Object> routers = new HashMap<>();
        routers.put("routers", menuService.buildMenus(menus));
        routers.put("modules", modules);
        return AjaxResult.success(routers);
    }

    /**
     * 根据用户名获取租户列表
     * @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 userName
     * @return
     */
    @GetMapping("/snms/getTenantId/{userName}")
    public AjaxResult getTenantId(@PathVariable("userName") String userName){
        Tenant tenant = loginService.getTenantByUserName(userName);
        if(tenant==null){
            return error("用户企业信息异常！");
        }
        if(tenant.getId()==null ){
            return error("用户不存在或密码错误！");
        }else{
            return success(tenant.getId());
        }
    }

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


}
