shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
147
148
149
150
151
152
153
154
package com.iplatform.base.controller;
 
import com.iplatform.base.CategoryCacheProvider;
import com.iplatform.base.Constants;
import com.iplatform.base.PlatformAdapterController;
import com.iplatform.base.cache.FormCacheProvider;
import com.iplatform.base.service.CategoryServiceImpl;
import com.iplatform.model.po.S_category;
import com.iplatform.model.vo.CategoryTreeVo;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import com.walker.web.WebRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
/**
 * 平台端基础分类服务
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController extends PlatformAdapterController {
 
    private CategoryServiceImpl categoryService;
    private CategoryCacheProvider categoryCacheProvider;
    private FormCacheProvider formCacheProvider;
 
    @Autowired
    public CategoryController(CategoryServiceImpl categoryService
            , CategoryCacheProvider categoryCacheProvider, FormCacheProvider formCacheProvider){
        this.categoryService = categoryService;
        this.categoryCacheProvider = categoryCacheProvider;
        this.formCacheProvider = formCacheProvider;
    }
 
    @RequestMapping("/list/tree")
    public ResponseValue getListTree(@RequestParam(name = "type") Integer type,
                                     @RequestParam(name = "status") Integer status,
                                     @RequestParam(name = "name", required = false) String name) {
        int owner = (int)this.getOwner();
//        return ResponseValue.success(this.categoryService.getListTree(type, status, name, owner));
        // 2023-05-18 表加上关联表单名称。
        List<CategoryTreeVo> data = this.categoryCacheProvider.getListTree(type, status, name, owner);
        if(!StringUtils.isEmptyList(data)){
            data.stream().forEach(e -> {
                if(e.getType().intValue() == Constants.CATEGORY_TYPE_CONFIG && StringUtils.isNotEmpty(e.getExtra())){
                    // 平台配置分类,extra存储的是formId
                    e.setFormName(this.formCacheProvider.getCacheData(e.getExtra()).getName());
                    if(!StringUtils.isEmptyList(e.getChild())){
                        for(CategoryTreeVo child : e.getChild()){
                            if(StringUtils.isNotEmpty(child.getExtra())){
                                child.setFormName(this.formCacheProvider.getCacheData(child.getExtra()).getName());
                            }
                        }
                    }
                }
            });
        }
        return ResponseValue.success(data);
    }
 
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public ResponseValue getInfo(@PathVariable(value = "id") Integer id){
        S_category category = this.categoryCacheProvider.get(id);
        if(category == null){
            throw new WebRuntimeException("分类不存在");
        }
        return ResponseValue.success(category);
    }
 
//    @ApiOperation(value = "新增基础分类")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseValue save(@RequestBody S_category category){
        if(category == null){
            return ResponseValue.error("提交内容错误");
        }
        logger.debug(category.toString());
 
        if(this.categoryService.queryNameUnique(category.getName(), category.getType(), (int)this.getOwner(), 0) > 0){
            return ResponseValue.error("相同分类已经存在");
        }
 
        long dateTime = DateUtils.getDateTimeNumber();
        int id = this.categoryService.queryNextId();
 
        category.setId(id);
        category.setPath(this.getPathByPId(category.getPid()));
        category.setOwner((int)this.getOwner());
        category.setExtra(this.clearCdnPrefix(category.getExtra()));    // 这里额外保存的可能是文件路径信息,清除了前缀。
        category.setCreate_time(dateTime);
        category.setUpdate_time(dateTime);
        this.categoryService.insert(category);
        this.categoryCacheProvider.save(category);
 
        return ResponseValue.success();
    }
 
//    @ApiOperation(value = "修改基础分类")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseValue update(@RequestBody S_category category){
        if(category == null || category.getId() <= 0){
            throw new WebRuntimeException("分类参数错误");
        }
        S_category exist = this.categoryCacheProvider.get(category.getId());
        if(exist == null){
            throw new WebRuntimeException("分类数据不存在,id=" + category.getId());
        }
        if(exist.getId().intValue() == category.getPid()){
            throw new WebRuntimeException("分类父级不能是自己");
        }
        if(!exist.getName().equals(category.getName())){
            if(this.categoryService.queryNameUnique(category.getName(), category.getType(), (int)this.getOwner(), exist.getId()) > 0){
                return ResponseValue.error("分类名称重复");
            }
        }
 
        if(StringUtils.isNotEmpty(category.getExtra())){
            category.setExtra(this.clearCdnPrefix(category.getExtra()));
        }
        category.setPath(this.getPathByPId(category.getPid()));
        category.setOwner((int)this.getOwner());
        category.setUpdate_time(DateUtils.getDateTimeNumber());
        this.categoryService.execUpdateCategory(category);
        this.categoryCacheProvider.update(category);
        return ResponseValue.success();
    }
 
    @RequestMapping("/delete")
    public ResponseValue delete(Integer id){
//        S_category category = this.categoryCacheProvider.get(id);
        if(this.categoryService.queryChildCategorySize(id) > 0){
            return ResponseValue.error("当前分类下有子类,请先删除子类!");
        }
        this.categoryService.delete(new S_category(id));
        this.categoryCacheProvider.remove(id);
        return ResponseValue.success();
    }
 
    private String getPathByPId(Integer pid) {
        S_category category = this.categoryCacheProvider.get(pid);
        if (category != null) {
            return category.getPath() + pid + StringUtils.FOLDER_SEPARATOR;
        }
        return "/0/";
    }
}