MrShi
昨天 7ee466ebc953bb5640bcf42f2b8e2a87aa471c21
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
package com.doumee.biz.system.impl;
 
import com.doumee.biz.system.AgreementConfigBiz;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.dao.dto.AgreementConfigDTO;
import com.doumee.dao.system.model.SystemDictData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 协议配置业务实现
 * @author rk
 * @date 2026/04/13
 */
@Service
public class AgreementConfigBizImpl implements AgreementConfigBiz {
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Override
    public AgreementConfigDTO getConfig() {
        AgreementConfigDTO dto = new AgreementConfigDTO();
        dto.setPrivacyAgreement(getValue(Constants.PRIVACY_AGREEMENT));
        dto.setUserAgreement(getValue(Constants.USER_AGREEMENT));
        dto.setAboutUs(getValue(Constants.ABOUT_US));
        return dto;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class, BusinessException.class})
    public void saveConfig(AgreementConfigDTO dto) {
        validate(dto);
        saveOrUpdate(Constants.PRIVACY_AGREEMENT, "隐私协议", dto.getPrivacyAgreement());
        saveOrUpdate(Constants.USER_AGREEMENT, "用户协议", dto.getUserAgreement());
        saveOrUpdate(Constants.ABOUT_US, "关于我们", dto.getAboutUs());
    }
 
    private String getValue(String label) {
        SystemDictData data = systemDictDataBiz.queryByCode(Constants.SYSTEM, label);
        return data != null ? data.getCode() : null;
    }
 
    private void saveOrUpdate(String label, String remark, String value) {
        SystemDictData existing = systemDictDataBiz.queryByCode(Constants.SYSTEM, label);
        if (existing != null && existing.getId() != null) {
            existing.setCode(value);
            systemDictDataBiz.updateById(existing);
        } else {
            SystemDictData newData = new SystemDictData();
            newData.setDictId(100);
            newData.setLabel(label);
            newData.setRemark(remark);
            newData.setCode(value);
            newData.setDisabled(false);
            newData.setDeleted(false);
            systemDictDataBiz.create(newData);
        }
    }
 
    private void validate(AgreementConfigDTO dto) {
        if (dto == null
                || StringUtils.isBlank(dto.getPrivacyAgreement())
                || StringUtils.isBlank(dto.getUserAgreement())
                || StringUtils.isBlank(dto.getAboutUs())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "所有配置项均为必填");
        }
    }
}