ConsultTeamService.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*******************************************************************************
  2. * Copyright (c) 2005, 2014 springside.github.io
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. *******************************************************************************/
  6. package com.yihu.wlyy.service.app.consult;
  7. import java.util.*;
  8. import com.yihu.wlyy.entity.consult.Consult;
  9. import com.yihu.wlyy.entity.consult.ConsultTeam;
  10. import com.yihu.wlyy.entity.consult.ConsultTeamDoctor;
  11. import com.yihu.wlyy.entity.consult.ConsultTeamLog;
  12. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  13. import com.yihu.wlyy.entity.doctor.team.sign.DoctorTeam;
  14. import com.yihu.wlyy.entity.doctor.team.sign.DoctorTeamMember;
  15. import com.yihu.wlyy.entity.patient.Patient;
  16. import com.yihu.wlyy.entity.patient.SignFamily;
  17. import com.yihu.wlyy.repository.consult.ConsultTeamDao;
  18. import com.yihu.wlyy.repository.consult.ConsultTeamDoctorDao;
  19. import com.yihu.wlyy.repository.consult.ConsultTeamLogDao;
  20. import com.yihu.wlyy.repository.doctor.DoctorPatientDao;
  21. import com.yihu.wlyy.repository.doctor.DoctorTeamDao;
  22. import com.yihu.wlyy.repository.doctor.DoctorTeamMemberDao;
  23. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  24. import com.yihu.wlyy.util.HttpUtil;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.data.domain.Page;
  28. import org.springframework.data.domain.PageImpl;
  29. import org.springframework.data.domain.PageRequest;
  30. import org.springframework.data.domain.Sort;
  31. import org.springframework.data.domain.Sort.Direction;
  32. import org.springframework.data.jpa.domain.Specification;
  33. import org.springframework.jdbc.core.JdbcTemplate;
  34. import org.springframework.stereotype.Component;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import org.springside.modules.persistence.DynamicSpecifications;
  37. import org.springside.modules.persistence.SearchFilter;
  38. import org.springside.modules.persistence.SearchFilter.Operator;
  39. import org.springside.modules.utils.Clock;
  40. import com.yihu.wlyy.task.PushMsgTask;
  41. import com.yihu.wlyy.util.MessageType;
  42. /**
  43. * 網絡諮詢类.
  44. *
  45. * @author George
  46. */
  47. // Spring Service Bean的标识.
  48. @Component
  49. @Transactional(rollbackFor = Exception.class)
  50. public class ConsultTeamService extends ConsultService {
  51. private Clock clock = Clock.DEFAULT;
  52. // 咨询详细记录
  53. @Autowired
  54. private ConsultTeamLogDao consultTeamLogDao;
  55. @Autowired
  56. private ConsultTeamDoctorDao consultTeamDoctorDao;
  57. @Autowired
  58. private SignFamilyDao signFamilyDao;
  59. @Autowired
  60. private DoctorPatientDao doctorPatientDao;
  61. @Autowired
  62. private DoctorTeamDao doctorTeamDao;
  63. @Autowired
  64. private ConsultTeamDao consultTeamDao;
  65. @Autowired
  66. private DoctorTeamMemberDao doctorTeamDoctor;
  67. @Autowired
  68. private JdbcTemplate jdbcTemplate;
  69. /**
  70. * 查询患者是否还有未结束的三师咨询
  71. *
  72. * @param patient
  73. * @return
  74. */
  75. public boolean exist(String patient, Integer type) {
  76. int count = consultTeamDao.countByPatient(patient, type);
  77. return count > 0;
  78. }
  79. /**
  80. * 查詢醫生網絡諮詢列表
  81. *
  82. * @param type 咨询类型:1、咨询我的,2、公共的, 3、参与过的,4、已结束的 5 名医咨询 全部 6 名医咨询 进行中 7 名医咨询 已结束 8名医咨询 待处理 9咨询我的三师 + 家庭 + 名医
  83. * @param id
  84. * @param pagesize 每页显示数,默认为10
  85. * @return
  86. */
  87. public Page<ConsultTeam> findByDoctor(String uid, int type, long id, int pagesize, String title) {
  88. if (id < 0) {
  89. id = 0;
  90. }
  91. if (pagesize <= 0) {
  92. pagesize = 10;
  93. }
  94. switch (type) {
  95. case 0:
  96. // 全部
  97. return findByDoctorType0(uid, id, pagesize);
  98. case 1:
  99. // 咨询我的
  100. return findByDoctorType1(uid, type, id, pagesize);
  101. case 2:
  102. // 我感兴趣的
  103. return findByDoctorType2(uid, type, id, pagesize);
  104. case 3:
  105. // 我回复的
  106. return findByDoctorType3(uid, id, pagesize);
  107. case 4:
  108. // 已完成的
  109. return findByDoctorType4(uid, id, pagesize);
  110. case 5:
  111. // 5 名医咨询 全部
  112. return findByDoctorType5(uid, id, pagesize, title);
  113. case 6:
  114. //6 名医咨询 进行中
  115. return findByDoctorTypeSix(uid, id, pagesize, title);
  116. case 7:
  117. //7 名医咨询 已结束
  118. return findByDoctorType6(uid, 1, id, pagesize, title);
  119. case 8:
  120. return findByDoctorType8(uid, id, pagesize, title);
  121. case 9:
  122. return findByDoctorType9(uid, id, pagesize);
  123. }
  124. return null;
  125. }
  126. private Page<ConsultTeam> findByDoctorTypeSix(String uid,long id,int pagesize,String title){
  127. Sort sort = new Sort(Direction.DESC, "id");
  128. // 分页信息
  129. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  130. if (StringUtils.isNoneEmpty(title)) {
  131. title = "%" + title + "%";
  132. if (id > 0) {
  133. return consultTeamDao.findFamousDoctorIngList(uid, id, title, pageRequest);
  134. } else {
  135. return consultTeamDao.findFamousDoctorIngList(uid, title, pageRequest);
  136. }
  137. } else {
  138. if (id > 0) {
  139. return consultTeamDao.findFamousDoctorIngNoTitleList(uid, id, pageRequest);
  140. } else {
  141. return consultTeamDao.findFamousDoctorIngNoTitleList(uid, pageRequest);
  142. }
  143. }
  144. }
  145. private Page<ConsultTeam> findByDoctorType8(String uid,long id,int pagesize,String title){
  146. Sort sort = new Sort(Direction.DESC, "id");
  147. sort.and(new Sort(Direction.DESC, "czrq"));
  148. sort.and(new Sort(Direction.DESC, "doctorRead"));
  149. // 分页信息
  150. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  151. if (StringUtils.isNoneEmpty(title)) {
  152. title = "%" + title + "%";
  153. if (id > 0) {
  154. return consultTeamDao.findFamousDoctorUnReplyReadList(uid, id, title, pageRequest);
  155. } else {
  156. return consultTeamDao.findFamousDoctorUnReplyReadList(uid, title, pageRequest);
  157. }
  158. } else {
  159. if (id > 0) {
  160. return consultTeamDao.findFamousDoctorUnReplyReadNoTitleList(uid, id, pageRequest);
  161. } else {
  162. return consultTeamDao.findFamousDoctorUnReplyReadNoTitleList(uid, pageRequest);
  163. }
  164. }
  165. }
  166. private Page<ConsultTeam> findByDoctorType5(String uid, long id, int pagesize, String title) {
  167. Sort sort = new Sort(Direction.DESC, "id");
  168. // 分页信息
  169. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  170. if (StringUtils.isNoneEmpty(title)) {
  171. title = "%" + title + "%";
  172. if (id > 0) {
  173. return consultTeamDao.findFamousDoctorAllList(uid, id, title, pageRequest);
  174. } else {
  175. return consultTeamDao.findFamousDoctorAllList(uid, title, pageRequest);
  176. }
  177. } else {
  178. if (id > 0) {
  179. return consultTeamDao.findFamousDoctorAllList(uid, id, pageRequest);
  180. } else {
  181. return consultTeamDao.findFamousDoctorAllList(uid, pageRequest);
  182. }
  183. }
  184. }
  185. private Page<ConsultTeam> findByDoctorType6(String uid, int type, long id, int pagesize, String title) {
  186. // 排序
  187. Sort sort = new Sort(Direction.DESC, "id");
  188. // 分页信息
  189. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  190. if (StringUtils.isNoneEmpty(title)) {
  191. title = "%" + title + "%";
  192. if (id > 0) {
  193. return consultTeamDao.findFamousDoctorDoingList(uid, id, title, type, pageRequest);
  194. } else {
  195. return consultTeamDao.findFamousDoctorDoingList(uid, title, type, pageRequest);
  196. }
  197. } else {
  198. if (id > 0) {
  199. return consultTeamDao.findFamousDoctorDoingList(uid, id, type, pageRequest);
  200. } else {
  201. return consultTeamDao.findFamousDoctorDoingList(uid, type, pageRequest);
  202. }
  203. }
  204. }
  205. /**
  206. * 查询全部咨询记录
  207. *
  208. * @param uid
  209. * @param id
  210. * @param pagesize
  211. * @return
  212. */
  213. public Page<ConsultTeam> findByDoctorType0(String uid, long id, int pagesize) {
  214. // 排序
  215. Sort sort = new Sort(Direction.DESC, "id");
  216. // 分页信息
  217. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  218. if (id > 0) {
  219. return consultTeamDao.findDoctorList(uid, id, pageRequest);
  220. } else {
  221. return consultTeamDao.findDoctorList(uid, pageRequest);
  222. }
  223. }
  224. /**
  225. * 指定医生三师咨询列表查询
  226. *
  227. * @param uid 医生标识
  228. * @param type 咨询类型:1、咨询我的,2、公共的, 3、参与过的,4、已结束的
  229. * @param pagesize
  230. * @return
  231. */
  232. public Page<ConsultTeam> findByDoctorType1(String uid, int type, long id, int pagesize) {
  233. // 排序
  234. Sort sort = new Sort(Direction.DESC, "id");
  235. // 分页信息
  236. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  237. if (id > 0) {
  238. return consultTeamDao.findDoctorPointList(uid, id, pageRequest);
  239. } else {
  240. return consultTeamDao.findDoctorPointList(uid, pageRequest);
  241. }
  242. }
  243. /**
  244. * 指定医生三师咨询列表查询
  245. *
  246. * @param uid 医生标识
  247. * @param pagesize
  248. * @return
  249. */
  250. public Page<ConsultTeam> findByDoctorType9(String uid, long id, int pagesize) {
  251. // 排序
  252. Sort sort = new Sort(Direction.DESC, "id");
  253. // 分页信息
  254. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  255. if (id > 0) {
  256. return consultTeamDao.findDoctorAllList(uid, id, pageRequest);
  257. } else {
  258. return consultTeamDao.findDoctorAllList(uid, pageRequest);
  259. }
  260. }
  261. /**
  262. * 公共三师咨询列表查询
  263. *
  264. * @param uid 医生标识
  265. * @param type 咨询类型:1、咨询我的,2、公共的, 3、参与过的,4、已结束的
  266. * @param pagesize
  267. * @return
  268. */
  269. public Page<ConsultTeam> findByDoctorType2(String uid, int type, long id, int pagesize) {
  270. // 排序
  271. Sort sort = new Sort(Direction.DESC, "id");
  272. // 分页信息
  273. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  274. // 设置查询条件
  275. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  276. // 未指定医生
  277. filters.put("type", new SearchFilter("type", Operator.EQ, 0));
  278. if (id > 0) {
  279. filters.put("id", new SearchFilter("id", Operator.LT, id));
  280. }
  281. // 未回复
  282. filters.put("status", new SearchFilter("status", Operator.EQ, 0));
  283. // 未作废
  284. filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
  285. Specification<ConsultTeam> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultTeam.class);
  286. return consultTeamDao.findAll(spec, pageRequest);
  287. }
  288. /**
  289. * 医生参与过的未结束的三师咨询列表查询
  290. *
  291. * @param uid
  292. * @param pagesize
  293. * @return
  294. */
  295. public Page<ConsultTeam> findByDoctorType3(String uid, long id, int pagesize) {
  296. // 排序
  297. Sort sort = new Sort(Direction.DESC, "id");
  298. // 分页信息
  299. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  300. if (id > 0) {
  301. return consultTeamDao.findDoctorJoinList(uid, id, pageRequest);
  302. } else {
  303. return consultTeamDao.findDoctorJoinList(uid, pageRequest);
  304. }
  305. }
  306. /**
  307. * 医生参与过的已结束的三师咨询列表查询
  308. *
  309. * @param uid
  310. * @param pagesize
  311. * @return
  312. */
  313. public Page<ConsultTeam> findByDoctorType4(String uid, long id, int pagesize) {
  314. // 排序
  315. Sort sort = new Sort(Direction.DESC, "id");
  316. // 分页信息
  317. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  318. if (id > 0) {
  319. return consultTeamDao.findDoctorFinishList(uid, id, pageRequest);
  320. } else {
  321. return consultTeamDao.findDoctorFinishList(uid, pageRequest);
  322. }
  323. }
  324. /**
  325. * 网络咨询咨询日志查询
  326. *
  327. * @param consult 咨询标识
  328. * @param pagesize 每页显示数,默认为10
  329. * @return
  330. */
  331. public Page<ConsultTeamLog> findLogByConsult(String consult, long id, int pagesize) {
  332. if (id < 0) {
  333. id = 0;
  334. }
  335. // 设置查询条件
  336. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  337. filters.put("consult", new SearchFilter("consult", Operator.EQ, consult));
  338. if (id > 0) {
  339. if (pagesize > 0)
  340. filters.put("id", new SearchFilter("id", Operator.LT, id));
  341. else
  342. filters.put("id", new SearchFilter("id", Operator.GT, id));
  343. }
  344. filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
  345. Specification<ConsultTeamLog> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultTeamLog.class);
  346. if (pagesize <= 0) {
  347. Page<ConsultTeamLog> p = new PageImpl<>(consultTeamLogDao.findAll(spec));
  348. return p;
  349. }
  350. // 排序
  351. Sort sort = new Sort(Direction.DESC, "id");
  352. // 分页信息
  353. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  354. return consultTeamLogDao.findAll(spec, pageRequest);
  355. }
  356. /**
  357. * 批量回复
  358. *
  359. * @param logs
  360. * @return
  361. */
  362. public void reply(List<ConsultTeamLog> logs, String patient) {
  363. for (ConsultTeamLog log : logs) {
  364. reply(log, patient, null, log.getType());
  365. }
  366. }
  367. /**
  368. * 添加咨询记录
  369. *
  370. * @param log 日志对象
  371. * @param type 类型,0问,1回复,2追问,3评价
  372. * @return
  373. */
  374. public ConsultTeamLog reply(ConsultTeamLog log, String patient, String teamOrDoctor, int type) {
  375. log.setCzrq(clock.getCurrentDate());
  376. // 保存咨询记录
  377. ConsultTeamLog temp = consultTeamLogDao.save(log);
  378. if (temp != null) {
  379. // 发送消息
  380. if (type == 1) {
  381. // 医生回复,给患者发消息
  382. // sendMessage(patient, teamOrDoctor, "三师咨询", log.getDoctorName() + "回复了您的咨询", log.getConsult(), 214, 1, 0, 0);
  383. // 患者未读数量+1
  384. consultTeamDao.increasePatientRead(log.getConsult());
  385. // 医生有回复
  386. consultTeamDoctorDao.updateReply(log.getConsult(), teamOrDoctor);
  387. //shenzaixin v1.2.0 推送消息给患者
  388. HttpUtil.sendWeixinWebsocketMsg(patient, "{busiType:'qianyuezixun',msgid:'" + log.getId() + "'}");
  389. } else if (type == 0 || type == 2) {
  390. // 查询相关联的医生
  391. Iterable<ConsultTeamDoctor> iterable = consultTeamDoctorDao.findByConsult(log.getConsult());
  392. if (iterable != null && iterable.iterator() != null && iterable.iterator().hasNext()) {
  393. Iterator<ConsultTeamDoctor> iterator = iterable.iterator();
  394. while (iterator.hasNext()) {
  395. // 患者提问或追问,给医生发消息
  396. ConsultTeamDoctor ctd = iterator.next();
  397. if (ctd == null) {
  398. continue;
  399. }
  400. // 推送消息给医生
  401. PushMsgTask.getInstance().put(ctd.getTo(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM_REPLY.D_CT_02.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM_REPLY.指定咨询.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM_REPLY.您有新的消息.name(), temp.getConsult());
  402. }
  403. }
  404. // 医生未读数量+1
  405. consultTeamDao.increaseDoctorRead(log.getConsult());
  406. }
  407. }
  408. return temp;
  409. }
  410. /**
  411. * 添加三师咨询
  412. *
  413. * @param ct 三师咨询对象
  414. * @param patient 患者标识
  415. * @return
  416. * @throws Exception
  417. */
  418. public int addTeamConsult(ConsultTeam ct, String patient) throws Exception {
  419. // 咨询三师
  420. if (ct.getType() == 1) {
  421. // 查询三师签约信息
  422. SignFamily sc = signFamilyDao.findBySanshiPatientYes(patient);
  423. if (sc == null) {
  424. // 不存在三师签约
  425. return -2;
  426. }
  427. ct.setTeam(sc.getTeamCode());
  428. // 设置健康管理师,三师咨询默认给健康管理师处理
  429. //查找病人所在的团队
  430. DoctorTeam doctorTeam = doctorTeamDao.findBySanshiParientCode(patient);
  431. //得到团队的健康管理师
  432. DoctorTeamMember doctorTeamMember = doctorTeamDoctor.findDoctorSanshi2ByTeam(doctorTeam.getCode(), 3);
  433. // 设置家庭医生
  434. ct.setDoctor(doctorTeamMember.getMemberCode());
  435. ct.setAdminTeamId(sc.getAdminTeamId());
  436. } else if (ct.getType() == 2) {
  437. // 咨询家庭医生
  438. SignFamily sf = signFamilyDao.findByjiatingPatient(patient);
  439. if (sf == null) {
  440. // 不存在家庭签约
  441. return -1;
  442. }
  443. // 设置健康管理师,家庭医生咨询默认给健康管理师处理
  444. //查找病人所在的团队
  445. DoctorTeam doctorTeam = doctorTeamDao.findByParientCode(patient);
  446. //得到团队的健康管理师
  447. DoctorTeamMember doctorTeamMember = doctorTeamDoctor.findDoctorJiating2ByTeam(doctorTeam.getCode(), 3);
  448. // 设置家庭医生
  449. ct.setDoctor(doctorTeamMember.getMemberCode());
  450. ct.setAdminTeamId(sf.getAdminTeamId());
  451. }
  452. // 设置患者信息
  453. ct.setPatient(patient);
  454. // 查询患者信息
  455. Patient tempPatient = patientDao.findByCode(patient);
  456. // 设置患者姓名
  457. ct.setName(tempPatient.getName());
  458. // 设置患者生日
  459. ct.setBirthday(tempPatient.getBirthday());
  460. //新增性别
  461. ct.setSex(tempPatient.getSex());
  462. // 设置患者头像
  463. ct.setPhoto(tempPatient.getPhoto());
  464. // 设置操作日期
  465. ct.setCzrq(new Date());
  466. ct.setDel("1");
  467. ct.setStatus(0);
  468. // 医生未读数量为1
  469. ct.setDoctorRead(1);
  470. // 添加咨询记录
  471. Consult consult = addConsult(ct.getPatient(), null, ct.getSymptoms(), ct.getImages(), ct.getType());
  472. // 设置咨询标识
  473. ct.setConsult(consult.getCode());
  474. // 保存医生咨询信息
  475. if (consultTeamDao.save(ct) == null) {
  476. throw new Exception("保存失败!");
  477. }
  478. // 添加咨询转发记录
  479. ConsultTeamDoctor cd = new ConsultTeamDoctor();
  480. cd.setConsult(consult.getCode());
  481. cd.setDel("1");
  482. cd.setCzrq(new Date());
  483. cd.setTo(ct.getDoctor());
  484. consultTeamDoctorDao.save(cd);
  485. // 添加医生咨询日志
  486. addLogs(ct);
  487. return 1;
  488. }
  489. /**
  490. * 添加三师咨询日志
  491. *
  492. * @param ct
  493. * @throws Exception
  494. */
  495. private void addLogs(ConsultTeam ct) throws Exception {
  496. List<ConsultTeamLog> logs = new ArrayList<ConsultTeamLog>();
  497. // 添加问题咨询日志
  498. String content = "";
  499. content = "咨询问题:" + (StringUtils.isEmpty(ct.getSymptoms()) ? "无" : ct.getSymptoms());
  500. // 生成提问日志,并推送相关消息
  501. ConsultTeamLog infoLog = new ConsultTeamLog();
  502. infoLog.setConsult(ct.getConsult());
  503. infoLog.setContent(content);
  504. infoLog.setDel("1");
  505. infoLog.setType(0);
  506. infoLog.setChatType(1);
  507. infoLog.setCzrq(new Date());
  508. logs.add(infoLog);
  509. // 图片日志
  510. if (StringUtils.isNotEmpty(ct.getImages())) {
  511. String[] images = ct.getImages().split(",");
  512. for (String image : images) {
  513. if (StringUtils.isNoneEmpty(image)) {
  514. ConsultTeamLog imgLog = new ConsultTeamLog();
  515. // 设置咨询标识
  516. imgLog.setConsult(ct.getConsult());
  517. // 设置图片URL
  518. imgLog.setContent(image);
  519. imgLog.setDel("1");
  520. infoLog.setType(0);
  521. imgLog.setCzrq(new Date());
  522. // 图片类型
  523. imgLog.setChatType(2);
  524. // 添加到待保存队列
  525. logs.add(imgLog);
  526. }
  527. }
  528. }
  529. // 语音日志
  530. if (StringUtils.isNotEmpty(ct.getVoice())) {
  531. ConsultTeamLog voiceLog = new ConsultTeamLog();
  532. // 设置咨询标识
  533. voiceLog.setConsult(ct.getConsult());
  534. // 设置语音URL
  535. voiceLog.setContent(ct.getVoice());
  536. voiceLog.setDel("1");
  537. infoLog.setType(0);
  538. // 语音类型
  539. voiceLog.setChatType(3);
  540. voiceLog.setCzrq(new Date());
  541. // 添加到待保存队列
  542. logs.add(voiceLog);
  543. }
  544. if (!logs.isEmpty()) {
  545. Iterable<ConsultTeamLog> iterable = consultTeamLogDao.save(logs);
  546. if (iterable == null || iterable.iterator() == null || !iterable.iterator().hasNext()) {
  547. // 日志保存失败
  548. throw new Exception("consult team log save failed!");
  549. }
  550. }
  551. // 患者提问或追问,给医生发消息
  552. // sendMessage(ct.getDoctor(), ct.getPatient(), "三师咨询", "您有新的三师咨询消息", ct.getConsult(), 116, 1, 0, 0);
  553. }
  554. /**
  555. * 查询咨询列表
  556. *
  557. * @param patientCode 患者标志
  558. * @param status 咨询状态(0未结束,1已结束,-1 已取消)
  559. * @param pageSize 页数
  560. * @return
  561. */
  562. public Page<ConsultTeam> findByPatient(String patientCode, int status, long id, int pageSize) {
  563. if (id < 0) {
  564. id = 0;
  565. }
  566. if (pageSize <= 0) {
  567. pageSize = 10;
  568. }
  569. // 排序
  570. Sort sort = new Sort(Direction.DESC, "id");
  571. // 分页信息
  572. PageRequest pageRequest = new PageRequest(0, pageSize, sort);
  573. Page<ConsultTeam> list = null;
  574. switch (status) {
  575. case 0:
  576. // 未结束
  577. if (id > 0) {
  578. list = consultTeamDao.findNotFinishedBypatient(patientCode, id, pageRequest);
  579. } else {
  580. list = consultTeamDao.findNotFinishedBypatient(patientCode, pageRequest);
  581. }
  582. break;
  583. case 1:
  584. // 已结束
  585. if (id > 0) {
  586. list = consultTeamDao.findFinishedBypatient(patientCode, id, pageRequest);
  587. } else {
  588. list = consultTeamDao.findFinishedBypatient(patientCode, pageRequest);
  589. }
  590. break;
  591. case -1:
  592. // 已取消
  593. if (id > 0) {
  594. list = consultTeamDao.findCancelBypatient(patientCode, id, pageRequest);
  595. } else {
  596. list = consultTeamDao.findCancelBypatient(patientCode, pageRequest);
  597. }
  598. break;
  599. }
  600. return list;
  601. }
  602. /**
  603. * 医生关闭三师咨询
  604. *
  605. * @param consult 三师咨询标识
  606. * @return
  607. */
  608. public int finish(String consult) {
  609. return consultTeamDao.updateStatusByConsult(consult);
  610. }
  611. /**
  612. * 设置咨询已读
  613. *
  614. * @param consult
  615. * @return
  616. */
  617. public int readMessage(String consult) {
  618. return consultTeamDao.updateReadedByConsult(consult);
  619. }
  620. /**
  621. * 取消三师咨询
  622. *
  623. * @param consult
  624. * @return
  625. */
  626. public int cancel(String consult) {
  627. return consultTeamDao.cancel(consult);
  628. }
  629. public ConsultTeam findByCode(String code) {
  630. return consultTeamDao.findByConsult(code);
  631. }
  632. /**
  633. * 三师咨询转接医生
  634. *
  635. * @param from 转出医生标识
  636. * @param to 转入医生标识
  637. * @param consult 三师咨询标识
  638. * @return
  639. */
  640. public void transfer(String from, String to, String consult) {
  641. // 检查是否存在
  642. if (consultTeamDoctorDao.isExist(consult, to) == 0) {
  643. // 查询医生信息
  644. Doctor d = doctorDao.findByCode(from);
  645. // 再保存
  646. ConsultTeamDoctor cd = new ConsultTeamDoctor();
  647. cd.setConsult(consult);
  648. cd.setDel("1");
  649. cd.setCzrq(new Date());
  650. cd.setFrom(d.getCode());
  651. cd.setFromName(d.getName());
  652. cd.setTo(to);
  653. consultTeamDoctorDao.save(cd);
  654. }
  655. }
  656. /**
  657. * 查询患者视频和三师咨询记录
  658. *
  659. * @param patient 患者标识
  660. * @param id
  661. * @param pagesize
  662. * @return
  663. */
  664. public Page<Consult> findConsultRecordByPatientType(String patient, long id, int pagesize) {
  665. if (pagesize <= 0) {
  666. pagesize = 10;
  667. }
  668. // 排序
  669. Sort sort = new Sort(Direction.DESC, "id");
  670. // 分页信息
  671. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  672. // 设置查询条件
  673. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  674. filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
  675. filters.put("type", new SearchFilter("type", Operator.LT, 3));
  676. if (id > 0) {
  677. filters.put("id", new SearchFilter("id", Operator.LT, id));
  678. }
  679. // 未作废
  680. filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
  681. Specification<Consult> spec = DynamicSpecifications.bySearchFilter(filters.values(), Consult.class);
  682. return consultDao.findAll(spec, pageRequest);
  683. }
  684. /**
  685. * 清空医生未读数量
  686. *
  687. * @param consult
  688. */
  689. public void clearDoctorRead(String consult) {
  690. consultTeamDao.clearDoctorRead(consult);
  691. }
  692. /**
  693. * 清空患者未读数量
  694. *
  695. * @param consult
  696. */
  697. public void clearPatientRead(String consult) {
  698. consultTeamDao.clearPatientRead(consult);
  699. }
  700. /**
  701. * 查询医生的咨询统计
  702. *
  703. * @param doctorCode
  704. * @return
  705. */
  706. public Map<String, Long> getAllCount(String doctorCode) {
  707. Map<String, Long> result = new HashMap<>();
  708. //根据医生code查询总咨询数
  709. long allCount = consultTeamDoctorDao.getAllCountByDoctorCode(doctorCode);
  710. //根据医生code查询今日咨询数
  711. long todayCount = consultTeamDoctorDao.getTodayCountByDoctorCode(doctorCode);
  712. result.put("all", allCount);
  713. result.put("today", todayCount);
  714. return result;
  715. }
  716. /**
  717. * 查找未完成咨询
  718. *
  719. * @param patient
  720. * @return
  721. */
  722. public List<ConsultTeam> getUnfinishedConsult(String patient) {
  723. return consultTeamDao.findUnfinishedConsult(patient);
  724. }
  725. /**
  726. * 查询患者全部的咨询数目
  727. *
  728. * @param patientCode
  729. * @return
  730. */
  731. public Integer findByDoctorSize(String patientCode, String doctorCode) {
  732. return consultDao.findByPatient(patientCode, doctorCode);
  733. }
  734. public List<Map<String, Object>> getConsultSign(String[] consults) {
  735. String params = "";
  736. List<String> paramList = new ArrayList<>();
  737. for (int i = 0; i < consults.length; i++) {
  738. params += (i == 0 ? "?" : ",?");
  739. }
  740. paramList.addAll(Arrays.asList(consults));
  741. paramList.addAll(Arrays.asList(consults));
  742. String sqlgp = "select DISTINCT consult,to_doctor from wlyy_consult_team_doctor ctd,wlyy_doctor d where ctd.to_doctor = d.code and d.level = 2 and ctd.consult in (" + params + ") and ctd.del = '1'";
  743. String sqlgpm = "select DISTINCT to_doctor from wlyy_consult_team_doctor ctd,wlyy_doctor d where ctd.to_doctor = d.code and d.level = 2 and ctd.consult in (" + params + ") and ctd.del = '1'";
  744. String sqlQu = "select * from wlyy_quota_result where qkdoctor_code in (" + sqlgpm + ") and quato_code = '1' and level1_type = '1'";
  745. String sqlQuSum = "select a.consult,sum(ifnull(b.result,0)) sign from (" + sqlgp + ") a left join (" + sqlQu + ") b on a.to_doctor = b.qkdoctor_code group by a.consult ";
  746. return jdbcTemplate.queryForList(sqlQuSum, paramList.toArray());
  747. }
  748. public ConsultTeamLog oneLog(Long logId) {
  749. return consultTeamLogDao.findOne(logId);
  750. }
  751. public boolean isExistFamousConsult(String uid) {
  752. ConsultTeam consultTeam = consultTeamDao.findFamousConsultByPatient(uid);
  753. if (consultTeam != null) {
  754. return true;
  755. } else {
  756. return false;
  757. }
  758. }
  759. public void addFamousTeamConsult(ConsultTeam ct, String uid) throws Exception {
  760. // 设置患者信息
  761. ct.setPatient(uid);
  762. // 查询患者信息
  763. Patient tempPatient = patientDao.findByCode(uid);
  764. // 设置患者姓名
  765. ct.setName(tempPatient.getName());
  766. // 设置患者生日
  767. ct.setBirthday(tempPatient.getBirthday());
  768. //新增性别
  769. ct.setSex(tempPatient.getSex());
  770. // 设置患者头像
  771. ct.setPhoto(tempPatient.getPhoto());
  772. // 设置操作日期
  773. ct.setCzrq(new Date());
  774. ct.setDel("1");
  775. ct.setStatus(0);
  776. // 医生未读数量为1
  777. ct.setDoctorRead(1);
  778. // 患者未读数量为0
  779. ct.setPatientRead(0);
  780. // 添加咨询记录
  781. Consult consult = addConsult(ct.getPatient(), null, ct.getSymptoms(), ct.getImages(), ct.getType());
  782. // 设置咨询标识
  783. ct.setConsult(consult.getCode());
  784. // 保存医生咨询信息
  785. if (consultTeamDao.save(ct) == null) {
  786. throw new Exception("保存失败!");
  787. }
  788. // 添加咨询转发记录
  789. ConsultTeamDoctor cd = new ConsultTeamDoctor();
  790. cd.setConsult(consult.getCode());
  791. cd.setDel("1");
  792. cd.setCzrq(new Date());
  793. cd.setTo(ct.getDoctor());
  794. consultTeamDoctorDao.save(cd);
  795. // 添加医生咨询日志
  796. addLogs(ct);
  797. }
  798. /**
  799. * 根据健康管理师和患者 找到正在进行中的咨询
  800. *
  801. * @param patientCode
  802. * @param doctor
  803. */
  804. public ConsultTeam getConsultByPatientAndDoctor(String patientCode, String doctor) {
  805. ConsultTeam consultTeam = consultTeamDao.findByParientCodeAndSignTypeAndDoctor(patientCode, doctor, 2);
  806. return consultTeam;
  807. }
  808. public void transfers(String uid, String doctor, String consult) {
  809. String[] doctors = doctor.split(",");
  810. for (int i = 0; i < doctors.length; i++) {
  811. transfer(uid, doctors[i], consult);
  812. }
  813. }
  814. }