- 新增仓位列表接口 /api/stocks/list - 新增数据字典列表接口 /api/dicts/list - 重构 BaseRepository 类,支持自定义查询条件和分页 - 优化 DictRepository 和 DictService,增加 countRow 方法 - 修改 DictResource 和 StockResource,支持带参数的列表查询 - 更新前端菜单编辑组件,添加分页相关功能
125 lines
4.7 KiB
Java
125 lines
4.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.getOrganization(), 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.getOrganization(), dict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
dict.setChildren(tuple.getT2());
|
|
return dict;
|
|
})
|
|
);
|
|
}
|
|
|
|
public Mono<Long> countDicts(Map<String, Object> params) {
|
|
return SecurityUtils.getCurrentOrganization().flatMap(organization -> dictRepository.countDicts(organization, params));
|
|
}
|
|
|
|
public Flux<Dict> getDicts(Map<String, Object> params, Pageable pageable) {
|
|
return SecurityUtils.getCurrentOrganization()
|
|
.map(organization -> dictRepository.findDicts(organization, params, pageable))
|
|
.flatMapMany(dicts -> dicts);
|
|
}
|
|
|
|
public Mono<Long> countRootDicts(Map<String, Object> params) {
|
|
return SecurityUtils.getCurrentOrganization().flatMap(organization -> dictRepository.countRootDicts(organization, params));
|
|
}
|
|
|
|
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.getOrganization(), rootDict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
rootDict.setChildren(tuple.getT2());
|
|
return rootDict;
|
|
})
|
|
)
|
|
);
|
|
}
|
|
|
|
public Mono<Long> countRow(String table, Map<String, Object> params) {
|
|
return SecurityUtils.getCurrentOrganization().flatMap(organization -> dictRepository.countRow(organization, table, params));
|
|
}
|
|
|
|
public Flux<Map<String, Object>> getList(String table, Map<String, Object> params, Pageable pageable) {
|
|
return SecurityUtils.getCurrentOrganization()
|
|
.flatMapMany(organization -> dictRepository.getList(organization, table, params, pageable));
|
|
}
|
|
|
|
private Flux<Dict> getChildren(String organization, String number) {
|
|
return dictRepository
|
|
.findByParentNumber(organization, number)
|
|
.flatMap(dict ->
|
|
Mono.just(dict)
|
|
.zipWith(getChildren(dict.getOrganization(), dict.getNumber()).collectList())
|
|
.map(tuple -> {
|
|
dict.setChildren(tuple.getT2());
|
|
return dict;
|
|
})
|
|
);
|
|
}
|
|
}
|