liujie
2026-07-30 fc767bf18e262b724c78b5f860b6d908d4993dcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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<String> 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<String, String> 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<String, String> loadValues() {
        List<SystemConfig> configs = configMapper.selectList(
                new LambdaQueryWrapper<SystemConfig>().in(SystemConfig::getConfigKey, SETTING_KEYS));
        Map<String, String> values = new HashMap<String, String>();
        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<SystemConfig>().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<String, String> values, String key, int defaultValue) {
        String value = values.get(key);
        return value == null ? defaultValue : Integer.valueOf(value);
    }
 
    private BigDecimal decimalValue(Map<String, String> values, String key, String defaultValue) {
        return new BigDecimal(values.getOrDefault(key, defaultValue));
    }
 
    private String decimalText(BigDecimal value) {
        return value.stripTrailingZeros().toPlainString();
    }
}