shinnosuke
2026-08-06 f12cbba923cebd30416d170eff2b116f4f013a29
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
package com.talentcard.server.business.admin;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import com.talentcard.server.business.DatabaseRows;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class ProtocolSettingAdminService {
    private static final Map<String, String> PROTOCOLS = Map.of(
            "visitor_user_agreement", "用户协议-游客",
            "guide_user_agreement", "用户协议-导游",
            "visitor_privacy_policy", "隐私与权限-游客",
            "pure_service_notice", "纯讲解服务须知协议",
            "guide_service_agreement", "讲解服务合作协议",
            "guide_reward_policy", "导游奖惩机制"
    );
 
    private final JdbcTemplate jdbc;
 
    public ProtocolSettingAdminService(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }
 
    @Transactional(readOnly = true)
    public Map<String, Object> detail(String key) {
        String title = title(key);
        List<Map<String, Object>> rows = DatabaseRows.normalize(jdbc.queryForList(
                "select protocol_key, title, content_html, updated_at from admin_protocol_setting where protocol_key = ?", key));
        if (rows.isEmpty()) return Map.of("key", key, "title", title, "contentHtml", "");
        return rows.get(0);
    }
 
    @Transactional
    public Map<String, Object> save(String key, String contentHtml) {
        String title = title(key);
        String content = contentHtml == null ? "" : contentHtml.trim();
        if (content.length() > 100_000) throw new BusinessException(ErrorCode.BAD_REQUEST, "协议内容不能超过 100000 个字符");
        jdbc.update("""
                insert into admin_protocol_setting(protocol_key, title, content_html)
                values (?, ?, ?)
                on duplicate key update title = values(title), content_html = values(content_html)
                """, key, title, content);
        return detail(key);
    }
 
    private String title(String key) {
        String title = PROTOCOLS.get(key);
        if (title == null) throw new BusinessException(ErrorCode.BAD_REQUEST, "不支持的协议类型");
        return title;
    }
}