From 5fb26fc7e16624f4cfe7273fe06f7ece8775c896 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 13 May 2021 19:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=200.11.0=20=E5=A2=9E=E5=8A=A0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9DateTime=E7=B1=BB=E5=9E=8B=E6=96=B9=E6=B3=95=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9Repo=20get=E6=96=B9=E6=B3=95=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../gaea/mongo/repository/MongoRepository.kt | 4 +- .../com/synebula/gaea/data/date/AlignTime.kt | 10 +- .../com/synebula/gaea/data/date/DateTime.kt | 196 +++++++++++++++--- .../gaea/domain/repository/IRepository.kt | 2 +- 5 files changed, 176 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 1a019b2..8562eaa 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ allprojects { subprojects { ext { - version '0.10.3' + version '0.11.0' spring_version = "2.3.0.RELEASE" } diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt index 06c0fd7..75c7869 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt @@ -20,8 +20,8 @@ open class MongoRepository(private var repo: MongoTemplate) : IRepository { override fun , TKey> get( id: TKey, clazz: Class - ): TAggregateRoot { - return this.repo.findOne(whereId(id), clazz) as TAggregateRoot + ): TAggregateRoot? { + return this.repo.findOne(whereId(id), clazz) } override fun , TKey> update( diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/AlignTime.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/AlignTime.kt index 0b10fe4..76b4a1f 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/AlignTime.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/AlignTime.kt @@ -1,6 +1,8 @@ package com.synebula.gaea.data.date import java.util.* +import kotlin.math.ceil +import kotlin.math.floor /** * 校准时间。 @@ -61,9 +63,9 @@ class AlignTime { */ fun ceilingTime(lastTime: DateTime, intervalSeconds: Int): DateTime { val span = lastTime - this.baseTime - val count = Math.ceil(span.totalSeconds / intervalSeconds).toInt() + val count = ceil(span.totalSeconds / intervalSeconds).toInt() val newTime = DateTime(this.baseTime.date) - newTime.addSeconds(count * intervalSeconds * 1L) + newTime.addSecond(count * intervalSeconds) return newTime } @@ -97,9 +99,9 @@ class AlignTime { */ fun floorTime(lastTime: DateTime, intervalSeconds: Int): DateTime { val span = lastTime - this.baseTime - val count = Math.floor(span.totalSeconds / intervalSeconds).toInt() + val count = floor(span.totalSeconds / intervalSeconds).toInt() val newTime = DateTime(this.baseTime.date) - newTime.addSeconds(count * intervalSeconds * 1L) + newTime.addSecond(count * intervalSeconds) return newTime } } \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/DateTime.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/DateTime.kt index b227570..03a0eab 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/DateTime.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/date/DateTime.kt @@ -7,7 +7,7 @@ import java.util.* /** * 时间格式,方便用于获取Date格式的多种形式 */ -class DateTime : Comparable { +class DateTime() : Comparable { // 内部存储日历格式方便操作 /** @@ -195,28 +195,25 @@ class DateTime : Comparable { /** * 从Date格式转化 */ - constructor(date: Date) { + constructor(date: Date) : this() { this.calendar.time = date - this.calendar.set(Calendar.MILLISECOND, 0) } /** * 从Calendar格式转化 */ - constructor(calendar: Calendar) { + constructor(calendar: Calendar) : this() { this.calendar = calendar - this.calendar.set(Calendar.MILLISECOND, 0) } /** * 从Date格式转化 */ - constructor(date: String, format: String) { + constructor(date: String, format: String = "yyyy-MM-dd HH:mm:ss") : this() { val formatter = SimpleDateFormat(format) try { val value = formatter.parse(date) this.calendar.time = value - this.calendar.set(Calendar.MILLISECOND, 0) } catch (e: ParseException) { throw RuntimeException("date string can't format to date", e) } @@ -225,9 +222,8 @@ class DateTime : Comparable { /** * 从Date格式转化。需要注意的是月0代表是一月,以此类推。 */ - constructor(year: Int, month: Int, day: Int = 0, hour: Int = 0, minute: Int = 0, second: Int = 0) { + constructor(year: Int, month: Int, day: Int = 0, hour: Int = 0, minute: Int = 0, second: Int = 0) : this() { this.calendar.set(year, month, day, hour, minute, second) - this.calendar.set(Calendar.MILLISECOND, 0) } /** @@ -294,25 +290,12 @@ class DateTime : Comparable { return this.compareTo(start, level) >= 0 && this.compareTo(end, level) <= 0 } - /** - * 增加秒 - */ - fun addSeconds(seconds: Long) { - if (seconds <= Int.MAX_VALUE) - this.calendar.add(Calendar.SECOND, seconds.toInt()) - else { - val span = TimeSpan(seconds * 1000) - this.calendar.add(Calendar.DAY_OF_MONTH, span.day) - this.calendar.add(Calendar.HOUR_OF_DAY, span.hour) - this.calendar.add(Calendar.MINUTE, span.minute) - this.calendar.add(Calendar.SECOND, span.second) - } - } /** * 判断当前时间是否在某时间后 * * @param other 另一时间 + * @return new DateTime object */ fun after(other: DateTime): Boolean { return this.date.after(other.date) @@ -322,31 +305,165 @@ class DateTime : Comparable { * 判断当前时间是否在某时间前 * * @param other 另一时间 + * @return new DateTime object */ fun before(other: DateTime): Boolean { return this.date.before(other.date) } + + /** + * 加减年。 + * + * @param year 月份。可以为负数,即为减。 + * @return this + */ + fun addYear(year: Int): DateTime { + this.calendar.add(Calendar.YEAR, year) + return this + } + /** * 加减月份。 * * @param month 月份。可以为负数,即为减。 + * @return this */ fun addMonth(month: Int): DateTime { - val instance = calendar.clone() as Calendar - instance.add(Calendar.MONTH, month) - return DateTime(instance) + this.calendar.add(Calendar.MONTH, month) + return this } + /** * 加减日期。 * * @param day 天数。可以为负数,即为减。 + * @return this */ fun addDay(day: Int): DateTime { - val instance = calendar.clone() as Calendar - instance.add(Calendar.DAY_OF_MONTH, day) - return DateTime(instance) + this.calendar.add(Calendar.DAY_OF_MONTH, day) + return this + } + + + /** + * 加减小时。 + * + * @param hour 小时。可以为负数,即为减。 + * @return this + */ + fun addHour(hour: Int): DateTime { + this.calendar.add(Calendar.HOUR_OF_DAY, hour) + return this + } + + /** + * 加减分钟。 + * + * @param minute 分钟。可以为负数,即为减。 + * @return this + */ + fun addMinute(minute: Int): DateTime { + this.calendar.add(Calendar.MINUTE, minute) + return this + } + + /** + * 加减秒 + * @param second 秒数。可以为负数,即为减。 + * @return this + */ + fun addSecond(second: Int): DateTime { + this.calendar.add(Calendar.SECOND, second) + return this + } + + /** + * 加减毫秒 + * @param millisecond 毫秒数。可以为负数,即为减。 + * @return this + */ + fun addMillisecond(millisecond: Int): DateTime { + this.calendar.add(Calendar.MILLISECOND, millisecond) + return this + } + + /** + * 设置年。 + * + * @param year 月份。 + * @return this + */ + fun setYear(year: Int): DateTime { + this.calendar.set(Calendar.YEAR, year) + return this + } + + /** + * 设置月份。 + * + * @param month 月份。 + * @return this + */ + fun setMonth(month: Int): DateTime { + this.calendar.set(Calendar.MONTH, month) + return this + } + + + /** + * 设置日期。 + * + * @param day 天数。 + * @return this + */ + fun setDay(day: Int): DateTime { + this.calendar.set(Calendar.DAY_OF_MONTH, day) + return this + } + + + /** + * 设置小时。 + * + * @param hour 小时。 + * @return this + */ + fun setHour(hour: Int): DateTime { + this.calendar.set(Calendar.HOUR_OF_DAY, hour) + return this + } + + /** + * 设置分钟。 + * + * @param minute 分钟。 + * @return this + */ + fun setMinute(minute: Int): DateTime { + this.calendar.set(Calendar.MINUTE, minute) + return this + } + + /** + * 设置秒 + * @param second 总秒数。 + * @return this + */ + fun setSecond(second: Int): DateTime { + this.calendar.set(Calendar.SECOND, second) + return this + } + + /** + * 设置毫秒。 + * @param millisecond 毫秒数。 + * @return this + */ + fun setMillisecond(millisecond: Int): DateTime { + this.calendar.set(Calendar.MILLISECOND, millisecond) + return this } /** @@ -360,10 +477,29 @@ class DateTime : Comparable { * 当前天是否周末 */ fun isWeekend(): Boolean { - val dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) + val dayOfWeek = this.calendar.get(Calendar.DAY_OF_WEEK) return (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) } + /** + * 清空毫秒,避免误差 + */ + fun clearMillisecond() { + this.calendar.set(Calendar.MILLISECOND, 0) + } + + /** + * 克隆当前对象 + * + * @return 返回新的DateTime对象 + */ + fun clone(): DateTime { + return DateTime(this.calendar) + } + + /** + * 时间相见 + */ operator fun minus(other: DateTime): TimeSpan { return TimeSpan(this.milliseconds - other.milliseconds) } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt index 83dba92..8c653ef 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt @@ -46,7 +46,7 @@ interface IRepository { * @param clazz 操作数据的类型 * @return 聚合根 */ - fun , TKey> get(id: TKey, clazz: Class): TAggregateRoot + fun , TKey> get(id: TKey, clazz: Class): TAggregateRoot? /**