完成统一返回结构封装

master
Gary 2 months ago
parent b649967128
commit 4e6a091c98

@ -1 +1,4 @@
# 解析excel 然后设置定时任务发送给前端消息内容
使用websocket和前端进行连接 websocket地址
ws://localhost:8080/exercise
# 这个行的顺序是从0开始的

@ -8,7 +8,12 @@ import lombok.Data;
*/
@Data
public class ActionDto {
private String human;
private String action;
private String description;
private String leftMinute;
private String leftSecond;
private String whichTeamAttack;
private int lakerScore;
private int wolfScore;
private String action;
private String getScoreType;
private Boolean isGetSocre;
}

@ -0,0 +1,34 @@
package com.gary.exercise.enums;
import lombok.Data;
import java.util.Arrays;
public enum LakerPlayer {
AUSTIN_REAVES("奥斯汀-里维斯"),
DANGELO_RUSSELL("德安杰洛-拉塞尔"),
LEBRON_JAMES("勒布朗-詹姆斯"),
RUI_HACHIMURA("八村塁"),
ANTHONY_DAVIS("安东尼-戴维斯"),
GABE_VINCENT("盖布文森特"),
TORIAN_PRINCE("道尔顿-克内克特"),
MAXWELL_HEIDT("马克斯-克里斯蒂"),
BRONNY_JAMES("布朗尼-詹姆斯");
private final String chineseName;
LakerPlayer(String chineseName) {
this.chineseName = chineseName;
}
public String getChineseName() {
return chineseName;
}
public static LakerPlayer searchLakerPlayerFormText(String text) {
return Arrays.stream(values())
.filter(player -> text.contains(player.getChineseName()))
.findFirst()
.orElse(null); // 返回 null 表示未找到匹配项
}
}

@ -0,0 +1,4 @@
package com.gary.exercise.enums;
public class WolfPlayer {
}

@ -1,6 +1,9 @@
package com.gary.exercise.service;
import com.gary.exercise.dto.ActionDto;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -20,7 +23,53 @@ public interface ExerciseService {
/**
*
*/
String getLakerAction(ArrayList<String> rowData);
/**
*
*/
String getWolfAction(ArrayList<String> rowData);
/**
* 1 0 rowIndex
*/
String getActionTeam(int rowIndex) throws IOException;
/**
*
*/
String getLeftMinute(int rowIndex) throws IOException;
/**
*
*/
String getLeftSecond(int rowIndex) throws IOException;
/**
*
*/
ActionDto getResult(int rowIndex) throws IOException;
/**
*
*/
int getLakerScore(int rowIndex) throws IOException;
/**
*
*/
int getWolfScore(int rowIndex) throws IOException;
/**
*
*/
String getScoreType(int rowIndex) throws IOException;
/**
*
*/
Boolean isGetScore(int rowIndex) throws IOException;
/**
*action
*/
String getAction(int rowIndex) throws IOException;
}

