package com.fs.baidu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.baidu.domain.BdApi;
import com.fs.baidu.mapper.BdApiMapper;
import com.fs.baidu.service.IBdApiService;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

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

/**
 * 百度信息Service业务层处理
 * 
 * @author fs
 * @date 2025-01-20
 */
@Service
public class BdApiServiceImpl extends ServiceImpl<BdApiMapper, BdApi> implements IBdApiService {

    private final String OAUTH_URL = "https://u.baidu.com/oauth/page/index?platformId=4960345965958561794&appId={appId}&scope={scope}&state={state}&callback={callback}";

    /**
     * 查询百度信息
     * 
     * @param id 百度信息主键
     * @return 百度信息
     */
    @Override
    public BdApi selectBdApiById(Long id)
    {
        return baseMapper.selectBdApiById(id);
    }

    /**
     * 查询百度信息列表
     * 
     * @param bdApi 百度信息
     * @return 百度信息
     */
    @Override
    public List<BdApi> selectBdApiList(BdApi bdApi)
    {
        return baseMapper.selectBdApiList(bdApi);
    }

    /**
     * 新增百度信息
     * 
     * @param bdApi 百度信息
     * @return 结果
     */
    @Override
    public int insertBdApi(BdApi bdApi)
    {
        bdApi.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertBdApi(bdApi);
    }

    /**
     * 修改百度信息
     * 
     * @param bdApi 百度信息
     * @return 结果
     */
    @Override
    public int updateBdApi(BdApi bdApi)
    {
        bdApi.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateBdApi(bdApi);
    }

    /**
     * 批量删除百度信息
     * 
     * @param ids 需要删除的百度信息主键
     * @return 结果
     */
    @Override
    public int deleteBdApiByIds(Long[] ids)
    {
        return baseMapper.deleteBdApiByIds(ids);
    }

    /**
     * 删除百度信息信息
     * 
     * @param id 百度信息主键
     * @return 结果
     */
    @Override
    public int deleteBdApiById(Long id)
    {
        return baseMapper.deleteBdApiById(id);
    }

    @Override
    public BdApi getAppByAppId(String s) {
        BdApi api = baseMapper.selectOne(new QueryWrapper<BdApi>().eq("app_id", s));
        if(api == null) throw new BaseException("未找到API：" + s + " 信息");
        return api;
    }

    @Override
    public String authorizationUrl(Long id) {
        BdApi api = selectBdApiById(id);

        Map<String, String> params = new HashMap<>();
        params.put("appId", api.getAppId());
        params.put("scope", "65,66,67,68,69,70,1009333,1009334,71,72,1009335,73,74,75,1001788,1001789,1001455,1001790,1001791,1002161,1002829,1004606");
        params.put("state", id.toString());
        params.put("callback", "https://bd.runtzh.com/prod-api/baiduBack/callback");
        String filledUri = UriComponentsBuilder
                .fromUriString(OAUTH_URL)
                .buildAndExpand(params)
                .encode() // 自动处理特殊字符
                .toString();

        return filledUri;
    }
}
