topics.repo.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 page
  159. * @param size
  160. * @param handler
  161. */
  162. static findAllByUserAndReplyAndStatusHealthTeamTopic(userId,reply,status,adminTeamCode,page,size,handler){
  163. let sql = "";
  164. var args =[];
  165. if(status==10){
  166. args.push(adminTeamCode,userId,status,page,size);
  167. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  168. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  169. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  170. "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 ?,?";
  171. }else{
  172. args.push(adminTeamCode,userId,status,reply,page,size);
  173. sql = "SELECT t.*, s.avatar,s.sex,s.birthdate, s.`name` AS patient_name,c.doctor FROM "+
  174. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  175. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  176. "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 ?,?";
  177. }
  178. ImDb.execQuery({
  179. sql: sql,
  180. args: args,
  181. handler: function (err, res) {
  182. handler(err, res);
  183. }
  184. });
  185. }
  186. static findReplyCount(userId,reply,status,adminTeamCode,handler){
  187. let sql = "";
  188. var args =[];
  189. if(status==10){
  190. args.push(adminTeamCode,userId,status);
  191. sql = "SELECT count(1) as count FROM "+
  192. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  193. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  194. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?) AND t. STATUS = ? ";
  195. }else if(status){
  196. args.push(adminTeamCode,userId,status,reply);
  197. sql = "SELECT count(1) as count FROM "+
  198. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  199. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  200. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?) AND t. STATUS = ? and t.reply=? ";
  201. }else{
  202. args.push(adminTeamCode,userId);
  203. sql = "SELECT count(1) as count FROM "+
  204. DB_TABLES.Topics+" t,"+DB_TABLES.Participants+" p,"+DB_TABLES.Doctors+" d,"+DB_TABLES.WlyyConsultTeam+" c,"+DB_TABLES.Patients+" s "+
  205. "WHERE d.id = p.participant_id AND c.consult = t.id AND c.patient = s.id AND p.session_id = t.session_id "+
  206. "AND c.admin_team_code=? AND c.type!=8 AND d.id in (?)";
  207. }
  208. ImDb.execQuery({
  209. sql: sql,
  210. args: args,
  211. handler: function (err, res) {
  212. if(err){
  213. log.error("get topic count error");
  214. handler(err,0);
  215. return;
  216. }
  217. handler(err, res[0].count);
  218. }
  219. });
  220. }
  221. static findLastTopicStatusAndType(sessionId, handler) {
  222. let sql = "select id from " + DB_TABLES.Topics + " where session_id = ? order by create_time desc limit 0, 1";
  223. ImDb.execQuery({
  224. sql: sql,
  225. args: [sessionId],
  226. handler: function (err, res) {
  227. if (res.length == 0) {
  228. handler(null, null);
  229. } else {
  230. TopicRepo.findTopicStatusAndType(res[0].id, handler);
  231. }
  232. }
  233. });
  234. }
  235. static findTopicStatus(topicId, handler) {
  236. let sql = "select id, name, description, status,agent from " + DB_TABLES.Topics + " where id = ?";
  237. ImDb.execQuery({
  238. sql: sql,
  239. args: [topicId],
  240. handler: handler || function (err, res) {
  241. if (err) log.error(err);
  242. }
  243. });
  244. }
  245. static findTopicStatusAndType(topicId, handler) {
  246. 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";
  247. ImDb.execQuery({
  248. sql: sql,
  249. args: [topicId],
  250. handler: handler || function (err, res) {
  251. if (err) log.error(err);
  252. }
  253. });
  254. }
  255. /**
  256. * 获取会话中的议题。
  257. *
  258. * @param sessionId
  259. * @param handler
  260. */
  261. static findAllBySessionId(sessionId, handler) {
  262. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  263. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where session_id = ? order by id";
  264. ImDb.execQuery({
  265. sql: sql,
  266. args: [sessionId],
  267. handler: handler || function (err, res) {
  268. if (err) log.error(err);
  269. }
  270. });
  271. }
  272. /**
  273. * 获取会话中的议题。
  274. *
  275. * @param id
  276. * @param handler
  277. */
  278. static findAllByTopicId(id, handler) {
  279. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  280. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where id = ? order by id";
  281. ImDb.execQuery({
  282. sql: sql,
  283. args: [id],
  284. handler: handler || function (err, res) {
  285. if (err) log.error(err);
  286. }
  287. });
  288. }
  289. static findAllBySessionIdsAndStatus(sessionIds, status, page, pagesize, handler) {
  290. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  291. " 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 ?,? ";
  292. ImDb.execQuery({
  293. sql: sql,
  294. args: [page, pagesize],
  295. handler: handler || function (err, res) {
  296. if (err) log.error(err);
  297. }
  298. });
  299. }
  300. /**
  301. * 保存议题
  302. *
  303. * @param topicName
  304. * @param topicId
  305. * @param sessionId
  306. * @param messageId
  307. * @param date
  308. * @param description
  309. * @param status
  310. * @param agent
  311. * @param handler
  312. */
  313. static saveTopic(topicName, topicId, sessionId, messageId, date, description, status, agent, handler) {
  314. let sql = "insert into " + DB_TABLES.Topics + " (id,session_id,name,create_time,start_message_id,description,status,agent) VALUES (?,?,?,?,?,?,?,?)";
  315. ImDb.execQuery({
  316. "sql": sql,
  317. "args": [topicId, sessionId, topicName, date, messageId, description, status, agent],
  318. "handler": function (err, res) {
  319. handler(err, res);
  320. }
  321. });
  322. }
  323. /**
  324. * 结束议题
  325. *
  326. * @param topicId
  327. * @param endUser
  328. * @param date
  329. * @param messageId
  330. * @param status
  331. */
  332. static endTopic(topicId, endUser, date, messageId, status) {
  333. let sql = "update " + DB_TABLES.Topics + " set end_by = ?,end_time=?,end_message_id=?,status = ? where id = ?";
  334. ImDb.execQuery({
  335. "sql": sql,
  336. "args": [endUser, date, messageId, status, topicId],
  337. "handler": function (err, res) {
  338. if (err) {
  339. log.error("endTopic is fail error: " + err);
  340. } else {
  341. log.info("endTopic is success");
  342. }
  343. }
  344. });
  345. }
  346. /**
  347. * 医生第一次回复咨询
  348. * @param reply_user
  349. * @param reply_message_id
  350. * @param topicId
  351. */
  352. static replyTopic(reply_user,reply_message_id,topicId,handler){
  353. let sql = "UPDATE " + DB_TABLES.Topics + " SET reply = 1,reply_time = now(),reply_user = ?,reply_message_id = ? WHERE id = ?";
  354. ImDb.execQuery({
  355. "sql": sql,
  356. "args": [reply_user, reply_message_id, topicId],
  357. "handler": handler
  358. });
  359. }
  360. /**
  361. * 更新议题状态。
  362. *
  363. * @param topicId
  364. * @param jsonValue
  365. * @param handler
  366. */
  367. static updateTopics(topicId, jsonValue, handler) {
  368. let values = [];
  369. let sql = "UPDATE topics SET ";
  370. let key = [];
  371. for (let j in jsonValue) {
  372. key.push(j + " = ?");
  373. values.push(jsonValue[j]);
  374. }
  375. sql = sql + key.join(",");
  376. sql = sql + " WHERE id = ?";
  377. values.push(topicId);
  378. ImDb.execQuery({
  379. "sql": sql,
  380. "args": values,
  381. "handler": handler
  382. });
  383. }
  384. /**
  385. * 搜索最后回复时间超过指定时限的议题,此议题最后一条消息的回复者必须是医生,即医生发送消息后,患者未理睬的,关闭。
  386. *
  387. * @param timespan 时限,以小时计
  388. * @param handler
  389. */
  390. static findAllBySessionLastActiveTime(timespan, handler) {
  391. let sql = "SELECT s.id session_id, s.name session_name, s.create_date session_create_time, s.last_message_time, " +
  392. "t.id topic_id, t.name topic_name, t.create_time topic_create_time, t.start_message_id " +
  393. "FROM sessions s, wlyy_consults t " +
  394. "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) " +
  395. "AND UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(s.last_message_time) > ? " +
  396. "ORDER BY t.create_time";
  397. ImDb.execQuery({
  398. sql: sql,
  399. args: [timespan * 3600],
  400. handler: handler
  401. });
  402. }
  403. }
  404. module.exports = TopicRepo;