topics.repo.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. * 搜索功能。
  3. */
  4. "use strict";
  5. let ImDb = require('../mysql/db/im.db');
  6. let log = require('../../util/log.js');
  7. const DB_TABLES = require('../../include/commons').DB_TABLES;
  8. class TopicRepo {
  9. constructor() {
  10. }
  11. /**
  12. * 查找议题.
  13. *
  14. * @param topicId
  15. * @param handler
  16. */
  17. static findOne(topicId, handler) {
  18. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  19. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where id = ?";
  20. ImDb.execQuery({
  21. sql: sql,
  22. args: [topicId],
  23. handler: handler || function (err, res) {
  24. if (err) log.error(err);
  25. }
  26. });
  27. }
  28. static findLastTopicStatus(sessionId, handler) {
  29. let sql = "select id from " + DB_TABLES.Topics + " where session_id = ? order by create_time desc limit 0, 1";
  30. ImDb.execQuery({
  31. sql: sql,
  32. args: [sessionId],
  33. handler: function (err, res) {
  34. if (res.length == 0) {
  35. handler(null, null);
  36. } else {
  37. TopicRepo.findTopicStatus(res[0].id, handler);
  38. }
  39. }
  40. });
  41. }
  42. static findLastBySessionId(sessionId, handler) {
  43. let sql = "select * from " + DB_TABLES.Topics + " where session_id = ? order by create_time desc limit 0, 1";
  44. ImDb.execQuery({
  45. sql: sql,
  46. args: [sessionId],
  47. handler: function (err, res) {
  48. handler(err, res);
  49. }
  50. });
  51. }
  52. static findAllByUserAndReplyAndStatus(userId,reply,status,page,size,handler){
  53. let sql = "";
  54. var args =[];
  55. if(status==10){
  56. args.push(userId,status,page,size);
  57. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  58. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  59. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  60. "AND d.id in (?) AND t. STATUS = ? ORDER BY create_time DESC limit ?,?";
  61. }else{
  62. args.push(userId,status,reply,page,size);
  63. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  64. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  65. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  66. "AND d.id in (?) AND t. STATUS = ? and t.reply=? ORDER BY create_time DESC limit ?,?";
  67. }
  68. ImDb.execQuery({
  69. sql: sql,
  70. args: args,
  71. handler: function (err, res) {
  72. handler(err, res);
  73. }
  74. });
  75. }
  76. /**
  77. * 过滤名医咨询和续方咨询
  78. * @param userId
  79. * @param reply
  80. * @param status
  81. * @param page
  82. * @param size
  83. * @param handler
  84. */
  85. static findAllByUserAndReplyAndStatusHealthTopic(userId,reply,status,page,size,handler){
  86. let sql = "";
  87. var args =[];
  88. if(status==10){
  89. args.push(userId,status,page,size);
  90. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  91. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  92. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  93. "AND d.id in (?) AND t. STATUS = ? AND c.type not in ('6','8') ORDER BY create_time DESC limit ?,?";
  94. }else{
  95. args.push(userId,status,reply,page,size);
  96. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  97. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  98. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  99. "AND d.id in (?) AND t. STATUS = ? and t.reply=? AND c.type not in ('6','8') ORDER BY create_time DESC limit ?,?";
  100. }
  101. ImDb.execQuery({
  102. sql: sql,
  103. args: args,
  104. handler: function (err, res) {
  105. handler(err, res);
  106. }
  107. });
  108. }
  109. /**
  110. * 按类型查找医生的未回复,进行中,已完成的咨询
  111. * @param userId
  112. * @param reply
  113. * @param status
  114. * @param type
  115. * @param page
  116. * @param size
  117. * @param handler
  118. */
  119. static findAllTopicByType(userId,reply,status,type,patientName,startTime,endTime,page,size,handler){
  120. let sql = "";
  121. var args =[];
  122. var tempParms = "";
  123. if(patientName){
  124. tempParms += " and s.name like '%"+patientName+"%' ";
  125. }
  126. if(startTime){
  127. tempParms += " and t.create_time >= '"+startTime+"' ";
  128. }
  129. if(endTime){
  130. tempParms += " and t.create_time <= '"+endTime+"' ";
  131. }
  132. if(status==10){
  133. args.push(userId,status,type,page,size);
  134. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  135. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  136. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  137. "AND d.id in (?) AND t. STATUS = ? AND c.type =? "+tempParms+" ORDER BY create_time DESC limit ?,?";
  138. }else{
  139. args.push(userId,status,reply,type,page,size);
  140. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  141. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  142. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  143. "AND d.id in (?) AND t. STATUS = ? and t.reply=? AND c.type = ? "+tempParms+" ORDER BY create_time DESC limit ?,?";
  144. }
  145. ImDb.execQuery({
  146. sql: sql,
  147. args: args,
  148. handler: function (err, res) {
  149. handler(err, res);
  150. }
  151. });
  152. }
  153. /**
  154. * 按类型查找医生的未回复,进行中,已完成的咨询 总数
  155. * @param userId
  156. * @param reply
  157. * @param status
  158. * @param type
  159. * @param handler
  160. */
  161. static topicListCountByType(userId,reply,status,type,patientName,startTime,endTime,handler){
  162. let sql = "";
  163. var args =[];
  164. var tempParms = "";
  165. if(patientName){
  166. tempParms += " and s.name like '%"+patientName+"%' ";
  167. }
  168. if(startTime){
  169. tempParms += " and t.create_time >= '"+startTime+"' ";
  170. }
  171. if(endTime){
  172. tempParms += " and t.create_time <= '"+endTime+"' ";
  173. }
  174. if(status==10){
  175. args.push(userId,status,type);
  176. sql = "SELECT count(t.id) count FROM "+
  177. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  178. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  179. "AND d.id in (?) AND t. STATUS = ? AND c.type =? "+tempParms;
  180. }else{
  181. args.push(userId,status,reply,type);
  182. sql = "SELECT count(t.id) count FROM "+
  183. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsult+" c,"+DB_TABLES.Patients+" s "+
  184. "WHERE d.id = p.participant_id AND c.id = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  185. "AND d.id in (?) AND t. STATUS = ? and t.reply=? AND c.type = ? "+tempParms;
  186. }
  187. ImDb.execQuery({
  188. sql: sql,
  189. args: args,
  190. handler: function (err, res) {
  191. handler(err, res);
  192. }
  193. });
  194. }
  195. /**
  196. * 过滤名医咨询和续方咨询(区分团队)
  197. * @param userId
  198. * @param reply
  199. * @param status
  200. * @param page
  201. * @param size
  202. * @param handler
  203. */
  204. static findAllByUserAndReplyAndStatusHealthTeamTopic(userId,reply,status,adminTeamCode,page,size,handler){
  205. let sql = "";
  206. var args =[];
  207. if(status==10){
  208. args.push(adminTeamCode,userId,status,page,size);
  209. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  210. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  211. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  212. "AND c.admin_team_code =? AND d.id in (?) AND t. STATUS = ? AND c.type not in ('6','8') ORDER BY create_time DESC limit ?,?";
  213. }else{
  214. args.push(adminTeamCode,userId,status,reply,page,size);
  215. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  216. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  217. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  218. "AND c.admin_team_code =? AND d.id in (?) AND t. STATUS = ? and t.reply=? AND c.type not in ('6','8') ORDER BY create_time DESC limit ?,?";
  219. }
  220. ImDb.execQuery({
  221. sql: sql,
  222. args: args,
  223. handler: function (err, res) {
  224. handler(err, res);
  225. }
  226. });
  227. }
  228. static findReplyCount(userId,reply,status,adminTeamCode,handler){
  229. let sql = "";
  230. var args =[];
  231. if(status==10){
  232. args.push(adminTeamCode,userId,status);
  233. sql = "SELECT count(1) as count FROM "+
  234. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  235. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  236. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?) AND t. STATUS = ? ";
  237. }else if(status){
  238. args.push(adminTeamCode,userId,status,reply);
  239. sql = "SELECT count(1) as count FROM "+
  240. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  241. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  242. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?) AND t. STATUS = ? and t.reply=? ";
  243. }else{
  244. args.push(adminTeamCode,userId);
  245. sql = "SELECT count(1) as count FROM "+
  246. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  247. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  248. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?)";
  249. }
  250. ImDb.execQuery({
  251. sql: sql,
  252. args: args,
  253. handler: function (err, res) {
  254. if(err){
  255. log.error("get topic count error");
  256. handler(err,0);
  257. return;
  258. }
  259. handler(err, res[0].count);
  260. }
  261. });
  262. }
  263. static findLastTopicStatusAndType(sessionId, handler) {
  264. let sql = "select id from " + DB_TABLES.Topics + " where session_id = ? order by create_time desc limit 0, 1";
  265. ImDb.execQuery({
  266. sql: sql,
  267. args: [sessionId],
  268. handler: function (err, res) {
  269. if (res.length == 0) {
  270. handler(null, null);
  271. } else {
  272. TopicRepo.findTopicStatusAndType(res[0].id, handler);
  273. }
  274. }
  275. });
  276. }
  277. static findTopicStatus(topicId, handler) {
  278. let sql = "select id, name, description, status,agent from " + DB_TABLES.Topics + " where id = ?";
  279. ImDb.execQuery({
  280. sql: sql,
  281. args: [topicId],
  282. handler: handler || function (err, res) {
  283. if (err) log.error(err);
  284. }
  285. });
  286. }
  287. static findTopicStatusAndType(topicId, handler) {
  288. let sql = "select t.id, t.name, t.description, t.status,t.agent,c.type from topics t,wlyy.wlyy_consult c where t.id = ? and t.id = c.code";
  289. ImDb.execQuery({
  290. sql: sql,
  291. args: [topicId],
  292. handler: handler || function (err, res) {
  293. if (err) log.error(err);
  294. }
  295. });
  296. }
  297. /**
  298. * 获取会话中的议题。
  299. *
  300. * @param sessionId
  301. * @param handler
  302. */
  303. static findAllBySessionId(sessionId, handler) {
  304. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  305. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where session_id = ? order by id";
  306. ImDb.execQuery({
  307. sql: sql,
  308. args: [sessionId],
  309. handler: handler || function (err, res) {
  310. if (err) log.error(err);
  311. }
  312. });
  313. }
  314. /**
  315. * 获取会话中的议题。
  316. *
  317. * @param id
  318. * @param handler
  319. */
  320. static findAllByTopicId(id, handler) {
  321. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  322. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where id = ? order by id";
  323. ImDb.execQuery({
  324. sql: sql,
  325. args: [id],
  326. handler: handler || function (err, res) {
  327. if (err) log.error(err);
  328. }
  329. });
  330. }
  331. static findAllBySessionIdsAndStatus(sessionIds, status, page, pagesize, handler) {
  332. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  333. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where session_id in ('" + sessionIds + "') and status in (" + status + ") order by status desc limit ?,? ";
  334. ImDb.execQuery({
  335. sql: sql,
  336. args: [page, pagesize],
  337. handler: handler || function (err, res) {
  338. if (err) log.error(err);
  339. }
  340. });
  341. }
  342. /**
  343. * 保存议题
  344. *
  345. * @param topicName
  346. * @param topicId
  347. * @param sessionId
  348. * @param messageId
  349. * @param date
  350. * @param description
  351. * @param status
  352. * @param agent
  353. * @param handler
  354. */
  355. static saveTopic(topicName, topicId, sessionId, messageId, date, description, status, agent, handler) {
  356. let sql = "insert into " + DB_TABLES.Topics + " (id,session_id,name,create_time,start_message_id,description,status,agent) VALUES (?,?,?,?,?,?,?,?)";
  357. ImDb.execQuery({
  358. "sql": sql,
  359. "args": [topicId, sessionId, topicName, date, messageId, description, status, agent],
  360. "handler": function (err, res) {
  361. handler(err, res);
  362. }
  363. });
  364. }
  365. /**
  366. * 结束议题
  367. *
  368. * @param topicId
  369. * @param endUser
  370. * @param date
  371. * @param messageId
  372. * @param status
  373. */
  374. static endTopic(topicId, endUser, date, messageId, status) {
  375. let sql = "update " + DB_TABLES.Topics + " set end_by = ?,end_time=?,end_message_id=?,status = ? where id = ?";
  376. ImDb.execQuery({
  377. "sql": sql,
  378. "args": [endUser, date, messageId, status, topicId],
  379. "handler": function (err, res) {
  380. if (err) {
  381. log.error("endTopic is fail error: " + err);
  382. } else {
  383. log.info("endTopic is success");
  384. }
  385. }
  386. });
  387. }
  388. /**
  389. * 医生第一次回复咨询
  390. * @param reply_user
  391. * @param reply_message_id
  392. * @param topicId
  393. */
  394. static replyTopic(reply_user,reply_message_id,topicId,handler){
  395. let sql = "UPDATE " + DB_TABLES.Topics + " SET reply = 1,reply_time = now(),reply_user = ?,reply_message_id = ? WHERE id = ?";
  396. ImDb.execQuery({
  397. "sql": sql,
  398. "args": [reply_user, reply_message_id, topicId],
  399. "handler": handler
  400. });
  401. }
  402. /**
  403. * 更新议题状态。
  404. *
  405. * @param topicId
  406. * @param jsonValue
  407. * @param handler
  408. */
  409. static updateTopics(topicId, jsonValue, handler) {
  410. let values = [];
  411. let sql = "UPDATE topics SET ";
  412. let key = [];
  413. for (let j in jsonValue) {
  414. key.push(j + " = ?");
  415. values.push(jsonValue[j]);
  416. }
  417. sql = sql + key.join(",");
  418. sql = sql + " WHERE id = ?";
  419. values.push(topicId);
  420. ImDb.execQuery({
  421. "sql": sql,
  422. "args": values,
  423. "handler": handler
  424. });
  425. }
  426. /**
  427. * 搜索最后回复时间超过指定时限的议题,此议题最后一条消息的回复者必须是医生,即医生发送消息后,患者未理睬的,关闭。
  428. *
  429. * @param timespan 时限,以小时计
  430. * @param handler
  431. */
  432. static findAllBySessionLastActiveTime(timespan, handler) {
  433. let sql = "SELECT s.id session_id, s.name session_name, s.create_date session_create_time, s.last_message_time, " +
  434. "t.id topic_id, t.name topic_name, t.create_time topic_create_time, t.start_message_id " +
  435. "FROM sessions s, wlyy_consults t " +
  436. "WHERE s.id = t.session_id AND t.end_message_id IS NULL AND s.last_content_type in(1,2,3,4) and s.last_sender_id IN (SELECT id FROM doctors d where d.id<>t.patient) " +
  437. "AND UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(s.last_message_time) > ? " +
  438. "ORDER BY t.create_time";
  439. ImDb.execQuery({
  440. sql: sql,
  441. args: [timespan * 3600],
  442. handler: handler
  443. });
  444. }
  445. }
  446. module.exports = TopicRepo;