101 lines
3.1 KiB
Java
101 lines
3.1 KiB
Java
package com.senmesh.sfile.service.impl;
|
||
|
||
import com.senmesh.common.exception.util.Assert;
|
||
import com.senmesh.common.util.ListUtil;
|
||
import com.senmesh.common.util.UserContextHolder;
|
||
import com.senmesh.frame.redis.dto.LockDTO;
|
||
import com.senmesh.frame.redis.redisson.RedissonUtils;
|
||
import com.senmesh.sfile.convert.UserSpaceConvert;
|
||
import com.senmesh.sfile.dao.UserSpaceMapper;
|
||
import com.senmesh.sfile.dto.UserSpaceCreateDTO;
|
||
import com.senmesh.sfile.dto.UserSpaceDTO;
|
||
import com.senmesh.sfile.dto.UserSpaceModifyDTO;
|
||
import com.senmesh.sfile.dto.UserSpaceQueryDTO;
|
||
import com.senmesh.sfile.entity.UserSpace;
|
||
import com.senmesh.sfile.service.UserSpaceService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 用户空间ServiceImpl
|
||
*/
|
||
@Service
|
||
public class UserSpaceServiceImpl implements UserSpaceService {
|
||
|
||
@Autowired
|
||
private UserSpaceMapper userSpaceMapper;
|
||
|
||
/**
|
||
* 默认总容量:5GB
|
||
*/
|
||
private static final Long DEFAULT_TOTAL_SIZE = 5L * 1024 * 1024 * 1024;
|
||
|
||
|
||
public static LockDTO getLockDTO(Long userId) {
|
||
Assert.notNull(userId, "用户id不能为空");
|
||
return LockDTO.builder().methodName("userspace").tryLockWaitTime(0L)
|
||
.arg(userId.toString())
|
||
.failMsg("该用户空间正在被操作")
|
||
.build();
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public Long create(UserSpaceCreateDTO createDTO) {
|
||
Long userId = createDTO.getUserId();
|
||
UserSpaceQueryDTO queryDTO = UserSpaceQueryDTO.builder().userId(userId).build();
|
||
|
||
return RedissonUtils.lockAndSave(getLockDTO(userId),
|
||
() -> this.find(queryDTO), () -> {
|
||
UserSpace userSpace = UserSpaceConvert.INSTANCE.toEntity(createDTO);
|
||
userSpace.setTotalSize(DEFAULT_TOTAL_SIZE);
|
||
userSpace.setUsedSize(0L);
|
||
userSpaceMapper.saveEntity(userSpace);
|
||
return userSpace.getId();
|
||
});
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public void modify(UserSpaceModifyDTO modifyDTO) {
|
||
Long userId = modifyDTO.getUserId();
|
||
UserSpaceQueryDTO queryDTO = UserSpaceQueryDTO.builder().userId(userId).build();
|
||
UserSpaceDTO userSpaceDTO = this.find(queryDTO);
|
||
Assert.notNull(userSpaceDTO, "用户空间不存在");
|
||
|
||
UserSpace userSpace = UserSpace.builder()
|
||
.id(userSpaceDTO.getId())
|
||
.usedSize(modifyDTO.getUsedSize())
|
||
.build();
|
||
userSpaceMapper.updateEntitySelective(userSpace);
|
||
}
|
||
|
||
@Override
|
||
@Transactional
|
||
public void remove(Long id) {
|
||
userSpaceMapper.deleteEntity(UserSpace.builder().id(id).build(),
|
||
UserContextHolder.getLoginUserId());
|
||
}
|
||
|
||
@Override
|
||
public List<UserSpaceDTO> list(UserSpaceQueryDTO queryDTO) {
|
||
queryDTO.disablePage();
|
||
return userSpaceMapper.list(queryDTO);
|
||
}
|
||
|
||
@Override
|
||
public void page(UserSpaceQueryDTO queryDTO) {
|
||
userSpaceMapper.list(queryDTO);
|
||
}
|
||
|
||
@Override
|
||
public UserSpaceDTO find(UserSpaceQueryDTO queryDTO) {
|
||
List<UserSpaceDTO> userSpaceDTOs = this.list(queryDTO);
|
||
return ListUtil.first(userSpaceDTOs);
|
||
}
|
||
}
|
||
|