package com.yunhai.userhome.service.impl; import com.yunhai.platform.utils.model.BoaUser; import com.yunhai.userhome.entity.SchSettings; import com.yunhai.userhome.entity.UserCurrentRole; import com.yunhai.userhome.repository.SchSettingsRepository; import com.yunhai.userhome.service.ISchSettingsService; import com.yunhai.userhome.service.IUserCurrentRoleService; import com.yunhai.userhome.utils.ThemeCodec; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Optional; /** * 学校设置服务:学校设置完全本地化(sch_settings 表),不再依赖跨仓 schConfig。 *

findBySchId 直接查本地表,查不到兜底默认主题;saveSchTheme/saveSchLogo 写本地表即可闭环。

*/ @Service public class SchSettingsServiceImpl implements ISchSettingsService { @Autowired private SchSettingsRepository schSettingsRepository; @Autowired private IUserCurrentRoleService userCurrentRoleService; @Override public void save(SchSettings schSettings) { schSettingsRepository.save(schSettings); } @Override public Optional findBySchIdRaw(Integer schId) { return schSettingsRepository.findFirstBySchId(schId); } @Override public Optional findBySchId(Integer schId) { Optional local = schSettingsRepository.findFirstBySchId(schId); if (local.isPresent()) { return local; } SchSettings def = new SchSettings(); def.setSchId(schId); def.setThemeName("default-theme"); def.setAppBgUrl(""); def.setLogoUrl(""); return Optional.of(def); } @Override public java.util.List findBySchIds(java.util.List schId) { return schSettingsRepository.getSchSettingList(schId); } @Override public Integer resolveSchId(BoaUser user) { if (user.getSchool() != null) { return user.getSchool().getId(); } if (user.getBureau() != null) { return user.getBureau().getId(); } return 0; } @Override public Integer resolveThemeSchId(BoaUser user, Integer explicitSchId) { if (user.getType() != null && user.getType() == 1) { return explicitSchId == null ? 0 : explicitSchId; } return userCurrentRoleService.findById(user.getId()) .map(UserCurrentRole::getSchId) .filter(schId -> schId != null && schId > 0) .orElseGet(() -> resolveSchId(user)); } @Override public ISchSettingsService.LayerValue readThemeWithFallback(Integer schId) { String themeState = decodeTheme(schId); if (themeState != null || schId == null || schId == ISchSettingsService.PLATFORM_SCH_ID) { return new ISchSettingsService.LayerValue(themeState, schId); } return new ISchSettingsService.LayerValue(decodeTheme(ISchSettingsService.PLATFORM_SCH_ID), ISchSettingsService.PLATFORM_SCH_ID); } @Override public ISchSettingsService.LayerValue readAvatarWithFallback(Integer schId) { String avatarStyle = readAvatarAt(schId); if (avatarStyle != null || schId == null || schId == ISchSettingsService.PLATFORM_SCH_ID) { return new ISchSettingsService.LayerValue(avatarStyle, schId); } return new ISchSettingsService.LayerValue(readAvatarAt(ISchSettingsService.PLATFORM_SCH_ID), ISchSettingsService.PLATFORM_SCH_ID); } /** 原样读某层主题档并解码(查无行/旧格式/脏值 → null) */ private String decodeTheme(Integer schId) { return schSettingsRepository.findFirstBySchId(schId) .map(SchSettings::getThemeName) .map(ThemeCodec::decodeThemeState) .orElse(null); } /** 原样读某层头像样式(查无行/非法值 → null) */ private String readAvatarAt(Integer schId) { return schSettingsRepository.findFirstBySchId(schId) .map(SchSettings::getAvatarStyle) .filter(ThemeCodec::isAvatarStyle) .orElse(null); } }