Files
UserhomeService/src/main/java/com/yunhai/userhome/service/impl/UserSettingsServiceImpl.java
T
chenxing e7ca6dd846
build / build-push (push) Failing after 40s
logo,壁纸修复
2026-08-28 10:23:36 +08:00

288 lines
12 KiB
Java

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;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 用户设置服务(对齐旧 UserHome service.impl.UserSettingsService)。
*/
@Service
public class UserSettingsServiceImpl implements IUserSettingsService {
@Autowired
private WallpaperRepository wallpaperRepository;
@Autowired
private LogoRepository logoRepository;
@Autowired
private UserSettingsRepository userSettingsRepository;
@Autowired
private IUserCurrentRoleService userCurrentRoleService;
@Autowired
private LightAppRepository lightAppRepository;
@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);
if (!currentRoleOpt.isPresent()) {
return new ArrayList<>();
}
UserCurrentRole currentRole = currentRoleOpt.get();
List<Wallpaper> resultList = new ArrayList<>();
List<Wallpaper> baseList = wallpaperRepository.findBySchIdAndType(0, 0);
List<Wallpaper> schList = wallpaperRepository.findBySchIdAndType(currentRole.getSchId(), 1);
List<Wallpaper> userList = wallpaperRepository.findByUserIdAndType(userId, 2);
if (currentRole.getSchId() == 0) baseList.forEach(x -> x.setIsEdit(true));
if (OtherUtil.isAdminById(currentRole.getRoleId())) schList.forEach(x -> x.setIsEdit(true));
userList.forEach(x -> x.setIsEdit(true));
resultList.addAll(baseList);
resultList.addAll(schList);
resultList.addAll(userList);
return resultList;
}
@Override
@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
public void saveWallpaper(Wallpaper wallpaper) {
wallpaperRepository.save(wallpaper);
}
@Override
public void checkWallpaper(Integer userId, String wallpaperUrl) {
UserSettings model = getUserSettings(null, userId);
model.setWallpaperUrl(wallpaperUrl);
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);
if (!currentRoleOpt.isPresent()) {
return Optional.empty();
}
UserCurrentRole currentRole = currentRoleOpt.get();
return userSettingsRepository.findByUserIdAndSchIdAndRoleId(currentRole.getUserId(), currentRole.getSchId(), currentRole.getRoleId());
}
@Override
public void saveUserSettings(UserSettings userSettings) {
userSettingsRepository.save(userSettings);
}
@Override
public void saveUserWidget(UserSettings userSettings) {
UserSettings model = getUserSettings(userSettings.getId(), userSettings.getUserId());
model.setXAxis(userSettings.getXAxis());
model.setYAxis(userSettings.getYAxis());
model.setWidth(userSettings.getWidth());
model.setHeight(userSettings.getHeight());
model.setIsWidgetShow(true);
userSettingsRepository.save(model);
}
@Override
public void saveUserAppIds(Integer id, Integer userId, String appIds) {
UserSettings model = getUserSettings(id, userId);
model.setWidgetAppIds(appIds);
userSettingsRepository.save(model);
}
@Override
public void toggleWidgetShow(Integer id, Integer userId) {
UserSettings model = getUserSettings(id, userId);
if (model.getIsWidgetShow() == null) model.setIsWidgetShow(true);
model.setIsWidgetShow(!model.getIsWidgetShow());
userSettingsRepository.save(model);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public List<LightApp> findLightAppList(Integer schId, Integer userId) {
Specification specification = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
Predicate basePre = criteriaBuilder.and(
criteriaBuilder.equal(root.get("schId"), 0),
criteriaBuilder.equal(root.get("isPublic"), true));
Predicate schPre = criteriaBuilder.and(
criteriaBuilder.equal(root.get("schId"), schId),
criteriaBuilder.equal(root.get("isPublic"), true));
Predicate userPre = criteriaBuilder.and(
criteriaBuilder.equal(root.get("schId"), schId),
criteriaBuilder.equal(root.get("userId"), userId));
if (schId == 0)
predicates.add(basePre);
else if (userId == null)
predicates.add(criteriaBuilder.or(basePre, schPre));
else
predicates.add(criteriaBuilder.or(basePre, schPre, userPre));
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
};
Sort sort = Sort.by(Sort.Direction.DESC, "id");
List<LightApp> result = lightAppRepository.findAll(specification, sort);
if (schId == 0) result.forEach(x -> x.setIsEdit(true));
else if (userId == null)
result.stream().filter(x -> x.getSchId() != null && x.getSchId().equals(schId) && x.getUserId() == null)
.forEach(x -> x.setIsEdit(true));
else
result.stream().filter(x -> x.getSchId() != null && x.getUserId() != null && x.getSchId().equals(schId) && x.getUserId().equals(userId))
.forEach(x -> x.setIsEdit(true));
return result;
}
@Override
public void saveLightApp(LightApp lightApp) {
if (lightApp.getSchId() == 0) lightApp.setUserId(null);
if (lightApp.getUserId() == null) lightApp.setIsPublic(true);
lightAppRepository.save(lightApp);
}
@Override
public void deleteLightAppById(Integer id) {
lightAppRepository.deleteById(id);
}
@Override
public void deleteDesktopAppById(Integer id) {
userSettingsDesktopAppRepository.deleteById(id);
}
@Override
public void saveDesktopApp(UserSettingsDesktopApp model) {
userSettingsDesktopAppRepository.save(model);
}
@Override
public List<UserSettingsDesktopApp> saveDesktopAppList(List<UserSettingsDesktopApp> list) {
return userSettingsDesktopAppRepository.saveAll(list);
}
@Override
public UserSettings initUserSettings(Integer userId) {
UserCurrentRole currentRole = userCurrentRoleService.findById(userId)
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
UserSettings model = new UserSettings();
model.setSchId(currentRole.getSchId());
model.setRoleId(currentRole.getRoleId());
model.setUserId(currentRole.getUserId());
return userSettingsRepository.save(model);
}
private UserSettings getUserSettings(Integer id, Integer userId) {
Optional<UserSettings> optional;
if (id != null)
optional = userSettingsRepository.findById(id);
else
optional = findUserSettings(userId);
if (optional.isPresent()) return optional.get();
else {
return initUserSettings(userId);
}
}
}