package com.ruoyi.web.controller.common;

import com.ruoyi.bgt.service.IBgtUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.BgtUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.BgtLoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.AppLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.wgz.service.IWgzUserService;
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.HashMap;
import java.util.Map;

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

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

	@Autowired
	private IBgtUserService iBgtUserService;

	@Autowired
	private IWgzUserService wgzUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/app/login")
    public AjaxResult login(@RequestBody BgtLoginBody loginBody)
    {
		Map<String,Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.login(loginBody.getPhone(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getUserTypePrefix());
        ajax.put(Constants.TOKEN, token);
        return AjaxResult.success(ajax);
    }

	/**
	 * 获取用户信息
	 *
	 * @return 用户信息
	 */
	@GetMapping("/app/getInfo")
	public AjaxResult getInfo()
	{
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		String userType = loginUser.getUserType();
		Map<String,Object> ajax = new HashMap<>();
		switch (userType){
			case Constants.BGT:
				BgtUser bgtUser = iBgtUserService.selectUserByPhone(user.getPhonenumber());
				ajax.put("user", bgtUser);
				break;
			case Constants.WGZ:
				ajax.put("user", wgzUserService.userLongIn(user.getPhonenumber()));
				break;
			default:
				break;
		}
		// 角色集合
//		Set<String> roles = permissionService.getRolePermission(user);
		// 权限集合
//		Set<String> permissions = permissionService.getMenuPermission(user);
//		ajax.put("roles", roles);
//		ajax.put("permissions", permissions);
		return AjaxResult.success(ajax);
	}


	@GetMapping("/test")
	public AjaxResult test(){
		return AjaxResult.success("test");
	}

//    /**
//     * 获取用户信息
//     *
//     * @return 用户信息
//     */
//    @GetMapping("getInfo")
//    public AjaxResult getInfo()
//    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
//		Map<String,Object> ajax = new HashMap<>();
//        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
//        return AjaxResult.success(ajax);
//    }
//
//    /**
//     * 获取路由信息
//     *
//     * @return 路由信息
//     */
//    @GetMapping("getRouters")
//    public AjaxResult getRouters()
//    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        // 用户信息
//        SysUser user = loginUser.getUser();
//        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
//        return AjaxResult.success(menuService.buildMenus(menus));
//    }
}
