package ${entity.controllerPackage};
|
|
import cn.exrick.xboot.core.common.constant.CommonConstant;
|
import cn.exrick.xboot.core.common.redis.RedisTemplateHelper;
|
import cn.exrick.xboot.core.common.utils.CommonUtil;
|
import cn.exrick.xboot.core.common.utils.ResultUtil;
|
import cn.exrick.xboot.core.common.vo.Result;
|
import ${entity.entityPackage}.${entity.className};
|
import ${entity.servicePackage}.${entity.className}Service;
|
import cn.hutool.core.util.StrUtil;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.cache.annotation.CacheConfig;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import java.util.List;
|
|
/**
|
* @author ${entity.author}
|
*/
|
@Slf4j
|
@RestController
|
@Api(tags = "${entity.description}管理接口")
|
@RequestMapping("/xboot/${entity.classNameLowerCase}")
|
@CacheConfig(cacheNames = "${entity.classNameLowerCase}")
|
@Transactional
|
public class ${entity.className}Controller {
|
|
@Autowired
|
private ${entity.className}Service ${entity.classNameLowerCase}Service;
|
|
@Autowired
|
private RedisTemplateHelper redisTemplate;
|
|
@RequestMapping(value = "/getByParentId/{parentId}", method = RequestMethod.GET)
|
@ApiOperation(value = "通过id获取")
|
@Cacheable(key = "#parentId")
|
public Result<List<${entity.className}>> getByParentId(@PathVariable String parentId) {
|
|
List<${entity.className}> list = ${entity.classNameLowerCase}Service.findByParentIdOrderBySortOrder(parentId);
|
setInfo(list);
|
return new ResultUtil<List<${entity.className}>>().setData(list);
|
}
|
|
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
@ApiOperation(value = "添加")
|
public Result<Object> add(${entity.className} ${entity.classNameLowerCase}) {
|
|
${entity.classNameLowerCase}Service.save(${entity.classNameLowerCase});
|
// 如果不是添加的一级 判断设置上级为父节点标识
|
if (!CommonConstant.PARENT_ID.equals(${entity.classNameLowerCase}.getParentId())) {
|
${entity.className} parent = ${entity.classNameLowerCase}Service.get(${entity.classNameLowerCase}.getParentId());
|
if (parent.getIsParent() == null || !parent.getIsParent()) {
|
parent.setIsParent(true);
|
${entity.classNameLowerCase}Service.update(parent);
|
}
|
}
|
// 更新缓存
|
redisTemplate.deleteByPattern("${entity.classNameLowerCase}::*");
|
return ResultUtil.success("添加成功");
|
}
|
|
@RequestMapping(value = "/edit", method = RequestMethod.POST)
|
@ApiOperation(value = "编辑")
|
public Result<Object> edit(${entity.className} ${entity.classNameLowerCase}) {
|
|
if (${entity.classNameLowerCase}.getId().equals(${entity.classNameLowerCase}.getParentId())) {
|
return ResultUtil.error("上级节点不能为自己");
|
}
|
${entity.className} old = ${entity.classNameLowerCase}Service.get(${entity.classNameLowerCase}.getId());
|
String oldParentId = old.getParentId();
|
${entity.classNameLowerCase}Service.update(${entity.classNameLowerCase});
|
// 如果该节点不是一级节点 且修改了级别 判断上级还有无子节点
|
if (!CommonConstant.PARENT_ID.equals(oldParentId) && !oldParentId.equals(${entity.classNameLowerCase}.getParentId())) {
|
${entity.className} parent = ${entity.classNameLowerCase}Service.get(oldParentId);
|
List<${entity.className}> children = ${entity.classNameLowerCase}Service.findByParentIdOrderBySortOrder(parent.getId());
|
if (parent != null && (children == null || children.isEmpty())) {
|
parent.setIsParent(false);
|
${entity.classNameLowerCase}Service.update(parent);
|
}
|
}
|
// 手动删除所有分类缓存
|
redisTemplate.deleteByPattern("${entity.classNameLowerCase}:*");
|
return ResultUtil.success("编辑成功");
|
}
|
|
@RequestMapping(value = "/delByIds", method = RequestMethod.POST)
|
@ApiOperation(value = "批量通过id删除")
|
public Result<Object> delByIds(@RequestParam String[] ids) {
|
|
for (String id : ids) {
|
deleteRecursion(id, ids);
|
}
|
// 手动删除所有缓存
|
redisTemplate.deleteByPattern("${entity.classNameLowerCase}:*");
|
return ResultUtil.success("批量通过id删除数据成功");
|
}
|
|
public void deleteRecursion(String id, String[] ids) {
|
|
// 获得其父节点
|
${entity.className} o = ${entity.classNameLowerCase}Service.get(id);
|
${entity.className} parent = null;
|
if (o != null && StrUtil.isNotBlank(o.getParentId())) {
|
parent = ${entity.classNameLowerCase}Service.get(o.getParentId());
|
}
|
${entity.classNameLowerCase}Service.delete(id);
|
// 判断父节点是否还有子节点
|
if (parent != null) {
|
List<${entity.className}> children = ${entity.classNameLowerCase}Service.findByParentIdOrderBySortOrder(parent.getId());
|
if (children == null || children.isEmpty()) {
|
parent.setIsParent(false);
|
${entity.classNameLowerCase}Service.update(parent);
|
}
|
}
|
// 递归删除
|
List<${entity.className}> objs = ${entity.classNameLowerCase}Service.findByParentIdOrderBySortOrder(id);
|
for (${entity.className} obj : objs) {
|
if (!CommonUtil.judgeIds(obj.getId(), ids)) {
|
deleteRecursion(obj.getId(), ids);
|
}
|
}
|
}
|
|
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
@ApiOperation(value = "名称模糊搜索")
|
public Result<List<${entity.className}>> searchByTitle(@RequestParam String title) {
|
|
List<${entity.className}> list = ${entity.classNameLowerCase}Service.findByTitleLikeOrderBySortOrder("%" + title + "%");
|
setInfo(list);
|
return new ResultUtil<List<${entity.className}>>().setData(list);
|
}
|
|
public void setInfo(List<${entity.className}> list) {
|
|
// lambda表达式
|
list.forEach(item -> {
|
if (!CommonConstant.PARENT_ID.equals(item.getParentId())) {
|
${entity.className} parent = ${entity.classNameLowerCase}Service.get(item.getParentId());
|
item.setParentTitle(parent.getTitle());
|
}else{
|
item.setParentTitle("一级节点");
|
}
|
});
|
}
|
}
|