package com.tourguide.modules.operation.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tourguide.modules.operation.dto.PlatformSettingsRequest; import com.tourguide.modules.operation.dto.PlatformSettingsResponse; import com.tourguide.modules.operation.entity.SystemConfig; import com.tourguide.modules.operation.mapper.SystemConfigMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** 平台运营参数读取和保存服务。 */ @Service public class PlatformSettingsService { private static final String AUTO_REFUND_MINUTES = "platform.order.unaccepted-auto-refund-minutes"; private static final String COMMISSION_RATE = "platform.guide.commission-rate"; private static final String PRECISE_ORDER_COUNT = "platform.dispatch.precise-order-count"; private static final String PRECISE_RATING = "platform.dispatch.precise-rating"; private static final String SETTLEMENT_DAYS = "platform.finance.settlement-days"; private static final String CUSTOMER_SERVICE_WECHAT = "platform.customer-service.wechat"; private static final String CUSTOMER_SERVICE_PHONE = "platform.customer-service.phone"; private static final List SETTING_KEYS = Arrays.asList( AUTO_REFUND_MINUTES, COMMISSION_RATE, PRECISE_ORDER_COUNT, PRECISE_RATING, SETTLEMENT_DAYS, CUSTOMER_SERVICE_WECHAT, CUSTOMER_SERVICE_PHONE); private final SystemConfigMapper configMapper; public PlatformSettingsService(SystemConfigMapper configMapper) { this.configMapper = configMapper; } public PlatformSettingsResponse getSettings() { Map values = loadValues(); PlatformSettingsResponse response = new PlatformSettingsResponse(); response.setUnacceptedAutoRefundMinutes(integerValue(values, AUTO_REFUND_MINUTES, 5)); response.setCommissionRate(decimalValue(values, COMMISSION_RATE, "15")); response.setPreciseDispatchOrderCount(integerValue(values, PRECISE_ORDER_COUNT, 10)); response.setPreciseDispatchRating(decimalValue(values, PRECISE_RATING, "4.8")); response.setSettlementDays(integerValue(values, SETTLEMENT_DAYS, 1)); response.setCustomerServiceWechat(values.getOrDefault(CUSTOMER_SERVICE_WECHAT, "TourGuide客服")); response.setCustomerServicePhone(values.getOrDefault(CUSTOMER_SERVICE_PHONE, "400-000-0000")); return response; } @Transactional public PlatformSettingsResponse save(PlatformSettingsRequest request) { upsert("未接单自动退款时间", AUTO_REFUND_MINUTES, request.getUnacceptedAutoRefundMinutes().toString(), "单位:分钟"); upsert("个人导游提成比例", COMMISSION_RATE, decimalText(request.getCommissionRate()), "单位:百分比"); upsert("精准可用最低接单数", PRECISE_ORDER_COUNT, request.getPreciseDispatchOrderCount().toString(), "达到该接单数后参与精准派单"); upsert("精准可用最低综合评分", PRECISE_RATING, decimalText(request.getPreciseDispatchRating()), "达到该评分后参与精准派单"); upsert("订单结算周期", SETTLEMENT_DAYS, request.getSettlementDays().toString(), "T+N天结算"); upsert("客服微信", CUSTOMER_SERVICE_WECHAT, request.getCustomerServiceWechat().trim(), "平台客服微信号"); upsert("客服电话", CUSTOMER_SERVICE_PHONE, request.getCustomerServicePhone().trim(), "平台客服电话"); return getSettings(); } private Map loadValues() { List configs = configMapper.selectList( new LambdaQueryWrapper().in(SystemConfig::getConfigKey, SETTING_KEYS)); Map values = new HashMap(); for (SystemConfig config : configs) { values.put(config.getConfigKey(), config.getConfigValue()); } return values; } private void upsert(String name, String key, String value, String remark) { SystemConfig config = configMapper.selectOne( new LambdaQueryWrapper().eq(SystemConfig::getConfigKey, key)); if (config == null) { config = new SystemConfig(); config.setConfigKey(key); config.setConfigType(1); } config.setConfigName(name); config.setConfigValue(value); config.setRemark(remark); if (config.getId() == null) { configMapper.insert(config); } else { configMapper.updateById(config); } } private Integer integerValue(Map values, String key, int defaultValue) { String value = values.get(key); return value == null ? defaultValue : Integer.valueOf(value); } private BigDecimal decimalValue(Map values, String key, String defaultValue) { return new BigDecimal(values.getOrDefault(key, defaultValue)); } private String decimalText(BigDecimal value) { return value.stripTrailingZeros().toPlainString(); } }