xuefei
2020-12-10 29ff0c02969bba14b17ea58eb7cf2b7e928d914d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ${entity.controllerPackage};
 
import CommonConstant;
import RedisTemplateHelper;
import CommonUtil;
import ResultUtil;
import 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.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
 
 
import java.util.List;
import java.util.Set;
 
/**
 * @author ${entity.author}
 */
@Slf4j
@RestController
@Api(description = "${entity.description}管理接口")
@RequestMapping("/platform/${entity.classNameLowerCase}")
@CacheConfig(cacheNames = "${entity.classNameLowerCase}")
@Transactional
public class ${entity.className}Controller {
 
    @Autowired
    private ${entity.className}Service ${entity.classNameLowerCase}Service;
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    @Autowired
    private RedisTemplateHelper redisTemplateHelper;
 
    @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);
            }
        }
        // 更新缓存
        Set<String> keys = redisTemplateHelper.keys("${entity.classNameLowerCase}::*");
        redisTemplate.delete(keys);
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public Result<Object> edit(${entity.className} ${entity.classNameLowerCase}){
 
        ${entity.classNameLowerCase}Service.update(${entity.classNameLowerCase});
        // 手动删除所有分类缓存
        Set<String> keys = redisTemplateHelper.keys("${entity.classNameLowerCase}:" + "*");
        redisTemplate.delete(keys);
        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);
        }
        // 手动删除所有缓存
        Set<String> keys = redisTemplateHelper.keys("${entity.classNameLowerCase}:" + "*");
        redisTemplate.delete(keys);
        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}> childrenDeps = ${entity.classNameLowerCase}Service.findByParentIdOrderBySortOrder(parent.getId());
            if(childrenDeps==null||childrenDeps.size()==0){
                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 List<${entity.className}> 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("一级节点");
            }
        });
        return list;
    }
}