package com.talentcard.server.business.admin;
|
|
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(
|
"talent_user_agreement", "用户协议-人才",
|
"merchant_user_agreement", "用户协议-商户",
|
"talent_privacy_policy", "隐私与权限-人才",
|
"merchant_privacy_policy", "隐私与权限-商户"
|
);
|
private static final Map<String, AgreementSyncTarget> AGREEMENT_SYNC_TARGETS = Map.of(
|
"talent_user_agreement", new AgreementSyncTarget("app_agreement", "user", "用户协议", "2026.07"),
|
"talent_privacy_policy", new AgreementSyncTarget("app_agreement", "privacy", "隐私与权限", "2026.07"),
|
"merchant_user_agreement", new AgreementSyncTarget("merchant_agreement", "user", "用户协议", "1.0.0"),
|
"merchant_privacy_policy", new AgreementSyncTarget("merchant_agreement", "privacy", "隐私与权限", "1.0.0")
|
);
|
|
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);
|
syncAgreement(key, 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;
|
}
|
|
private void syncAgreement(String key, String contentHtml) {
|
AgreementSyncTarget target = AGREEMENT_SYNC_TARGETS.get(key);
|
if (target == null) return;
|
jdbc.update("""
|
insert into %s(agreement_type, title, version, effective_date, content_html, status)
|
values (?, ?, ?, current_date, ?, 1)
|
on duplicate key update title = values(title), content_html = values(content_html)
|
""".formatted(target.tableName), target.agreementType, target.title, target.defaultVersion, contentHtml);
|
}
|
|
private static final class AgreementSyncTarget {
|
private final String tableName;
|
private final String agreementType;
|
private final String title;
|
private final String defaultVersion;
|
|
private AgreementSyncTarget(String tableName, String agreementType, String title, String defaultVersion) {
|
this.tableName = tableName;
|
this.agreementType = agreementType;
|
this.title = title;
|
this.defaultVersion = defaultVersion;
|
}
|
}
|
}
|