Commit 1027c09e authored by 何金镒's avatar 何金镒

建发接口优化 2

parent 6870a890
...@@ -6,8 +6,8 @@ import org.springframework.stereotype.Service; ...@@ -6,8 +6,8 @@ import org.springframework.stereotype.Service;
/** /**
* 访客相关 * 访客相关
*/ */
@Slf4j //@Slf4j
@Service //@Service
public class CDCVisitorTools { //public class CDCVisitorTools {
//
} //}
package com.ikonke.konkeaialibabamcp.aitools; //package com.ikonke.konkeaialibabamcp.aitools;
//
//
import com.ikonke.konkeaialibabamcp.entity.mongodb.DeviceStatus; //import com.ikonke.konkeaialibabamcp.entity.mongodb.DeviceStatus;
import com.ikonke.konkeaialibabamcp.service.SycDeviceService; //import com.ikonke.konkeaialibabamcp.service.SycDeviceService;
import com.ikonke.konkeaialibabamcp.service.mongdbservice.DeviceStatusService; //import com.ikonke.konkeaialibabamcp.service.mongdbservice.DeviceStatusService;
import com.ikonke.konkeaialibabamcp.utils.CcuUtils; //import com.ikonke.konkeaialibabamcp.utils.CcuUtils;
import com.ikonke.konkeaialibabamcp.utils.KonkeIotUtils; //import com.ikonke.konkeaialibabamcp.utils.KonkeIotUtils;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool; //import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam; //import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service; //import org.springframework.stereotype.Service;
//
@Slf4j //@Slf4j
@Service //@Service
public class DeviceTools { //public class DeviceTools {
//
private final DeviceStatusService deviceStatusUtil; // private final DeviceStatusService deviceStatusUtil;
private final KonkeIotUtils konkeIotServer; // private final KonkeIotUtils konkeIotServer;
private final SycDeviceService sycDeviceService; // private final SycDeviceService sycDeviceService;
//
public DeviceTools(DeviceStatusService deviceStatusUtil, KonkeIotUtils konkeIotServer, SycDeviceService sycDeviceService){ // public DeviceTools(DeviceStatusService deviceStatusUtil, KonkeIotUtils konkeIotServer, SycDeviceService sycDeviceService){
this.deviceStatusUtil = deviceStatusUtil; // this.deviceStatusUtil = deviceStatusUtil;
this.konkeIotServer = konkeIotServer; // this.konkeIotServer = konkeIotServer;
this.sycDeviceService = sycDeviceService; // this.sycDeviceService = sycDeviceService;
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID查询设备状态信息.") // @Tool(description = "智能家居相关:根据主机号和设备ID查询设备状态信息.")
public DeviceStatus findDeviceState(@ToolParam(description = "主机号") String ccuName, // public DeviceStatus findDeviceState(@ToolParam(description = "主机号") String ccuName,
@ToolParam(description = "设备ID") Integer devId) { // @ToolParam(description = "设备ID") Integer devId) {
log.info("mcp调用--->根据主机号和设备ID查询设备状态信息....ccuName:{},devId:{}",ccuName,devId); // log.info("mcp调用--->根据主机号和设备ID查询设备状态信息....ccuName:{},devId:{}",ccuName,devId);
return deviceStatusUtil.findUserByUserId(CcuUtils.getCcuName(ccuName), devId); // return deviceStatusUtil.findUserByUserId(CcuUtils.getCcuName(ccuName), devId);
} // }
@Tool(description = "智能家居相关:根据主机号同步设备") // @Tool(description = "智能家居相关:根据主机号同步设备")
public boolean synchronousDevice(@ToolParam(description = "主机号")String ccuName){ // public boolean synchronousDevice(@ToolParam(description = "主机号")String ccuName){
log.info("mcp调用--->根据主机号同步设备....ccuName:{}",ccuName); // log.info("mcp调用--->根据主机号同步设备....ccuName:{}",ccuName);
return sycDeviceService.synchronousDevice(CcuUtils.getCcuName(ccuName)); // return sycDeviceService.synchronousDevice(CcuUtils.getCcuName(ccuName));
} // }
//
//==============================设备控制============================================= // //==============================设备控制=============================================
@Tool(description = "智能家居相关:根据主机号和设备ID控制:打开设备") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:打开设备")
public String optDeviceOpen(@ToolParam(description = "主机号")String ccuName, // public String optDeviceOpen(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId){ // @ToolParam(description = "设备类型ID")String typeId){
log.info("mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId); // log.info("mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId);
return konkeIotServer.optDeviceOpen(CcuUtils.getCcuName(ccuName), devId, typeId); // return konkeIotServer.optDeviceOpen(CcuUtils.getCcuName(ccuName), devId, typeId);
} // }
@Tool(description = "智能家居相关:根据主机号和多个设备ID批量控制:打开设备") // @Tool(description = "智能家居相关:根据主机号和多个设备ID批量控制:打开设备")
public String batchOptDeviceOpen(@ToolParam(description = "主机号")String ccuName, // public String batchOptDeviceOpen(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID,多个使用逗号隔开")String devIds){ // @ToolParam(description = "设备ID,多个使用逗号隔开")String devIds){
log.info("mcp调用--->根据主机号和多个设备ID与设备类型ID批量控制....ccuName:{},devIds:{}",ccuName,devIds); // log.info("mcp调用--->根据主机号和多个设备ID与设备类型ID批量控制....ccuName:{},devIds:{}",ccuName,devIds);
return konkeIotServer.batchOptDeviceOpen(CcuUtils.getCcuName(ccuName), devIds); // return konkeIotServer.batchOptDeviceOpen(CcuUtils.getCcuName(ccuName), devIds);
} // }
@Tool(description = "智能家居相关:根据主机号和设备ID控制:关闭设备") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:关闭设备")
public String optDeviceClosed(@ToolParam(description = "主机号")String ccuName, // public String optDeviceClosed(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId){ // @ToolParam(description = "设备类型ID")String typeId){
log.info("mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId); // log.info("mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId);
return konkeIotServer.optDeviceClosed(CcuUtils.getCcuName(ccuName), devId, typeId); // return konkeIotServer.optDeviceClosed(CcuUtils.getCcuName(ccuName), devId, typeId);
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID控制:设置亮度") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:设置亮度")
public String setBrightness(@ToolParam(description = "主机号")String ccuName, // public String setBrightness(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "设置的亮度百分比大小,范围是0-100")int brightness){ // @ToolParam(description = "设置的亮度百分比大小,范围是0-100")int brightness){
log.info("mcp调用--->根据主机号和设备ID控制设备亮度....ccuName:{},devId:{},type:{},brightness:{}",ccuName,devId,typeId,brightness); // log.info("mcp调用--->根据主机号和设备ID控制设备亮度....ccuName:{},devId:{},type:{},brightness:{}",ccuName,devId,typeId,brightness);
return konkeIotServer.setBrightness(CcuUtils.getCcuName(ccuName), devId, typeId,brightness); // return konkeIotServer.setBrightness(CcuUtils.getCcuName(ccuName), devId, typeId,brightness);
} // }
@Tool(description = "智能家居相关:根据主机号和设备ID控制:设置色温") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:设置色温")
public String setColourTemperature(@ToolParam(description = "主机号")String ccuName, // public String setColourTemperature(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "设置的色温百分比大小,范围是0-100")int colourTemperature){ // @ToolParam(description = "设置的色温百分比大小,范围是0-100")int colourTemperature){
log.info("mcp调用--->根据主机号和设备ID控制设备的色温....ccuName:{},devId:{},type:{},colourTemperature:{}",ccuName,devId,typeId,colourTemperature); // log.info("mcp调用--->根据主机号和设备ID控制设备的色温....ccuName:{},devId:{},type:{},colourTemperature:{}",ccuName,devId,typeId,colourTemperature);
return konkeIotServer.setColourTemperature(CcuUtils.getCcuName(ccuName), devId, typeId,colourTemperature); // return konkeIotServer.setColourTemperature(CcuUtils.getCcuName(ccuName), devId, typeId,colourTemperature);
} // }
@Tool(description = "智能家居相关:根据主机号和设备ID控制:设置显示的颜色") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:设置显示的颜色")
public String setColor(@ToolParam(description = "主机号")String ccuName, // public String setColor(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "要设置的颜色,支持的颜色:Red,Yellow,Blue,Green,White,Black,Cyan,Purple,Orange,Pink,Violet,Magenta,Indigo,SlateBlue,DarkBlue,SkyBlue,DarkCyan,Beige,Brown")String color){ // @ToolParam(description = "要设置的颜色,支持的颜色:Red,Yellow,Blue,Green,White,Black,Cyan,Purple,Orange,Pink,Violet,Magenta,Indigo,SlateBlue,DarkBlue,SkyBlue,DarkCyan,Beige,Brown")String color){
log.info("mcp调用--->根据主机号和设备ID控制设备的颜色....ccuName:{},devId:{},type:{},color:{}",ccuName,devId,typeId,color); // log.info("mcp调用--->根据主机号和设备ID控制设备的颜色....ccuName:{},devId:{},type:{},color:{}",ccuName,devId,typeId,color);
return konkeIotServer.setColor(CcuUtils.getCcuName(ccuName), devId, typeId,color); // return konkeIotServer.setColor(CcuUtils.getCcuName(ccuName), devId, typeId,color);
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID控制:暂停设备.只支持窗帘类型") // @Tool(description = "智能家居相关:根据主机号和设备ID控制:暂停设备.只支持窗帘类型")
public String setPause(@ToolParam(description = "主机号")String ccuName, // public String setPause(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId){ // @ToolParam(description = "设备类型ID")String typeId){
log.info("mcp调用--->根据主机号和设备ID控制暂停设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId); // log.info("mcp调用--->根据主机号和设备ID控制暂停设备....ccuName:{},devId:{},type:{}",ccuName,devId,typeId);
return konkeIotServer.setPause(CcuUtils.getCcuName(ccuName), devId, typeId); // return konkeIotServer.setPause(CcuUtils.getCcuName(ccuName), devId, typeId);
} // }
@Tool(description = "智能家居相关:根据主机号和设备ID控制设备:移动位置.只支持窗帘电机(CURTAIN_MOTOR)类型") // @Tool(description = "智能家居相关:根据主机号和设备ID控制设备:移动位置.只支持窗帘电机(CURTAIN_MOTOR)类型")
public String setMove(@ToolParam(description = "主机号")String ccuName, // public String setMove(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "移动的距离,范围是0-100")int progress){ // @ToolParam(description = "移动的距离,范围是0-100")int progress){
log.info("mcp调用--->根据主机号和设备ID控制其移动位置....ccuName:{},devId:{},type:{},progress:{}",ccuName,devId,typeId,progress); // log.info("mcp调用--->根据主机号和设备ID控制其移动位置....ccuName:{},devId:{},type:{},progress:{}",ccuName,devId,typeId,progress);
return konkeIotServer.setMove(CcuUtils.getCcuName(ccuName), devId, typeId,progress); // return konkeIotServer.setMove(CcuUtils.getCcuName(ccuName), devId, typeId,progress);
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID控制设备:延时关机.只支持新风、地暖、空调") // @Tool(description = "智能家居相关:根据主机号和设备ID控制设备:延时关机.只支持新风、地暖、空调")
public String setDelayedShutdown(@ToolParam(description = "主机号")String ccuName, // public String setDelayedShutdown(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "多少分钟后关机,范围30-1440,必须是30的倍数,最小是半小时")int delayTime){ // @ToolParam(description = "多少分钟后关机,范围30-1440,必须是30的倍数,最小是半小时")int delayTime){
log.info("mcp调用--->根据主机号和设备ID控制设备延时关机....ccuName:{},devId:{},type:{},delayTime:{}",ccuName,devId,typeId,delayTime); // log.info("mcp调用--->根据主机号和设备ID控制设备延时关机....ccuName:{},devId:{},type:{},delayTime:{}",ccuName,devId,typeId,delayTime);
return konkeIotServer.setDelayedShutdown(CcuUtils.getCcuName(ccuName), devId, typeId,delayTime); // return konkeIotServer.setDelayedShutdown(CcuUtils.getCcuName(ccuName), devId, typeId,delayTime);
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作温度.只支持空调、地暖") // @Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作温度.只支持空调、地暖")
public String setTemperature(@ToolParam(description = "主机号")String ccuName, // public String setTemperature(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "设置的温度大小,范围是5-35")double temperature){ // @ToolParam(description = "设置的温度大小,范围是5-35")double temperature){
log.info("mcp调用--->根据主机号和设备ID设置设备的工作温度....ccuName:{},devId:{},type:{},temperature:{}",ccuName,devId,typeId,temperature); // log.info("mcp调用--->根据主机号和设备ID设置设备的工作温度....ccuName:{},devId:{},type:{},temperature:{}",ccuName,devId,typeId,temperature);
return konkeIotServer.setTemperature(CcuUtils.getCcuName(ccuName), devId, typeId,temperature); // return konkeIotServer.setTemperature(CcuUtils.getCcuName(ccuName), devId, typeId,temperature);
} // }
@Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作模式.只支持地暖、空调、新风") // @Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作模式.只支持地暖、空调、新风")
public String setModel(@ToolParam(description = "主机号")String ccuName, // public String setModel(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "设置的工作模式,枚举值:WIND、HOT、COLD、DEHUMIDIFICATION、AUTO、MANUAL")String mode){ // @ToolParam(description = "设置的工作模式,枚举值:WIND、HOT、COLD、DEHUMIDIFICATION、AUTO、MANUAL")String mode){
log.info("mcp调用--->根据主机号和设备ID设置设备的工作模式....ccuName:{},devId:{},type:{},mode:{}",ccuName,devId,typeId,mode); // log.info("mcp调用--->根据主机号和设备ID设置设备的工作模式....ccuName:{},devId:{},type:{},mode:{}",ccuName,devId,typeId,mode);
return konkeIotServer.setModel(CcuUtils.getCcuName(ccuName), devId, typeId,mode); // return konkeIotServer.setModel(CcuUtils.getCcuName(ccuName), devId, typeId,mode);
} // }
//
@Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作风速.只支持空调、新风") // @Tool(description = "智能家居相关:根据主机号和设备ID设置设备的工作风速.只支持空调、新风")
public String setSpeed(@ToolParam(description = "主机号")String ccuName, // public String setSpeed(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId, // @ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型ID")String typeId, // @ToolParam(description = "设备类型ID")String typeId,
@ToolParam(description = "设置的工作风速,枚举值:LOW、MID、HIGH、AUTO、STOP")String speed){ // @ToolParam(description = "设置的工作风速,枚举值:LOW、MID、HIGH、AUTO、STOP")String speed){
log.info("mcp调用--->根据主机号和设备ID设置设备的工作风速....ccuName:{},devId:{},type:{},mode:{}",ccuName,devId,typeId,speed); // log.info("mcp调用--->根据主机号和设备ID设置设备的工作风速....ccuName:{},devId:{},type:{},mode:{}",ccuName,devId,typeId,speed);
return konkeIotServer.setSpeed(CcuUtils.getCcuName(ccuName), devId, typeId,speed); // return konkeIotServer.setSpeed(CcuUtils.getCcuName(ccuName), devId, typeId,speed);
} // }
//
//
//
@Tool(description = "智能家居相关:根据主机号和场景ID:触发场景") // @Tool(description = "智能家居相关:根据主机号和场景ID:触发场景")
public String triggerScene(@ToolParam(description = "主机号")String ccuName, // public String triggerScene(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "场景ID")String devId, // @ToolParam(description = "场景ID")String devId,
@ToolParam(description = "场景类型ID,type等于scene")String typeId){ // @ToolParam(description = "场景类型ID,type等于scene")String typeId){
log.info("mcp调用--->根据主机号和场景ID触发场景....ccuName:{},devId:{},type:{}",ccuName,devId,typeId); // log.info("mcp调用--->根据主机号和场景ID触发场景....ccuName:{},devId:{},type:{}",ccuName,devId,typeId);
return konkeIotServer.optScene(CcuUtils.getCcuName(ccuName), devId); // return konkeIotServer.optScene(CcuUtils.getCcuName(ccuName), devId);
} // }
//
//=================================================================================================== // //===================================================================================================
//
//
} //}
...@@ -9,6 +9,7 @@ import cn.hutool.json.JSONObject; ...@@ -9,6 +9,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.ikonke.konkeaialibabamcp.service.cdc.CdcHttpUtils; import com.ikonke.konkeaialibabamcp.service.cdc.CdcHttpUtils;
import com.ikonke.konkeaialibabamcp.utils.CDCUtils; import com.ikonke.konkeaialibabamcp.utils.CDCUtils;
import com.ikonke.konkeaialibabamcp.utils.TimeSlotMerger;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -164,22 +165,33 @@ public class SpacesService { ...@@ -164,22 +165,33 @@ public class SpacesService {
} }
} }
JSONArray response_time = new JSONArray(); // JSONArray response_time = new JSONArray();
for( int j = timeSegmentIndex; j < 48; j++){ // for( int j = timeSegmentIndex; j < 48; j++){
Integer number = info.getInt(j);//0=不可预定 1=可预订 2=已被预定 // Integer number = info.getInt(j);//0=不可预定 1=可预订 2=已被预定
JSONObject jsonObject1 = new JSONObject(); // JSONObject jsonObject1 = new JSONObject();
jsonObject1.set("timeSegment", CDCUtils.time_segment[j]); // jsonObject1.set("timeSegment", CDCUtils.time_segment[j]);
//
if(number == 1){ // if(number == 1){
jsonObject1.set("availability", "可预订"); // jsonObject1.set("availability", "可预订");
}else if(number == 2){ // }else if(number == 2){
jsonObject1.set("availability", "已被预定"); // jsonObject1.set("availability", "已被预定");
// }else{
// jsonObject1.set("availability", "不可预定");
// }
// response_time.set(jsonObject1);
// }
int[] intArray = new int[info.size()];
for (int i = 0; i < info.size(); i++) {
if(i < timeSegmentIndex){
intArray[i] = 0;
}else{ }else{
jsonObject1.set("availability", "不可预定"); intArray[i] = info.getInt(i);
} }
response_time.set(jsonObject1);
} }
response.set("time", response_time); List<TimeSlotMerger.MergedSlot> merged = TimeSlotMerger.mergeTimeSlots(intArray);
response.set("time", merged);
}else{ }else{
response.set("time", "不可预约"); response.set("time", "不可预约");
} }
......
package com.ikonke.konkeaialibabamcp.utils;
import java.util.ArrayList;
import java.util.List;
public class TimeSlotMerger {
public static class MergedSlot {
private String start;
private String end;
private String available;
public MergedSlot(String start, String end, String available) {
this.start = start;
this.end = end;
this.available = available;
}
public String getStart() { return start; }
public String getEnd() { return end; }
public String getAvailable() { return available; }
@Override
public String toString() {
return String.format("%s-%s: %s", start, end, available);
}
}
/**
* 合并时间段的方法
* @param slots 包含48个元素的数组,每个元素为0或1
* @return 合并后的时间段列表
* @throws IllegalArgumentException 如果输入不符合要求
*/
public static List<MergedSlot> mergeTimeSlots(int[] slots) {
// 验证输入
if (slots == null || slots.length != 48) {
throw new IllegalArgumentException("输入必须是包含48个元素的数组");
}
// for (int slot : slots) {
// if (slot != 0 && slot != 1) {
// throw new IllegalArgumentException("数组元素只能是0或1");
// }
// }
List<MergedSlot> mergedSlots = new ArrayList<>();
if (slots.length == 0) {
return mergedSlots;
}
int currentStatus = slots[0];
int startTimeIndex = 0;
// 遍历所有时间段
for (int i = 1; i < slots.length; i++) {
if (slots[i] != currentStatus) {
// 状态改变,记录上一个时间段
mergedSlots.add(new MergedSlot(
formatTime(startTimeIndex),
formatTime(i),
currentStatus == 1 ? "可预约" : "不可预约"
));
// 更新当前状态和开始索引
currentStatus = slots[i];
startTimeIndex = i;
}
}
// 添加最后一个时间段
mergedSlots.add(new MergedSlot(
formatTime(startTimeIndex),
formatTime(slots.length),
currentStatus == 1 ? "可预约" : "不可预约"
));
return mergedSlots;
}
/**
* 将时间段索引转换为时间字符串
* @param index 时间段索引(0-48)
* @return 格式化的时间字符串,如"00:00"
*/
private static String formatTime(int index) {
int hours = index / 2;
int minutes = (index % 2) * 30;
// 格式化小时和分钟为两位数
return String.format("%02d:%02d", hours, minutes);
}
public static void main(String[] args) {
int[] slots = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
List<MergedSlot> merged = mergeTimeSlots(slots);
// 输出结果
for (MergedSlot slot : merged) {
System.out.println(slot);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment