package org.jeecg.modules.oauth2_server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.oauth2_server.service.Oauth2Service;
import org.jeecg.modules.system.constant.Oauth2RedisConstant;
import org.jeecg.modules.system.entity.SysPermission;
import org.jeecg.modules.system.entity.SysTenant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysPermissionService;
import org.jeecg.modules.system.service.ISysTenantService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author liushuai
 * @create 2022-03-04 14:56
 */
@Service
public class Oauth2ServiceImpl implements Oauth2Service {
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private ISysTenantService sysTenantService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ISysUserService sysUserService;
    @Override
    public ArrayList<HashMap<String, String>> getRegulatorySupport() {
        List<SysPermission> parent = sysPermissionService.list(new QueryWrapper<SysPermission>().eq("parent_id", "1483696145060663297").eq("hidden",0));
        ArrayList<HashMap<String, String>> array = new ArrayList<>();
        Object loginId = StpUtil.getLoginId();
        parent.forEach(sysPermission -> {
            HashMap<String, String> map = new HashMap<>();
            SysTenant tenant = sysTenantService.getOne(new QueryWrapper<SysTenant>().eq("id", sysPermission.getTenantId()));
            String oAuth2Code;
            if (!oConvertUtils.isEmpty(redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()))){
                oAuth2Code = redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()).toString();
            }else {
                oAuth2Code = sysUserService.getOAuth2Code(loginId,tenant.getClientId());
                redisUtil.set(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId(),oAuth2Code,7200);
            }
            map.put("permissionName",sysPermission.getName());
            map.put("permissionUrl",sysPermission.getUrl());
            map.put("permissionIcon",sysPermission.getIcon());
            map.put("Code",oAuth2Code);
            array.add(map);
        });
        return array;
    }

    @Override
    public Object getBannerMenus() {
        ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
        Object loginId = StpUtil.getLoginId();
        if(loginId==null) {
            return array;
        }
        //1.通过loginId获取username
        SysUser currentUser = sysUserService.getUserById(loginId.toString());
        List<SysTenant> tenants = sysPermissionService.querySystemByUser(currentUser.getUsername());

        for (SysTenant tenant : tenants) {
            HashMap<String, String> map = new HashMap<>();
            String oAuth2Code = null;
            if (!oConvertUtils.isEmpty(redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()))){
                oAuth2Code = redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()).toString();
            }else {
                oAuth2Code = sysUserService.getOAuth2Code(loginId,tenant.getClientId());
                redisUtil.set(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId(),oAuth2Code,7200);
            }
            map.put("status",tenant.getStatus().toString());
            map.put("name",tenant.getSystemName());
            map.put("url",tenant.getUrl());
            map.put("Code",oAuth2Code);
            array.add(map);
        }
//        if (currentUser.getUsername().equals(UserConstant.ADMIN_USER)&& currentUser.getId().equals(UserConstant.USER_ADMIN)){
//            HashMap<String, String> map = new HashMap<>();
//            SysTenant tenant = sysTenantService.getById(UserConstant.TENANT_DATACENTER);
//            String oAuth2Code = null;
//            if (!oConvertUtils.isEmpty(redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()))){
//                oAuth2Code = redisUtil.get(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId()).toString();
//            }else {
//                oAuth2Code = sysUserService.getOAuth2Code(loginId,tenant.getClientId());
//                redisUtil.set(Oauth2RedisConstant.CODE_CACHE_PREFIX+loginId+tenant.getClientId(),oAuth2Code,7200);
//            }
//            map.put("status",tenant.getStatus().toString());
//            map.put("name",tenant.getSystemName());
//            map.put("url",tenant.getUrl());
//            map.put("Code",oAuth2Code);
//            array.add(map);
//        }
        return array;
    }
}
