//package com.mcwl.communityCenter.service.impl;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.mcwl.common.utils.StringUtils;
//import com.mcwl.communityCenter.domain.DeepSeekRequest;
//import com.mcwl.communityCenter.service.AIService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Service;
//import org.springframework.web.reactive.function.client.WebClient;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//import reactor.core.publisher.SignalType;
//
//import java.util.ArrayList;
//import java.util.concurrent.CopyOnWriteArrayList;
//
//@Service
//@RequiredArgsConstructor
//public class AIServiceImpl implements AIService {
//
//    @Value("${spring.ai.dashscope.base-url}")
//    private String DEEPSEEK_API_URL;
//    @Value("${spring.ai.dashscope.api-key}")
//    private String API_KEY;
//    @Value("${spring.ai.dashscope.chat.options.model}")
//    private String apiModel;
//
//
//    private final ObjectMapper objectMapper;
//
//    @Override
//    public Flux<String> getDeepSeekResponseStream(String message) {
//        WebClient client = WebClient.builder()
//                .baseUrl(DEEPSEEK_API_URL)
//                .defaultHeader("Authorization", "Bearer " + API_KEY)
//                .build();
//
//        // 构建请求体（推荐使用对象映射）
//        DeepSeekRequest request = new DeepSeekRequest();
//        request.setModel(apiModel);
//        // 添加对话历史
//        request.addMessage("user", message);
//        request.setMaxTokens(500);
//        request.setTemperature(0.7);
//        request.setStream(true);
//
//        return client.post()
//                .contentType(MediaType.APPLICATION_JSON)
//                .bodyValue(request)
//                .retrieve()
//                .bodyToFlux(String.class) // 原始数据流
//                .takeUntil(data -> data.contains("[DONE]")) // 遇到结束标记停止
//                .flatMap(json -> parseContentFromJson(json)) // 解析内容
//                .onErrorResume(e -> Flux.just(""));// 错误处理
//
//    }
//
//
//    // 辅助方法：从 JSON 中提取 content
//    private Mono<String> parseContentFromJson(String json) {
//        try {
//            JsonNode root = objectMapper.readTree(json);
//            String reasoning_content = root.path("choices")
//                    .get(0)
//                    .path("delta")
//                    .path("reasoning_content")
//                    .asText("");
//            String content = root.path("choices")
//                    .get(0)
//                    .path("delta")
//                    .path("content")
//                    .asText("");
//            System.out.print(StringUtils.isNotEmpty(reasoning_content) ? reasoning_content : content);
//            return Mono.just(StringUtils.isNotEmpty(reasoning_content) ? reasoning_content : content);
//        } catch (JsonProcessingException e) {
//            return Mono.error(e);
//        }
//    }
//
//}
