jiaosong
2023-08-18 8e1a9aa9e896f24f5244f042ecfb9e49effbe6bf
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package doumeemes.api.ext;
 
import doumeemes.api.BaseController;
import doumeemes.core.annotation.excel.ExcelExporter;
import doumeemes.core.annotation.pr.PreventRepeat;
import doumeemes.core.model.ApiResponse;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.dao.business.model.Category;
import doumeemes.dao.business.model.CategoryUnion;
import doumeemes.dao.ext.dto.QueryCategoryExtDTO;
import doumeemes.dao.ext.vo.CategoryExtListVO;
import doumeemes.service.business.CategoryService;
import doumeemes.service.business.CategoryUnionService;
import doumeemes.service.ext.CategoryExtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 设置类分类信息表接口
 *
 * @author 江蹄蹄
 * @date 2022/04/27 16:15
 */
@RestController
@RequestMapping("/ext/categoryExt")
@Api(tags = "设置类分类信息表接口")
public class CategoryExtController extends BaseController {
 
    @Autowired
    private CategoryExtService categoryExtService;
 
    @Autowired
    private CategoryService categoryService;
 
    @Autowired
    private CategoryUnionService categoryUnionService;
 
 
    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresPermissions("ext:categoryext:create")
    public ApiResponse create(@RequestBody Category category) {
 
        if(StringUtils.isBlank(category.getCode())){
            category.setCode(categoryService.getNextCode(getLoginUser().getCompany().getId()));
        }
 
        Category query = new Category();
        query.setDeleted(Constants.ZERO);
        query.setRootDepartId(getLoginUser().getRootDepartment().getId());
        query.setCode(category.getCode());
        List<Category> list = categoryService.findList(query);
 
        Category query1 = new Category();
        query1.setDeleted(Constants.ZERO);
        query1.setRootDepartId(getLoginUser().getRootDepartment().getId());
        query1.setName(category.getName());
        List<Category> list1 = categoryService.findList(query1);
        if (list.size() > 0 || list1.size() > 0) {
            return ApiResponse.failed("分类编码/分类名称已存在,不允许添加");
        }
        category.setDeleted(Constants.ZERO);
        category.setCreateTime(new Date());
        category.setCreateUser(getLoginUser().getId());
        category.setRootDepartId(getLoginUser().getRootDepartment().getId());
        int r =  categoryService.create(category);
        CategoryUnion categoryUnion = new CategoryUnion();
        categoryUnion.setDeleted(Constants.ZERO);
        categoryUnion.setCreateUser(getLoginUser().getId());
        categoryUnion.setCreateTime(new Date());
        categoryUnion.setUpdateUser(getLoginUser().getId());
        categoryUnion.setUpdateTime(new Date());
        categoryUnion.setRemark("");
        categoryUnion.setRootDepartId(getLoginUser().getRootDepartment().getId());
        categoryUnion.setCateBigId(category.getId());
        categoryUnionService.create(categoryUnion);
        categoryExtService.loadCom(getLoginUser().getCompany());
        return ApiResponse.success(r );
    }
 
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(value = "主键id", name = "id", paramType = "query", dataType = "Integer")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("ext:categoryext:delete")
    public ApiResponse deleteById(@PathVariable Integer id) {
 
        categoryService.deleteCategory(id);
        return ApiResponse.success(null);
    }
 
