feat(myInvitation): 调整
parent
535b175fab
commit
489db2410d
|
@ -1,30 +1,30 @@
|
|||
package com.mcwl.web.controller.communityCenter;
|
||||
|
||||
|
||||
import com.mcwl.communityCenter.service.AIService;
|
||||
import com.mcwl.communityCenter.webSocket.ChatWebSocket;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/chat")
|
||||
public class ChatController {
|
||||
|
||||
private final AIService aiService;
|
||||
|
||||
|
||||
/**
|
||||
* ai
|
||||
*/
|
||||
@GetMapping("/ai")
|
||||
public Flux<String> switchUserMode(String msg) throws Exception {
|
||||
return aiService.getDeepSeekResponseStream(msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
//package com.mcwl.web.controller.communityCenter;
|
||||
//
|
||||
//
|
||||
//import com.mcwl.communityCenter.service.AIService;
|
||||
//import com.mcwl.communityCenter.webSocket.ChatWebSocket;
|
||||
//import lombok.RequiredArgsConstructor;
|
||||
//import org.springframework.web.bind.annotation.GetMapping;
|
||||
//import org.springframework.web.bind.annotation.RequestMapping;
|
||||
//import org.springframework.web.bind.annotation.RestController;
|
||||
//import reactor.core.publisher.Flux;
|
||||
//
|
||||
//@RequiredArgsConstructor
|
||||
//@RestController
|
||||
//@RequestMapping("/chat")
|
||||
//public class ChatController {
|
||||
//
|
||||
// private final AIService aiService;
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * ai
|
||||
// */
|
||||
// @GetMapping("/ai")
|
||||
// public Flux<String> switchUserMode(String msg) throws Exception {
|
||||
// return aiService.getDeepSeekResponseStream(msg);
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
|
|
@ -20,10 +20,7 @@ import org.springframework.web.bind.annotation.*;
|
|||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
import static com.mcwl.common.core.domain.AjaxResult.success;
|
||||
|
||||
|
@ -113,6 +110,9 @@ public class InvitationController {
|
|||
public R<Double> totalAmount() {
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
Double totalAmount = invitationService.getTotalAmount(userId);
|
||||
if (Objects.isNull(totalAmount)) {
|
||||
totalAmount = 0.0;
|
||||
}
|
||||
return R.ok(totalAmount);
|
||||
}
|
||||
|
||||
|
|
|
@ -38,17 +38,17 @@
|
|||
<!-- <artifactId>dashscope-sdk-java</artifactId>-->
|
||||
<!-- <version>2.18.3</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud.ai</groupId>
|
||||
<artifactId>spring-ai-alibaba-starter</artifactId>
|
||||
<version>1.0.0-M5.1</version>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba.cloud.ai</groupId>-->
|
||||
<!-- <artifactId>spring-ai-alibaba-starter</artifactId>-->
|
||||
<!-- <version>1.0.0-M5.1</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.netty</groupId>
|
||||
<artifactId>reactor-netty-http</artifactId>
|
||||
<version>1.1.6</version> <!-- 使用与你的 Spring Boot 版本兼容的版本 -->
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>io.projectreactor.netty</groupId>-->
|
||||
<!-- <artifactId>reactor-netty-http</artifactId>-->
|
||||
<!-- <version>1.1.6</version> <!– 使用与你的 Spring Boot 版本兼容的版本 –>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -1,32 +1,32 @@
|
|||
package com.mcwl.communityCenter.config;
|
||||
|
||||
import com.mcwl.communityCenter.webSocket.ChatWebSocket;
|
||||
import com.mcwl.communityCenter.webSocket.HumanWebSocket;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(chatWebSocket(), "/chat")
|
||||
.addHandler(humanWebSocket(), "/chat/human")
|
||||
.setAllowedOrigins("*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ChatWebSocket chatWebSocket() {
|
||||
return new ChatWebSocket();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HumanWebSocket humanWebSocket() {
|
||||
return new HumanWebSocket();
|
||||
}
|
||||
|
||||
}
|
||||
//package com.mcwl.communityCenter.config;
|
||||
//
|
||||
//import com.mcwl.communityCenter.webSocket.ChatWebSocket;
|
||||
//import com.mcwl.communityCenter.webSocket.HumanWebSocket;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
//import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
//import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
//
|
||||
//@Configuration
|
||||
//@EnableWebSocket
|
||||
//public class WebSocketConfig implements WebSocketConfigurer {
|
||||
//
|
||||
// @Override
|
||||
// public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
// registry.addHandler(chatWebSocket(), "/chat")
|
||||
// .addHandler(humanWebSocket(), "/chat/human")
|
||||
// .setAllowedOrigins("*");
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public ChatWebSocket chatWebSocket() {
|
||||
// return new ChatWebSocket();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public HumanWebSocket humanWebSocket() {
|
||||
// return new HumanWebSocket();
|
||||
// }
|
||||
//
|
||||
//}
|
|
@ -1,84 +1,84 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//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);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
|
|
@ -1,82 +1,82 @@
|
|||
package com.mcwl.communityCenter.webSocket;
|
||||
|
||||
import com.mcwl.communityCenter.service.AIService;
|
||||
import com.mcwl.communityCenter.service.HumanService;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
||||
import reactor.core.Disposable;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/chat")
|
||||
@NoArgsConstructor
|
||||
public class ChatWebSocket extends AbstractWebSocketHandler {
|
||||
private final Map<String, Boolean> userModes = new ConcurrentHashMap<>();
|
||||
|
||||
// 存储会话与订阅的映射关系
|
||||
private final Map<String, Disposable> sessionSubscriptions = new ConcurrentHashMap<>();
|
||||
|
||||
@Autowired
|
||||
private AIService aiService;
|
||||
|
||||
// 构造函数注入服务...
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String userId = session.getId();
|
||||
String userMessage = message.getPayload();
|
||||
|
||||
// AI 流式响应模式
|
||||
Flux<String> responseStream = aiService.getDeepSeekResponseStream(userMessage);
|
||||
|
||||
// 订阅响应流并存储 Disposable
|
||||
Disposable disposable = responseStream
|
||||
.doOnNext(chunk -> sendText(session, chunk)) // 发送每个数据块到客户端
|
||||
.doOnComplete(() -> sendText(session, "[END]")) // 当流处理完成时,发送结束标记
|
||||
.doOnError(e -> sendText(session, "[ERROR] " + e.getMessage()))
|
||||
.subscribe();
|
||||
|
||||
sessionSubscriptions.put(userId, disposable);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
super.afterConnectionEstablished(session);
|
||||
// userModes.put(session.getId(), false);
|
||||
session.sendMessage(new TextMessage("[AI] 您好,请问有什么问题?"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
||||
// 清理订阅资源
|
||||
String sessionId = session.getId();
|
||||
Disposable disposable = sessionSubscriptions.remove(sessionId);
|
||||
if (disposable != null && disposable.isDisposed()) {
|
||||
disposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 线程安全的发送方法
|
||||
private void sendText(WebSocketSession session, String text) {
|
||||
try {
|
||||
if (session.isOpen()) {
|
||||
synchronized (session) { // WebSocketSession 非线程安全
|
||||
session.sendMessage(new TextMessage(text));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("WebSocket 发送失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
//package com.mcwl.communityCenter.webSocket;
|
||||
//
|
||||
//import com.mcwl.communityCenter.service.AIService;
|
||||
//import com.mcwl.communityCenter.service.HumanService;
|
||||
//import lombok.NoArgsConstructor;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.web.socket.CloseStatus;
|
||||
//import org.springframework.web.socket.TextMessage;
|
||||
//import org.springframework.web.socket.WebSocketSession;
|
||||
//import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
||||
//import reactor.core.Disposable;
|
||||
//import reactor.core.publisher.Flux;
|
||||
//
|
||||
//import javax.websocket.server.ServerEndpoint;
|
||||
//import java.io.IOException;
|
||||
//import java.util.Map;
|
||||
//import java.util.concurrent.ConcurrentHashMap;
|
||||
//
|
||||
//@ServerEndpoint("/chat")
|
||||
//@NoArgsConstructor
|
||||
//public class ChatWebSocket extends AbstractWebSocketHandler {
|
||||
// private final Map<String, Boolean> userModes = new ConcurrentHashMap<>();
|
||||
//
|
||||
// // 存储会话与订阅的映射关系
|
||||
// private final Map<String, Disposable> sessionSubscriptions = new ConcurrentHashMap<>();
|
||||
//
|
||||
// @Autowired
|
||||
// private AIService aiService;
|
||||
//
|
||||
// // 构造函数注入服务...
|
||||
//
|
||||
// @Override
|
||||
// public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
// String userId = session.getId();
|
||||
// String userMessage = message.getPayload();
|
||||
//
|
||||
// // AI 流式响应模式
|
||||
// Flux<String> responseStream = aiService.getDeepSeekResponseStream(userMessage);
|
||||
//
|
||||
// // 订阅响应流并存储 Disposable
|
||||
// Disposable disposable = responseStream
|
||||
// .doOnNext(chunk -> sendText(session, chunk)) // 发送每个数据块到客户端
|
||||
// .doOnComplete(() -> sendText(session, "[END]")) // 当流处理完成时,发送结束标记
|
||||
// .doOnError(e -> sendText(session, "[ERROR] " + e.getMessage()))
|
||||
// .subscribe();
|
||||
//
|
||||
// sessionSubscriptions.put(userId, disposable);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
// super.afterConnectionEstablished(session);
|
||||
//// userModes.put(session.getId(), false);
|
||||
// session.sendMessage(new TextMessage("[AI] 您好,请问有什么问题?"));
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
||||
// // 清理订阅资源
|
||||
// String sessionId = session.getId();
|
||||
// Disposable disposable = sessionSubscriptions.remove(sessionId);
|
||||
// if (disposable != null && disposable.isDisposed()) {
|
||||
// disposable.dispose();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// // 线程安全的发送方法
|
||||
// private void sendText(WebSocketSession session, String text) {
|
||||
// try {
|
||||
// if (session.isOpen()) {
|
||||
// synchronized (session) { // WebSocketSession 非线程安全
|
||||
// session.sendMessage(new TextMessage(text));
|
||||
// }
|
||||
// }
|
||||
// } catch (IOException e) {
|
||||
// System.out.println("WebSocket 发送失败: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -1,55 +1,55 @@
|
|||
package com.mcwl.communityCenter.webSocket;
|
||||
|
||||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.communityCenter.service.HumanService;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
||||
import reactor.core.Disposable;
|
||||
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/chat/human")
|
||||
@NoArgsConstructor
|
||||
public class HumanWebSocket extends AbstractWebSocketHandler {
|
||||
|
||||
@Autowired
|
||||
private HumanService humanService;
|
||||
|
||||
// 构造函数注入服务...
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String userId = session.getId();
|
||||
String userMessage = message.getPayload();
|
||||
|
||||
humanService.handleHumanMessage(userId, userMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
super.afterConnectionEstablished(session);
|
||||
humanService.transferToHuman(SecurityUtils.getUserId().toString(), session);
|
||||
System.out.println("客服:" + SecurityUtils.getUsername() + " 已上线");
|
||||
}
|
||||
|
||||
|
||||
// 线程安全的发送方法
|
||||
private void sendText(WebSocketSession session, String text) {
|
||||
try {
|
||||
if (session.isOpen()) {
|
||||
synchronized (session) { // WebSocketSession 非线程安全
|
||||
session.sendMessage(new TextMessage(text));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("WebSocket 发送失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
//package com.mcwl.communityCenter.webSocket;
|
||||
//
|
||||
//import com.mcwl.common.utils.SecurityUtils;
|
||||
//import com.mcwl.communityCenter.service.HumanService;
|
||||
//import lombok.NoArgsConstructor;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.web.socket.CloseStatus;
|
||||
//import org.springframework.web.socket.TextMessage;
|
||||
//import org.springframework.web.socket.WebSocketSession;
|
||||
//import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
||||
//import reactor.core.Disposable;
|
||||
//
|
||||
//import javax.websocket.server.ServerEndpoint;
|
||||
//import java.io.IOException;
|
||||
//import java.util.Map;
|
||||
//import java.util.concurrent.ConcurrentHashMap;
|
||||
//
|
||||
//@ServerEndpoint("/chat/human")
|
||||
//@NoArgsConstructor
|
||||
//public class HumanWebSocket extends AbstractWebSocketHandler {
|
||||
//
|
||||
// @Autowired
|
||||
// private HumanService humanService;
|
||||
//
|
||||
// // 构造函数注入服务...
|
||||
//
|
||||
// @Override
|
||||
// public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
// String userId = session.getId();
|
||||
// String userMessage = message.getPayload();
|
||||
//
|
||||
// humanService.handleHumanMessage(userId, userMessage);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
// super.afterConnectionEstablished(session);
|
||||
// humanService.transferToHuman(SecurityUtils.getUserId().toString(), session);
|
||||
// System.out.println("客服:" + SecurityUtils.getUsername() + " 已上线");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// // 线程安全的发送方法
|
||||
// private void sendText(WebSocketSession session, String text) {
|
||||
// try {
|
||||
// if (session.isOpen()) {
|
||||
// synchronized (session) { // WebSocketSession 非线程安全
|
||||
// session.sendMessage(new TextMessage(text));
|
||||
// }
|
||||
// }
|
||||
// } catch (IOException e) {
|
||||
// System.out.println("WebSocket 发送失败: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -1,6 +1,8 @@
|
|||
package com.mcwl.memberCenter.task;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.mcwl.common.constant.QueueConstants;
|
||||
import com.mcwl.common.core.domain.entity.SysUser;
|
||||
import com.mcwl.memberCenter.domain.*;
|
||||
|
@ -131,16 +133,38 @@ public class UserMemberTask {
|
|||
/**
|
||||
* 会员参与的促销活动是否过期 每天晚上0点检查 0 0 0 * * ?
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void checkPromotionExpiredTask() {
|
||||
|
||||
List<Long> promotionIdList = new ArrayList<>();
|
||||
List<Promotion> promotionList = promotionService.lambdaQuery()
|
||||
.lt(Promotion::getEndTime, new Date()) // 活动结束日期小于当前时间
|
||||
.list();
|
||||
for (Promotion promotion : promotionList) {
|
||||
promotionIdList.add(promotion.getId());
|
||||
}
|
||||
int pageNum = 1;
|
||||
int pageSize = 1000;
|
||||
|
||||
// 根据活动结束日期小于当前时间,获取活动id
|
||||
List<Long> promotionIdList = new ArrayList<>();
|
||||
|
||||
while (true) {
|
||||
Page<Promotion> page = new Page<>(pageNum, pageSize);
|
||||
|
||||
promotionService.lambdaQuery()
|
||||
.lt(Promotion::getEndTime, new Date()) // 活动结束日期小于当前时间
|
||||
.page(page);
|
||||
List<Promotion> promotionList = page.getRecords();
|
||||
if (promotionList.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
for (Promotion promotion : promotionList) {
|
||||
promotionIdList.add(promotion.getId());
|
||||
}
|
||||
|
||||
memberPromotionService.lambdaUpdate()
|
||||
.set(MemberPromotion::getStatus, PromotionEnum.EXPIRED)
|
||||
.eq(MemberPromotion::getStatus, PromotionEnum.PARTICIPATE)
|
||||
.in(MemberPromotion::getPromotionId, promotionIdList)
|
||||
.update();
|
||||
pageNum++;
|
||||
promotionIdList.clear();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue