participant.repo.js 12 KB

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