From 5a4cc55917771b5ca63d4832bc30906f47705547 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 29 Dec 2022 12:52:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E7=B1=BB=E5=9E=8B=E9=80=BB=E8=BE=91=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=94=AF=E4=B8=80=E7=B4=A2=E5=BC=95=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/common/util/date/LocalDateTimeUtils.java | 5 +++++ .../module/system/dal/mysql/dict/DictTypeMapper.java | 8 ++------ .../module/system/service/dict/DictTypeServiceImpl.java | 6 ++++-- .../src/test/resources/sql/create_tables.sql | 1 + 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java index 7af32ea82..a29d5fb2e 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java @@ -12,6 +12,11 @@ import java.time.LocalDateTime; */ public class LocalDateTimeUtils { + /** + * 空的 LocalDateTime 对象,主要用于 DB 唯一索引的默认值 + */ + public static LocalDateTime EMPTY = buildTime(1970, 1, 1); + public static LocalDateTime addTime(Duration duration) { return LocalDateTime.now().plus(duration); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictTypeMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictTypeMapper.java index 31abfc8f6..261f18cc4 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictTypeMapper.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictTypeMapper.java @@ -5,9 +5,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO; import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO; -import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; @@ -43,10 +41,8 @@ public interface DictTypeMapper extends BaseMapperX { return selectOne(DictTypeDO::getName, name); } - @Update("UPDATE system_dict_type SET DELETED = 1,DELETED_TIME=#{deletedTime} WHERE id = #{id}") int deleteById(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime); - default int deleteById(Long id) { - return deleteById(id, LocalDateTime.now()); - } + @Update("UPDATE system_dict_type SET deleted = 1, deleted_time = #{deletedTime} WHERE id = #{id}") + void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java index af698a2b0..07fcf40f3 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.dict; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; @@ -58,7 +59,8 @@ public class DictTypeServiceImpl implements DictTypeService { // 校验正确性 checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); // 插入字典类型 - DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO); + DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO) + .setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引,避免 null 值 dictTypeMapper.insert(dictType); return dictType.getId(); } @@ -81,7 +83,7 @@ public class DictTypeServiceImpl implements DictTypeService { throw exception(DICT_TYPE_HAS_CHILDREN); } // 删除字典类型 - dictTypeMapper.deleteById(id); + dictTypeMapper.updateToDelete(id, LocalDateTime.now()); } @Override diff --git a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql index 56fc42704..7590e57a3 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql +++ b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql @@ -111,6 +111,7 @@ CREATE TABLE IF NOT EXISTS "system_dict_type" ( "updater" varchar(64) DEFAULT '', "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, + "deleted_time" timestamp NOT NULL, PRIMARY KEY ("id") ) COMMENT '字典类型表';