    @ApiOperation("批量删除")
    @ApiImplicitParam(value = "主键ids", name = "ids", paramType = "query", dataType = "Integer")
    @GetMapping("/delete/batch")
    @RequiresPermissions("ext:categoryext:delete")
    public ApiResponse deleteByIds(@RequestParam String ids) {
        String[] idArray = ids.split(",");
        List<Category> idList = new ArrayList<>();
        for (String id : idArray) {
            Category category = categoryService.findById(Integer.valueOf(id));
            category.setDeleted(Constants.ONE);
            idList.add(category);
        }
        categoryService.updateByIdInBatch(idList);
        return ApiResponse.success(null);
    }
 
    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @RequiresPermissions("ext:categoryext:update")
    public ApiResponse updateById(@RequestBody Category category) {
 
        Category query = new Category();
        query.setDeleted(Constants.ZERO);
        query.setRootDepartId(getLoginUser().getRootDepartment().getId());
        query.setCode(category.getCode());
        List<Category> list = categoryService.findList(query);
        if (list.size() > 0) {
            if (!Constants.equalsInteger(list.get(0).getId(),category.getId()) ) {
                return ApiResponse.failed("分类编码/分类名称已存在,不允许编辑");
            }
        }
 
        Category query1 = new Category();
        query1.setDeleted(Constants.ZERO);
        query1.setRootDepartId(getLoginUser().getRootDepartment().getId());
        query1.setName(category.getName());
        List<Category> list1 = categoryService.findList(query1);
        if (list1.size() > 0) {
            if (!Constants.equalsInteger(list1.get(0).getId(),category.getId())) {
                return ApiResponse.failed("分类编码/分类名称已存在,不允许编辑");
            }
        }
 
        Category find= categoryService.findById(category.getId());
        find.setUpdateTime(new Date());
        find.setCateType(category.getCateType());
        find.setCode(category.getCode());
        find.setName(category.getName());
        find.setType(category.getType());
        find.setParentId(category.getParentId());
        category.setUpdateUser(getLoginUser().getId());
        categoryService.updateById(find);
        return ApiResponse.success(null);
    }
 
    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresPermissions("ext:categoryext:query")
    public ApiResponse<PageData<CategoryExtListVO>> findPage(@RequestBody PageWrap<QueryCategoryExtDTO> pageWrap) {
        pageWrap.getModel().setDeleted(Constants.ZERO);
        pageWrap.getModel().setRootDepartId(getLoginUser().getRootDepartment().getId());
        return ApiResponse.success(categoryExtService.findPage(pageWrap));
    }
 
    @ApiOperation("列表查询")
    @PostMapping("/list")
    @RequiresPermissions("ext:categoryext:query")
    public ApiResponse<List<CategoryExtListVO>> list(@RequestBody QueryCategoryExtDTO queryCategoryExtDTO) {
        queryCategoryExtDTO.setDeleted(Constants.ZERO);
        queryCategoryExtDTO.setRootDepartId(getLoginUser().getRootDepartment().getId());
        return ApiResponse.success(categoryExtService.findList(queryCategoryExtDTO));
    }
 
    @ApiOperation("导出Excel")
    @PostMapping("/exportExcel")
    @RequiresPermissions("ext:categoryext:exportExcel")
    public void exportExcel(@RequestBody PageWrap<QueryCategoryExtDTO> pageWrap, HttpServletResponse response) {
        ExcelExporter.build(CategoryExtListVO.class).export(categoryExtService.findPage(pageWrap).getRecords(), "设置类分类信息表", response);
    }
 
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "主键id", name = "id", paramType = "query", dataType = "Integer")
    @GetMapping("/{id}")
    @RequiresPermissions("ext:categoryext:query")
    public ApiResponse<Category> findById(@PathVariable Integer id) {
        return ApiResponse.success(categoryService.findById(id));
    }
 
    @ApiOperation("查询当前用户主组织下所有从属属性名称")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类属性0大类 1中类 2小类", name = "type", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(value = "型 0物料 1客户 2工装器具 3不良品", name = "cateType", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(value = "主类型id", name = "id", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/getListByType")
    public ApiResponse<List<CategoryExtListVO>> getListByType(@RequestParam String type, @RequestParam String cateType,String id) {
        if (StringUtils.isEmpty(type)) {
            return ApiResponse.failed("参数不正确");
        }
        return ApiResponse.success(categoryExtService.getListByType(type, getLoginUser().getRootDepartment().getId(), cateType,id));
    }
 
 
    @ApiOperation("分类导入Excel")
    @PostMapping("/importExcel")
    @RequiresPermissions("ext:categoryext:importExcel")
    public ApiResponse importExcel(@RequestParam(name = "file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        categoryService.importcategory(file);
        return ApiResponse.success(null);
    }
 
 
    @ApiOperation("分类树形列表查询")
    @PostMapping("/treeCategoryList")
    public ApiResponse<List<CategoryExtListVO>> treeCategoryList (@RequestBody QueryCategoryExtDTO pageWrap) {
        return ApiResponse.success(categoryExtService.treeCategoryList(pageWrap));
    }
}