Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 字典项id默认根据字典id及ordinal生成 #270

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
@Table(name = "s_dictionary_item",indexes = {
@Index(name = "idx_dic_item_dic_id",columnList = "dict_id"),
@Index(name = "idx_dic_item_ordinal",columnList = "ordinal"),
@Index(name = "idx_dic_item_path",columnList = "path"),
@Index(name = "idx_dic_item_ordinal_unique", columnList = "dict_id,ordinal",unique = true)
@Index(name = "idx_dic_item_path",columnList = "path")
})
@Comment("数据字典选项")
public class DictionaryItemEntity extends GenericTreeSortSupportEntity<String> implements EnumDict<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.hswebframework.web.dictionary.event.ClearDictionaryCacheEvent;
import org.hswebframework.web.exception.BusinessException;
import org.hswebframework.web.id.IDGenerator;
import org.hswebframework.web.utils.DigestUtils;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -94,7 +96,9 @@ public Flux<DictionaryItemEntity> fillOrdinal(Publisher<DictionaryItemEntity> pu
boolean isAllNull = list.stream().allMatch(item -> item.getOrdinal() == null);
boolean notNull = list.stream().allMatch(item -> item.getOrdinal() != null);
if (notNull) {
return Flux.fromIterable(list);
return Flux
.fromIterable(list)
.doOnNext(this::generateId);
}
if (isAllNull) {
return fillOrdinal(group.key(), list);
Expand All @@ -117,12 +121,23 @@ private Flux<DictionaryItemEntity> fillOrdinal(String dictId, List<DictionaryIte
.fromIterable(list)
.index()
.map(tp2 -> {
DictionaryItemEntity t2 = tp2.getT2();
DictionaryItemEntity item = tp2.getT2();
int ordinal = tp2.getT1().intValue() + maxOrdinal + 1;
t2.setOrdinal(ordinal);
return t2;
item.setOrdinal(ordinal);
generateId(item);
return item;
}));
}

private void generateId(DictionaryItemEntity item) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写到DictionaryItemEntity里?

if (StringUtils.hasText(item.getId())) {
return;
}
item.setId(generateId(item.getDictId(), item.getOrdinal()));
}

private String generateId(String dictId, Integer ordinal) {
return DigestUtils.md5Hex(String.join("|", dictId, ordinal.toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ public void save() {
.save(Flux.just(itemEntity, itemEntity2))
.then()
.as(StepVerifier::create)
.expectError(DuplicateKeyException.class)
.expectComplete()
.verify();


defaultDictionaryItemService
.query(new QueryParamEntity().noPaging())
.doOnNext(System.out::println)
.count()
.as(StepVerifier::create)
.expectNext(1L)
.verifyComplete();

itemEntity2.setOrdinal(null);

defaultDictionaryItemService
Expand Down Expand Up @@ -100,7 +109,7 @@ public void save() {
.doOnNext(System.out::println)
.count()
.as(StepVerifier::create)
.expectNext(4L)
.expectNext(3L)
.verifyComplete();


Expand Down