package chat.user;
|
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
import chat.module.ConcurrentMapList;
|
import chat.module.ModuleLoader;
|
import chat.module.UserMessageList;
|
import chat.module.entity.FriendRequest;
|
import chat.module.entity.FriendShip;
|
import chat.module.entity.MessageContainer;
|
import chat.module.entity.MessageRecord;
|
import chat.module.entity.MessageRelation;
|
import chat.module.entity.MessageType;
|
import chat.module.entity.PrivateFriend;
|
import chat.module.entity.PublishOperator;
|
import chat.module.friendcircle.FriendCircle;
|
import chat.server.call.IJSONWriter;
|
import chat.server.call.IJsonProvider;
|
import chat.server.im.DataPool;
|
import chat.server.im.IMTopic;
|
import chat.util.MessageShardingUtil;
|
import cn.wildfirechat.proto.ProtoConstants;
|
import cn.wildfirechat.proto.WFCMessage;
|
import cn.wildfirechat.proto.WFCMessage.HandleFriendRequest;
|
import cn.wildfirechat.proto.WFCMessage.Message;
|
import cn.wildfirechat.proto.WFCMessage.Message.Builder;
|
import frame.object.data.Entity;
|
import frame.object.data.ID;
|
import frame.persist.NamedSQL;
|
import frame.persist.SQLRunner;
|
|
public class User implements IJsonProvider {
|
|
private String id;
|
private String mobile;
|
private String name;
|
private String displayName;
|
private String portrait;
|
private String password;
|
private int gender;
|
private boolean stranger;
|
private String roletype;
|
private long timestamp;
|
private String bizid;
|
private String extra;
|
private String rejectreason;
|
|
private WFCMessage.UserResult userResult;
|
private Set<Client> clientSet;
|
|
protected ConcurrentMapList<List<FriendShip>> friendShipList;
|
protected ConcurrentMapList<List<FriendShip>> pushFriendList;
|
//protected ConcurrentMapList<FriendShip> pushFriendList;
|
private UserMessageList messageList;
|
private FriendCircle friendCircle;
|
|
public User() {}
|
|
public User(String id) {
|
clientSet = new HashSet<Client>();
|
friendShipList = new ConcurrentMapList<List<FriendShip>>();
|
pushFriendList = new ConcurrentMapList<List<FriendShip>>();
|
messageList = new UserMessageList();
|
friendCircle = new FriendCircle();
|
}
|
|
public static User getInstance(String userId) {
|
User result = UserStore.getById(userId);
|
return result;
|
}
|
|
public static User setUserInfo(NotifyContentDetail contentDetail) {
|
String rejectReasonMerge = "";
|
User user = new User();
|
user.setId(ID.newValue());
|
user.setBizid(contentDetail.getDoctorId());
|
user.setName(contentDetail.getName());
|
user.setDisplayName(contentDetail.getName());
|
user.setPortrait(contentDetail.getPortrait());
|
user.setMobile(contentDetail.getMobile());
|
user.setRoletype(contentDetail.getRoletype());
|
user.setExtra(contentDetail.getDoctorId());
|
if (contentDetail.getRejectreason() == null) {
|
rejectReasonMerge = "";
|
} else {
|
for (String rejectReason : contentDetail.getRejectreason()) {
|
rejectReasonMerge = rejectReasonMerge + rejectReason + "#separate#";
|
}
|
}
|
|
user.setRejectreason(suitableStr(rejectReasonMerge, "#separate#"));
|
return user;
|
}
|
|
public static String suitableStr(String body, String sep) {
|
String result = "";
|
if (body == null || body.equals("")) {
|
return "";
|
}
|
int index = body.lastIndexOf(sep);
|
result = body.substring(0, index);
|
return result;
|
}
|
|
public static User getInstance(Client client) throws Exception {
|
String userId = client.getUserId();
|
|
if (userId == null) {
|
return null;
|
}
|
|
NamedSQL namedSQL = NamedSQL.getInstance("getOneUserByClientId");
|
namedSQL.setParam("clientId", client.getId());
|
|
Entity entity = namedSQL.getEntity();
|
|
User user = new User(userId);
|
user.load(entity);
|
|
return user;
|
}
|
|
public void createInitFrieldShip(User user) {
|
// TODO Auto-generated method stub
|
try {
|
FriendShip friendShip = new FriendShip();
|
User friend = UserStore.getById("user004");
|
|
friendShip = new FriendShip(user, "user004", "open");
|
friendShip.addFriendShipToDataBase();
|
user.addOneFrieldShip(user.getId(), friendShip);
|
|
friendShip = friendShip.getReverseInstance();
|
friend.addOneFrieldShip(friendShip.getUserid(), friendShip);
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void modifyFrieldShipStatus(FriendShip friendShip, String status) {
|
try {
|
friendShip.updateFriendShipToDataBase("open");
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public Client getClient(String userId) {
|
Client clientReturn = null;
|
if (userId == null || ("").equals(userId)) {
|
return clientReturn;
|
}
|
for (Client client:clientSet) {
|
if (client.getUser().getId().equals(userId)) {
|
clientReturn = client;
|
break;
|
}
|
}
|
return clientReturn;
|
}
|
|
public void load(DataPool dataPool) {
|
try {
|
mobile = dataPool.getString("mobile");
|
if (mobile == null) {
|
mobile = dataPool.getPhone();
|
}
|
} catch (Exception e) {
|
mobile = dataPool.getPhone();
|
} finally {
|
if (dataPool.getString("roletype") != null) {
|
if (dataPool.getString("roletype").equals("doctor")) {
|
roletype = "1";
|
}
|
else if (dataPool.getString("roletype").equals("")) {
|
roletype = "2";
|
}
|
}
|
}
|
}
|
|
public void reLoad(User user) {
|
|
name = user.getName();
|
portrait = user.getPortrait();
|
roletype = user.getRoletype();
|
mobile = user.getMobile();
|
displayName = user.getDisplayName();
|
gender = user.getGender();
|
rejectreason = user.getRejectreason();
|
|
}
|
|
public void load(Entity entity) {
|
if (entity == null) {
|
return;
|
}
|
|
id = entity.getString("id");
|
mobile = entity.getString("mobile");
|
name = entity.getString("name");
|
displayName = entity.getString("displayName");
|
portrait = entity.getString("portrait");
|
password = entity.getString("password");
|
stranger = entity.getBoolean("stranger", false);
|
roletype = entity.getString("roletype");
|
extra = entity.getString("extra");
|
bizid = entity.getString("bizid");
|
rejectreason = entity.getString("rejectreason");
|
|
userResult = null;
|
}
|
|
//将没有接收到的消息放入消息队列中
|
public void addOneUnreceivedMessage(MessageRelation messageRelation) {
|
// TODO Auto-generated method stub
|
if (messageRelation == null) {
|
return;
|
}
|
|
if (MessageType.unreceived == MessageType.parse(messageRelation.getStatuscode())) {
|
messageRelation.getMessageRecord().setMessage(createOneMessage(messageRelation.getMessageRecord(), messageRelation));
|
messageList.getMessageList().add(messageRelation);
|
|
}
|
else if (MessageType.received == MessageType.parse(messageRelation.getStatuscode())){
|
messageList.setPos(messageList.getPos() + 1);
|
}
|
}
|
|
public WFCMessage.Message createOneMessage(MessageRecord message, MessageRelation messageRelation) {
|
WFCMessage.Message messageWFC = null;
|
|
try {
|
WFCMessage.Message.Builder builder = WFCMessage.Message.newBuilder();
|
builder.setMessageId(message.getMessageid());
|
builder.setServerTimestamp(message.getTimestamp());
|
builder.setFromUser(message.getSenderId());
|
|
WFCMessage.Conversation.Builder cb = WFCMessage.Conversation.newBuilder();
|
cb.setType(messageRelation.getType());
|
cb.setTarget(messageRelation.getTargetid());
|
cb.setLine(messageRelation.getLine());
|
builder.setConversation(cb.build());
|
|
WFCMessage.MessageContent.Builder contentBuilder = WFCMessage.MessageContent.newBuilder();
|
contentBuilder.setType(message.getType());
|
contentBuilder.setSearchableContent(message.getContent());
|
contentBuilder.setMediaType(0);
|
contentBuilder.setPersistFlag(message.getPersistflag());
|
contentBuilder.setExpireDuration(0);
|
contentBuilder.setMentionedType(0);
|
|
WFCMessage.MessageContent messageContent = contentBuilder.build();
|
|
builder.setContent(messageContent);
|
|
messageWFC = builder.build();
|
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
return messageWFC;
|
}
|
|
public void addOneFrieldShip(String friendId, FriendShip friendShip) {
|
List<FriendShip> friendShipS = friendShipList.get(friendId);
|
if (friendShipS == null) {
|
friendShipS = new ArrayList<FriendShip>();
|
}
|
|
friendShipS.add(friendShip);
|
friendShipList.addMapList(friendId, friendShipS);
|
}
|
|
public void addOnePushFriendShip(String friendId, FriendShip friendShip) {
|
List<FriendShip> friendShipS = pushFriendList.get(friendId);
|
if (friendShipS == null) {
|
friendShipS = new ArrayList<FriendShip>();
|
}
|
|
friendShipS.add(friendShip);
|
pushFriendList.addMapList(friendId, friendShipS);
|
}
|
|
//增加朋友请求消息
|
public void addUserFriendShip(String userid, WFCMessage.AddFriendRequest request) {
|
long[] head = new long[1];
|
|
WFCMessage.FriendRequest.Builder friendRequestBuilder = WFCMessage.FriendRequest.newBuilder();
|
friendRequestBuilder.setFromUid(userid);
|
friendRequestBuilder.setToUid(request.getTargetUid());
|
friendRequestBuilder.setReason(request.getReason());
|
friendRequestBuilder.setStatus(ProtoConstants.FriendRequestStatus.RequestStatus_Sent);
|
friendRequestBuilder.setToReadStatus(false);
|
friendRequestBuilder.setUpdateDt(System.currentTimeMillis());
|
WFCMessage.FriendRequest friendRequest = friendRequestBuilder.build();
|
|
FriendShip friendShip = new FriendShip(UserStore.getById(userid), request.getTargetUid(), "apply");
|
friendShip.setFriendRequest(friendRequest);
|
|
friendShip.addFriendShipToDataBase();
|
|
UserStore.getById(userid).addOnePushFriendShip(userid, friendShip);
|
UserStore.getById(request.getTargetUid()).addOnePushFriendShip(request.getTargetUid(), friendShip);
|
|
head[0] = System.currentTimeMillis();
|
//发通知
|
FriendRequest friendData = new FriendRequest(userid, request.getTargetUid());
|
ModuleLoader.getImBusinessScheduler().execute(() -> friendData.notifyOneFriendRequest(IMTopic.NotifyFriendRequestTopic, request, head[0], userid));
|
}
|
|
//添加朋友的对方,接受请求
|
public void handleFriendRequest(String userId, WFCMessage.HandleFriendRequest request, WFCMessage.Message.Builder msgBuilder, long[] heads) {
|
// TODO Auto-generated method stub
|
if (request.getStatus() != ProtoConstants.FriendRequestStatus.RequestStatus_Accepted) {
|
return;
|
}
|
|
List<FriendShip> friendShipList = getApplyFriendShip(userId);
|
FriendShip loadFriendShip = null;
|
MessageContainer messageContainer;
|
|
WFCMessage.FriendRequest existRequest = null;
|
|
try {
|
for (FriendShip friendShip : friendShipList) {
|
if (friendShip.getUserid().equals(request.getTargetUid()) && friendShip.getFriendid().equals(userId)) {
|
existRequest = friendShip.getFriendRequest();
|
loadFriendShip = friendShip;
|
break;
|
}
|
}
|
|
if (existRequest != null) {
|
existRequest = existRequest.toBuilder().setStatus(request.getStatus()).setUpdateDt(System.currentTimeMillis()).build();
|
|
//1.创建chatspace 单聊
|
PrivateFriend privateGroup = new PrivateFriend(UserStore.getById(request.getTargetUid()).getName() + "--" + UserStore.getById(userId).getName(), UserStore.getById(request.getTargetUid()));
|
privateGroup.createPrivateFriend();
|
ModuleLoader.getPrivateFriendBucket().addOne(privateGroup.getId(), privateGroup);
|
|
//2.创建member
|
List<String> userList = new ArrayList<String>();
|
userList.add(request.getTargetUid());
|
userList.add(userId);
|
messageContainer = ModuleLoader.getPrivateFriendBucket().getOne(privateGroup.getId());
|
messageContainer.addInitMembers(privateGroup.getId(), userList);
|
|
//3.修改frienship中的状态,将申请(apply)状态修改成通过(open)状态
|
modifyFrieldShipStatus(loadFriendShip, "open");
|
|
//4.将申请状态中的记录删除
|
delApplyFriendShip(request.getTargetUid());
|
getApplyFriendShip(userId);
|
|
//5.将双方加入friendship关系中
|
addOneFrieldShip(request.getTargetUid(), loadFriendShip);
|
heads[0] = System.currentTimeMillis();
|
loadFriendShip = loadFriendShip.getReverseInstance();
|
addOneFrieldShip(userId, loadFriendShip);
|
heads[1] = System.currentTimeMillis();
|
|
msgBuilder.setConversation(WFCMessage.Conversation.newBuilder().setTarget(userId).setLine(0).setType(ProtoConstants.ConversationType.ConversationType_Private).build());
|
msgBuilder.setContent(WFCMessage.MessageContent.newBuilder().setType(1).setSearchableContent(existRequest.getReason()).build());
|
}
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void pushTo(Entity entity) throws Exception {
|
String id = ID.newValue();
|
entity.set("id", id);
|
setId(id);
|
entity.set("mobile", mobile);
|
entity.set("name", name);
|
entity.set("displayName", displayName);
|
entity.set("portrait", portrait);
|
entity.set("password", password);
|
entity.set("roletype", roletype);
|
entity.set("extra", extra);
|
entity.set("bizid", bizid);
|
entity.set("rejectreason", rejectreason);
|
}
|
|
public void pushToModify(Entity entity) throws Exception {
|
entity.set("id", id);
|
entity.set("mobile", mobile);
|
entity.set("name", name);
|
entity.set("displayName", displayName);
|
entity.set("portrait", portrait);
|
entity.set("password", password);
|
entity.set("roletype", roletype);
|
entity.set("extra", extra);
|
entity.set("bizid", bizid);
|
entity.set("rejectreason", rejectreason);
|
}
|
|
public void updateDoctorId() throws Exception {
|
|
}
|
|
public WFCMessage.UserResult toWFCUserResult() {
|
if (userResult != null) {
|
return userResult;
|
}
|
|
WFCMessage.User wfcUser = toWFCUser();
|
|
WFCMessage.UserResult.Builder resultBuilder = WFCMessage.UserResult.newBuilder();
|
resultBuilder.setUser(wfcUser);
|
resultBuilder.setCode(ProtoConstants.UserResultCode.Success);
|
|
userResult = resultBuilder.build();
|
|
return userResult;
|
}
|
|
public WFCMessage.User toWFCUser() {
|
WFCMessage.User.Builder userBuilder = WFCMessage.User.newBuilder();
|
userBuilder.setUid(id);
|
userBuilder.setName(name);
|
userBuilder.setDisplayName(displayName);
|
|
userBuilder.setPortrait(portrait);
|
userBuilder.setMobile(mobile);
|
|
if (extra != null && !extra.equals("")) {
|
userBuilder.setExtra(extra);
|
} else {
|
userBuilder.setExtra("");
|
}
|
|
return userBuilder.build();
|
}
|
|
/**
|
* 将消息关系挂到接收客户下面
|
* @param relation
|
* private UserMessageList messageList;
|
*/
|
public void pushOneMessageRelation(MessageRelation relation) {
|
// TODO Auto-generated method stub
|
this.messageList.getMessageList().add(relation);
|
}
|
|
public List<Message> getUnreceivedMessageList() throws Exception {
|
List<Message> result = new ArrayList<WFCMessage.Message>();
|
|
int pos = messageList.getPos() ;
|
int size = messageList.getMessageList().size();
|
|
//add one message
|
for (int i = pos; i < size; i++) {
|
//1.
|
MessageRelation relation = messageList.getMessageList().get(i);
|
result.add(relation.getMessageRecord().getMessage());
|
|
//2. update state
|
relation.updateStatusToDataBase(relation, "received");
|
|
//3.修改已读下标指针
|
messageList.setPos(pos+1);
|
}
|
|
return result;
|
}
|
|
public void addClient(Client client) {
|
for (Client clientP: clientSet) {
|
if (clientP.getId().equals(client.getId())) {
|
clientSet.remove(clientP);
|
break;
|
}
|
}
|
clientSet.add(client);
|
}
|
|
public void setDefaultValues() throws Exception {
|
//1. name
|
NamedSQL namedSQL = NamedSQL.getInstance("nextval");
|
namedSQL.setParam("name", "usr");
|
int value = SQLRunner.getInteger(namedSQL);
|
|
name = "User" + value;
|
displayName = name;
|
|
//2.
|
portrait = "http://cdn2.wildfirechat.cn/robot.png";
|
|
//3.
|
stranger = true;
|
timestamp = System.currentTimeMillis();
|
// if (!roletype.equals("")) {
|
// this.extra = getRoleUserId(mobile, roletype);
|
// }
|
}
|
|
public Map<String, String> getRoleUserId(String phone, String roleType) throws Exception {
|
String strSQL = "";
|
Map<String, String> returnMap = new HashMap<String, String>();
|
if (roletype.equals("1")) {
|
strSQL = " select id, doctor_name as name from medo_doctor where doctor_mobile = '"+phone+"' ";
|
} else if (roletype.equals("2")) {
|
strSQL = " select id, client_name from as name from medeasy_client.mecl_client where mobile = '"+phone+"' ";
|
}
|
|
PreparedStatement st = ModuleLoader.connDB.prepareStatement(strSQL);
|
ResultSet rs = st.executeQuery();
|
while(rs.next()){
|
returnMap.put("id", rs.getString("id"));
|
returnMap.put("name", rs.getString("name"));
|
}
|
return returnMap;
|
}
|
|
/**
|
*
|
* @param 查询关键字
|
* @param fuzzy 0 模糊查询
|
* @param page 页数
|
* @author hefeixia 20210216
|
* @return
|
*/
|
public List<cn.wildfirechat.proto.WFCMessage.User> searchUser(String keyword, int searchType, int page) {
|
// TODO Auto-generated method stub
|
ArrayList<WFCMessage.User> outUser = new ArrayList<WFCMessage.User>();
|
|
Map<String, User> userAll = UserStore.getIdUserMap();
|
|
for (User user : userAll.values()) {
|
if (user.name.contains(keyword) || user.mobile.contains(keyword) || user.displayName.contains(keyword)) {
|
WFCMessage.User.Builder builder = WFCMessage.User.newBuilder();
|
builder.setUid(user.getId());
|
builder.setName(user.getName());
|
builder.setPortrait(user.getPortrait());
|
builder.setMobile(user.getMobile());
|
builder.setDisplayName(user.getDisplayName());
|
builder.setExtra(user.getExtra());
|
|
WFCMessage.User usr = builder.build();
|
outUser.add(usr);
|
}
|
}
|
|
return outUser;
|
}
|
|
@Override
|
public void writeJSONObject(IJSONWriter writer) {
|
writer.beginObject();
|
writeJSONData(writer);
|
writer.endObject();
|
}
|
|
@Override
|
public void writeJSONData(IJSONWriter writer) {
|
writer.write("userId", id);
|
writer.write("name", name);
|
writer.write("displayName", displayName);
|
writer.write("portrait", portrait);
|
writer.write("gender", gender);
|
writer.write("mobile", mobile);
|
writer.write("roletype", roletype);
|
writer.write("extra", extra);
|
writer.write("email", "");
|
writer.write("roletype", 1);
|
writer.write("bizid", bizid);
|
writer.write("rejectreason", rejectreason);
|
}
|
|
@Override
|
public int hashCode() {
|
if (id == null) {
|
return 0;
|
}
|
|
return id.hashCode();
|
}
|
|
public String getDisplayName() {
|
return displayName;
|
}
|
|
public void setDisplayName(String displayName) {
|
this.displayName = displayName;
|
}
|
|
public int getGender() {
|
return gender;
|
}
|
|
public void setGender(int gender) {
|
this.gender = gender;
|
}
|
|
public void setId(String id) {
|
this.id = id;
|
}
|
|
public void setMobile(String mobile) {
|
this.mobile = mobile;
|
}
|
|
public void setName(String name) {
|
this.name = name;
|
}
|
|
public void setPortrait(String portrait) {
|
this.portrait = portrait;
|
}
|
|
public void setPassword(String password) {
|
this.password = password;
|
}
|
|
public List<FriendShip> getFriendship(String userId) {
|
List<FriendShip> result = friendShipList.get(userId);
|
return result;
|
}
|
|
public List<FriendShip> getApplyFriendShip(String userId) {
|
List<FriendShip> result = pushFriendList.get(userId);
|
return result;
|
}
|
|
public void delApplyFriendShip(String userId) {
|
pushFriendList.delete(userId);
|
}
|
|
public String getExtra() {
|
return extra;
|
}
|
|
public void setExtra(String extra) {
|
this.extra = extra;
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public String getPassword() {
|
return password;
|
}
|
|
public String getPortrait() {
|
return portrait;
|
}
|
|
public String getMobile() {
|
return mobile;
|
}
|
|
public Set<Client> getClientSet() {
|
return clientSet;
|
}
|
|
public void notify(PublishOperator operator) {
|
|
}
|
|
public boolean isStranger() {
|
return stranger;
|
}
|
|
public void setStranger(boolean stranger) {
|
this.stranger = stranger;
|
}
|
|
public long getUpdateDt() {
|
return timestamp;
|
}
|
|
public String getRejectreason() {
|
return rejectreason;
|
}
|
|
public void setRejectreason(String rejectreason) {
|
this.rejectreason = rejectreason;
|
}
|
|
public String getBizid() {
|
return bizid;
|
}
|
|
public void setBizid(String bizid) {
|
this.bizid = bizid;
|
}
|
|
public String getRoletype() {
|
return roletype;
|
}
|
|
public void setRoletype(String roletype) {
|
this.roletype = roletype;
|
}
|
|
public FriendCircle getFriendCircle() {
|
return friendCircle;
|
}
|
|
}
|