|
| 1 | +package me.chanjar.weixin.cp.api.impl; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonPrimitive; |
| 6 | +import lombok.NonNull; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 10 | +import me.chanjar.weixin.cp.api.WxCpSchoolUserService; |
| 11 | +import me.chanjar.weixin.cp.api.WxCpService; |
| 12 | +import me.chanjar.weixin.cp.bean.WxCpBaseResp; |
| 13 | +import me.chanjar.weixin.cp.bean.school.user.WxCpCreateParentRequest; |
| 14 | +import me.chanjar.weixin.cp.bean.school.user.WxCpUpdateParentRequest; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.School.*; |
| 20 | + |
| 21 | +/** |
| 22 | + * 企业微信家校沟通相关接口. |
| 23 | + * https://developer.work.weixin.qq.com/document/path/91638 |
| 24 | + * |
| 25 | + * @author <a href="https://github.com/0katekate0">Wang_Wong</a> |
| 26 | + * @date: 2022/6/18 9:10 |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +@RequiredArgsConstructor |
| 30 | +public class WxCpSchoolUserServiceImpl implements WxCpSchoolUserService { |
| 31 | + |
| 32 | + private final WxCpService cpService; |
| 33 | + |
| 34 | + @Override |
| 35 | + public WxCpBaseResp createStudent(@NonNull String studentUserId, @NonNull String name, @NonNull List<Integer> departments) throws WxErrorException { |
| 36 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(CREATE_STUDENT); |
| 37 | + JsonObject jsonObject = new JsonObject(); |
| 38 | + jsonObject.addProperty("student_userid", studentUserId); |
| 39 | + jsonObject.addProperty("name", name); |
| 40 | + JsonArray jsonArray = new JsonArray(); |
| 41 | + for (Integer depart : departments) { |
| 42 | + jsonArray.add(new JsonPrimitive(depart)); |
| 43 | + } |
| 44 | + jsonObject.add("department", jsonArray); |
| 45 | + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); |
| 46 | + return WxCpBaseResp.fromJson(responseContent); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public WxCpBaseResp deleteStudent(@NonNull String studentUserId) throws WxErrorException { |
| 51 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(DELETE_STUDENT) + studentUserId; |
| 52 | + String responseContent = this.cpService.get(apiUrl, null); |
| 53 | + return WxCpBaseResp.fromJson(responseContent); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public WxCpBaseResp updateStudent(@NonNull String studentUserId, String newStudentUserId, String name, List<Integer> departments) throws WxErrorException { |
| 58 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(UPDATE_STUDENT); |
| 59 | + JsonObject jsonObject = new JsonObject(); |
| 60 | + jsonObject.addProperty("student_userid", studentUserId); |
| 61 | + if (StringUtils.isNotEmpty(newStudentUserId)) { |
| 62 | + jsonObject.addProperty("new_student_userid", newStudentUserId); |
| 63 | + } |
| 64 | + if (StringUtils.isNotEmpty(name)) { |
| 65 | + jsonObject.addProperty("name", name); |
| 66 | + } |
| 67 | + if (departments != null && departments.size() > 0) { |
| 68 | + JsonArray jsonArray = new JsonArray(); |
| 69 | + for (Integer depart : departments) { |
| 70 | + jsonArray.add(new JsonPrimitive(depart)); |
| 71 | + } |
| 72 | + jsonObject.add("department", jsonArray); |
| 73 | + } |
| 74 | + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); |
| 75 | + return WxCpBaseResp.fromJson(responseContent); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public WxCpBaseResp createParent(@NonNull WxCpCreateParentRequest request) throws WxErrorException { |
| 80 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(CREATE_PARENT); |
| 81 | + String responseContent = this.cpService.post(apiUrl, request.toJson()); |
| 82 | + return WxCpBaseResp.fromJson(responseContent); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public WxCpBaseResp updateParent(@NonNull WxCpUpdateParentRequest request) throws WxErrorException { |
| 87 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(UPDATE_PARENT); |
| 88 | + String responseContent = this.cpService.post(apiUrl, request.toJson()); |
| 89 | + return WxCpBaseResp.fromJson(responseContent); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public WxCpBaseResp deleteParent(@NonNull String userId) throws WxErrorException { |
| 94 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(DELETE_PARENT) + userId; |
| 95 | + String responseContent = this.cpService.get(apiUrl, null); |
| 96 | + return WxCpBaseResp.fromJson(responseContent); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public WxCpBaseResp setArchSyncMode(@NonNull Integer archSyncMode) throws WxErrorException { |
| 101 | + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(SET_ARCH_SYNC_MODE); |
| 102 | + JsonObject jsonObject = new JsonObject(); |
| 103 | + jsonObject.addProperty("arch_sync_mode", archSyncMode); |
| 104 | + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); |
| 105 | + return WxCpBaseResp.fromJson(responseContent); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments