郑楚滨
3 months ago
8 changed files with 297 additions and 12 deletions
@ -0,0 +1,140 @@ |
|||
package demo.Controller; |
|||
|
|||
import com.github.pagehelper.PageHelper; |
|||
import com.github.pagehelper.PageInfo; |
|||
import demo.Entity.*; |
|||
import demo.Service.CaseInfoService; |
|||
import demo.Service.CaseTreeService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Slf4j |
|||
@RestController |
|||
@RequestMapping("/caseinfo") |
|||
@Validated |
|||
public class CaseInfoController { |
|||
@Autowired |
|||
private CaseInfoService caseInfoService; |
|||
@Autowired |
|||
private CaseTreeService caseTreeService; |
|||
|
|||
@PostMapping("/insert") |
|||
public Result<String> insert(@Validated CaseInfoVO caseInfoVO) { |
|||
if(caseInfoVO.getCaseName() == null || caseInfoVO.getNodeId() == null){ |
|||
return Result.error(401, "新增用例包括用例名称、用例树节点"); |
|||
} |
|||
|
|||
CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); |
|||
if(caseInfo != null){ |
|||
return Result.error(401, "用例已存在"); |
|||
} |
|||
|
|||
CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); |
|||
if(caseTree == null){ |
|||
return Result.error(401, "用例树节点不存在"); |
|||
} |
|||
else if(caseTree != null && caseTree.getIsLeaf() != 0) { |
|||
return Result.error(401, "非叶子节点不能新增用例"); |
|||
} |
|||
|
|||
caseInfoService.insert(caseInfoVO); |
|||
return Result.success("新建用例成功"); |
|||
} |
|||
|
|||
@DeleteMapping("/delete") |
|||
public Result<String> delete(@Validated CaseInfoVO caseInfoVO) { |
|||
if(caseInfoVO.getId() != null){ |
|||
CaseInfo caseInfo = caseInfoService.findById(caseInfoVO.getId()); |
|||
if(caseInfo != null){ |
|||
if(caseInfoVO.getCaseName() != null && caseInfo.getCaseName() != caseInfoVO.getCaseName()){ |
|||
return Result.error(401, "用例不存在"); |
|||
} |
|||
caseInfoService.delete(caseInfo.getId()); |
|||
return Result.success("删除用例成功"); |
|||
} |
|||
else { |
|||
return Result.error(401, "用例不存在"); |
|||
} |
|||
} |
|||
else if(caseInfoVO.getCaseName() != null){ |
|||
CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); |
|||
if(caseInfo != null){ |
|||
caseInfoService.delete(caseInfo.getId()); |
|||
return Result.success("删除用例成功"); |
|||
} |
|||
else { |
|||
return Result.error(401, "用例不存在"); |
|||
} |
|||
} |
|||
else { |
|||
return Result.error(401, "id或用例名称不能全为空"); |
|||
} |
|||
} |
|||
|
|||
@PutMapping("/update") |
|||
public Result<String> update(@Validated CaseInfoVO caseInfoVO) { |
|||
if(caseInfoVO.getId() != null){ |
|||
CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); |
|||
if(caseInfo != null){ |
|||
CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); |
|||
if(caseTree == null || caseTree != null && caseTree.getIsLeaf() > 0){ |
|||
return Result.error(401, "新的用例树节点不是叶子节点,不能更新用例"); |
|||
} |
|||
caseInfo.setCaseName(caseInfoVO.getCaseName() != null ? caseInfoVO.getCaseName() : caseInfo.getCaseName()); |
|||
caseInfo.setNodeId(caseInfoVO.getNodeId() != null ? caseInfoVO.getNodeId() : caseInfo.getNodeId()); |
|||
caseInfo.setEditor(caseInfoVO.getEditor() != null ? caseInfoVO.getEditor() : caseInfo.getEditor()); |
|||
caseInfoService.update(caseInfo); |
|||
return Result.success("更新用例成功"); |
|||
} |
|||
else { |
|||
return Result.error(401, "用例不存在"); |
|||
} |
|||
} |
|||
else if(caseInfoVO.getCaseName() != null){ |
|||
CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); |
|||
if(caseInfo != null){ |
|||
CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); |
|||
if(caseTree == null || caseTree != null && caseTree.getIsLeaf() > 0){ |
|||
return Result.error(401, "新的用例树节点不是叶子节点,不能更新用例"); |
|||
} |
|||
caseInfo.setNodeId(caseInfoVO.getNodeId() != null ? caseInfoVO.getNodeId() : caseInfo.getNodeId()); |
|||
caseInfo.setEditor(caseInfoVO.getEditor() != null ? caseInfoVO.getEditor() : caseInfo.getEditor()); |
|||
log.info(caseInfo.getEditor()); |
|||
caseInfoService.update(caseInfo); |
|||
return Result.success("更新用例成功"); |
|||
} |
|||
else { |
|||
return Result.error(401, "用例不存在"); |
|||
} |
|||
}else { |
|||
return Result.error(401, "缺少用例主键或用例名称,无法更新用例"); |
|||
} |
|||
} |
|||
|
|||
@PostMapping("/query") |
|||
public Result<PageResult<List<CaseInfo>>> find( |
|||
@RequestParam(defaultValue = "0", value = "offset") int offset, |
|||
@RequestParam(defaultValue = "4", value = "limit") int limit) { |
|||
PageHelper.offsetPage(offset, limit); |
|||
List<CaseInfo> caseInfos = caseInfoService.find(); |
|||
PageInfo<CaseInfo> pageInfo = new PageInfo<>(caseInfos); |
|||
PageResult<List<CaseInfo>> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos); |
|||
return Result.success(pageResult); |
|||
} |
|||
|
|||
@PostMapping("queryByCaseName") |
|||
public Result<PageResult<List<CaseInfo>>> findByCaseName( |
|||
@RequestParam(defaultValue = "0", value = "offset") int offset, |
|||
@RequestParam(defaultValue = "2", value = "limit") int limit, |
|||
@RequestParam(value = "caseName") String caseName) { |
|||
PageHelper.offsetPage(offset, limit); |
|||
List<CaseInfo> caseInfos = caseInfoService.findByLikeCaseName(caseName); |
|||
PageInfo<CaseInfo> pageInfo = new PageInfo<>(caseInfos); |
|||
PageResult<List<CaseInfo>> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos); |
|||
return Result.success(pageResult); |
|||
} |
|||
} |
@ -1,4 +1,35 @@ |
|||
package demo.Entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.Date; |
|||
|
|||
@Entity |
|||
@Data |
|||
@Table(name = "case_info") |
|||
public class CaseInfo { |
|||
} |
|||
@Id |
|||
@Column(length = 32, nullable = false) |
|||
private int id; |
|||
|
|||
@Column(length = 50) |
|||
private String caseName; |
|||
|
|||
@Column(length = 50) |
|||
private int nodeId; |
|||
|
|||
@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,26 @@ |
|||
package demo.Entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.Date; |
|||
|
|||
@Entity |
|||
@Data |
|||
public class CaseInfoVO { |
|||
@Id |
|||
@Column(length = 32, nullable = false) |
|||
private Integer id; |
|||
|
|||
@Column(length = 50) |
|||
private String caseName; |
|||
|
|||
@Column(length = 50) |
|||
private Integer nodeId; |
|||
|
|||
@Column(length = 50) |
|||
private String creator; |
|||
|
|||
@Column(length = 50) |
|||
private String editor; |
|||
} |
@ -0,0 +1,16 @@ |
|||
package demo.Entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class PageResult<T> { |
|||
private long total; |
|||
private T items; |
|||
|
|||
public PageResult(long total, T items) { |
|||
this.total = total; |
|||
this.items = items; |
|||
} |
|||
|
|||
// Getters and Setters for the fields...
|
|||
} |
@ -0,0 +1,32 @@ |
|||
package demo.Mapper; |
|||
|
|||
import demo.Entity.CaseInfo; |
|||
import org.apache.ibatis.annotations.*; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface CaseInfoMapper { |
|||
@Insert("INSERT INTO case_info (CASE_NAME, NODE_ID, CREATOR, CREATE_TIME, EDITOR, UPDATE_TIME) " + |
|||
"VALUES (#{caseName}, #{nodeId}, #{creator}, NOW(), #{creator}, NOW())") |
|||
void insert(@Param("caseName") String caseName, @Param("nodeId") Integer nodeId, @Param("creator") String creator); |
|||
|
|||
@Select("SELECT * FROM case_info WHERE CASE_NAME = #{caseName}") |
|||
CaseInfo findByCaseName(@Param("caseName") String caseName); |
|||
|
|||
@Select("SELECT * FROM case_info WHERE ID = #{id}") |
|||
CaseInfo findById(@Param("id") Integer id); |
|||
|
|||
@Delete("DELETE FROM case_info WHERE ID = #{id}") |
|||
void delete(@Param("id") Integer id); |
|||
|
|||
@Update("UPDATE case_info SET CASE_NAME = #{caseName}, NODE_ID = #{nodeId}, EDITOR = #{editor}, " + |
|||
"UPDATE_TIME = NOW() WHERE ID = #{id}") |
|||
void update(@Param("id") Integer id, @Param("caseName") String caseName, @Param("nodeId") Integer nodeId, @Param("editor") String editor); |
|||
|
|||
@Select("SELECT * FROM case_info") |
|||
List<CaseInfo> find(); |
|||
|
|||
@Select("SELECT * FROM case_info WHERE CASE_NAME LIKE CONCAT('%', #{likeCaseName}, '%')") |
|||
List<CaseInfo> findByLikeCaseName(@Param("likeCaseName") String likeCaseName); |
|||
} |
Loading…
Reference in new issue