package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.OauthConfig;
import cn.com.ctop.common.module.mapper.OauthConfigMapper;
import cn.com.ctop.common.module.service.IOauthConfigService;
import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 授权配置信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-01-26
 */
@Service
public class OauthConfigServiceImpl extends ServiceImpl<OauthConfigMapper, OauthConfig> implements IOauthConfigService {
    @Autowired
    private OauthConfigMapper configMapper;

    @Override
    public OauthConfig getConfigByAppId(Long appId) {
        QueryWrapper<OauthConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id", appId);
        OauthConfig config = this.getOne(queryWrapper);
        return config;
    }

    @Override
    public Map<Long, String> getConfigMapByMediaId(int mediaId) {

        Map<Long, String> map = new HashMap<>();
        List<JSONObject> list = configMapper.getListByMediaId(mediaId);
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject jsonObject = list.get(i);
                map.put(jsonObject.getLong("app_id"), jsonObject.getString("secret"));
            }

        }

        return map;
    }
}
