From 8a84450b5dddbbd48d0ae298eab9f5b56b44686f Mon Sep 17 00:00:00 2001 From: wulin Date: Fri, 20 Oct 2023 09:37:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/qiuguo/iot/base/date/DateEnum.java | 343 ----------------- .../iot/base/utils/ChineseDateTimeUtils.java | 361 ++++++++++++++++++ .../iot/base/utils/ChineseToAlaboUtils.java | 1 - .../iot/third/nlp/action/ActionTime.java | 18 +- .../api/handler/BoxWebSocketHandler.java | 23 +- 5 files changed, 373 insertions(+), 373 deletions(-) create mode 100644 iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseDateTimeUtils.java diff --git a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java index 38547a1..c9ac193 100644 --- a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java +++ b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java @@ -3,8 +3,6 @@ package com.qiuguo.iot.base.date; import com.qiuguo.iot.base.utils.ChineseToAlaboUtils; import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.Map; public enum DateEnum implements IDate{ //现在,今天,明天,后天,昨天,前天,*天后,*天, @@ -485,346 +483,6 @@ public enum DateEnum implements IDate{ public int getAmPm() {return ampm;} - public static LocalDateTime getDateWithCode(String c){ - return getDateWithCodeAndLocalDateTime(c, LocalDateTime.now(), 0); - } - - public static LocalDateTime getDateWithCodeAndLocalDateTime(String c, LocalDateTime localDateTime, int ampm){ - DateEnum dateEnu = null; - for(DateEnum enum1 : values()){ - if(c.startsWith(enum1.getCode())){ - dateEnu = enum1; - localDateTime = dateEnu.getDateTime(localDateTime); - c = c.substring(enum1.getCode().length()); - localDateTime = getDateWithCodeAndLocalDateTime(c, localDateTime, dateEnu.getAmPm()); - break; - } - } - if(dateEnu == null){ - Integer i = -1; - if(c.contains("天后")){ - c = c.replace("天后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusDays(i); - }else if(c.contains("天前")){ - c = c.replace("天前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusDays(i); - }else if(c.contains("天")){ - c = c.replace("天", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusDays(i); - }else if(c.contains("个月后")){ - c = c.replace("个月后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMonths(i); - }else if(c.contains("个月前")){ - c = c.replace("个月前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMonths(i); - }else if(c.contains("月后")){ - c = c.replace("月后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMonths(i); - }else if(c.contains("月前")){ - c = c.replace("月前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMonths(i); - }else if(c.contains("月份")){ - c = c.replace("月份", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 12){ - i = 12; - }else if(i < 1){ - i = 1; - } - return localDateTime.withMonth(i); - }else if(c.contains("月")){ - //c = c.replace("月", ""); - int index = c.indexOf("月"); - i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); - if(i > 12){ - i = 12; - }else if(i < 1){ - i = 1; - } - return getDateWithCodeAndLocalDateTime( - c.substring(index + 1), - localDateTime.withMonth(i), - ampm); - }else if(c.contains("年后")){ - c = c.replace("年后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusYears(i); - }else if(c.contains("年前")){ - c = c.replace("年前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusYears(i); - }else if(c.contains("年")){ - c = c.replace("年", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusYears(i); - }else if(c.contains("小时后")){ - c = c.replace("小时后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("小时前")){ - c = c.replace("小时前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("小时")){ - c = c.replace("小时", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("点钟后")){ - c = c.replace("点钟后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("点钟前")){ - c = c.replace("点钟前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("点钟")){ - //c = c.replace("点钟", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 23){ - i = 23; - } - return getDateWithCodeAndLocalDateTime( - c.substring(c.indexOf("点钟") + 1), - localDateTime.withHour(i).withMinute(0).withSecond(0), - ampm); - }else if(c.contains("点前")){ - c = c.replace("点前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("点后")){ - c = c.replace("点后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("点")){ - int index = c.indexOf("点"); - i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); - if(i > 23){ - i = 23; - }else if(i < 0){ - i = 0; - } - if(ampm == 1){ - //下午 - if(i < 12){ - i += 12; - } - }else if(ampm == 2){ - //晚上 - if(i < 12 && i > 5){ - i += 12; - }else{ - localDateTime = localDateTime.plusDays(1); - } - } - return getDateWithCodeAndLocalDateTime( - c.substring(index + 1), - localDateTime.withHour(i).withMinute(0).withSecond(0), - ampm); - }else if(c.contains("号")){ - c = c.replace("号", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - //判断闰月大小月 - return localDateTime.withDayOfMonth(i); - }else if(c.contains("时后")){ - c = c.replace("时后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("时前")){ - c = c.replace("时前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusHours(i); - }else if(c.contains("时")){ - int index = c.indexOf("时"); - i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); - if(i > 23){ - i = 23; - }else if(i < 0){ - i = 0; - } - if(ampm == 1){ - //下午 - if(i < 12){ - i += 12; - } - }else if(ampm == 2){ - //晚上 - if(i < 12 && i > 5){ - i += 12; - }else{ - localDateTime = localDateTime.plusDays(1); - } - } - return getDateWithCodeAndLocalDateTime( - c.substring(index + 1), - localDateTime.withHour(i).withMinute(0).withSecond(0), - ampm); - }else if(c.contains("分钟后")){ - c = c.replace("分钟后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMinutes(i); - }else if(c.contains("分钟前")){ - c = c.replace("分钟前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusMinutes(i); - }else if(c.contains("分钟")){ - int index = c.indexOf("分钟"); - i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); - if(i > 59){ - i = 59; - }else if(i < 0){ - i = 0; - } - return getDateWithCodeAndLocalDateTime( - c.substring(index + 1), - localDateTime.withMinute(i), - ampm); - }else if(c.contains("分")){ - int index = c.indexOf("分"); - i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); - if(i > 59){ - i = 59; - }else if(i < 0){ - i = 0; - } - return getDateWithCodeAndLocalDateTime( - c.substring(index + 1), - localDateTime.withMinute(i), - ampm); - }else if(c.contains("秒后")){ - c = c.replace("秒后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusSeconds(i); - }else if(c.contains("秒前")){ - c = c.replace("秒前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusSeconds(i); - }else if(c.contains("秒")){ - c = c.replace("秒", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.withSecond(i); - }else if(c.contains("上周")){// - c = c.replace("上周", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - now = 7 - now; - i += now; - return localDateTime.minusDays(i); - }else if(c.contains("下周")){// - c = c.replace("下周", ""); - i = 1; - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - i = 7 - i + now; - return localDateTime.plusDays(i); - }else if(c.contains("上星期")){// - c = c.replace("上星期", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - - i = 7 - i + now; - return localDateTime.minusDays(i); - }else if(c.contains("下星期")){// - c = c.replace("下星期", ""); - i = 1; - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - now = 7 - now; - i += now; - return localDateTime.plusDays(i); - }else if(c.contains("周后")){// - c = c.replace("周后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else if(c.contains("周前")){// - c = c.replace("周前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else if(c.contains("星期后")){// - c = c.replace("星期后", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else if(c.contains("星期前")){// - c = c.replace("星期前", ""); - i = 1; - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else if(c.indexOf("周") == 0){//周几 - c = c.replace("周", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - i = now - i; - - return localDateTime.minusDays(i); - }else if(c.indexOf("周") > 0){//几周 - c = c.replace("周", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else if(c.indexOf("星期") == 0){//星期几 - c = c.replace("星期", ""); - i = ChineseToAlaboUtils.getLong(c).intValue(); - if(i > 7){ - i = 7; - }else if(i < 1){ - i = 1; - } - int now = localDateTime.getDayOfWeek().getValue(); - i = now - i; - return localDateTime.minusDays(i); - }else if(c.indexOf("星期") > 0){//几星期 - c = c.replace("星期", ""); - i *= ChineseToAlaboUtils.getLong(c).intValue(); - return localDateTime.minusWeeks(i); - }else{ - //24节气,调用网络接口 - } - - } - return localDateTime; - } - public static DateEnum getWithCode(String c) { for (DateEnum dateEnum:values() ) { @@ -832,7 +490,6 @@ public enum DateEnum implements IDate{ return dateEnum; } } - //中英文数字 return DEFAULT; } } diff --git a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseDateTimeUtils.java b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseDateTimeUtils.java new file mode 100644 index 0000000..e1fae76 --- /dev/null +++ b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseDateTimeUtils.java @@ -0,0 +1,361 @@ +package com.qiuguo.iot.base.utils; + +import com.qiuguo.iot.base.date.DateEnum; + +import java.time.LocalDateTime; + +public class ChineseDateTimeUtils { + /** + * 基于当前时间的字符串转时间 + * @param c 时间字符串 + * @return + */ + public static LocalDateTime getDateWithCode(String c){ + return getDateWithCodeAndLocalDateTime(c, LocalDateTime.now(), 0, 0); + } + + private static int checkDayOfMonth(LocalDateTime localDateTime, int i){ + if(i < 1){ + i = 1; + }else{ + //判断闰月大小月 + if(localDateTime.getMonthValue() == 2){ + //闰月 + if(localDateTime.getYear() % 4 == 0){ + if(i > 29){ + i = 29; + } + }else if(i > 28){ + i = 28; + } + }else if((localDateTime.getMonthValue() < 8 && localDateTime.getMonthValue() % 2 != 0) || + (localDateTime.getMonthValue() > 7 && localDateTime.getMonthValue() % 2 == 0)){ + if(i > 31){ + i = 31; + } + }else{ + if(i > 30){ + i = 30; + } + } + } + return i; + } + + private static LocalDateTime setDayOfMonth(String c, String key, LocalDateTime localDateTime, int ampm){ + int index = c.indexOf(key); + int i = checkDayOfMonth(localDateTime, ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue()); + + + return getDateWithCodeAndLocalDateTime( + c.substring(index + key.length()), + localDateTime.withDayOfMonth(i).withHour(0).withMinute(0).withSecond(0), + ampm, + 1); + } + + private static int checkDayOfWeek(int i){ + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + return i; + } + private static LocalDateTime setDayOfWeek(String c, String key, LocalDateTime localDateTime){ + c = c.replace(key, ""); + int i = checkDayOfWeek(ChineseToAlaboUtils.getLong(c).intValue()); + int now = localDateTime.getDayOfWeek().getValue(); + i = now - i; + return localDateTime.minusDays(i); + } + + private static LocalDateTime setNextDayOfWeek(String c, String key, LocalDateTime localDateTime){ + c = c.replace(key, ""); + int i = checkDayOfWeek(ChineseToAlaboUtils.getLong(c).intValue()); + int now = localDateTime.getDayOfWeek().getValue(); + i = 7 - now + i; + return localDateTime.plusDays(i); + } + + private static LocalDateTime setLastDayOfWeek(String c, String key, LocalDateTime localDateTime){ + c = c.replace(key, ""); + int i = checkDayOfWeek(ChineseToAlaboUtils.getLong(c).intValue()); + int now = localDateTime.getDayOfWeek().getValue(); + + i = 7 - i + now; + return localDateTime.minusDays(i); + } + + /** + * 汉语时间字符串转时间 + * @param c 时间字符串 + * @param localDateTime 时间基础 + * @return + */ + public static LocalDateTime getDateWithCodeAndLocalDateTime(String c, LocalDateTime localDateTime){ + return getDateWithCodeAndLocalDateTime(c, localDateTime, 0, 0); + } + /** + * 汉语时间字符串转时间 + * @param c 时间字符串 + * @param localDateTime 时间基础 + * @param ampm 0:上午、1:下午、2:晚上 + * @param type 0:无感 1:月 2:时 3:分 + * @return + */ + public static LocalDateTime getDateWithCodeAndLocalDateTime(String c, LocalDateTime localDateTime, int ampm, int type){ + DateEnum dateEnu = null; + for(DateEnum enum1 : DateEnum.values()){ + if(c.startsWith(enum1.getCode())){ + dateEnu = enum1; + localDateTime = dateEnu.getDateTime(localDateTime); + c = c.substring(enum1.getCode().length()); + localDateTime = getDateWithCodeAndLocalDateTime(c, localDateTime, dateEnu.getAmPm(), 0); + break; + } + } + if(dateEnu == null){ + if(c.contains("年后")){ + return setYears(c, "年后", localDateTime, -1); + }else if(c.contains("年前")){ + return setYears(c, "年前", localDateTime, 1); + }else if(c.contains("年")){ + return setYear(c, "年", localDateTime, ampm); + }else if(c.contains("个月后")){ + return setMonths(c, "个月后", localDateTime, -1); + }else if(c.contains("个月前")){ + return setMonths(c, "个月前", localDateTime, 1); + }else if(c.contains("月后")){ + return setMonths(c, "月后", localDateTime, -1); + }else if(c.contains("月前")){ + return setMonths(c, "月前", localDateTime, 1); + }else if(c.contains("月份")){ + return setMonth(c, "月份", localDateTime, ampm); + }else if(c.contains("月")){ + return setMonth(c, "月", localDateTime, ampm); + }else if(c.contains("上周")){// + return setLastDayOfWeek(c, "上周", localDateTime); + }else if(c.contains("下周")){// + return setNextDayOfWeek(c, "下周", localDateTime); + }else if(c.contains("上星期")){// + return setLastDayOfWeek(c, "上星期", localDateTime); + }else if(c.contains("下星期")){// + return setNextDayOfWeek(c, "下星期", localDateTime); + }else if(c.contains("周后")){// + return setWeeks(c, "周后", localDateTime, -1); + }else if(c.contains("周前")){// + return setWeeks(c, "周前", localDateTime, 1); + }else if(c.contains("星期后")){// + return setWeeks(c, "周后", localDateTime, -1); + }else if(c.contains("星期前")){// + return setWeeks(c, "星期前", localDateTime, 1); + }else if(c.indexOf("周") == 0){//周几 + return setDayOfWeek(c, "周", localDateTime); + }else if(c.indexOf("周") > 0){//几周 + return setWeeks(c, "周", localDateTime, -1); + }else if(c.indexOf("星期") == 0){//星期几 + return setDayOfWeek(c, "星期", localDateTime); + }else if(c.indexOf("星期") > 0){//几星期 + return setWeeks(c, "星期", localDateTime, -1); + }else if(c.contains("天后")){ + return setDays(c, "天后", localDateTime, -1); + }else if(c.contains("天前")){ + return setDays(c, "天前", localDateTime, 1); + }else if(c.contains("天")){ + return setDayOfMonth(c, "天", localDateTime, ampm); + }else if(c.contains("号")){ + return setDayOfMonth(c, "号", localDateTime, ampm); + }else if(c.contains("日")){ + return setDayOfMonth(c, "日", localDateTime, ampm); + }else if(c.contains("小时后")){ + return setHours(c, "小时后", localDateTime, -1); + }else if(c.contains("小时前")){ + return setHours(c, "小时前", localDateTime, 1); + }else if(c.contains("小时")){ + return setHours(c, "小时", localDateTime, -1); + }else if(c.contains("点钟后")){ + return setHours(c, "点钟后", localDateTime, -1); + }else if(c.contains("点钟前")){ + return setHours(c, "点钟前", localDateTime, 1); + }else if(c.contains("点钟")){ + return setHour(c, "点钟", localDateTime, ampm); + }else if(c.contains("点前")){ + return setHours(c, "点前", localDateTime, 1); + }else if(c.contains("点后")){ + return setHours(c, "点后", localDateTime, -1); + }else if(c.contains("点")){ + return setHour(c, "点", localDateTime, ampm); + }else if(c.contains("时后")){ + return setHours(c, "时后", localDateTime, -1); + }else if(c.contains("时前")){ + return setHours(c, "时前", localDateTime, 1); + }else if(c.contains("时")){ + return setHour(c, "时", localDateTime, ampm); + }else if(c.contains("分钟后")){ + return setMinutes(c, "分钟后", localDateTime, -1); + }else if(c.contains("分钟前")){ + return setMinutes(c, "分钟前", localDateTime, 1); + }else if(c.contains("分钟")){ + return setMinute(c, "分钟", localDateTime, ampm); + }else if(c.contains("分")){ + return setMinute(c, "分", localDateTime, ampm); + }else if(c.contains("秒后")){ + return setSeconds(c, "秒后", localDateTime, -1); + }else if(c.contains("秒前")){ + return setSeconds(c, "秒前", localDateTime, 1); + }else if(c.contains("秒")){ + return setSecond(c, "秒", localDateTime); + }else if(c.length() > 0){ + //最后一个数字转换 + Integer i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 0){ + if(type == 1){ + return localDateTime.withDayOfMonth(checkDayOfMonth(localDateTime, i)); + }else if(type == 2){ + return localDateTime.withMinute(checkMinuteAndSecond(i)); + }else if(type == 3){ + return localDateTime.withSecond(checkMinuteAndSecond(i)); + } + } + + }else{ + //24节气,调用网络接口 + } + + } + return localDateTime; + } + + private static LocalDateTime setSecond(String c, String 秒, LocalDateTime localDateTime) { + c = c.replace("秒", ""); + int i = ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.withSecond(i); + } + + private static LocalDateTime setSeconds(String c, String key, LocalDateTime localDateTime, Integer i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusSeconds(i); + } + + private static LocalDateTime setMinutes(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMinutes(i); + } + + + + private static LocalDateTime setHours(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + } + + + + private static LocalDateTime setWeeks(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + } + + private static LocalDateTime setYear(String c, String key, LocalDateTime localDateTime, int ampm) { + int index = c.indexOf(key); + int i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + return getDateWithCodeAndLocalDateTime( + c.substring(index + key.length()), + localDateTime.withYear(i), + ampm, + 0); + } + + + + private static LocalDateTime setYears(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusYears(i); + } + + private static LocalDateTime setMonths(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMonths(i); + } + + + private static LocalDateTime setDays(String c, String key, LocalDateTime localDateTime, int i) { + c = c.replace(key, ""); + + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusDays(i); + } + + private static int checkMonth(int i){ + if(i > 12){ + i = 12; + }else if(i < 1){ + i = 1; + } + return i; + } + + private static LocalDateTime setMonth(String c, String key, LocalDateTime localDateTime, int ampm) { + int index = c.indexOf(key); + int i = checkMonth(ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue()); + return getDateWithCodeAndLocalDateTime( + c.substring(index + key.length()), + localDateTime.withMonth(i), + ampm, + 1); + } + + private static int checkMinuteAndSecond(int i){ + if(i > 59){ + i = 59; + }else if(i < 0){ + i = 0; + } + return i; + } + + private static LocalDateTime setMinute(String c, String key, LocalDateTime localDateTime, int ampm) { + int index = c.indexOf(key); + int i = checkMinuteAndSecond(ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue()); + return getDateWithCodeAndLocalDateTime( + c.substring(index + key.length()), + localDateTime.withMinute(i), + ampm, + 3); + } + + private static LocalDateTime setHour(String c, String key, LocalDateTime localDateTime, int ampm) { + int index = c.indexOf(key); + int i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 23){ + i = 23; + }else if(i < 0){ + i = 0; + } + if(ampm == 1){ + //下午 + if(i < 12){ + i += 12; + } + }else if(ampm == 2){ + //晚上 + if(i < 12 && i > 5){ + i += 12; + }else{ + localDateTime = localDateTime.plusDays(1); + } + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + key.length()), + localDateTime.withHour(i).withMinute(0).withSecond(0), + ampm, + 2); + } +} diff --git a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java index 0bcb39e..df8dfa5 100644 --- a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java +++ b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java @@ -89,7 +89,6 @@ public class ChineseToAlaboUtils { if(isHi){ n += p; } - System.out.println("传入数字:" + c + ",转换后数字:" + n); return n; } } diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java index 28c5866..47ea11e 100644 --- a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java @@ -1,6 +1,7 @@ package com.qiuguo.iot.third.nlp.action; import com.qiuguo.iot.base.date.DateEnum; +import com.qiuguo.iot.base.utils.ChineseDateTimeUtils; import com.qiuguo.iot.base.utils.ChineseToAlaboUtils; import com.qiuguo.iot.base.utils.StringUtils; import lombok.Data; @@ -51,25 +52,10 @@ public class ActionTime { DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - localDateTime = DateEnum.getDateWithCodeAndLocalDateTime(t, localDateTime, 0);//.getDateTime(localDateTime); + localDateTime = ChineseDateTimeUtils.getDateWithCodeAndLocalDateTime(t, localDateTime); detailTime = localDateTime; dateTime = localDateTime.format(df); dateDetailTime = localDateTime.format(df1); } - - public static void main(String[] args) { - - DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss E"); - LocalDateTime localDateTime = LocalDateTime.now(); - //localDateTime = DateEnum.getWithCode("国庆").getDateTime(DateEnum.getWithCode("拂晓").getDateTime(localDateTime)); - System.out.println(localDateTime.format(df1)); - System.out.println(DateEnum.getDateWithCodeAndLocalDateTime("周日", localDateTime, 0).format(df1)); - //System.out.println(DateEnum.getDateWithCodeAndLocalDateTime("下周一", localDateTime, 0).format(df1)); - - - - - } - } diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java index 2d060fe..e863a62 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java @@ -125,7 +125,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock log.info("设备端收到消息:{}", text); BoxTalkMessage boxTalkMessage = JSONObject.parseObject(text, BoxTalkMessage.class); BoxSession boxSession1 = getBoxSessionWithSn(boxTalkMessage.getSn()); - if(!boxSession.equals(boxSession1)){ + if(boxSession != boxSession1){ log.info("消息发送异常,或者未验签就收到信息不是同一个链接。可能传错SN"); return closeSendMsg(boxSession, "请等待验签结束或者SN可能错误", AskTypeEnum.TTS.getCode()).flatMap(b -> { return Mono.empty(); @@ -221,7 +221,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock DeviceInfoEntity dv = (DeviceInfoEntity)d; boxSession.setDeviceId(dv.getId()); - return bindBox(dv, userId, isBind).flatMap(db ->{ + return bindBox(boxSession, dv, userId, isBind).flatMap(db ->{ BoxSession oldBoxSession = getBoxSessionWithSn(sn); boxGroup.put(sn, boxSession); if(oldBoxSession != null){ @@ -233,7 +233,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock } - private Mono bindBox(DeviceInfoEntity dv, Long userId, Integer isBind){ + private Mono bindBox(BaseSession baseSession, DeviceInfoEntity dv, Long userId, Integer isBind){ log.info("开始绑定设备userId:{}, SN:{}", userId, dv); DeviceUserBindRequest request = new DeviceUserBindRequest(); @@ -277,18 +277,15 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock }); }); }else{ - BaseSession boxSession = getBoxSessionWithSn(dv.getSn()); - if(boxSession != null){ - return closeSendMsg(boxSession, "设备已解绑无法继续使用", AskTypeEnum.DEVICE_UNBIND.getCode()).flatMap( - v -> { - return Mono.just(entity); - } - ); - } + + return closeSendMsg(baseSession, "设备已解绑无法继续使用", AskTypeEnum.DEVICE_UNBIND.getCode()).flatMap( + v -> { + return Mono.just(entity); + } + ); + } - } - } return Mono.just(entity); });