Commit 5a4cc55917771b5ca63d4832bc30906f47705547
1 parent
2c3aaa8d
【修复】字典类型逻辑删除时,唯一索引冲突的问题
Showing
4 changed files
with
12 additions
and
8 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java
| ... | ... | @@ -12,6 +12,11 @@ import java.time.LocalDateTime; |
| 12 | 12 | */ |
| 13 | 13 | public class LocalDateTimeUtils { |
| 14 | 14 | |
| 15 | + /** | |
| 16 | + * 空的 LocalDateTime 对象,主要用于 DB 唯一索引的默认值 | |
| 17 | + */ | |
| 18 | + public static LocalDateTime EMPTY = buildTime(1970, 1, 1); | |
| 19 | + | |
| 15 | 20 | public static LocalDateTime addTime(Duration duration) { |
| 16 | 21 | return LocalDateTime.now().plus(duration); |
| 17 | 22 | } | ... | ... |
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; |
| 5 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; |
| 8 | -import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO; | |
| 9 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO; |
| 10 | -import org.apache.ibatis.annotations.Delete; | |
| 11 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 12 | 10 | import org.apache.ibatis.annotations.Param; |
| 13 | 11 | import org.apache.ibatis.annotations.Update; |
| ... | ... | @@ -43,10 +41,8 @@ public interface DictTypeMapper extends BaseMapperX<DictTypeDO> { |
| 43 | 41 | return selectOne(DictTypeDO::getName, name); |
| 44 | 42 | } |
| 45 | 43 | |
| 46 | - @Update("UPDATE system_dict_type SET DELETED = 1,DELETED_TIME=#{deletedTime} WHERE id = #{id}") | |
| 47 | 44 | int deleteById(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime); |
| 48 | 45 | |
| 49 | - default int deleteById(Long id) { | |
| 50 | - return deleteById(id, LocalDateTime.now()); | |
| 51 | - } | |
| 46 | + @Update("UPDATE system_dict_type SET deleted = 1, deleted_time = #{deletedTime} WHERE id = #{id}") | |
| 47 | + void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime); | |
| 52 | 48 | } | ... | ... |
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; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; | |
| 5 | 6 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; |
| ... | ... | @@ -58,7 +59,8 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 58 | 59 | // 校验正确性 |
| 59 | 60 | checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); |
| 60 | 61 | // 插入字典类型 |
| 61 | - DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO); | |
| 62 | + DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO) | |
| 63 | + .setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引,避免 null 值 | |
| 62 | 64 | dictTypeMapper.insert(dictType); |
| 63 | 65 | return dictType.getId(); |
| 64 | 66 | } |
| ... | ... | @@ -81,7 +83,7 @@ public class DictTypeServiceImpl implements DictTypeService { |
| 81 | 83 | throw exception(DICT_TYPE_HAS_CHILDREN); |
| 82 | 84 | } |
| 83 | 85 | // 删除字典类型 |
| 84 | - dictTypeMapper.deleteById(id); | |
| 86 | + dictTypeMapper.updateToDelete(id, LocalDateTime.now()); | |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | @Override | ... | ... |
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" ( |
| 111 | 111 | "updater" varchar(64) DEFAULT '', |
| 112 | 112 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 113 | 113 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 114 | + "deleted_time" timestamp NOT NULL, | |
| 114 | 115 | PRIMARY KEY ("id") |
| 115 | 116 | ) COMMENT '字典类型表'; |
| 116 | 117 | ... | ... |