郑楚滨
3 months ago
16 changed files with 388 additions and 52 deletions
@ -0,0 +1,108 @@ |
|||
package demo.Controller; |
|||
|
|||
import demo.Entity.CaseTree; |
|||
import demo.Entity.CaseTreeVO; |
|||
import demo.Entity.Result; |
|||
import demo.Service.CaseTreeService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import javax.validation.Valid; |
|||
import java.util.List; |
|||
|
|||
@RestController |
|||
@RequestMapping("/casetree") |
|||
@Validated |
|||
public class CaseTreeController { |
|||
@Autowired |
|||
private CaseTreeService caseTreeService; |
|||
|
|||
@PostMapping("/insert") |
|||
public Result<String> insertNode(@Valid CaseTreeVO caseTreeVO) { |
|||
//确认新建节点名称未被占用
|
|||
CaseTree caseTree = caseTreeService.findByNodeName(caseTreeVO.getNodeName()); |
|||
if(caseTree == null) { |
|||
caseTreeService.addNode(caseTreeVO.getNodeName(), caseTreeVO.getParentNodeId(), caseTreeVO.getCreator()); |
|||
return Result.success("新建节点成功"); |
|||
} |
|||
else{ |
|||
return Result.error(401,"节点已存在"); |
|||
} |
|||
} |
|||
|
|||
@DeleteMapping("/delete") |
|||
public Result<String> deleteNode(@Valid CaseTreeVO caseTreeVO) { |
|||
if(caseTreeVO.getId() != null){ |
|||
CaseTree caseTreeByNodeId = caseTreeService.findByNodeId(caseTreeVO.getId()); |
|||
if(caseTreeByNodeId == null){ |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
if(caseTreeByNodeId.getIsLeaf() > 0){ |
|||
return Result.error(401,"节点非叶子节点,无法删除"); |
|||
} |
|||
else{ |
|||
caseTreeService.deleteNode(caseTreeByNodeId); |
|||
return Result.success("删除节点成功"); |
|||
} |
|||
} |
|||
else if(caseTreeVO.getNodeName() != null){ |
|||
CaseTree caseTreeByNodeName = caseTreeService.findByNodeName(caseTreeVO.getNodeName()); |
|||
if(caseTreeByNodeName == null){ |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
if(caseTreeByNodeName.getIsLeaf() > 0){ |
|||
return Result.error(401,"节点非叶子节点,无法删除"); |
|||
} |
|||
else{ |
|||
caseTreeService.deleteNode(caseTreeByNodeName); |
|||
return Result.success("删除节点成功"); |
|||
} |
|||
} |
|||
else{ |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
} |
|||
|
|||
@PutMapping("/update") |
|||
public Result<String> updateNode(@Valid CaseTreeVO caseTreeVO) { |
|||
CaseTree caseTree = caseTreeService.findByNodeId(caseTreeVO.getId()); |
|||
caseTree.setNodeName(caseTreeVO.getNodeName() == null? caseTree.getNodeName() : caseTreeVO.getNodeName()); |
|||
caseTree.setParentNodeId(caseTreeVO.getParentNodeId() == null? caseTree.getParentNodeId() : caseTreeVO.getParentNodeId()); |
|||
caseTree.setEditor(caseTreeVO.getEditor() == null? caseTree.getEditor() : caseTreeVO.getEditor()); |
|||
if(caseTree == null) { |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
else{ |
|||
caseTreeService.updateNode(caseTreeVO); |
|||
return Result.success("修改节点成功"); |
|||
} |
|||
} |
|||
|
|||
@GetMapping("/query") |
|||
public Result<List<CaseTree>> queryNode() { |
|||
List<CaseTree> caseTreeList = caseTreeService.query(); |
|||
return Result.success(caseTreeList); |
|||
} |
|||
|
|||
@PostMapping("/query") |
|||
public Result<CaseTree> queryByNode(@Valid CaseTreeVO caseTreeVO) { |
|||
CaseTree caseTree = new CaseTree(); |
|||
if(caseTreeVO.getId() != null){ |
|||
caseTree = caseTreeService.findByNodeId(caseTreeVO.getId()); |
|||
if(caseTreeVO.getNodeName() != null && !caseTreeVO.getNodeName().equals(caseTree.getNodeName())){ |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
} |
|||
else if(caseTreeVO.getNodeName() != null){ |
|||
caseTree = caseTreeService.findByNodeName(caseTreeVO.getNodeName()); |
|||
} |
|||
else{ |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
if(caseTree == null) { |
|||
return Result.error(401,"节点不存在"); |
|||
} |
|||
else return Result.success(caseTree); |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
package demo.Entity; |
|||
|
|||
public class CaseInfo { |
|||
} |
@ -0,0 +1,41 @@ |
|||
package demo.Entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.Date; |
|||
|
|||
@Entity |
|||
@Data |
|||
@Table(name = "case_tree") |
|||
public class CaseTree { |
|||
@Id |
|||
@Column(length = 32, nullable = false) |
|||
private int id; |
|||
|
|||
@Column(length = 50) |
|||
private String nodeName; |
|||
|
|||
@Column(length = 50) |
|||
private int parentNodeId; |
|||
|
|||
@Column(length = 32, nullable = false) |
|||
private int isLeaf; |
|||
|
|||
@Column(length = 350) |
|||
private String idPath; |
|||
|
|||
@Column(length = 50) |
|||
private String creator; |
|||
|
|||
@Temporal(TemporalType.TIMESTAMP) |
|||
@Column() |
|||
private Date createTime; |
|||
|
|||
@Column(length = 50) |
|||
private String editor; |
|||
|
|||
@Temporal(TemporalType.TIMESTAMP) |
|||
@Column() |
|||
private Date updateTime; |
|||
} |
@ -0,0 +1,12 @@ |
|||
package demo.Entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class CaseTreeVO { |
|||
private Integer id; |
|||
private String nodeName; |
|||
private Integer parentNodeId; |
|||
private String creator; |
|||
private String editor; |
|||
} |
@ -0,0 +1,39 @@ |
|||
package demo.Mapper; |
|||
|
|||
import demo.Entity.CaseTree; |
|||
import org.apache.ibatis.annotations.*; |
|||
|
|||
import javax.persistence.criteria.CriteriaBuilder; |
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface CaseTreeMapper { |
|||
//根据id查找是否存在
|
|||
@Select("SELECT * FROM case_tree WHERE ID = #{id}") |
|||
CaseTree findByNodeId(@Param("id") int id); |
|||
|
|||
//根据节点名称找id
|
|||
@Select("SELECT * FROM case_tree WHERE NODE_NAME = #{nodeName}") |
|||
CaseTree findByNodeName(@Param("nodeName") String nodeName); |
|||
|
|||
@Insert("INSERT INTO case_tree (NODE_NAME, ID_PATH, CREATOR, CREATE_TIME, EDITOR, UPDATE_TIME) VALUES (#{nodeName}, NULL, #{creator}, NOW(), #{creator}, NOW())") |
|||
void addNode(@Param("nodeName") String nodeName, @Param("creator") String creator); |
|||
|
|||
@Update("UPDATE case_tree SET PARENT_NODE_ID = #{parentNodeId}, IS_LEAF = #{isLeaf} WHERE ID = #{id}") |
|||
void set(@Param("id") Integer id, @Param("parentNodeId") Integer parentNodeId, @Param("isLeaf") Integer isLeaf); |
|||
|
|||
@Update("UPDATE case_tree SET IS_LEAF = #{isLeaf} WHERE ID = #{id}") |
|||
void setIsLeaf(@Param("id") Integer id, @Param("isLeaf") Integer isLeaf); |
|||
|
|||
@Update("UPDATE case_tree SET ID_PATH = #{idPath} WHERE ID = #{id}") |
|||
void setPath(@Param("id") int id, @Param("idPath") String path); |
|||
|
|||
@Update("UPDATE case_tree SET NODE_NAME = #{nodeName}, PARENT_NODE_ID = #{parentNodeId}, EDITOR = #{editor}, UPDATE_TIME = NOW() WHERE ID = #{id}") |
|||
void updateNode(@Param("id") int id, @Param("nodeName") String nodeName, @Param("parentNodeId") Integer parentNodeId, @Param("editor") String editor); |
|||
|
|||
@Delete("DELETE FROM case_tree WHERE ID = #{id}") |
|||
void deleteNodeByNodeId(@Param("id") Integer id); |
|||
|
|||
@Select("SELECT * FROM case_tree") |
|||
List<CaseTree> findAll(); |
|||
} |
@ -0,0 +1,70 @@ |
|||
package demo.Service; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.persistence.EntityManager; |
|||
import javax.persistence.PersistenceContext; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
|
|||
@Service |
|||
public class CaseInfoService { |
|||
|
|||
// @PersistenceContext
|
|||
// private EntityManager entityManager;
|
|||
//
|
|||
// public void addCase(String caseName, String nodeId, String creator) {
|
|||
// // 确认节点为叶子节点
|
|||
// Optional<CaseTree> optionalNode = entityManager.createQuery(
|
|||
// "SELECT n FROM CaseTree n WHERE n.id = :id", CaseTree.class)
|
|||
// .setParameter("id", nodeId).getResultList().stream().findFirst();
|
|||
//
|
|||
// if (optionalNode.isPresent() && "1".equals(optionalNode.get().getIsLeaf())) {
|
|||
// String id = generateNewId();
|
|||
// CaseInfo newCase = new CaseInfo();
|
|||
// newCase.setId(id);
|
|||
// newCase.setCaseName(caseName);
|
|||
// newCase.setNodeId(nodeId);
|
|||
// newCase.setCreator(creator);
|
|||
// newCase.setCreateTime(new Date());
|
|||
// newCase.setEditor(creator);
|
|||
// newCase.setUpdateTime(new Date());
|
|||
//
|
|||
// entityManager.persist(newCase);
|
|||
// } else {
|
|||
// throw new IllegalArgumentException("节点不是叶子节点,不能新增用例");
|
|||
// }
|
|||
// }
|
|||
//
|
|||
// @Transactional
|
|||
// public void deleteCase(String id) {
|
|||
// CaseInfo caseInfo = entityManager.find(CaseInfo.class, id);
|
|||
// if (caseInfo != null) {
|
|||
// entityManager.remove(caseInfo);
|
|||
// }
|
|||
// }
|
|||
//
|
|||
// public void updateCase(String id, String caseName, String editor) {
|
|||
// Optional<CaseInfo> optionalCase = entityManager.createQuery(
|
|||
// "SELECT c FROM CaseInfo c WHERE c.id = :id", CaseInfo.class)
|
|||
// .setParameter("id", id).getResultList().stream().findFirst();
|
|||
//
|
|||
// optionalCase.ifPresent(caseInfo -> {
|
|||
// caseInfo.setCaseName(caseName);
|
|||
// caseInfo.setEditor(editor);
|
|||
// caseInfo.setUpdateTime(new Date());
|
|||
// entityManager.merge(caseInfo);
|
|||
// });
|
|||
// }
|
|||
//
|
|||
// public List<CaseInfo> getCases() {
|
|||
// return entityManager.createQuery("FROM CaseInfo", CaseInfo.class).getResultList();
|
|||
// }
|
|||
//
|
|||
// private String generateNewId() {
|
|||
// // 实现生成唯一ID的逻辑
|
|||
// return UUID.randomUUID().toString().replace("-", "");
|
|||
// }
|
|||
} |
@ -0,0 +1,73 @@ |
|||
package demo.Service; |
|||
|
|||
import demo.Entity.CaseTree; |
|||
import demo.Entity.CaseTreeVO; |
|||
import demo.Mapper.CaseTreeMapper; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import javax.transaction.Transactional; |
|||
import java.util.List; |
|||
|
|||
@Service |
|||
public class CaseTreeService { |
|||
|
|||
@Autowired |
|||
private CaseTreeMapper caseTreeMapper; |
|||
|
|||
@Transactional(rollbackOn = Exception.class) |
|||
public void addNode(String nodeName, Integer parentNodeId, String creator) { |
|||
if(parentNodeId == null) { |
|||
caseTreeMapper.addNode(nodeName, creator); |
|||
CaseTree caseTree = caseTreeMapper.findByNodeName(nodeName); |
|||
caseTreeMapper.set(caseTree.getId(), caseTree.getId(), 0); |
|||
caseTreeMapper.setPath(caseTree.getId(), caseTree.getId() + ""); |
|||
} |
|||
else { |
|||
caseTreeMapper.addNode(nodeName, creator); |
|||
CaseTree caseTree = caseTreeMapper.findByNodeName(nodeName); |
|||
caseTreeMapper.set(caseTree.getId(), parentNodeId, 0); |
|||
CaseTree parentNode = caseTreeMapper.findByNodeId(parentNodeId); |
|||
caseTreeMapper.setIsLeaf(parentNodeId, parentNode.getIsLeaf() + 1); |
|||
caseTreeMapper.setPath(caseTree.getId(), parentNode.getIdPath() + "/" + caseTree.getId()); |
|||
} |
|||
} |
|||
|
|||
public CaseTree findByNodeId(Integer id){ |
|||
return caseTreeMapper.findByNodeId(id); |
|||
} |
|||
|
|||
public CaseTree findByNodeName(String nodeName) { |
|||
return caseTreeMapper.findByNodeName(nodeName); |
|||
} |
|||
|
|||
@Transactional(rollbackOn = Exception.class) |
|||
public void deleteNode(CaseTree caseTree){ |
|||
caseTreeMapper.deleteNodeByNodeId(caseTree.getId()); |
|||
CaseTree parentNode = caseTreeMapper.findByNodeId(caseTree.getParentNodeId()); |
|||
if(parentNode != null) { |
|||
caseTreeMapper.setIsLeaf(caseTree.getParentNodeId(), parentNode.getIsLeaf() - 1); |
|||
} |
|||
} |
|||
|
|||
@Transactional(rollbackOn = Exception.class) |
|||
public void updateNode(CaseTreeVO caseTreeVO) { |
|||
caseTreeMapper.updateNode(caseTreeVO.getId(), caseTreeVO.getNodeName(), caseTreeVO.getParentNodeId(), caseTreeVO.getEditor()); |
|||
caseTreeMapper.set(caseTreeVO.getId(), caseTreeVO.getParentNodeId(), 0); |
|||
CaseTree parentNode = caseTreeMapper.findByNodeId(caseTreeVO.getParentNodeId()); |
|||
caseTreeMapper.setIsLeaf(caseTreeVO.getParentNodeId(), parentNode.getIsLeaf() + 1); |
|||
if(parentNode.getIsLeaf() > 0) |
|||
caseTreeMapper.setPath(caseTreeVO.getId(), parentNode.getIdPath() + "/" + caseTreeVO.getId()); |
|||
else |
|||
caseTreeMapper.setPath(caseTreeVO.getId(), caseTreeVO.getId() + ""); |
|||
} |
|||
|
|||
public List<CaseTree> query() { |
|||
return caseTreeMapper.findAll(); |
|||
} |
|||
//
|
|||
// private String generateNewId() {
|
|||
// // 实现生成唯一ID的逻辑
|
|||
// return UUID.randomUUID().toString().replace("-", "");
|
|||
// }
|
|||
} |
Loading…
Reference in new issue