【修复】字典类型逻辑删除时,唯一索引冲突的问题

This commit is contained in:
YunaiV 2022-12-29 12:52:37 +08:00
parent 2c3aaa8d7e
commit 5a4cc55917
4 changed files with 12 additions and 8 deletions

View File

@ -12,6 +12,11 @@ import java.time.LocalDateTime;
*/ */
public class LocalDateTimeUtils { public class LocalDateTimeUtils {
/**
* 空的 LocalDateTime 对象主要用于 DB 唯一索引的默认值
*/
public static LocalDateTime EMPTY = buildTime(1970, 1, 1);
public static LocalDateTime addTime(Duration duration) { public static LocalDateTime addTime(Duration duration) {
return LocalDateTime.now().plus(duration); return LocalDateTime.now().plus(duration);
} }

View File

@ -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.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.DictTypeExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; 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 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.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
@ -43,10 +41,8 @@ public interface DictTypeMapper extends BaseMapperX<DictTypeDO> {
return selectOne(DictTypeDO::getName, name); 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); int deleteById(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime);
default int deleteById(Long id) { @Update("UPDATE system_dict_type SET deleted = 1, deleted_time = #{deletedTime} WHERE id = #{id}")
return deleteById(id, LocalDateTime.now()); void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime);
}
} }

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.dict;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.DictTypeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; 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.controller.admin.dict.vo.type.DictTypePageReqVO;
@ -58,7 +59,8 @@ public class DictTypeServiceImpl implements DictTypeService {
// 校验正确性 // 校验正确性
checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); 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); dictTypeMapper.insert(dictType);
return dictType.getId(); return dictType.getId();
} }
@ -81,7 +83,7 @@ public class DictTypeServiceImpl implements DictTypeService {
throw exception(DICT_TYPE_HAS_CHILDREN); throw exception(DICT_TYPE_HAS_CHILDREN);
} }
// 删除字典类型 // 删除字典类型
dictTypeMapper.deleteById(id); dictTypeMapper.updateToDelete(id, LocalDateTime.now());
} }
@Override @Override

View File

@ -111,6 +111,7 @@ CREATE TABLE IF NOT EXISTS "system_dict_type" (
"updater" varchar(64) DEFAULT '', "updater" varchar(64) DEFAULT '',
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE, "deleted" bit NOT NULL DEFAULT FALSE,
"deleted_time" timestamp NOT NULL,
PRIMARY KEY ("id") PRIMARY KEY ("id")
) COMMENT '字典类型表'; ) COMMENT '字典类型表';