participant.repo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * 搜索功能。
  3. */
  4. "use strict";
  5. let ImDb = require('../mysql/db/im.db');
  6. let DbUtil = require('../../util/db.util');
  7. let log = require('../../util/log.js');
  8. const DB_TABLES = require('../../include/commons').DB_TABLES;
  9. const SESSION_USER_STATUS = require('../../include/commons').SESSION_USER_STATUS;
  10. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  11. const SESSION_BUSINESS_TYPE = require('../../include/commons').SESSION_BUSINESS_TYPE;
  12. class ParticipantRepo {
  13. constructor() {
  14. }
  15. /**
  16. * 获取会话的成员列表
  17. *
  18. * @param sessionId
  19. * @param handler
  20. */
  21. static findAll(sessionId, handler) {
  22. let sql = "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, false is_patient,p.last_fetch_time,u.level FROM sessions s, participants p, doctors u " +
  23. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id union " +
  24. "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, true is_patient,p.last_fetch_time,0 as level FROM sessions s, participants p, patients u " +
  25. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
  26. ImDb.execQuery({
  27. "sql": sql,
  28. "args": [sessionId, sessionId],
  29. "handler": function(err,res){
  30. if(res&&res.length>0){
  31. for(var j in res){
  32. if(res[j].last_fetch_time){
  33. res[j].last_fetch_time = res[j].last_fetch_time.getTime();
  34. }
  35. }
  36. }
  37. handler(err,res);
  38. }
  39. });
  40. }
  41. /**
  42. * 获取会话的成员ID列表
  43. *
  44. * @param sessionId
  45. * @param handler
  46. */
  47. static findIds(sessionId, handler) {
  48. let sql =
  49. "SELECT u.id, u.name, false is_patient, p.participant_role,u.avatar FROM sessions s, participants p, doctors u " +
  50. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id " +
  51. "UNION " +
  52. "SELECT u.id, u.name, true is_patient, p.participant_role,u.avatar FROM sessions s, participants p, patients u " +
  53. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id ";
  54. ImDb.execQuery({
  55. "sql": sql,
  56. "args": [sessionId, sessionId],
  57. "handler": handler
  58. });
  59. }
  60. /**
  61. * 根据会话id查找会话成员
  62. * @param sessionId
  63. * @param handler
  64. */
  65. static findParricipantBySessionId(sessionId,handler){
  66. let sql = "select participant_id,participant_role from participants p where p.session_id = ?";
  67. ImDb.execQuery({
  68. "sql": sql,
  69. "args": [sessionId],
  70. "handler": handler
  71. });
  72. }
  73. /**
  74. * 获取会话医生的id
  75. * @param sessionId
  76. * @param handler
  77. */
  78. static findDoctorIds(sessionId,handler){
  79. let sql =
  80. "SELECT u.id, u.name, false is_patient, p.participant_role,u.avatar FROM sessions s, participants p, doctors u " +
  81. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id ";
  82. ImDb.execQuery({
  83. "sql": sql,
  84. "args": [sessionId],
  85. "handler": handler
  86. });
  87. }
  88. /**
  89. * 获取会话的居民的家庭成员
  90. * @param sessionId
  91. * @param handler
  92. */
  93. static findFamilyIds(sessionId, handler){
  94. let sql =
  95. "SELECT u.id, u.name, true is_patient, p.participant_role,u.avatar,ps.name pname,ps.id pid " +
  96. "FROM sessions s, participants p, patients u ,wlyy.wlyy_patient_family_member m,patients ps " +
  97. "WHERE s.id = ? and s.id = p.session_id and p.participant_id = m.patient AND m.family_member = u.id and m.is_authorize = 1 and p.participant_id = ps.id ";
  98. //新增发送代理人
  99. ImDb.execQuery({
  100. "sql": sql,
  101. "args": [sessionId],
  102. "handler": handler
  103. });
  104. }
  105. /**
  106. * 获取会话的成员头像列表
  107. *
  108. * @param sessionId
  109. * @param handler
  110. */
  111. static findAllAvatars(sessionId, handler) {
  112. let sql = "SELECT u.id, u.avatar,'0' as ispatient,u.name as name FROM sessions s, participants p, doctors u " +
  113. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id union " +
  114. "SELECT u.id, u.avatar,'1' as ispatient,u.name as name FROM sessions s, participants p, patients u " +
  115. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
  116. ImDb.execQuery({
  117. "sql": sql,
  118. "args": [sessionId, sessionId],
  119. "handler": handler
  120. });
  121. }
  122. /**
  123. * 获取会话的成员列表
  124. *
  125. * @param sessionId
  126. * @param participantId
  127. * @param role
  128. * @param handler
  129. */
  130. static updateParticipant(sessionId, participantId, role, handler) {
  131. let sql = "update participants set participant_role = ? WHERE session_id = ? AND participant_id = ?";
  132. ImDb.execQuery({
  133. "sql": sql,
  134. "args": [role, sessionId, participantId],
  135. "handler": handler
  136. });
  137. }
  138. /**
  139. * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。
  140. *
  141. * @param userId
  142. * @param anotherUserId
  143. * @param handler
  144. */
  145. static findSessionIdByParticipantIds(userId, anotherUserId, handler) {
  146. let sessionId = DbUtil.stringArrayHash([userId, anotherUserId]);
  147. handler(null, sessionId);
  148. }
  149. static getBusinessType(users, handler) {
  150. let sql = "SELECT count(1) as count FROM patients p WHERE p.id in (?)";
  151. ImDb.execQuery({
  152. "sql": sql,
  153. "args": [users],
  154. "handler": function (err, res) {
  155. if (err) {
  156. console.log("err businessType : " + err);
  157. } else {
  158. if (res[0].count > 0) {
  159. handler(err, SESSION_BUSINESS_TYPE.PATIENT);
  160. } else {
  161. handler(err, SESSION_BUSINESS_TYPE.DOCTOR);
  162. }
  163. }
  164. }
  165. });
  166. }
  167. static findNameById(userId, handler) {
  168. let sql = "SELECT p.name,p.sex,p.birthdate FROM patients p WHERE p.id =? union SELECT d.name,d.sex,d.birthdate FROM doctors d WHERE d.id =?";
  169. ImDb.execQuery({
  170. "sql": sql,
  171. "args": [userId, userId],
  172. "handler": function (err, res) {
  173. if (err) {
  174. console.log("err businessType : " + err);
  175. } else {
  176. handler(null, res);
  177. }
  178. }
  179. });
  180. }
  181. static findMucSessionIdByUser(users, handler) {
  182. //先匹配出在线用户
  183. let userTemp = [];
  184. users.forEach(function (user) {
  185. if (user == SESSION_USER_STATUS.ONLINE) {
  186. userTemp.push(user);
  187. }
  188. });
  189. let sql = "SELECT DISTINCT s.* FROM " + DB_TABLES.Participants + " p1," + DB_TABLES.Participants + " p2," +
  190. DB_TABLES.Sessions + " s WHERE p1.session_id = s.id AND p2.session_id = s.id AND s.type =? " +
  191. "AND ((p1.participant_id =? AND p2.participant_id = ?) or (p1.participant_id =? AND p2.participant_id = ?))";
  192. ImDb.execQuery({
  193. "sql": sql,
  194. "args": [SESSION_TYPES.MUC, userTemp[0], userTemp[1], userTemp[1], userTemp[0]],
  195. "handler": handler
  196. });
  197. }
  198. /**
  199. * 更新最后消息获取时间。
  200. *
  201. * @param lastMessageTime
  202. * @param sessionId
  203. * @param participantId
  204. * @param handler
  205. */
  206. static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) {
  207. let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? WHERE session_id = ? AND participant_id =?";
  208. ImDb.execQuery({
  209. "sql": sql,
  210. "args": [lastMessageTime, sessionId, participantId],
  211. "handler": handler
  212. });
  213. }
  214. /**
  215. * 用户是否在指定Session中
  216. *
  217. * @param sessionId
  218. * @param userId
  219. * @param handler
  220. */
  221. static existsParticipant(sessionId, userId, handler) {
  222. let sql = "SELECT case when count(*) > 0 then true else false end exist FROM participants w WHERE w.session_id =? AND w.participant_id = ? ";
  223. log.info("用户是否在指定Session中:sql:" + sql);
  224. log.info("用户是否在指定Session中:args:" + [sessionId, userId]);
  225. ImDb.execQuery({
  226. "sql": sql,
  227. "args": [sessionId, userId],
  228. "handler": handler
  229. });
  230. }
  231. /**
  232. * mysql成员创建
  233. *
  234. * @param sessionId
  235. * @param users JSON
  236. * @param handler
  237. */
  238. static saveParticipantsToMysql(sessionId, users, handler) {
  239. let sql = "INSERT INTO " + DB_TABLES.Participants + " (session_id,participant_id,participant_role,last_fetch_time) VALUES ";
  240. let args = [];
  241. let nowDate = new Date();
  242. log.info("saveParticipantsToMysql:[sql] = " + sql);
  243. log.info("saveParticipantsToMysql:[users.length] = " + users.length);
  244. for (let j in users) {
  245. let tokens = users[j].split(":");
  246. sql += "(?,?,?,?)";
  247. args.push(sessionId);
  248. args.push(tokens[0]);
  249. args.push(tokens.length > 1 ? tokens[1] : '0');
  250. args.push(nowDate);
  251. if (j != users.length - 1) sql += ", ";
  252. log.info("saveParticipantsToMysql:[args] = " + args);
  253. }
  254. sql += " ON DUPLICATE KEY UPDATE participant_role = VALUES(participant_role)";
  255. ImDb.execQuery({
  256. "sql": sql,
  257. "args": args,
  258. "handler": handler
  259. });
  260. }
  261. static deleteUserFromMysql(sessionId, userId, handler) {
  262. let sql = "delete from " + DB_TABLES.Participants + " where participant_id=? and session_id=? ";
  263. ImDb.execQuery({
  264. "sql": sql,
  265. "args": [userId, sessionId],
  266. "handler": handler || function (err, res) {
  267. log.info("deleteUserFromMysql");
  268. }
  269. });
  270. }
  271. static deleteAllUser(sessionId, handler) {
  272. let sql = "delete from " + DB_TABLES.Participants + " where session_id=? ";
  273. ImDb.execQuery({
  274. "sql": sql,
  275. "args": [sessionId],
  276. "handler": handler || function (err, res) {
  277. log.info("deleteUserFromMysql");
  278. }
  279. });
  280. }
  281. static findLastFetchTime(sessionId,userId,handler){
  282. let sql = "select last_fetch_time from participants p where p.session_id = ? and p.participant_id=?";
  283. ImDb.execQuery({
  284. "sql": sql,
  285. "args": [sessionId,userId],
  286. "handler": handler
  287. });
  288. }
  289. }
  290. module.exports = ParticipantRepo;