MaterialFolderServiceImpl.java
10.4 KB
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
225
226
227
228
229
230
231
package com.bs.reception.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.utils.SecurityUtils;
import com.bs.reception.entity.materials.BizType;
import com.bs.reception.entity.materials.MaterialFolder;
import com.bs.reception.mapper.MaterialFolderMapper;
import com.bs.reception.model.materials.MaterialFolderNodeVO;
import com.bs.reception.service.IMaterialFolderService;
import com.bs.common.core.page.TableDataInfo;
import com.bs.reception.model.materials.MaterialFolderPageQuery;
import com.bs.reception.service.IMaterialService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class MaterialFolderServiceImpl extends ServiceImpl<MaterialFolderMapper, MaterialFolder> implements IMaterialFolderService {
@jakarta.annotation.Resource
private com.bs.reception.service.IBizTypeService bizTypeService;
@Resource
private IMaterialService materialService;
@Override
/** 查询文件夹树(扁平列表) */
public List<MaterialFolderNodeVO> listFolderTree() {
return this.baseMapper.selectFolderTree();
}
@Override
@org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
public boolean sort(String typeCode, List<Long> ids) {
if (ids == null || ids.isEmpty()) {
return false;
}
// 校验业务类型是否存在
BizType bizType = bizTypeService.getOne(new LambdaQueryWrapper<BizType>()
.eq(BizType::getTypeCode, typeCode)
.eq(BizType::getDelFlag, 0));
if (bizType == null) {
throw new RuntimeException("业务类型不存在");
}
// 校验文件夹是否存在
LambdaQueryWrapper<MaterialFolder> qw = new LambdaQueryWrapper<>();
qw.in(MaterialFolder::getId, ids)
.eq(MaterialFolder::getTypeCode, typeCode)
.eq(MaterialFolder::getDelFlag, 0);
List<MaterialFolder> list = this.list(qw);
if (list.size() != ids.size()) {
throw new RuntimeException("部分文件夹不存在或已删除,或不属于该业务类型");
}
// 更新排序
List<MaterialFolder> updates = new java.util.ArrayList<>();
for (int i = 0; i < ids.size(); i++) {
Long id = ids.get(i);
MaterialFolder item = new MaterialFolder();
item.setId(id);
item.setSortOrder(i + 1);
item.setUpdateTime(new Date());
item.setUpdateById(SecurityUtils.getUserId());
item.setUpdateBy(SecurityUtils.getUserName());
updates.add(item);
}
return this.updateBatchById(updates);
}
@Override
public TableDataInfo<List<MaterialFolder>> pageFolders(MaterialFolderPageQuery query) {
int pageNum = query.getPageNum() == null || query.getPageNum() < 1 ? 1 : query.getPageNum();
int pageSize = query.getPageSize() == null || query.getPageSize() < 1 ? 10 : query.getPageSize();
LambdaQueryWrapper<MaterialFolder> qw = new LambdaQueryWrapper<>();
qw.like(query.getFolderName() != null && !query.getFolderName().isBlank(), MaterialFolder::getFolderName, query.getFolderName())
.eq(query.getTypeCode() != null && !query.getTypeCode().isBlank(), MaterialFolder::getTypeCode, query.getTypeCode())
.eq(query.getStatus() != null, MaterialFolder::getStatus, query.getStatus())
.eq(MaterialFolder::getDelFlag, 0)
.orderByAsc(MaterialFolder::getSortOrder);
IPage<MaterialFolder> page = this.page(new Page<>(pageNum, pageSize), qw);
TableDataInfo<List<MaterialFolder>> rspData = new TableDataInfo<>();
rspData.setCode(com.bs.common.constant.HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(page.getRecords());
rspData.setTotal(page.getTotal());
return rspData;
}
@Override
public List<MaterialFolder> getFolderList(MaterialFolder body) {
LambdaQueryWrapper<MaterialFolder> qw = new LambdaQueryWrapper<>();
qw.like(body.getFolderName() != null && !body.getFolderName().isBlank(), MaterialFolder::getFolderName, body.getFolderName())
.eq(body.getTypeCode() != null, MaterialFolder::getTypeCode, body.getTypeCode())
.eq(body.getStatus() != null, MaterialFolder::getStatus, body.getStatus())
.eq(MaterialFolder::getDelFlag, 0)
.orderByAsc(MaterialFolder::getSortOrder);
return this.list(qw);
}
@Override
public MaterialFolder createFolder(MaterialFolder body) {
if (body.getFolderName() == null || body.getFolderName().isBlank()) {
throw new RuntimeException("文件夹名称不能为空");
}
if (body.getTypeCode() == null || body.getTypeCode().isBlank()) {
throw new RuntimeException("业务类型编码不能为空");
}
body.setId(null);
body.setFolderCode("FC" + System.currentTimeMillis());
// 名称去重校验
LambdaQueryWrapper<MaterialFolder> qw = new LambdaQueryWrapper<>();
qw.eq(MaterialFolder::getTypeCode, body.getTypeCode())
.eq(MaterialFolder::getFolderName, body.getFolderName())
.eq(MaterialFolder::getDelFlag, 0);
List<MaterialFolder> list = this.list(qw);
if (list != null && !list.isEmpty()) {
throw new RuntimeException("名称已存在");
}
body.setCreateBy(SecurityUtils.getUserName());
body.setCreateById(SecurityUtils.getUserId());
body.setCreateTime(new java.util.Date());
this.save(body);
return body;
}
@Override
public boolean updateFolder(MaterialFolder body) {
if (body.getId() == null) {
throw new RuntimeException("ID不能为空");
}
if (body.getFolderName() == null || body.getFolderName().isBlank()) {
throw new RuntimeException("文件夹名称不能为空");
}
if (body.getTypeCode() == null || body.getTypeCode().isBlank()) {
throw new RuntimeException("业务类型编码不能为空");
}
body.setFolderCode(null);
// 名称去重校验
LambdaQueryWrapper<MaterialFolder> qw = new LambdaQueryWrapper<>();
qw.eq(MaterialFolder::getTypeCode, body.getTypeCode())
.eq(MaterialFolder::getFolderName, body.getFolderName())
.ne(MaterialFolder::getId, body.getId())
.eq(MaterialFolder::getDelFlag, 0);
List<MaterialFolder> list = this.list(qw);
if (list != null && !list.isEmpty()) {
throw new RuntimeException("名称已存在");
}
body.setUpdateBy(SecurityUtils.getUserName());
body.setUpdateById(SecurityUtils.getUserId());
body.setUpdateTime(new java.util.Date());
return this.updateById(body);
}
@Override
public boolean setFolderStatus(MaterialFolder body) {
if (body.getId() == null) {
throw new RuntimeException("ID不能为空");
}
if (body.getStatus() == null) {
throw new RuntimeException("状态不能为空");
}
return this.update(new LambdaUpdateWrapper<MaterialFolder>()
.eq(MaterialFolder::getId, body.getId())
.set(MaterialFolder::getUpdateBy, SecurityUtils.getUserName())
.set(MaterialFolder::getUpdateById, SecurityUtils.getUserId())
.set(MaterialFolder::getUpdateTime, new java.util.Date())
.set(MaterialFolder::getStatus, body.getStatus()));
}
@Override
public boolean setFolderSort(MaterialFolder body) {
if (body.getId() == null) {
throw new RuntimeException("ID不能为空");
}
if (body.getSortOrder() == null) {
throw new RuntimeException("排序不能为空");
}
return this.update(new LambdaUpdateWrapper<MaterialFolder>()
.eq(MaterialFolder::getId, body.getId())
.set(MaterialFolder::getUpdateBy, SecurityUtils.getUserName())
.set(MaterialFolder::getUpdateById, SecurityUtils.getUserId())
.set(MaterialFolder::getUpdateTime, new java.util.Date())
.set(MaterialFolder::getSortOrder, body.getSortOrder()));
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteFolder(MaterialFolder body) {
if (body.getId() == null) {
throw new RuntimeException("ID不能为空");
}
MaterialFolder folder = this.getById(body.getId());
if (folder != null && folder.getFolderCode() != null) {
// 删除该文件夹下的所有素材
materialService.update(new LambdaUpdateWrapper<com.bs.reception.entity.materials.Material>()
.eq(com.bs.reception.entity.materials.Material::getFolderCode, folder.getFolderCode())
.set(com.bs.reception.entity.materials.Material::getUpdateBy, SecurityUtils.getUserName())
.set(com.bs.reception.entity.materials.Material::getUpdateById, SecurityUtils.getUserId())
.set(com.bs.reception.entity.materials.Material::getUpdateTime, new java.util.Date())
.set(com.bs.reception.entity.materials.Material::getDelFlag, 1));
}
return this.update(new LambdaUpdateWrapper<MaterialFolder>()
.eq(MaterialFolder::getId, body.getId())
.set(MaterialFolder::getUpdateBy, SecurityUtils.getUserName())
.set(MaterialFolder::getUpdateById, SecurityUtils.getUserId())
.set(MaterialFolder::getUpdateTime, new java.util.Date())
.set(MaterialFolder::getDelFlag, 1));
}
@Override
public MaterialFolder getFolderDetail(MaterialFolder body) {
if (body.getId() == null) {
throw new RuntimeException("ID不能为空");
}
return this.getById(body.getId());
}
}