@@ -0,0 +1,203 @@
|
||||
package com.yunhai.userhome.service.impl;
|
||||
|
||||
import com.yunhai.platform.apistd.BizException;
|
||||
import com.yunhai.platform.utils.exception.ErrorCode;
|
||||
import com.yunhai.userhome.entity.*;
|
||||
import com.yunhai.userhome.repository.LightAppRepository;
|
||||
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.OtherUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
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 UserSettingsRepository userSettingsRepository;
|
||||
|
||||
@Autowired
|
||||
private IUserCurrentRoleService userCurrentRoleService;
|
||||
|
||||
@Autowired
|
||||
private LightAppRepository lightAppRepository;
|
||||
|
||||
@Autowired
|
||||
private UserSettingsDesktopAppRepository userSettingsDesktopAppRepository;
|
||||
|
||||
@Override
|
||||
public List<Wallpaper> findWallList(Integer userId) {
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(userId)
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
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
|
||||
public void deleteWallById(Integer id) {
|
||||
if (!wallpaperRepository.existsById(id))
|
||||
throw new BizException(ErrorCode.INTERNAL_SERVER_ERROR);
|
||||
wallpaperRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@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 Optional<UserSettings> findUserSettings(Integer userId) {
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(userId)
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user