package com.yunfeiyun.agmp.common.web.system.controller;

import com.yunfeiyun.agmp.common.constant.Constants;
import com.yunfeiyun.agmp.common.framework.web.service.ISysMenuService;
import com.yunfeiyun.agmp.common.framework.web.service.PortalService;
import com.yunfeiyun.agmp.common.framework.web.service.SysPermissionService;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.service.SysLoginService;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseMenu;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseUser;
import com.yunfeiyun.agmp.common.core.domain.model.LoginBody;
import com.yunfeiyun.agmp.common.core.domain.model.PortalInfo;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author 杨晓辉
 */

@RestController
public class SysLoginController
{
    private static final Logger log = LoggerFactory.getLogger(SysLoginController.class);

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ResManager resManager;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody  @Valid LoginBody loginBody)
    {
        PortalInfo portalInfo = SpringUtils.getBean(PortalService.class).getSsoInfo();
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getLoginname(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        Map<String, Object> claims = new HashMap<>();
        claims.put("sysId", portalInfo.getAppName());
        claims.put(Constants.TOKEN, token);
        claims.put(Constants.REDIRECT, "");
        log.info("子系统登录返回前端的数据"+ claims.toString());
        ajax.put(Constants.DATA, claims);
        return ajax;
    }

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

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


    /**
     * 得到子系统系统菜单的配置信息
     *  @return 菜单信息
     */
    @GetMapping("/getMenus")
    public AjaxResult getMenus(@NotEmpty(message = "配置参数不能为空") String menucfgId)
    {
        List<IBaseMenu> menus  = menuService.selectMenuTreeByCfgId(menucfgId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }


    /**
     * 得到门户路由信息
     *  @return 菜单信息
     */
    @GetMapping("/getSystem")
    public AjaxResult getPTSRouters()
    {
        List<IBaseMenu> menus  = menuService.selectMenuTreeByUserId("0");
        return AjaxResult.success(menuService.buildMenus(menus));
    }



    /**
     * 得到资源列表
     *  @return 资源列表
     */
    @GetMapping("/getRes")
    public AjaxResult getRes(@NotEmpty(message = "业务参数不能为空") String busId, @NotEmpty(message = "业务类型不能为空") String bizType )
    {
        return AjaxResult.success(resManager.queryRes(busId,bizType));
    }


    /**
     * 删除资源列表
     *  @return 删除资源列表
     */
    @PostMapping("/delRes")
    public AjaxResult delRes(@RequestBody  @Valid List<SysRes> sysRes)
    {
        for(SysRes item: sysRes){
            resManager.deleteRes(item.getResId() , item.getResOssUrl());
        }
        return AjaxResult.success();
    }
}
