perf: 加强校验

dev
huangfeng 1 year ago
parent 02e7513f69
commit 09a420ef22

@ -45,4 +45,8 @@ public class Jg {
@Column(name = "xh")
private BigDecimal xh;
@ApiModelProperty("变电站名称")
@Transient
private String bdzName;
}

@ -10,9 +10,9 @@ public interface JgService {
Jg add(Jg item) throws Exception;
void update(Jg item);
void update(Jg item) throws Exception;
void delete(Integer id);
void delete(Integer id) throws Exception;
Jg detail(Integer id) throws Exception;
}

@ -2,12 +2,15 @@ package com.xydl.cac.service.impl;
import com.xydl.cac.entity.Bdz;
import com.xydl.cac.entity.Jg;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.repository.BdzRepository;
import com.xydl.cac.repository.JgRepository;
import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.JgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
@ -22,33 +25,66 @@ public class JgServiceImpl implements JgService {
JgRepository repository;
@Resource
BdzRepository bdzRepository;
@Resource
ZsbRepository zsbRepository;
@Override
public List<Jg> listAll(Integer bdzid) {
if (bdzid == null) {
return repository.findAll();
List<Jg> result = repository.findAll();
this.fillBdzName(result);
return result;
} else {
return repository.findByBdzid(bdzid);
List<Jg> result = repository.findByBdzid(bdzid);
Optional<Bdz> optional = bdzRepository.findById(bdzid);
if (optional.isPresent() && !CollectionUtils.isEmpty(result)) {
for (Jg jg : result) {
jg.setBdzName(optional.get().getMc());
}
}
return result;
}
}
private void fillBdzName(List<Jg> list) {
if (!CollectionUtils.isEmpty(list)) {
List<Bdz> bdzList = bdzRepository.findAll();
for (Jg jg : list) {
for (Bdz bdz : bdzList) {
if (bdz.getId().equals(jg.getBdzid())) {
jg.setBdzName(bdz.getMc());
break;
}
}
}
}
}
@Override
public Jg add(Jg item) throws Exception {
item.setId(null);
Optional<Bdz> optional = bdzRepository.findById(item.getBdzid());
if (!optional.isPresent()) {
Optional<Bdz> optionalBdz = bdzRepository.findById(item.getBdzid());
if (!optionalBdz.isPresent()) {
throw new Exception("未找到该变电站");
}
return repository.save(item);
}
@Override
public void update(Jg item) {
public void update(Jg item) throws Exception {
Optional<Bdz> optionalBdz = bdzRepository.findById(item.getBdzid());
if (!optionalBdz.isPresent()) {
throw new Exception("未找到该变电站");
}
repository.save(item);
}
@Override
public void delete(Integer id) {
public void delete(Integer id) throws Exception {
List<Zsb> zsbList = zsbRepository.findByJgid(id);
if (!CollectionUtils.isEmpty(zsbList)) {
throw new Exception("已被主设备使用不能删除");
}
repository.deleteById(id);
}
@ -58,6 +94,11 @@ public class JgServiceImpl implements JgService {
if (!optional.isPresent()) {
throw new Exception("未找到该区域");
}
return null;
Jg jg = optional.get();
Optional<Bdz> optionalBdz = bdzRepository.findById(jg.getBdzid());
if (optionalBdz.isPresent()) {
jg.setBdzName(optionalBdz.get().getMc());
}
return jg;
}
}

Loading…
Cancel
Save