topics.repo.js 16 KB

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