分组CRUD
# 增加分组
接口:
@PostMapping("/api/short-link/admin/v1/group")
public Result<Void> save(@RequestBody ShortLinkGroupSaveReqDTO requestParam) {
groupService.saveGroup(requestParam.getName());
return Results.success();
}
1
2
3
4
5
2
3
4
5
@Slf4j
@Service
@RequiredArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, GroupDO> implements GroupService {
private final RedissonClient redissonClient;
@Value("${short-link.group.max-num}")
private Integer groupMaxNum;
@Override
public void saveGroup(String groupName) {
saveGroup(UserContext.getUsername(), groupName);
}
public void saveGroup(String username,String groupName){
RLock lock = redissonClient.getLock(String.format(LOCK_GROUP_CREATE_KEY, username));
lock.lock();
try {
LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
.eq(GroupDO::getUsername, username)
.eq(GroupDO::getDelFlag, 0);
List<GroupDO> groupDOList = baseMapper.selectList(queryWrapper);
if(CollUtil.isNotEmpty(groupDOList) && groupDOList.size()==groupMaxNum){
throw new ClientException(String.format("已超出最大分组数:%d", groupMaxNum));
}
String gid;
do {
gid = RandomGenerator.generateRandom();
} while (!hasGid(username, gid));
GroupDO groupDO = GroupDO.builder()
.gid(gid)
.sortOrder(0)
.username(username)
.name(groupName)
.build();
baseMapper.insert(groupDO);
} finally {
lock.unlock();
}
}
private boolean hasGid(String username, String gid) {
LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
.eq(GroupDO::getGid, gid)
.eq(GroupDO::getUsername, Optional.ofNullable(username).orElse(UserContext.getUsername()));
GroupDO hasGroupFlag = baseMapper.selectOne(queryWrapper);
return hasGroupFlag == null;
}
}
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
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
测试时先登录,带上请求头:
# 查询分组
接口:
@GetMapping("/api/short-link/admin/v1/group")
public Result<List<ShortLinkGroupRespDTO>> listGroup() {
return Results.success(groupService.listGroup());
}
1
2
3
4
2
3
4
@Override
public List<ShortLinkGroupRespDTO> listGroup() {
LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
.eq(GroupDO::getDelFlag, 0)
.eq(GroupDO::getUsername, UserContext.getUsername())
.orderByDesc(GroupDO::getSortOrder, GroupDO::getUpdateTime);
List<GroupDO> groupDOList = baseMapper.selectList(queryWrapper);
return BeanUtil.copyToList(groupDOList, ShortLinkGroupRespDTO.class);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 修改分组
接口:
@PutMapping("/api/short-link/admin/v1/group")
public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO requestParam) {
groupService.updateGroup(requestParam);
return Results.success();
}
1
2
3
4
5
2
3
4
5
@Override
public void updateGroup(ShortLinkGroupUpdateReqDTO requestParam) {
LambdaUpdateWrapper<GroupDO> updateWrapper = Wrappers.lambdaUpdate(GroupDO.class)
.eq(GroupDO::getUsername, UserContext.getUsername())
.eq(GroupDO::getGid, requestParam.getGid())
.eq(GroupDO::getDelFlag, 0);
GroupDO groupDO = new GroupDO();
groupDO.setName(requestParam.getName());
baseMapper.update(groupDO, updateWrapper);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 删除分组
逻辑删除
接口:
@DeleteMapping("/api/short-link/admin/v1/group")
public Result<Void> updateGroup(@RequestParam String gid) {
groupService.deleteGroup(gid);
return Results.success();
}
1
2
3
4
5
2
3
4
5
@Override
public void deleteGroup(String gid) {
LambdaUpdateWrapper<GroupDO> updateWrapper = Wrappers.lambdaUpdate(GroupDO.class)
.eq(GroupDO::getUsername, UserContext.getUsername())
.eq(GroupDO::getGid, gid)
.eq(GroupDO::getDelFlag, 0);
GroupDO groupDO = new GroupDO();
groupDO.setDelFlag(1);
baseMapper.update(groupDO, updateWrapper);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 分组排序
接口:
@PostMapping("/api/short-link/admin/v1/group/sort")
public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> requestParam) {
groupService.sortGroup(requestParam);
return Results.success();
}
1
2
3
4
5
2
3
4
5
@Override
public void sortGroup(List<ShortLinkGroupSortReqDTO> requestParam) {
requestParam.forEach(each->{
GroupDO groupDO = GroupDO.builder()
.sortOrder(each.getSortOrder())
.build();
LambdaUpdateWrapper<GroupDO> updateWrapper = Wrappers.lambdaUpdate(GroupDO.class)
.eq(GroupDO::getUsername, UserContext.getUsername())
.eq(GroupDO::getGid, each.getGid())
.eq(GroupDO::getDelFlag, 0);
baseMapper.update(groupDO, updateWrapper);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13