liujie
2026-09-16 a3a56121a476cd23d0f29bc391c07374dc5008f2
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
102
103
104
105
package com.tourguide.modules.operation.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.operation.dto.AgreementResponse;
import com.tourguide.modules.operation.dto.MiniAgreementSummaryResponse;
import com.tourguide.modules.operation.entity.AgreementSetting;
import com.tourguide.modules.operation.mapper.AgreementSettingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
 
@Service
public class AgreementSettingService {
    private static final Map<String, String> MINI_AGREEMENT_TITLES = createMiniAgreementTitles();
    private static final Map<String, String> GUIDE_AGREEMENT_TITLES = createGuideAgreementTitles();
    private final AgreementSettingMapper agreementMapper;
 
    public AgreementSettingService(AgreementSettingMapper agreementMapper) {
        this.agreementMapper = agreementMapper;
    }
 
    public List<AgreementResponse> list() {
        return agreementMapper.selectList(new LambdaQueryWrapper<AgreementSetting>()
                        .orderByAsc(AgreementSetting::getSortOrder, AgreementSetting::getId))
                .stream().map(AgreementResponse::new).collect(Collectors.toList());
    }
 
    public AgreementResponse get(String code) {
        return new AgreementResponse(requireAgreement(code));
    }
 
    public List<MiniAgreementSummaryResponse> listMiniAgreements() {
        return listAllowedAgreements(MINI_AGREEMENT_TITLES);
    }
 
    public List<MiniAgreementSummaryResponse> listGuideAgreements() {
        return listAllowedAgreements(GUIDE_AGREEMENT_TITLES);
    }
 
    private List<MiniAgreementSummaryResponse> listAllowedAgreements(Map<String, String> allowedTitles) {
        Map<String, AgreementSetting> agreements = agreementMapper.selectList(
                        new LambdaQueryWrapper<AgreementSetting>()
                                .in(AgreementSetting::getAgreementCode, allowedTitles.keySet()))
                .stream().collect(Collectors.toMap(AgreementSetting::getAgreementCode, value -> value));
        return allowedTitles.entrySet().stream()
                .filter(entry -> agreements.containsKey(entry.getKey()))
                .map(entry -> new MiniAgreementSummaryResponse(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
    }
 
    public AgreementResponse getMiniAgreement(String code) {
        return getAllowedAgreement(code, MINI_AGREEMENT_TITLES);
    }
 
    public AgreementResponse getGuideAgreement(String code) {
        return getAllowedAgreement(code, GUIDE_AGREEMENT_TITLES);
    }
 
    private AgreementResponse getAllowedAgreement(String code, Map<String, String> allowedTitles) {
        String title = allowedTitles.get(code);
        if (title == null) {
            throw new BusinessException(404, "协议类型不存在");
        }
        return new AgreementResponse(requireAgreement(code), title);
    }
 
    @Transactional
    public AgreementResponse update(String code, String content) {
        AgreementSetting agreement = requireAgreement(code);
        agreement.setContent(content);
        agreement.setUpdatedAt(LocalDateTime.now());
        agreementMapper.updateById(agreement);
        return new AgreementResponse(agreement);
    }
 
    private AgreementSetting requireAgreement(String code) {
        AgreementSetting agreement = agreementMapper.selectOne(new LambdaQueryWrapper<AgreementSetting>()
                .eq(AgreementSetting::getAgreementCode, code).last("LIMIT 1"));
        if (agreement == null) {
            throw new BusinessException(404, "协议类型不存在");
        }
        return agreement;
    }
 
    private static Map<String, String> createMiniAgreementTitles() {
        Map<String, String> titles = new LinkedHashMap<String, String>();
        titles.put("VISITOR_USER_AGREEMENT", "服务条款");
        titles.put("VISITOR_PRIVACY_PERMISSION", "隐私与权限");
        titles.put("EXPLANATION_SERVICE_NOTICE", "纯讲解服务须知协议");
        return titles;
    }
 
    private static Map<String, String> createGuideAgreementTitles() {
        Map<String, String> titles = new LinkedHashMap<String, String>();
        titles.put("GUIDE_USER_AGREEMENT", "用户协议");
        titles.put("EXPLANATION_SERVICE_COOPERATION", "讲解服务合作协议");
        titles.put("GUIDE_REWARD_PUNISHMENT", "平台奖惩机制");
        return titles;
    }
}