@ -1,6 +1,7 @@
package com.gary.exercise.service.impl;
import com.gary.exercise.config.FileConfig;
import com.gary.exercise.dto.ActionDto;
import com.gary.exercise.service.ExerciseService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -11,18 +12,17 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class ExerciseServiceImpl implements ExerciseService {
private final FileConfig fileConfig;
public ExerciseServiceImpl(FileConfig fileConfig) {
this.fileConfig = fileConfig;
}
/**
*
*/
@ -32,26 +32,33 @@ public class ExerciseServiceImpl implements ExerciseService {
}
return switch (cell.getCellType()) {
case STRING -> cell.getStringCellValue();
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
case FORMULA -> cell.getCellFormula();
case BLANK -> "";
case NUMERIC -> {
// 检查是否为日期格式
if (DateUtil.isCellDateFormatted(cell)) {
yield new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
// 将日期格式化为字符串
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
yield dateFormat.format(cell.getDateCellValue());
} else {
// 处理数字类型
yield String.valueOf(cell.getNumericCellValue());
}
yield String.valueOf(cell.getNumericCellValue());
}
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
case FORMULA -> cell.getCellFormula();
case BLANK -> "";
default -> "UNKNOWN";
};
}
/**
* Excel
* Excel 使List
*
* @param rowIndex 0
* @return
* @throws IOException
*/
@Override
public List<String> readRow( int rowIndex) throws IOException {
public List<String> readRow(int rowIndex) throws IOException {
List<String> rowData = new ArrayList<>();
String filePath = fileConfig.getFilePath();
String sheetName = fileConfig.getSheetNumber();
@ -72,19 +79,221 @@ public class ExerciseServiceImpl implements ExerciseService {
return rowData;
}
@Override
public Map<String, String> processRow(int rowIndex) throws IOException {
return Map.of();
}
@Override
public String getLakerAction(ArrayList<String> rowData) {
if (rowData.size() != 4)
return null;//说明表示第一节 第二节 没有数据
else {
if (rowData.get(3).isEmpty() && rowData.get(1).isEmpty()) {
//第一行和第二行的数据都是空 说明没有动作
return null;
}
return rowData.get(1);//直接返回第一行的数据
}
}
@Override
public String getWolfAction(ArrayList<String> rowData) {
if (rowData.size() != 4)
return null;//说明表示第一节 第二节 没有数据
else {
if (rowData.get(3).isEmpty() && rowData.get(2).isEmpty()) {
//第一行和第二行的数据都是空 说明没有动作
return null;
}
return rowData.get(3);//直接返回第三行的数据
}
}
@Override
public String getActionTeam(int rowIndex) throws IOException {
ArrayList<String> rowData = (ArrayList<String>) this.readRow(rowIndex);
String lakerAction = this.getLakerAction(rowData);
System.out.println(lakerAction);
String wolfAction = this.getWolfAction(rowData);
System.out.println(wolfAction);
if(Objects.equals(lakerAction, " ") &&!wolfAction.isEmpty()){
return "wolf";
}else if (!lakerAction.isEmpty()&& Objects.equals(wolfAction, " ")){
return "laker";
}
return "null";
}
/**
*
*
* @param rowIndex
*/
@Override
public String getLeftMinute(int rowIndex) throws IOException {
ArrayList<String> rowData = (ArrayList<String>) this.readRow(rowIndex);
String time = rowData.get(0); // 假设时间在第一列
String minute = "";
// 使用 ":" 分割时间字符串
String[] parts = time.split(":");
if (parts.length > 0) {
minute = parts[0]; // 提取分钟部分
}
return minute; // 返回分钟
}
/**
*
*
* @param rowIndex
*/
@Override
public String getLeftSecond(int rowIndex) throws IOException {
ArrayList<String> rowData = (ArrayList<String>) this.readRow(rowIndex);
String time = rowData.get(0); // 假设时间在第一列
String second = "";
// 使用 ":" 分割时间字符串
String[] parts = time.split(":");
if (parts.length > 1) {
second = parts[1]; // 提取秒数部分
}
return second; // 返回秒数
}
/**
*
*
* @param rowIndex
*/
@Override
public ActionDto getResult(int rowIndex) throws IOException {
ActionDto actionDto = new ActionDto();
String leftMinute = this.getLeftMinute(rowIndex);
System.out.println(leftMinute);
String leftSecond = this.getLeftSecond(rowIndex);
System.out.println(leftSecond);
String whichTeamAttack = this.getActionTeam(rowIndex);
System.out.println(whichTeamAttack);
String action = this.getLakerAction((ArrayList<String>) this.readRow(rowIndex));
System.out.println(action);
int lakerScore=this.getLakerScore(rowIndex);
System.out.println(lakerScore);
int wolfScore=this.getWolfScore(rowIndex);
System.out.println(wolfScore);
String getScoreType = this.getScoreType(rowIndex);
System.out.println(getScoreType);
Boolean isGetSocre = this.isGetScore(rowIndex);
System.out.println(isGetSocre);
actionDto.setIsGetSocre(isGetSocre);
actionDto.setGetScoreType(getScoreType);
actionDto.setLakerScore(lakerScore);
actionDto.setWolfScore(wolfScore);
actionDto.setLeftMinute(leftMinute);
actionDto.setLeftSecond(leftSecond);
actionDto.setWhichTeamAttack(whichTeamAttack);
actionDto.setAction(action);
System.out.println(actionDto);
return actionDto;
}
/**
*
*
* @param rowIndex
*/
@Override
public Map<String, String> processRow(int rowIndex) throws IOException{
List<String> rowData = readRow(rowIndex);
public int getLakerScore(int rowIndex) throws IOException {
List<String> rowData = this.readRow(rowIndex); // 读取 Excel 数据
System.out.println(rowData);
String data=rowData.get(2);
System.out.println(data);
// 使用 "- 分割时间字符串
String[] parts = data.split("-");
if (parts.length > 1) {
return Integer.parseInt(parts[0]); // 提取湖人队比分
}
return 0;
}
/**
*
*
* @param rowIndex
*/
@Override
public int getWolfScore(int rowIndex) throws IOException {
List<String> rowData = this.readRow(rowIndex); // 读取 Excel 数据
System.out.println(rowData);
String data=rowData.get(2);
System.out.println(data);
// 使用 "- 分割时间字符串
String[] parts = data.split("-");
if (parts.length > 1) {
return Integer.parseInt(parts[1]); // 提取森林狼队比分
}
return 0;
}
/**
*
*
*/
@Override
public String getScoreType(int rowIndex) throws IOException {
String action = this.getAction(rowIndex);
// 检查是否包含 "三分" 或 "两分"
if (action.contains("三分")) {
return "三分";
} else if (action.contains("两分")) {
return "两分"; // 如果有明确的两分投篮
}
// 其他得分类型
if (action.contains("篮板")) {
return "篮板";
} else if (action.contains("抢断")) {
return "抢断";
} else if (action.contains("罚球")) {
return "罚球";
} else if (action.contains("盖帽")) {
return "盖帽";
} else if (action.contains("犯规")) {
return "犯规";
}
return "未知"; // 如果没有匹配的类型
}
/**
*
*
*/
@Override
public Boolean isGetScore(int rowIndex) throws IOException {
String action = this.getAction(rowIndex);
return action.contains("命中") && !action.contains("不中");
}
return Map.of();
/**
* action
*
* @param rowIndex
*/
@Override
public String getAction(int rowIndex) throws IOException {
String whichTeamAttack = this.getActionTeam(rowIndex);
if (whichTeamAttack.equals("laker")) {
return this.getLakerAction((ArrayList<String>) this.readRow(rowIndex));
}else if (whichTeamAttack.equals("wolf")) {
return this.getWolfAction((ArrayList<String>) this.readRow(rowIndex));
}
else return "";
}

@ -57,34 +57,35 @@ public class ExerciseWebSocketHandler implements WebSocketHandler {
// 接收客户端的消息
String clientMessage = ((TextMessage) message).getPayload();
System.out.println("收到消息: " + clientMessage);
// 启动一个线程来每秒发送一行数据
new Thread(() -> {
try {
int rowIndex = 0; // 从第 0 行开始
while (true) {
List<String> rowData = exerciseService.readRow(rowIndex); // 读取 Excel 数据
String rowString = String.join(", ", rowData); // 格式化为字符串
session.sendMessage(new TextMessage(rowString)); // 发送给客户端
rowIndex++; // 移动到下一行
Thread.sleep(1000); // 每秒发送一次
// 停止条件:到达 Excel 文件的末尾
if (rowData.isEmpty()) {
session.sendMessage(new TextMessage("已到达文件末尾"));
break;
}
}
} catch (Exception e) {
log.error("发生错误: {}", e.getMessage());
if(clientMessage.equals("可以发送消息!!!")){
// 启动一个线程来每秒发送一行数据
new Thread(() -> {
try {
session.sendMessage(new TextMessage("发生错误: " + e.getMessage()));
} catch (IOException ioException) {
int rowIndex = 0; // 从第 0 行开始
while (true) {
List<String> rowData = exerciseService.readRow(rowIndex); // 读取 Excel 数据
String rowString = String.join(", ", rowData); // 格式化为字符串
session.sendMessage(new TextMessage(rowString)); // 发送给客户端
rowIndex++; // 移动到下一行
Thread.sleep(3000); // 每秒发送一次
// 停止条件:到达 Excel 文件的末尾
if (rowData.isEmpty()) {
session.sendMessage(new TextMessage("已到达文件末尾"));
break;
}
}
} catch (Exception e) {
log.error("发生错误: {}", e.getMessage());
try {
session.sendMessage(new TextMessage("发生错误: " + e.getMessage()));
} catch (IOException ioException) {
log.error("发生错误: {}", e.getMessage());
}
}
}
}).start();
}).start();
}
} else {
System.out.println("不支持的消息类型: " + message.getClass().getName());
}

Binary file not shown.

@ -1,14 +1,14 @@
package com.gary.exercise;
import cn.hutool.core.io.resource.ClassPathResource;
import com.gary.exercise.config.FileConfig;
import com.gary.exercise.dto.ActionDto;
import com.gary.exercise.enums.LakerPlayer;
import com.gary.exercise.service.ExerciseService;
import com.gary.exercise.service.impl.ExerciseServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
@SpringBootTest
class ExerciseApplicationTests {
@ -23,6 +23,15 @@ class ExerciseApplicationTests {
public static void main(String[] args) throws IOException {
FileConfig fileConfig =new FileConfig();
ExerciseService exerciseService = new ExerciseServiceImpl(fileConfig);
ArrayList<String> list= (ArrayList<String>) exerciseService.readRow(12);
/* String whichTeamAttack= exerciseService.getActionTeam(6);
System.out.println(whichTeamAttack);*/
ActionDto result= exerciseService.getResult(6);
System.out.println(result);
}
}

Binary file not shown.
Loading…
Cancel
Save