package com.ruoyi.web.controller.system;

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

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.utils.WxCodeSessionUtil;
import com.ruoyi.framework.web.domain.AppletLoginForm;
import com.ruoyi.framework.web.domain.AppletSessionDTO;
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 com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;

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

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    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;
    }

    /**
     * 微信登录方法
     *
     * @param  form 登录信息
     * @return 结果
     */
    @PostMapping("/weChatLogin")
    public AjaxResult weChatLogin(@RequestBody AppletLoginForm form)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.weChatLogin(form);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 解码微信Openid
     *
     * @param  form 登录信息
     * @return 结果
     */
    @PostMapping("/weChatOpenid")
    public AjaxResult weChatOpenid(@RequestBody AppletLoginForm form)
    {

        AppletSessionDTO dto = WxCodeSessionUtil.jscode2Session(form);
        return AjaxResult.success("成功",dto);
    }


    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Long schoolId = 0L;


        //formal_teacher_class
        if (roles != null && roles.size() > 0){
            for (String role : roles) {
                if ("teacher".equals(role)){
                    permissionService.getTeacherClass(user);
                }
                if("parents".equals(role)){
                    permissionService.getParentsStudent(user);
                }
            }
        }
        if(user.getDeptId()!=null && user.getDeptId()!=0L){
            schoolId = user.getDeptId();
        }else{
            if(user.getTeacherClass()!=null && user.getTeacherClass().size()>0){
                schoolId = user.getTeacherClass().get(0).getSchoolId();
            }
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("schoolId", schoolId);
        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));
    }
}
