108 lines
3.7 KiB
Java
108 lines
3.7 KiB
Java
package com.vxnet.pms.service;
|
|
|
|
import com.vxnet.pms.domain.Dict;
|
|
import com.vxnet.pms.repository.DictRepository;
|
|
import com.vxnet.pms.security.SecurityUtils;
|
|
import java.util.Map;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
@Service
|
|
public class DictService {
|
|
|
|
private final DictRepository dictRepository;
|
|
|
|
public DictService(DictRepository dictRepository) {
|
|
this.dictRepository = dictRepository;
|
|
}
|
|
|
|
public Mono<Dict> createDict(Dict dict) {
|
|
return SecurityUtils.getCurrentOrganization()
|
|
.map(organization -> {
|
|
dict.setOrganization(organization);
|
|
dict.setVersion(null);
|
|
return dict;
|
|
})
|
|
.flatMap(dictRepository::save);
|
|
}
|
|
|
|
public Mono<Dict> updateDict(Long id, Dict dict) {
|
|
return dictRepository
|
|
.findById(id)
|
|
.flatMap(existingDict -> {
|
|
existingDict.setNumber(dict.getNumber());
|
|
existingDict.setName(dict.getName());
|
|
existingDict.setParentNumber(dict.getParentNumber());
|
|
existingDict.setProperty(dict.getProperty());
|
|
existingDict.setStatus(dict.getStatus());
|
|
existingDict.setRemark(dict.getRemark());
|
|
existingDict.setSortNo(dict.getSortNo());
|
|
return dictRepository.save(existingDict);
|
|
});
|
|
}
|
|
|
|
public Mono<Void> deleteDict(Long id) {
|
|
return dictRepository
|
|
.findById(id)
|
|
.flatMap(dict ->
|
|
dictRepository
|
|
.countChildren(dict.getNumber())
|
|
.flatMap(count -> {
|
|
if (count > 0) {
|
|
return Mono.error(new IllegalStateException("Cannot delete dict with children"));
|
|
}
|
|
return dictRepository.deleteById(id);
|
|
})
|
|
);
|
|
}
|
|
|
|
public Mono<Dict> getDict(Long id) {
|
|
return dictRepository
|
|
.findById(id)
|
|
.flatMap(dict ->
|
|
Mono.just(dict)
|
|
.zipWith(getChildren(dict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
dict.setChildren(tuple.getT2());
|
|
return dict;
|
|
})
|
|
);
|
|
}
|
|
|
|
public Flux<Dict> getDicts(Map<String, Object> params, Pageable pageable) {
|
|
return SecurityUtils.getCurrentOrganization()
|
|
.map(organization -> dictRepository.findDicts(organization, params, pageable))
|
|
.flatMapMany(dicts -> dicts);
|
|
}
|
|
|
|
public Flux<Dict> getDictTree(Map<String, Object> params, Pageable pageable) {
|
|
return SecurityUtils.getCurrentOrganization()
|
|
.map(organization -> dictRepository.findRootDicts(organization, params, pageable))
|
|
.flatMapMany(rootDicts ->
|
|
rootDicts.flatMap(rootDict ->
|
|
Mono.just(rootDict)
|
|
.zipWith(getChildren(rootDict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
rootDict.setChildren(tuple.getT2());
|
|
return rootDict;
|
|
})
|
|
)
|
|
);
|
|
}
|
|
|
|
private Flux<Dict> getChildren(String number) {
|
|
return dictRepository
|
|
.findByParentNumber(number)
|
|
.flatMap(dict ->
|
|
Mono.just(dict)
|
|
.zipWith(getChildren(dict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
dict.setChildren(tuple.getT2());
|
|
return dict;
|
|
})
|
|
);
|
|
}
|
|
}
|