package com.designhub.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.designhub.entity.SystemConfig; import com.designhub.exception.BusinessException; import com.designhub.mapper.SystemConfigMapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 系统配置Service */ @Service @RequiredArgsConstructor public class SystemConfigService { private final SystemConfigMapper systemConfigMapper; /** * 获取所有配置列表 */ public List getAllConfigs() { return systemConfigMapper.selectList( new LambdaQueryWrapper() .orderByAsc(SystemConfig::getConfigGroup, SystemConfig::getSortOrder) ); } /** * 根据分组获取配置列表 */ public List getConfigsByGroup(String group) { return systemConfigMapper.selectList( new LambdaQueryWrapper() .eq(SystemConfig::getConfigGroup, group) .orderByAsc(SystemConfig::getSortOrder) ); } /** * 根据键获取配置值 */ public String getConfigValue(String key) { SystemConfig config = systemConfigMapper.selectOne( new LambdaQueryWrapper() .eq(SystemConfig::getConfigKey, key) ); return config != null ? config.getConfigValue() : null; } /** * 根据键获取配置值(带默认值) */ public String getConfigValue(String key, String defaultValue) { String value = getConfigValue(key); return value != null ? value : defaultValue; } /** * 获取整型配置值 */ public Integer getIntValue(String key, Integer defaultValue) { String value = getConfigValue(key); if (value != null) { try { return Integer.parseInt(value); } catch (NumberFormatException e) { return defaultValue; } } return defaultValue; } /** * 获取布尔型配置值 */ public Boolean getBooleanValue(String key, Boolean defaultValue) { String value = getConfigValue(key); if (value != null) { return Boolean.parseBoolean(value); } return defaultValue; } /** * 获取所有配置的Map */ public Map getAllConfigsMap() { List configs = getAllConfigs(); return configs.stream() .collect(Collectors.toMap( SystemConfig::getConfigKey, SystemConfig::getConfigValue, (v1, v2) -> v2 )); } /** * 根据分组获取配置Map */ public Map getConfigsMapByGroup(String group) { List configs = getConfigsByGroup(group); return configs.stream() .collect(Collectors.toMap( SystemConfig::getConfigKey, SystemConfig::getConfigValue, (v1, v2) -> v2 )); } /** * 创建配置 */ @Transactional(rollbackFor = Exception.class) public SystemConfig createConfig(SystemConfig config) { // 验证键的唯一性 long count = systemConfigMapper.selectCount( new LambdaQueryWrapper() .eq(SystemConfig::getConfigKey, config.getConfigKey()) ); if (count > 0) { throw new BusinessException("配置键已存在"); } // 设置默认值 if (config.getBuiltIn() == null) { config.setBuiltIn(0); } if (config.getSortOrder() == null) { config.setSortOrder(0); } systemConfigMapper.insert(config); return config; } /** * 更新配置 */ @Transactional(rollbackFor = Exception.class) public boolean updateConfig(SystemConfig config) { SystemConfig existConfig = systemConfigMapper.selectById(config.getId()); if (existConfig == null) { throw new BusinessException("配置不存在"); } // 内置配置不允许修改键和分组 if (existConfig.getBuiltIn() == 1) { config.setConfigKey(null); config.setConfigGroup(null); config.setBuiltIn(null); } return systemConfigMapper.updateById(config) > 0; } /** * 批量更新配置值 */ @Transactional(rollbackFor = Exception.class) public boolean batchUpdateValues(Map configMap) { for (Map.Entry entry : configMap.entrySet()) { SystemConfig config = systemConfigMapper.selectOne( new LambdaQueryWrapper() .eq(SystemConfig::getConfigKey, entry.getKey()) ); if (config != null) { config.setConfigValue(entry.getValue()); systemConfigMapper.updateById(config); } } return true; } /** * 删除配置 */ @Transactional(rollbackFor = Exception.class) public boolean deleteConfig(Long id) { SystemConfig config = systemConfigMapper.selectById(id); if (config == null) { throw new BusinessException("配置不存在"); } // 内置配置不允许删除 if (config.getBuiltIn() == 1) { throw new BusinessException("内置配置不允许删除"); } return systemConfigMapper.deleteById(id) > 0; } /** * 初始化默认配置 */ @Transactional(rollbackFor = Exception.class) public void initDefaultConfigs() { // 任务相关配置 createConfigIfNotExists("task.max_concurrent", "5", "task", "最大并发任务数", "设计师同时进行的最大任务数", "number", 1); createConfigIfNotExists("task.grab_timeout", "30", "task", "抢单超时时间(分钟)", "任务发布后多久无人抢单则自动关闭", "number", 1); createConfigIfNotExists("task.auto_close_days", "7", "task", "自动关闭天数", "未完成的任务多少天后自动关闭", "number", 1); // 积分相关配置 createConfigIfNotExists("score.low_balance_alert", "100", "score", "积分余额预警阈值", "部门积分低于此值时发出预警", "number", 1); createConfigIfNotExists("score.delay_penalty_rate", "10", "score", "延误扣分比例(%)", "任务延误时的扣分比例", "number", 1); createConfigIfNotExists("score.delay_penalty_per_hour", "0.5", "score", "延误每小时扣分", "任务延误完结时每小时扣除的分数", "number", 1); createConfigIfNotExists("score.monthly_reset_enabled", "true", "score", "月度积分清零", "接单部门积分是否按月清零重新统计", "boolean", 1); // 每日进度更新检查配置 createConfigIfNotExists("progress.daily_check_enabled", "true", "progress", "启用每日进度检查", "是否启用每天18点检查设计师进度更新", "boolean", 1); createConfigIfNotExists("progress.daily_check_hour", "18", "progress", "每日检查时间(小时)", "每天检查进度的时间,24小时制", "number", 1); createConfigIfNotExists("progress.no_update_deduction", "1", "progress", "未更新进度扣分", "设计师当日未更新进度时扣除的分数", "number", 1); createConfigIfNotExists("progress.notify_on_deduction", "true", "progress", "扣分时发送钉钉通知", "未更新进度扣分时是否发送钉钉提醒", "boolean", 1); // 通知相关配置 createConfigIfNotExists("notification.dingtalk_enabled", "false", "notification", "启用钉钉通知", "是否启用钉钉消息推送", "boolean", 1); createConfigIfNotExists("notification.email_enabled", "false", "notification", "启用邮件通知", "是否启用邮件通知", "boolean", 1); // 激励系统配置 createConfigIfNotExists("incentive.enabled", "true", "incentive", "启用激励系统", "是否启用激励功能", "boolean", 1); createConfigIfNotExists("incentive.daily_checkin_score", "5", "incentive", "每日签到奖励积分", "设计师每日签到可获得的积分", "number", 1); // 导航栏配置 - 主菜单 createConfigIfNotExists("navbar.menu.home", "true", "navbar", "显示工作台菜单", "是否在导航栏显示工作台菜单", "boolean", 1); createConfigIfNotExists("navbar.menu.tasks", "true", "navbar", "显示任务大厅菜单", "是否在导航栏显示任务大厅菜单", "boolean", 1); createConfigIfNotExists("navbar.menu.designer_hall", "true", "navbar", "显示设计师大厅菜单", "是否在导航栏显示设计师大厅菜单", "boolean", 1); createConfigIfNotExists("navbar.menu.incentive", "true", "navbar", "显示激励中心菜单", "是否在导航栏显示激励中心菜单", "boolean", 1); createConfigIfNotExists("navbar.menu.analytics", "true", "navbar", "显示数据分析菜单", "是否在导航栏显示数据分析菜单", "boolean", 1); // 导航栏配置 - 功能按钮 createConfigIfNotExists("navbar.action.notification", "true", "navbar", "显示通知按钮", "是否在导航栏显示通知铃铛按钮", "boolean", 1); createConfigIfNotExists("navbar.action.checkin", "true", "navbar", "显示签到按钮", "是否在导航栏显示每日签到按钮", "boolean", 1); createConfigIfNotExists("navbar.action.achievement", "true", "navbar", "显示成就按钮", "是否在导航栏显示成就中心按钮", "boolean", 1); createConfigIfNotExists("navbar.action.settings", "true", "navbar", "显示设置按钮", "是否在导航栏显示设置按钮", "boolean", 1); // 任务发布类型配置 createConfigIfNotExists("task.release_type.default", "true", "task", "显示默认模式按钮", "是否显示「默认模式」发布类型按钮", "boolean", 1); createConfigIfNotExists("task.release_type.recruitment", "true", "task", "显示招募任务按钮", "是否显示「招募任务」发布类型按钮", "boolean", 1); createConfigIfNotExists("task.release_type.assigned", "true", "task", "显示指定任务按钮", "是否显示「指定任务」发布类型按钮", "boolean", 1); } /** * 如果不存在则创建配置 */ private void createConfigIfNotExists(String key, String value, String group, String name, String description, String valueType, Integer builtIn) { long count = systemConfigMapper.selectCount( new LambdaQueryWrapper() .eq(SystemConfig::getConfigKey, key) ); if (count == 0) { SystemConfig config = new SystemConfig(); config.setConfigKey(key); config.setConfigValue(value); config.setConfigGroup(group); config.setConfigName(name); config.setDescription(description); config.setValueType(valueType); config.setBuiltIn(builtIn); config.setSortOrder(0); systemConfigMapper.insert(config); } } }