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 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 detail(String key) { String title = title(key); List> 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 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; } }