ConsultController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package com.yihu.wlyy.web.patient.consult;
  2. import java.util.*;
  3. import com.yihu.wlyy.entity.*;
  4. import com.yihu.wlyy.entity.consult.ConsultTeam;
  5. import com.yihu.wlyy.entity.consult.ConsultTeamLog;
  6. import com.yihu.wlyy.entity.doctor.Doctor;
  7. import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import com.yihu.wlyy.service.app.consult.ConsultTeamService;
  18. import com.yihu.wlyy.service.app.consult.DoctorCommentService;
  19. import com.yihu.wlyy.service.app.statistics.DoctorStatisticsService;
  20. import com.yihu.wlyy.service.app.talk.TalkGroupService;
  21. import com.yihu.wlyy.service.common.account.DoctorService;
  22. import com.yihu.wlyy.task.DoctorStatisticsTask;
  23. import com.yihu.wlyy.task.PushMsgTask;
  24. import com.yihu.wlyy.util.CommonUtil;
  25. import com.yihu.wlyy.util.DateUtil;
  26. import com.yihu.wlyy.util.MessageType;
  27. import com.yihu.wlyy.web.WeixinBaseController;
  28. /**
  29. * 患者端:三师咨询控制类
  30. * @author George
  31. *
  32. */
  33. @Controller
  34. @RequestMapping(value = "/patient/consult")
  35. public class ConsultController extends WeixinBaseController {
  36. @Autowired
  37. private ConsultTeamService consultTeamService;
  38. @Autowired
  39. private DoctorCommentService doctorCommentService;
  40. @Autowired
  41. private DoctorStatisticsService doctorStatisticsService;
  42. @Autowired
  43. private DoctorService doctorService;
  44. @Autowired
  45. private TalkGroupService talkGroupService;
  46. @Autowired
  47. private DoctorWorkTimeService doctorWorkTimeService;
  48. /**
  49. * 患者咨询记录查询
  50. * @param id
  51. * @param pagesize 分页大小
  52. * @return
  53. */
  54. @RequestMapping(value = "records")
  55. @ResponseBody
  56. public String consultRecords(long id, int pagesize) {
  57. try {
  58. JSONArray array = new JSONArray();
  59. Page<Object> data = consultTeamService.findConsultRecordByPatient(getUID(), id, pagesize);
  60. if (data != null) {
  61. for (Object consult : data.getContent()) {
  62. if (consult == null) {
  63. continue;
  64. }
  65. Object[] result = (Object[])consult;
  66. JSONObject json = new JSONObject();
  67. json.put("id", result[0]);
  68. // 设置咨询类型:1三师咨询,2视频咨询,3图文咨询,4公共咨询,5病友圈
  69. json.put("type", result[1]);
  70. // 设置咨询标识
  71. json.put("code", result[2]);
  72. // 设置显示标题
  73. json.put("title", result[3]);
  74. // 设置主诉
  75. json.put("symptoms", result[4]);
  76. // 咨询状态
  77. json.put("status", result[6]);
  78. // 设置咨询日期
  79. json.put("czrq", DateUtil.dateToStrLong((Date)result[5]));
  80. array.put(json);
  81. }
  82. }
  83. return write(200, "查询成功!", "list", array);
  84. } catch (Exception e) {
  85. error(e);
  86. return error(-1, "查询失败!");
  87. }
  88. }
  89. /**
  90. * 获取医生的排班时间
  91. *
  92. * @param doctor
  93. * @param week
  94. * @return
  95. */
  96. @RequestMapping(value = "/doctor_worktime/week")
  97. @ResponseBody
  98. public String getDoctorWeekWorkTime(String doctor,String week){
  99. try{
  100. JSONObject result = doctorWorkTimeService.findDoctorWeekWorkTime(doctor,week);
  101. return write(200,"查询成功!","data",result);
  102. }catch (Exception e){
  103. e.printStackTrace();
  104. return error(-1,"查询失败!");
  105. }
  106. }
  107. /**
  108. * 获取医生某天的排班时间
  109. *
  110. * @param doctor
  111. * @return
  112. */
  113. @RequestMapping(value = "/doctor_worktime")
  114. @ResponseBody
  115. public String getDoctorWorkTime(String doctor){
  116. try{
  117. JSONObject result = doctorWorkTimeService.findDoctorWorkTime(doctor);
  118. return write(200,"查询成功!","data",result);
  119. }catch (Exception e){
  120. e.printStackTrace();
  121. return error(-1,"查询失败!");
  122. }
  123. }
  124. /**
  125. * 医生是否在工作
  126. *
  127. * @param doctor
  128. * @return
  129. */
  130. @RequestMapping(value = "is_doctor_working")
  131. @ResponseBody
  132. public String isDoctorAtWorking(String doctor){
  133. try{
  134. JSONObject result = doctorWorkTimeService.isDoctorWorking(doctor);
  135. return write(200,result.getString("msg"),"data",result.getString("status"));
  136. }catch (Exception e){
  137. e.printStackTrace();
  138. return error(-1,"查询失败");
  139. }
  140. }
  141. /**
  142. * 名医咨询剩余次数查询
  143. *
  144. * @param doctor
  145. * @return
  146. */
  147. @RequestMapping(value = "/consult_times_remain")
  148. @ResponseBody
  149. public String famousDoctorTimesRemain(String doctor){
  150. try{
  151. int result = doctorWorkTimeService.getDoctorConsultTimesRemain(doctor);
  152. return write(200,"查询成功","data",result);
  153. }catch (Exception e){
  154. e.printStackTrace();
  155. return error(-1,"查询失败");
  156. }
  157. }
  158. /**
  159. * 获取未完成咨询
  160. *
  161. * @return
  162. */
  163. @RequestMapping(value = "/unfinished")
  164. @ResponseBody
  165. public String getUnFinishedConsult(){
  166. try{
  167. List<ConsultTeam> unfinishedConsult = consultTeamService.getUnfinishedConsult(getUID());
  168. JSONArray result = new JSONArray(unfinishedConsult);
  169. return write(200,"查询成功!","data",result);
  170. }catch (Exception e){
  171. e.printStackTrace();
  172. return error(-1,"查询失败!");
  173. }
  174. }
  175. /**
  176. * 三师咨询添加接口
  177. * @param type 咨询类型:1三师咨询,2家庭医生咨询
  178. * @param when 发病时间
  179. * @param symptoms 主要症状
  180. * @param images 图片URL地址,多图以逗号分隔
  181. * @param voice 语音URL地址
  182. * @return
  183. */
  184. @RequestMapping(value = "add")
  185. @ResponseBody
  186. public String add(@RequestParam(required = false) Integer type,
  187. String when,
  188. String symptoms,
  189. @RequestParam(required = false) String images,
  190. @RequestParam(required = false) String voice) {
  191. try {
  192. if (type == null) {
  193. type = 1;
  194. }
  195. if (type != 1 && type != 2) {
  196. return error(-1, "无效请求!");
  197. }
  198. if (consultTeamService.exist(getUID(),type)) {
  199. return error(-1, "还有咨询未结束,不允许再次提交咨询!");
  200. }
  201. if (StringUtils.isEmpty(images)) {
  202. images = fetchWxImages();
  203. }
  204. // 将临时图片拷贝到正式存储路径下
  205. if (StringUtils.isNotEmpty(images)) {
  206. images = CommonUtil.copyTempImage(images);
  207. }
  208. if (StringUtils.isEmpty(voice)) {
  209. voice = fetchWxVoices();
  210. }
  211. if (StringUtils.isNotEmpty(voice)) {
  212. voice = CommonUtil.copyTempVoice(voice);
  213. }
  214. ConsultTeam consult = new ConsultTeam();
  215. // 设置咨询类型:1三师咨询,2家庭医生咨询
  216. consult.setType(type);
  217. // 设置发病时间
  218. consult.setWhen(when);
  219. // 设置主要症状
  220. consult.setSymptoms(symptoms);
  221. // 设置咨询图片URL
  222. consult.setImages(images);
  223. // 设置咨询语音URL
  224. consult.setVoice(voice);
  225. // 保存到数据库
  226. int res = consultTeamService.addTeamConsult(consult, getUID());
  227. if (res == -1) {
  228. return error(-1, "家庭签约信息不存在或已过期,无法进行家庭医生咨询!");
  229. } else if (res == -2) {
  230. return error(-1, "家庭签约信息不存在或已过期,无法进行三师医生咨询!");
  231. }
  232. Doctor doctor = doctorService.findDoctorByCode(consult.getDoctor());
  233. //创建咨询讨论组
  234. talkGroupService.createConsultTalkGroup(consult.getDoctor(), doctor.getName(), doctor.getHospital(), doctor.getHosptialName(),
  235. consult.getPatient(), consult.getName(),consult.getConsult());
  236. // 添加到统计队列
  237. if (consult.getType() == 2) {
  238. DoctorStatisticsTask.getInstance(doctorStatisticsService).put(consult.getDoctor(), 1, 1, 0);
  239. }
  240. // 推送消息给医生
  241. PushMsgTask.getInstance().put(consult.getDoctor(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM.D_CT_01.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM.指定咨询.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_CONSULT_TEAM.您有新的指定咨询.name(), consult.getConsult());
  242. return success("提交成功");
  243. } catch (Exception ex) {
  244. error(ex);
  245. return invalidUserException(ex, -1, "提交失败!");
  246. }
  247. }
  248. /**
  249. * 名医咨询添加接口
  250. * @param when 发病时间
  251. * @param symptoms 主要症状
  252. * @param images 图片URL地址,多图以逗号分隔
  253. * @param voice 语音URL地址
  254. * @param doctorCode 名医的code
  255. * @return
  256. */
  257. @RequestMapping(value = "famousAdd")
  258. @ResponseBody
  259. public String famousAdd(
  260. String when,
  261. String symptoms,
  262. @RequestParam(required = false) String doctorCode,
  263. @RequestParam(required = false) String images,
  264. @RequestParam(required = false) String voice) {
  265. try {
  266. if (StringUtils.isEmpty(images)) {
  267. images = fetchWxImages();
  268. }
  269. // 将临时图片拷贝到正式存储路径下
  270. if (StringUtils.isNotEmpty(images)) {
  271. images = CommonUtil.copyTempImage(images);
  272. }
  273. if (StringUtils.isEmpty(voice)) {
  274. voice = fetchWxVoices();
  275. }
  276. if (StringUtils.isNotEmpty(voice)) {
  277. voice = CommonUtil.copyTempVoice(voice);
  278. }
  279. //判断是否已经存在还没有关闭的名医咨询
  280. if(consultTeamService.isExistFamousConsult(getUID())){
  281. return error(-1, "已经存在名医咨询!");
  282. }
  283. ConsultTeam consult = new ConsultTeam();
  284. // 设置咨询类型:1三师咨询,2家庭医生咨询 3.名医咨询
  285. consult.setType(3);
  286. // 设置发病时间
  287. consult.setWhen(when);
  288. // 设置主要症状
  289. consult.setSymptoms(symptoms);
  290. // 设置咨询图片URL
  291. consult.setImages(images);
  292. // 设置咨询语音URL
  293. consult.setVoice(voice);
  294. consult.setDoctor(doctorCode);//设置专科医生
  295. // 保存到数据库
  296. consultTeamService.addFamousTeamConsult(consult, getUID());
  297. // 推送消息给医生
  298. PushMsgTask.getInstance().put(consult.getDoctor(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_FAMOUS_CONSULT_TEAM.D_CT_01.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_FAMOUS_CONSULT_TEAM.名医咨询.name(), MessageType.MESSAGE_TYPE_DOCTOR_NEW_FAMOUS_CONSULT_TEAM.您有新的名医咨询.name(), consult.getConsult());
  299. return success("提交成功");
  300. } catch (Exception ex) {
  301. error(ex);
  302. return invalidUserException(ex, -1, "提交失败!");
  303. }
  304. }
  305. @RequestMapping(value = "status")
  306. @ResponseBody
  307. public String status(String consult) {
  308. try {
  309. ConsultTeam ct = consultTeamService.findByCode(consult);
  310. if (ct == null) {
  311. return error(-1, "获取状态失败!");
  312. } else {
  313. return write(200, "获取状态成功!", "data", ct.getStatus());
  314. }
  315. } catch (Exception e) {
  316. error(e);
  317. return invalidUserException(e, -1, "获取状态失败!");
  318. }
  319. }
  320. /**
  321. * 查询患者三师咨询咨询列表
  322. * @param status 咨询状态(0未结束,1已结束,-1 已取消)
  323. * @param pagesize 页数
  324. * @return 查询结果
  325. */
  326. @RequestMapping(value = "list")
  327. @ResponseBody
  328. public String list(int status, long id, int pagesize) {
  329. try {
  330. Page<ConsultTeam> consults = consultTeamService.findByPatient(getUID(), status, id, pagesize);
  331. JSONArray jsonArray = new JSONArray();
  332. if (consults != null) {
  333. for (ConsultTeam consult : consults) {
  334. if (consult == null) {
  335. continue;
  336. }
  337. JSONObject json = new JSONObject();
  338. json.put("id", consult.getId());
  339. // 设置咨询标志
  340. json.put("code", consult.getConsult());
  341. // 设置咨询类型:0公共咨询,1指定医生,2三师咨询
  342. json.put("type", consult.getType());
  343. // 设置标题
  344. json.put("title", consult.getSymptoms());
  345. // 设置发病时间
  346. json.put("when", consult.getWhen());
  347. // 设置患者未读数量
  348. json.put("patientRead", consult.getPatientRead());
  349. // 设置状态
  350. json.put("status", consult.getStatus());
  351. // 设置患者评价标识
  352. json.put("comment", consult.getComment());
  353. // 设置咨询或回复时间
  354. json.put("time", DateUtil.dateToStr(consult.getCzrq(), DateUtil.YYYY_MM_DD_HH_MM_SS));
  355. jsonArray.put(json);
  356. }
  357. }
  358. return write(200, "查询成功", "list", jsonArray);
  359. } catch (Exception ex) {
  360. error(ex);
  361. return invalidUserException(ex, -1, "查询失败!");
  362. }
  363. }
  364. /**
  365. * 患者取消三师咨询
  366. * @param consult
  367. * @return
  368. */
  369. @RequestMapping(value = "cancel")
  370. @ResponseBody
  371. public String cancel(String consult) {
  372. try {
  373. int row = consultTeamService.cancel(consult);
  374. if (row > 0) {
  375. return success("咨询已取消!");
  376. } else {
  377. return error(-1, "咨询不能取消!");
  378. }
  379. } catch (Exception e) {
  380. error(e);
  381. return invalidUserException(e, -1, "操作失败!");
  382. }
  383. }
  384. /**
  385. * 修改状态为1的咨询记录为结束
  386. * @param code 咨询标识
  387. * @return
  388. */
  389. @RequestMapping(value = "finish")
  390. @ResponseBody
  391. public String finish(String code) {
  392. try {
  393. int row = consultTeamService.finish(code);
  394. if (row > 0) {
  395. return success("操作成功!");
  396. } else {
  397. return error(-1, "操作失败!");
  398. }
  399. } catch (Exception e) {
  400. error(e);
  401. return invalidUserException(e, -1, "操作失败!");
  402. }
  403. }
  404. /**
  405. * 三师咨询追问接口
  406. * @param consult 咨询标识
  407. * @param content 追问内容
  408. * @param type 追问内容类型:1文字,2图片,3语音
  409. * @return
  410. */
  411. @RequestMapping(value = "append")
  412. @ResponseBody
  413. public String append(String consult, String content, int type) {
  414. try {
  415. List<ConsultTeamLog> logs = new ArrayList<ConsultTeamLog>();
  416. if (type == 2) {
  417. // 图片消息
  418. if (StringUtils.isEmpty(content)) {
  419. content = fetchWxImages();
  420. }
  421. // 将临时图片拷贝到正式存储路径下
  422. if (StringUtils.isNotEmpty(content)) {
  423. content = CommonUtil.copyTempImage(content);
  424. }
  425. if (StringUtils.isEmpty(content)) {
  426. return error(-1, "图片上传失败!");
  427. }
  428. String[] images = content.split(",");
  429. for (String image : images) {
  430. ConsultTeamLog log = new ConsultTeamLog();
  431. log.setConsult(consult);
  432. log.setContent(image);
  433. log.setDel("1");
  434. log.setChatType(type);
  435. log.setType(2);
  436. logs.add(log);
  437. }
  438. } else {
  439. ConsultTeamLog log = new ConsultTeamLog();
  440. log.setConsult(consult);
  441. log.setContent(content);
  442. log.setDel("1");
  443. log.setChatType(type);
  444. log.setType(2);
  445. logs.add(log);
  446. }
  447. consultTeamService.reply(logs, getUID());
  448. return write(200, "追问成功!", "data", content);
  449. } catch (Exception e) {
  450. error(e);
  451. return invalidUserException(e, -1, "追问失败!");
  452. }
  453. }
  454. /**
  455. * 网络咨询咨询日志查询
  456. * @param consult 咨询标识
  457. * @param pagesize 每页显示数,默认为10
  458. * @return
  459. */
  460. @RequestMapping(value = "loglist")
  461. @ResponseBody
  462. public String loglist(String consult, long id, int pagesize) {
  463. try {
  464. ConsultTeam consultModel = consultTeamService.findByCode(consult);
  465. if (consultModel == null) {
  466. return error(-1, "咨询记录不存在!");
  467. }
  468. if (id <= 0) {
  469. // 更新患者未读数量为0
  470. consultTeamService.clearPatientRead(consult);
  471. }
  472. // 查询日志列表
  473. JSONArray jsonArray = new JSONArray();
  474. Page<ConsultTeamLog> list = consultTeamService.findLogByConsult(consult, id, pagesize);
  475. if (list != null) {
  476. for (ConsultTeamLog log : list) {
  477. if (consult == null) {
  478. continue;
  479. }
  480. JSONObject json = new JSONObject();
  481. json.put("id", log.getId());
  482. // 设置回复医生姓名
  483. json.put("doctorName", log.getDoctorName());
  484. // 设置回复内容
  485. json.put("content", log.getContent());
  486. // 设置咨询或回复时间
  487. json.put("time", DateUtil.dateToStr(log.getCzrq(), DateUtil.YYYY_MM_DD_HH_MM_SS));
  488. // 设置记录类型:1文字,2图片,3语音
  489. json.put("msgType", log.getChatType());
  490. // 设置类型:0患者问,1医生回复,2患者追问,3患者评价
  491. json.put("type", log.getType());
  492. jsonArray.put(json);
  493. }
  494. }
  495. // 返回结果
  496. return write(200, "查询成功", "list", jsonArray);
  497. } catch (Exception e) {
  498. error(e);
  499. return invalidUserException(e, -1, "查询失败!");
  500. }
  501. }
  502. /**
  503. * 查找单个咨询记录
  504. * @param consult 咨询标识
  505. * @param logId 记录标识
  506. *
  507. * @return
  508. */
  509. @RequestMapping(value = "oneLog")
  510. @ResponseBody
  511. public String oneLog(String consult,Long logId) {
  512. try {
  513. ConsultTeam consultModel = consultTeamService.findByCode(consult);
  514. if (consultModel == null) {
  515. return error(-1, "咨询记录不存在!");
  516. }
  517. // 查询日志列表
  518. JSONObject json = new JSONObject();
  519. ConsultTeamLog log = consultTeamService.oneLog(logId);
  520. if (log != null) {
  521. json.put("id", log.getId());
  522. // 设置回复医生姓名
  523. json.put("doctorName", log.getDoctorName());
  524. // 设置回复内容
  525. json.put("content", log.getContent());
  526. // 设置咨询或回复时间
  527. json.put("time", DateUtil.dateToStr(log.getCzrq(), DateUtil.YYYY_MM_DD_HH_MM_SS));
  528. // 设置记录类型:1文字,2图片,3语音
  529. json.put("msgType", log.getChatType());
  530. // 设置类型:0患者问,1医生回复,2患者追问,3患者评价
  531. json.put("type", log.getType());
  532. }
  533. // 返回结果
  534. return write(200, "查询成功", "consult", json);
  535. } catch (Exception e) {
  536. error(e);
  537. return invalidUserException(e, -1, "查询失败!");
  538. }
  539. }
  540. /**
  541. * 三师咨询评论
  542. * @param consult 咨询标识
  543. * @param content 评价内容
  544. * @param star 星级
  545. * @return 操作结果
  546. */
  547. @RequestMapping(value = "comment")
  548. @ResponseBody
  549. public String comment(String consult, String content, int star) {
  550. try {
  551. // 保存评价
  552. JSONArray array = doctorCommentService.consultComment(getUID(), consult, content, star, 2);
  553. // 添加到统计队列
  554. if (array != null) {
  555. DoctorStatisticsTask.getInstance(doctorStatisticsService).put(array);
  556. }
  557. // 添加评价记录
  558. ConsultTeamLog log = new ConsultTeamLog();
  559. log.setConsult(consult);
  560. log.setContent(content);
  561. log.setChatType(1);
  562. log.setDel("1");
  563. log.setType(3);
  564. log = consultTeamService.reply(log, getUID(), null, log.getType());
  565. return success("感谢您的评价!");
  566. } catch (Exception e) {
  567. error(e);
  568. return invalidUserException(e, -1, "评价失败!");
  569. }
  570. }
  571. }