|
@@ -0,0 +1,110 @@
|
|
|
+package cn.org.bjca.trust.java.imserver.request;
|
|
|
+
|
|
|
+import cn.org.bjca.trust.java.imserver.HttpResult;
|
|
|
+import cn.org.bjca.trust.java.imserver.bean.group.GroupCreateData;
|
|
|
+import cn.org.bjca.trust.java.imserver.common.SpringUtilsAuTo;
|
|
|
+import cn.org.bjca.trust.java.imserver.common.TimeHelper;
|
|
|
+import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
|
|
|
+import cn.org.bjca.trust.java.imserver.entitys.GroupEntity;
|
|
|
+import cn.org.bjca.trust.java.imserver.im.msg.RequestMessage;
|
|
|
+import cn.org.bjca.trust.java.imserver.im.msg.ResultMessage;
|
|
|
+import cn.org.bjca.trust.java.imserver.repository.GroupRepository;
|
|
|
+import cn.org.bjca.trust.java.imserver.repository.MessageRepository;
|
|
|
+import cn.org.bjca.trust.java.imserver.repository.UserRepository;
|
|
|
+import cn.org.bjca.trust.java.imserver.repository.tenant.ApplicationRepository;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import static cn.org.bjca.trust.java.imserver.request.RequestConstant.Group_create_v1;
|
|
|
+
|
|
|
+public class RequestManager {
|
|
|
+ private static final class RequestManagerHolder {
|
|
|
+ static final RequestManager instance = new RequestManager();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static RequestManager getInstance() {
|
|
|
+ return RequestManagerHolder.instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultMessage request(String msg) {
|
|
|
+ try {
|
|
|
+ RequestMessage requestMessage = GsonImplHelp.get().toObject(msg, RequestMessage.class);
|
|
|
+ ResultMessage resultMessage = new ResultMessage();
|
|
|
+ resultMessage.setPath(requestMessage.getPath());
|
|
|
+ resultMessage.setAckId(requestMessage.getMsgId());
|
|
|
+ resultMessage.setAppId(requestMessage.getAppId());
|
|
|
+ resultMessage.setUserName(requestMessage.getUserName());
|
|
|
+ String body = GsonImplHelp.get().toJson(this.request(requestMessage.getAppId(),
|
|
|
+ requestMessage.getUserName(),
|
|
|
+ requestMessage.getOsType(),
|
|
|
+ requestMessage.getPath(),
|
|
|
+ requestMessage.getBody()));
|
|
|
+ resultMessage.setBody(body);
|
|
|
+ return resultMessage;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResultMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> HttpResult<T> request(String path, Object data, HttpHeaders headers) {
|
|
|
+
|
|
|
+ return this.request(headers.getFirst("appid"), headers.getFirst("userid"), headers.getFirst("ostype"), path, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private <T> HttpResult<T> request(String appId, String userId, String osType, String path, Object data) {
|
|
|
+
|
|
|
+ if (null != appId && null != userId && null != osType) {
|
|
|
+ if (!applicationRepository.existsByAppId(appId)) return new HttpResult<>(201, "appId不存在", null);
|
|
|
+ if (!userRepository.existsByAppIdAndUserId(appId, userId))
|
|
|
+ return new HttpResult<>(201, "当前用户不存在", null);
|
|
|
+ switch (path) {
|
|
|
+ case Group_create_v1:
|
|
|
+ return (HttpResult<T>) this.groupCreate(data instanceof String ?
|
|
|
+ GsonImplHelp.get().toObject((String) data, GroupCreateData.class)
|
|
|
+ : (GroupCreateData) data, appId, userId);
|
|
|
+ default:
|
|
|
+ return new HttpResult<>(201, "路径错误", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else return new HttpResult<>(201, "参数错误", null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private final UserRepository userRepository = SpringUtilsAuTo.getBean(UserRepository.class);
|
|
|
+ private final GroupRepository groupRepository = SpringUtilsAuTo.getBean(GroupRepository.class);
|
|
|
+ private final ApplicationRepository applicationRepository = SpringUtilsAuTo.getBean(ApplicationRepository.class);
|
|
|
+ private final MessageRepository messageRepository = SpringUtilsAuTo.getBean(MessageRepository.class);
|
|
|
+
|
|
|
+
|
|
|
+ public HttpResult<String> groupCreate(GroupCreateData data, String appid, String userid) {
|
|
|
+ if (null == data.getUserIdList() || data.getUserIdList().isEmpty() || data.getUserIdList().size() < 2) {
|
|
|
+ return new HttpResult<>(201, "群组成员需要大于一", "");
|
|
|
+ } else {
|
|
|
+ String groupId = UUID.randomUUID().toString();
|
|
|
+ String groupName = data.getGroupName();
|
|
|
+ if (null == groupName || groupName.equals("")) groupName = TimeHelper.getTimeString("yyyyMMddHHmm");
|
|
|
+ if (!data.getUserIdList().contains(userid)) {
|
|
|
+ addGroup(appid, groupId, groupName, data.getFaceUrl(), userid, userid);
|
|
|
+ }
|
|
|
+ for (String userId : data.getUserIdList()) {
|
|
|
+ if (userRepository.existsByAppIdAndUserId(appid, userId)) {
|
|
|
+ addGroup(appid, groupId, groupName, data.getFaceUrl(), userId, userid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new HttpResult<>(200, "成功", groupId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addGroup(String appId, String groupId, String groupName, String faceUrl, String userId, String adminId) {
|
|
|
+ GroupEntity groupEntity = new GroupEntity(appId, adminId, userId, groupId, groupName, faceUrl);
|
|
|
+ groupRepository.save(groupEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getHistoryForMsg(String appId, String userId, int pageSize, long timestamp) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|