SimulationService/src/main/java/com/simulationservice/service/TimeSyncThread.java

178 lines
5.2 KiB
Java
Raw Normal View History

package com.simulationservice.service;
import java.lang.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.simulationservice.util.CustomThread;
import com.simulationservice.util.RedisUtil;
/**
* 时间同步线程用于处理想定的倒计时和想定时间结束
*/
public class TimeSyncThread extends CustomThread {
private String _beginTime; //想定开始时间
private int _continueTime; //持续时间
private RedisUtil _redis;
private int[] _monArray = new int[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private String _resvTime = ""; //剩余时间
private int _year;
private int _month;
private int _day;
private int _hour;
private int _min;
private int _sec;
private int _residualHour;
private int _residualMin;
private int _residualSec;
@Override
public void processBuss() {
if (_residualHour <= 0 && _residualMin <= 0 && _residualSec <= 0)
{
//倒计时时间结束,处理时间结束业务逻辑
return;
}
if (_year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0))
_monArray[1]++;
_sec++;
if (_sec >= 60)
{
_sec -= 60;
_min++;
if (_min == 60)
{
_min = 0;
_hour++;
if (_hour == 24)
{
_hour = 0;
_day++;
if (_day == 32)
{
_day = 1;
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
}
}
}
String month = String.format("%02d", _month);
String day = String.format("%02d", _day);
String hour = String.format("%02d", _hour);
String min = String.format("%02d", _min);
String sec = String.format("%02d", _sec);
if (_residualSec > 0)
{
_residualSec--;
}
else
{
if (_residualMin > 0)
{
_residualSec = 59;
_residualMin--;
}
else
{
if (_residualHour > 0)
{
_residualMin = 59;
_residualSec = 59;
_residualHour--;
}
}
}
String residualHour = String.format("%02d", _residualHour);
String residualMin = String.format("%02d", _residualMin);
String residualSec = String.format("%02d", _residualSec);
String strTime = String.valueOf(_year) + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
_resvTime = residualHour + ":" + residualMin + ":" + residualSec;
System.out.println(getRoomId() + " " + strTime + " " + _resvTime);
// 推演的时间键值roomId-runTime
String key = getRoomId() + "-runTime";
SpringUtil.getBean(RedisUtil.class).set(key, strTime);
// 房间剩余时间键值roomId-resvTime
key = getRoomId() + "-resvTime";
SpringUtil.getBean(RedisUtil.class).set(key, _resvTime);
//推送时间到客户端
JSONObject jsonObject = new JSONObject();
jsonObject.putIfAbsent("roomId", getRoomId());
jsonObject.putIfAbsent("runTime", strTime);
jsonObject.putIfAbsent("resvTime", _resvTime);
String jsonString = jsonObject.toJSONString();
SendWebSocketMsg(jsonString, "1111");
}
public void setBeginTime(String beginTime) {
String[] strArr = beginTime.split(" ");
if (strArr.length == 2)
{
String strDate = strArr[0];
String strTime = strArr[1];
String[] strDateArr = strDate.split("-");
String[] strTimeArr = strTime.split(":");
if (strDateArr.length == 3)
{
_year = Integer.parseInt(strDateArr[0]);
_month = Integer.parseInt(strDateArr[1]);
_day = Integer.parseInt(strDateArr[2]);
}
if (strTimeArr.length == 3)
{
_hour = Integer.parseInt(strTimeArr[0]);
_min = Integer.parseInt(strTimeArr[1]);
_sec = Integer.parseInt(strTimeArr[2]);
}
}
this._beginTime = beginTime;
}
public void setContinueTime(int continueTime) {
int min = continueTime;
if (min < 60)
{
_residualHour = 0;
_residualMin = min;
_residualSec = 0;
}
else
{
int hour = min / 60;
if (hour >= 10)
{
_residualHour = hour;
_residualMin = min - hour * 60;
_residualSec = 0;
}
else
{
_residualHour = hour;
_residualMin = min - hour * 60;
_residualSec = 0;
}
}
this._continueTime = continueTime;
}
}