@@ -2,18 +2,23 @@ package com.yunhai.userhome.service.impl;
|
||||
|
||||
import com.yunhai.platform.apistd.BizException;
|
||||
import com.yunhai.platform.utils.exception.ErrorCode;
|
||||
import com.yunhai.platform.utils.model.BoaUser;
|
||||
import com.yunhai.userhome.entity.*;
|
||||
import com.yunhai.userhome.repository.LightAppRepository;
|
||||
import com.yunhai.userhome.repository.LogoRepository;
|
||||
import com.yunhai.userhome.repository.UserSettingsDesktopAppRepository;
|
||||
import com.yunhai.userhome.repository.UserSettingsRepository;
|
||||
import com.yunhai.userhome.repository.WallpaperRepository;
|
||||
import com.yunhai.userhome.service.IUserCurrentRoleService;
|
||||
import com.yunhai.userhome.service.IUserSettingsService;
|
||||
import com.yunhai.userhome.utils.FileUtil;
|
||||
import com.yunhai.userhome.utils.OtherUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
@@ -30,6 +35,9 @@ public class UserSettingsServiceImpl implements IUserSettingsService {
|
||||
@Autowired
|
||||
private WallpaperRepository wallpaperRepository;
|
||||
|
||||
@Autowired
|
||||
private LogoRepository logoRepository;
|
||||
|
||||
@Autowired
|
||||
private UserSettingsRepository userSettingsRepository;
|
||||
|
||||
@@ -42,6 +50,9 @@ public class UserSettingsServiceImpl implements IUserSettingsService {
|
||||
@Autowired
|
||||
private UserSettingsDesktopAppRepository userSettingsDesktopAppRepository;
|
||||
|
||||
@Value("${FILE_PATH:./upload/}")
|
||||
private String FILE_PATH;
|
||||
|
||||
@Override
|
||||
public List<Wallpaper> findWallList(Integer userId) {
|
||||
Optional<UserCurrentRole> currentRoleOpt = userCurrentRoleService.findById(userId);
|
||||
@@ -63,10 +74,27 @@ public class UserSettingsServiceImpl implements IUserSettingsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteWallById(Integer id) {
|
||||
if (!wallpaperRepository.existsById(id))
|
||||
throw new BizException(ErrorCode.INTERNAL_SERVER_ERROR);
|
||||
wallpaperRepository.deleteById(id);
|
||||
@Transactional
|
||||
public void deleteWallById(Integer id, BoaUser user) {
|
||||
Wallpaper wallpaper = wallpaperRepository.findById(id)
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
// 属主校验:超管可删任意;校管理员可删本校;普通用户仅可删自己上传的个人壁纸
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(user.getId())
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
boolean superAdmin = user.getType() == 1;
|
||||
boolean ownPersonal = wallpaper.getType() != null && wallpaper.getType() == 2
|
||||
&& wallpaper.getUserId() != null && wallpaper.getUserId().equals(user.getId());
|
||||
boolean schAdminOwn = wallpaper.getType() != null && wallpaper.getType() == 1
|
||||
&& OtherUtil.isAdminById(currentRole.getRoleId())
|
||||
&& currentRole.getSchId() != null && currentRole.getSchId().equals(wallpaper.getSchId());
|
||||
if (!(superAdmin || ownPersonal || schAdminOwn)) {
|
||||
throw new BizException(ErrorCode.FORBIDDEN);
|
||||
}
|
||||
// 使用中清理:被删壁纸被用户设置引用时置空,避免裂图
|
||||
userSettingsRepository.clearWallpaperUrl(wallpaper.getWallpaperUrl());
|
||||
// 清理物理文件(非本地上传的跨仓地址静默跳过)
|
||||
FileUtil.deleteFile(FILE_PATH, wallpaper.getWallpaperUrl());
|
||||
wallpaperRepository.delete(wallpaper);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,6 +109,56 @@ public class UserSettingsServiceImpl implements IUserSettingsService {
|
||||
userSettingsRepository.save(model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Logo> findLogoList(Integer userId) {
|
||||
Optional<UserCurrentRole> currentRoleOpt = userCurrentRoleService.findById(userId);
|
||||
if (!currentRoleOpt.isPresent()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
UserCurrentRole currentRole = currentRoleOpt.get();
|
||||
List<Logo> resultList = new ArrayList<>();
|
||||
List<Logo> baseList = logoRepository.findBySchIdAndType(0, 0);
|
||||
List<Logo> schList = logoRepository.findBySchIdAndType(currentRole.getSchId(), 1);
|
||||
if (currentRole.getSchId() == 0) baseList.forEach(x -> x.setIsEdit(true));
|
||||
if (OtherUtil.isAdminById(currentRole.getRoleId())) schList.forEach(x -> x.setIsEdit(true));
|
||||
resultList.addAll(baseList);
|
||||
resultList.addAll(schList);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveLogo(Logo logo) {
|
||||
// 同 type+schId 已存在则覆盖(一校/系统各一行)
|
||||
Optional<Logo> exist = logoRepository.findByTypeAndSchId(logo.getType(), logo.getSchId());
|
||||
if (exist.isPresent()) {
|
||||
Logo model = exist.get();
|
||||
model.setLogoUrl(logo.getLogoUrl());
|
||||
model.setUserId(logo.getUserId());
|
||||
logoRepository.save(model);
|
||||
} else {
|
||||
logoRepository.save(logo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteLogoById(Integer id, BoaUser user) {
|
||||
Logo logo = logoRepository.findById(id)
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
// 属主校验:超管可删系统默认;校管理员仅可删本校
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(user.getId())
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
boolean superAdmin = user.getType() == 1;
|
||||
boolean schAdminOwn = logo.getType() != null && logo.getType() == 1
|
||||
&& OtherUtil.isAdminById(currentRole.getRoleId())
|
||||
&& currentRole.getSchId() != null && currentRole.getSchId().equals(logo.getSchId());
|
||||
if (!(superAdmin || schAdminOwn)) {
|
||||
throw new BizException(ErrorCode.FORBIDDEN);
|
||||
}
|
||||
FileUtil.deleteFile(FILE_PATH, logo.getLogoUrl());
|
||||
logoRepository.delete(logo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UserSettings> findUserSettings(Integer userId) {
|
||||
Optional<UserCurrentRole> currentRoleOpt = userCurrentRoleService.findById(userId);
|
||||
|
||||
Reference in New Issue
Block a user