package com.yq.redAtlas.controller;
import com.yq.redAtlas.domain.CommanderEntity;
import com.yq.redAtlas.domain.HeroEntity;
import com.yq.redAtlas.domain.LinkVO;
import com.yq.redAtlas.domain.NodeVO;
import com.yq.redAtlas.service.CommanderService;
import com.yq.redAtlas.service.RelationService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.yq.redAtlas.util.ReturnVO;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* (Commander)表控制层
*
* @author ywb
* @since 2024-09-29 14:45:34
*/
@RestController
@RequestMapping("commander")
public class CommanderController {
/**
* 服务对象
*/
@Resource
private CommanderService commanderService;
@Resource
private RelationService relationService;
/**
* 分页查询
*
* @param commander 筛选条件
* @return 查询结果
*/
@RequestMapping("queryByPage")
public ReturnVO queryByPage(String condition, Integer page, Integer size, String orderCol, String orderDirect) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
CommanderEntity commander = new CommanderEntity();
if (condition != null){
commander.setCommName(condition);
}
//查询
Page<CommanderEntity> pageVO =
this.commanderService.queryByPage(
commander,
page, size,
orderCol, orderDirect);
//没有数据
if(pageVO.getContent().isEmpty()){
return returnVO;
}
//查询成功
returnVO = ReturnVO.getSuccessDataReturnVO(pageVO);
return returnVO;
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@RequestMapping("queryById/{id}")
public ReturnVO queryById(@PathVariable("id") Integer id) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//查询单个
CommanderEntity commander = this.commanderService.queryById(id);
if(commander == null){
return returnVO;
}
//查询成功
returnVO = ReturnVO.getSuccessDataReturnVO(commander);
return returnVO;
}
/**
* 新增数据
*
* @param commander 实体
* @return 新增结果
*/
@RequestMapping("add")
public ReturnVO add(CommanderEntity commander) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//入库
if(!this.commanderService.insert(commander)){
//入库失败
return returnVO;
}
//入库成功
returnVO.setCode(1);
returnVO.setMsg("添加成功");
return returnVO;
}
/**
* 编辑数据
*
* @param commander 实体
* @return 编辑结果
*/
@RequestMapping("edit")
public ReturnVO edit(CommanderEntity commander) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//修改
if(!this.commanderService.update(commander)){
//修改失败
return returnVO;
}
//修改成功
returnVO.setCode(1);
returnVO.setMsg("修改成功");
return returnVO;
}
/**
* 编辑数据
*
* @param commander 实体
* @return 编辑结果
*/
@RequestMapping("editDesc")
public ReturnVO editDesc(CommanderEntity commander) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
CommanderEntity param = new CommanderEntity();
param.setCommName(commander.getCommName());
Page<CommanderEntity> pageComm = this.commanderService.queryByPage(param, 1, 10, null, null);
if(pageComm.getContent().isEmpty()){
return returnVO;
}
commander.setCommId(pageComm.getContent().get(0).getCommId());
commander.setCommName(null);
//修改
if(!this.commanderService.update(commander)){
//修改失败
return returnVO;
}
//修改成功
returnVO.setCode(1);
returnVO.setMsg("修改成功");
return returnVO;
}
/**
* 删除数据
*
* @param id 主键
* @return 删除是否成功
*/
@RequestMapping("deleteById/{id}")
public ReturnVO deleteById(@PathVariable("id") Integer id) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//删除
if(!this.commanderService.deleteById(id)){
//删除失败
return returnVO;
}
//修改成功
returnVO.setCode(1);
returnVO.setMsg("删除成功");
return returnVO;
}
/**
* 查询关系图
*/
@RequestMapping("queryNodeAndLink")
public ReturnVO queryNodeAndLink(String commName) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//查询存在
List<NodeVO> nodes = this.commanderService.queryAllNode(commName);
List<LinkVO> links = this.relationService.getAllLink(commName);
if(nodes.isEmpty() || links.isEmpty()){
return returnVO;
}
Map<String, Object> result = new HashMap<>();
result.put("nodes", nodes);
result.put("links", links);
//查询存在
returnVO = ReturnVO.getSuccessDataReturnVO(result);
return returnVO;
}
/**
* 查询所有
*/
@RequestMapping("queryAll")
public ReturnVO queryAll() {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
//查询
Page<CommanderEntity> pageVO =
this.commanderService.queryByPage(
null,
1, 99999,
null, null);
//没有数据
if(pageVO.getContent().isEmpty()){
return returnVO;
}
//查询成功
returnVO = ReturnVO.getSuccessDataReturnVO(pageVO.getContent());
return returnVO;
}
/**
* 根据名字查英雄
*/
@RequestMapping("queryByName")
public ReturnVO queryByName(String name, Integer commId) {
//定义失败的返回对象
ReturnVO returnVO = ReturnVO.getNodataFoundReturnVO();
if(name == null || name.isEmpty()){
return returnVO;
}
List<CommanderEntity> heroList = this.commanderService.queryByName(name, commId);
//没有数据
if(heroList.isEmpty()){
return returnVO;
}
//查询成功
returnVO = ReturnVO.getSuccessDataReturnVO(heroList.get(0));
return returnVO;
}
}