1
zj
2025-08-21 01bd03652fef2399c2acfb1930d044b106393f85
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.ruoyi.im.config;
 
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
 
import java.util.HashMap;
import java.util.Map;
 
public class UpdateUserInfoRequest {
    private Map<String, Object> bodyData;
 
    public UpdateUserInfoRequest() {
        bodyData = new HashMap<>();
    }
 
    /**
     * 设置name字段
     */
    public UpdateUserInfoRequest setName(String name) {
        if (name != null) {
            bodyData.put("name", name);
        }
        return this;
    }
 
    /**
     * 设置avatar字段
     */
    public UpdateUserInfoRequest setAvatar(String avatar) {
        if (avatar != null) {
            bodyData.put("avatar", avatar);
        }
        return this;
    }
 
    /**
     * 设置sign字段
     */
    public UpdateUserInfoRequest setSign(String sign) {
        if (sign != null) {
            bodyData.put("sign", sign);
        }
        return this;
    }
 
    /**
     * 设置email字段
     */
    public UpdateUserInfoRequest setEmail(String email) {
        if (email != null) {
            bodyData.put("email", email);
        }
        return this;
    }
 
    /**
     * 设置birthday字段
     */
    public UpdateUserInfoRequest setBirthday(String birthday) {
        if (birthday != null) {
            bodyData.put("birthday", birthday);
        }
        return this;
    }
 
    /**
     * 设置mobile字段
     */
    public UpdateUserInfoRequest setMobile(String mobile) {
        if (mobile != null) {
            bodyData.put("mobile", mobile);
        }
        return this;
    }
 
    /**
     * 设置gender字段
     */
    public UpdateUserInfoRequest setGender(String gender) {
        if (gender != null) {
            bodyData.put("gender", gender);
        }
        return this;
    }
 
    /**
     * 设置extension字段
     */
    public UpdateUserInfoRequest setExtension(String extension) {
        if (extension != null) {
            bodyData.put("extension", extension);
        }
        return this;
    }
 
    /**
     * 批量设置所有字段
     */
    public UpdateUserInfoRequest setAllFields(Map<String, Object> fields) {
        if (fields != null) {
            for (Map.Entry<String, Object> entry : fields.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
 
                if (value != null) {
                    switch (key) {
                        case "name":
                            setName((String) value);
                            break;
                        case "avatar":
                            setAvatar((String) value);
                            break;
                        case "sign":
                            setSign((String) value);
                            break;
                        case "email":
                            setEmail((String) value);
                            break;
                        case "birthday":
                            setBirthday((String) value);
                            break;
                        case "mobile":
                            setMobile((String) value);
                            break;
                        case "gender":
                            setGender((String) value);
                            break;
                        case "extension":
                            setExtension((String) value);
                            break;
                        default:
                            // 忽略未知字段
                            break;
                    }
                }
            }
        }
        return this;
    }
 
    /**
     * 构建JSON字符串
     */
    public String build() {
        JSONObject jsonObject = new JSONObject(bodyData);
        return jsonObject.toString();
    }
 
    /**
     * 获取Map形式的数据
     */
    public Map<String, Object> getBodyData() {
        return new HashMap<>(bodyData);
    }
 
    /**
     * 清空所有已设置的数据
     */
    public void clear() {
        bodyData.clear();
    }
 
    /**
     * 使用示例
     */
    public static void main(String[] args) {
        // 示例1: 只设置部分字段
        UpdateUserInfoRequest builder = new UpdateUserInfoRequest();
        String partialBody = builder
                .setName("zhangsan")
                .setEmail("zhangsan@corp.xx.com")
                .setMobile("13312345678")
                .build();
        System.out.println("部分字段请求体: " + partialBody);
 
        // 示例2: 使用Map批量设置
        builder.clear();
        Map<String, Object> fields = new HashMap<>();
        fields.put("name", "lisi");
        fields.put("avatar", "http://xxxx.xx/lisi.png");
        fields.put("sign", "Hello World");
        fields.put("gender", "1");
 
        String batchBody = builder.setAllFields(fields).build();
        System.out.println("批量设置请求体: " + batchBody);
 
        // 示例3: 完全不设置任何字段
        builder.clear();
        String emptyBody = builder.build();
        System.out.println("空请求体: " + emptyBody);
 
        // 示例4: 设置所有字段
        builder.clear();
        String fullBody = builder
                .setName("zhangsan")
                .setAvatar("http://xxxx.xx/x.png")
                .setSign("Hello World")
                .setEmail("zhangsan@corp.xx.com")
                .setBirthday("1990-01-01")
                .setMobile("13312345678")
                .setGender("2")
                .setExtension("xxxx")
                .build();
        System.out.println("完整请求体: " + fullBody);
    }
}