package com.gxwebsoft.common.system.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 微信公共服务类
 * 
 * @author 科技小王子
 * @since 2025-09-08
 */
@Slf4j
@Service
public class WxService {

    @Autowired
    private SettingService settingService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String ACCESS_TOKEN_KEY = "WX_ACCESS_TOKEN";

    /**
     * 获取微信AccessToken（使用默认租户）
     */
    public String getAccessToken() {
        return getAccessToken(null);
    }

    /**
     * 获取微信AccessToken（支持指定租户ID）
     * 
     * @param tenantId 租户ID，为null时使用默认值
     * @return access_token
     */
    public String getAccessToken(Integer tenantId) {
        if (tenantId == null) {
            tenantId = 10048; // 默认租户ID，可以根据需要调整
        }
        
        String key = ACCESS_TOKEN_KEY + ":" + tenantId;
        
        // 从缓存获取
        String cachedToken = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(cachedToken)) {
            try {
                JSONObject tokenData = JSON.parseObject(cachedToken);
                String accessToken = tokenData.getString("access_token");
                if (StrUtil.isNotBlank(accessToken)) {
                    log.debug("从缓存获取access_token: {}", accessToken);
                    return accessToken;
                }
            } catch (Exception e) {
                // 解析失败，可能是旧格式，直接使用
                if (!cachedToken.startsWith("{")) {
                    log.debug("从缓存获取access_token(旧格式): {}", cachedToken);
                    return cachedToken;
                }
                log.warn("解析缓存的access_token失败: {}", e.getMessage());
                // 缓存数据异常，删除缓存，重新获取
                redisTemplate.delete(key);
            }
        }

        // 缓存中没有，重新获取
        try {
            JSONObject setting = settingService.getBySettingKey("mp-weixin");
            if (setting == null) {
                throw new RuntimeException("请先配置微信小程序");
            }

            String appId = setting.getString("appId");
            String appSecret = setting.getString("appSecret");
            if (StrUtil.isBlank(appId) || StrUtil.isBlank(appSecret)) {
                throw new RuntimeException("微信小程序配置不完整");
            }

            // 调用微信API获取AccessToken
            String apiUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" 
                + appId + "&secret=" + appSecret;
            String response = HttpRequest.get(apiUrl).execute().body();
            
            JSONObject result = JSON.parseObject(response);
            String accessToken = result.getString("access_token");
            if (StrUtil.isNotBlank(accessToken)) {
                // 存入缓存
                JSONObject tokenData = new JSONObject();
                tokenData.put("access_token", accessToken);
                tokenData.put("expires_in", result.get("expires_in"));
                redisTemplate.opsForValue().set(key, tokenData.toJSONString(), 7000L, TimeUnit.SECONDS);
                log.info("获取新的access_token成功: {}", accessToken);
                return accessToken;
            } else {
                throw new RuntimeException("获取AccessToken失败: " + response);
            }
        } catch (Exception e) {
            log.error("获取微信AccessToken失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取微信AccessToken失败: " + e.getMessage());
        }
    }
}
