package com.yccrc.web.controller.system;

import java.util.List;
import java.util.Set;
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.alibaba.fastjson.JSON;
import com.yccrc.common.constant.Constants;
import com.yccrc.common.core.domain.AjaxResult;
import com.yccrc.common.core.domain.entity.SysMenu;
import com.yccrc.common.core.domain.entity.SysUser;
import com.yccrc.common.core.domain.model.LoginBody;
import com.yccrc.common.exception.ServiceException;
import com.yccrc.common.utils.SecurityUtils;
import com.yccrc.framework.web.service.SysLoginService;
import com.yccrc.framework.web.service.SysPermissionService;
import com.yccrc.kede.domain.Enterprise;
import com.yccrc.kede.enums.UserTypeEnum;
import com.yccrc.kede.service.IEnterpriseService;
import com.yccrc.system.service.ISysMenuService;
import com.yccrc.system.service.ISysUserService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
@Slf4j
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IEnterpriseService enterpriseService;
    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        
        SysUser user = userService.selectUserByUserName(loginBody.getUsername());
        log.info("user.getUserType():{}",user.getUserType());
        if(user.getUserType()!=null&&StrUtil.startWith(user.getUserType().name(), "ENT")) {
        	Enterprise enterprise = enterpriseService.getById(user.getScopeId());
        	if(enterprise==null) return AjaxResult.error("企业信息不存在");
        	if(enterprise.getAuthState()==3) {
        		AjaxResult result = AjaxResult.error(-1, "审核资料已驳回，请重新提交");
        		result.put(Constants.TOKEN, token);
        		return result;
        	}
        	if(enterprise.getEntState()==1||enterprise.getAuthState()==1) return AjaxResult.error(-2,"企业审核中");
        	if(enterprise.getEntState()==3) return AjaxResult.error("您的服务已过期，无法登录。请联系管理员、");
        	if(enterprise.getEntState()==4) return AjaxResult.error("企业已冻结");
        	
        }
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @ApiOperation(value = "用户信息获取")
    @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 路由信息
     */
    @ApiOperation(value = "获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
