163 lines
5.5 KiB
Java
163 lines
5.5 KiB
Java
package com.hivekion.scenario.service.impl;
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.hivekion.Global;
|
|
import com.hivekion.baseData.entity.Scenario;
|
|
import com.hivekion.baseData.entity.WeatherResource;
|
|
import com.hivekion.baseData.service.IWeatherResourceService;
|
|
import com.hivekion.baseData.service.ScenarioService;
|
|
import com.hivekion.common.entity.ResponseCmdInfo;
|
|
import com.hivekion.common.redis.RedisUtil;
|
|
import com.hivekion.environment.entity.SimtoolWeather;
|
|
import com.hivekion.environment.service.SimtoolWeatherService;
|
|
import com.hivekion.scenario.entity.ScenarioTask;
|
|
import com.hivekion.scenario.mapper.ScenarioTaskMapper;
|
|
import com.hivekion.scenario.service.ScenarioTaskService;
|
|
import com.hivekion.thread.SpringGlobalTaskManager;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* <p>
|
|
* 服务实现类
|
|
* </p>
|
|
*
|
|
* @author liDongYu
|
|
* @since 2025-09-13
|
|
*/
|
|
@Service
|
|
public class ScenarioTaskServiceImpl extends
|
|
ServiceImpl<ScenarioTaskMapper, ScenarioTask> implements
|
|
ScenarioTaskService {
|
|
|
|
@Resource
|
|
private SpringGlobalTaskManager springGlobalTaskManager;
|
|
@Resource
|
|
private SimtoolWeatherService weatherService;
|
|
@Resource
|
|
private RedisUtil redisUtil;
|
|
@Resource
|
|
private ScenarioService scenarioService;
|
|
|
|
@Resource
|
|
private IWeatherResourceService weatherResourceService;
|
|
|
|
@Override
|
|
public void start(Integer id, String roomId) {
|
|
//想定当前持续时间
|
|
redisUtil.hset(roomId + "_" + id, "duringTime", 0);
|
|
//想定当前状态
|
|
redisUtil.hset(roomId + "_" + id, "states", "running");
|
|
|
|
Scenario currentScenario = scenarioService.getScenarioById(id);
|
|
//查询天气数据
|
|
List<SimtoolWeather> weatherList = weatherService.queryByScenarioUuid(
|
|
currentScenario.getGuid());
|
|
//放入天气数据
|
|
redisUtil.hset(roomId + "_" + id, "weather", weatherList);
|
|
//查询任务
|
|
ScenarioTask queryTask = new ScenarioTask();
|
|
queryTask.setScenarioId(id);
|
|
redisUtil.hset(roomId + "_" + id, "taskList", queryTaskList(queryTask));
|
|
new Thread(() -> {
|
|
|
|
springGlobalTaskManager.startPerSecondTask(roomId + "_" + id + "_task", () -> {
|
|
//时间累计
|
|
increaseTime(currentScenario, roomId);
|
|
//天气触发
|
|
weatherTrigger(currentScenario, roomId);
|
|
//任务触发
|
|
taskTrigger(currentScenario, roomId);
|
|
//物资变化
|
|
supplierChangeUseUp(currentScenario, roomId);
|
|
});
|
|
}).start();
|
|
}
|
|
|
|
@Override
|
|
public void stop(Integer id, String roomId) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void sleepWhile(Integer id, String roomId) {
|
|
redisUtil.hset(roomId + "_" + id, "states", "sleep");
|
|
}
|
|
|
|
@Override
|
|
public void wakeup(Integer id, String roomId) {
|
|
redisUtil.hset(roomId + "_" + id, "states", "running");
|
|
}
|
|
|
|
private void increaseTime(Scenario currentScenario, String roomId) {
|
|
int mag = Global.roomParamMap.get(currentScenario.getId() + "_" + roomId) == null ? 1
|
|
: Global.roomParamMap.get(currentScenario.getId() + "_" + roomId).getAmg();
|
|
//获取当前状态
|
|
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
|
|
if (statusObj != null && statusObj.toString().equals("running")) {
|
|
Object duringObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "duringTime");
|
|
if (duringObj != null) {
|
|
int oldValue = duringObj instanceof Integer ? (Integer) duringObj : 0;
|
|
oldValue = oldValue + mag;
|
|
redisUtil.hset(roomId + "_" + currentScenario.getId(), "duringTime", oldValue);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 天气触发
|
|
*
|
|
* @param currentScenario 当前想定
|
|
* @param roomId 房间ID
|
|
*/
|
|
private void weatherTrigger(Scenario currentScenario, String roomId) {
|
|
try {
|
|
QueryWrapper<WeatherResource> weatherResourceQueryWrapper = new QueryWrapper<>();
|
|
weatherResourceQueryWrapper.eq("scenario_id", currentScenario.getId()).eq("room_id", roomId);
|
|
WeatherResource weatherResource = this.weatherResourceService.getOne(weatherResourceQueryWrapper);
|
|
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
responseCmdInfo.setRoom(roomId);
|
|
JSONObject weatherMsg = new JSONObject();
|
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
weatherMsg.putOnce("begTime", dtf.format(weatherResource.getLastBegTime()));
|
|
weatherMsg.putOnce("endTime", dtf.format(weatherResource.getLastEndTime()));
|
|
responseCmdInfo.setCmdType("66-" + weatherResource.getWeatherType());
|
|
responseCmdInfo.setData(weatherMsg);
|
|
Global.sendCmdInfoQueue.add(responseCmdInfo);
|
|
}catch (Exception ex){
|
|
ex.printStackTrace();
|
|
log.error(ex.getMessage());
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void taskTrigger(Scenario currentScenario, String roomId) {
|
|
|
|
}
|
|
private void supplierChangeUseUp(Scenario currentScenario, String roomId) {
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
public List<ScenarioTask> queryTaskList(ScenarioTask task) {
|
|
QueryWrapper<ScenarioTask> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("scenario_id", task.getScenarioId());
|
|
if(StringUtils.isNotBlank(task.getResourceId())){
|
|
queryWrapper.eq("resource_id", task.getResourceId());
|
|
}
|
|
return this.list(queryWrapper);
|
|
}
|
|
|
|
}
|