package com.fs.web.controller.system;

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

import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.entity.SysRole;
import com.fs.common.utils.PatternUtils;
import com.fs.his.utils.ConfigUtil;
import com.fs.hisStore.config.MedicalMallConfig;
import com.fs.system.service.ISysRoleService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
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.fs.common.constant.Constants;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.entity.SysMenu;
import com.fs.common.core.domain.entity.SysUser;
import com.fs.common.core.domain.model.LoginBody;
import com.fs.common.utils.SecurityUtils;
import com.fs.framework.web.service.SysLoginService;
import com.fs.framework.web.service.SysPermissionService;
import com.fs.system.service.ISysMenuService;

/**
 * 登录验证
 *

 */
@RestController

public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ConfigUtil configUtil;

    @Autowired
    private MedicalMallConfig medicalMallConfig;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        if (!PatternUtils.checkPassword(loginBody.getPassword())) {
            return AjaxResult.error("密码格式不正确,需包含字母、数字和特殊字符,长度为 8-20 位");
        }
        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);
        //药品商城参数
        AjaxResult ajax = AjaxResult.success();

        if(medicalMallConfig!=null){
            ajax.put("medicalMallConfig", medicalMallConfig);
        }
        ajax.put("user", user);
        SysRole isAdmin = new SysRole();
        getIsAdmin(isAdmin, permissions, user);
        ajax.put("isAdmin", isAdmin);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);

        return ajax;
    }

    /**
     * 获取电话地址权限
     * @param isAdmin
     * @param permissions
     * @param user
     */
    private void getIsAdmin(SysRole isAdmin, Set<String> permissions, SysUser user) {
        isAdmin.setIsCheckAddress(0);
        isAdmin.setIsCheckPhone(0);
        if (permissions.contains("*:*:*")){
            isAdmin.setIsCheckAddress(1);
            isAdmin.setIsCheckPhone(1);
        }else {
            List<SysRole> roleList = user.getRoles();
            if (roleList != null && !roleList.isEmpty()) {
                Long[] roleIds = roleList.stream().map(SysRole::getRoleId).toArray(Long[]::new);
//                isAdmin = roleService.getIsCheckPhone(roleIds)?1:0;
                SysRole isCheckPermission = roleService.getIsCheckPermission(roleIds);
                if (isCheckPermission != null) {
                    isAdmin.setIsCheckPhone(isCheckPermission.getIsCheckPhone());
                    isAdmin.setIsCheckAddress(isCheckPermission.getIsCheckAddress());
                }
            }
        }
    }

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