Patient.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package controllers
  2. import (
  3. "FollowUp_Notice/conf"
  4. "FollowUp_Notice/http"
  5. "FollowUp_Notice/lib"
  6. "FollowUp_Notice/logs"
  7. "FollowUp_Notice/models/Account"
  8. "FollowUp_Notice/models/Illness"
  9. "FollowUp_Notice/models/Patient"
  10. "FollowUp_Notice/models/Surgical"
  11. "FollowUp_Notice/models/System"
  12. "FollowUp_Notice/models/Tag"
  13. "encoding/json"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/robfig/cron/v3"
  16. "math"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type PatientController struct {
  22. beego.Controller
  23. User Account.User
  24. }
  25. func (c *PatientController) Prepare() {
  26. if Account.User_r != nil {
  27. c.User = *Account.User_r
  28. }
  29. }
  30. // 添加患者信息
  31. func (c *PatientController) Patient_List() {
  32. // 分页参数 初始化
  33. page, _ := c.GetInt("page")
  34. if page < 1 {
  35. page = 1
  36. }
  37. page_z, _ := c.GetInt("page_z")
  38. if page_z < 1 {
  39. page_z = conf.Page_size
  40. }
  41. // 病历号
  42. T_number := c.GetString("T_number")
  43. // 姓名
  44. T_name := c.GetString("T_name")
  45. // 标签
  46. T_tag, _ := c.GetInt("T_tag")
  47. // 诊断
  48. T_illness, _ := c.GetInt("T_illness")
  49. // 术式
  50. T_surgical, _ := c.GetInt("T_surgical")
  51. // 通知状态 1待通知 2已通知
  52. T_notice, _ := c.GetInt("T_notice")
  53. // 复诊状态 1正常 2超时
  54. T_follow_up, _ := c.GetInt("T_follow_up")
  55. // 年龄排序 1-升序 2降序
  56. T_age_sort, _ := c.GetInt("T_age_sort")
  57. // 下次复诊时间 1-升序 2降序
  58. T_next_time_sort, _ := c.GetInt("T_next_time_sort")
  59. Tag.Read_Tag_All_Map()
  60. Illness.Read_Illness_All_Map()
  61. Surgical.Read_Surgical_All_Map()
  62. R_List, R_cnt := Patient.Read_Patient_List(c.User.Id, T_number, T_name, T_tag, T_illness, T_surgical, T_notice, T_follow_up, T_age_sort, T_next_time_sort, page, page_z)
  63. var r_jsons lib.R_JSONS
  64. r_jsons.Num = R_cnt
  65. r_jsons.Data = R_List
  66. r_jsons.Page = page
  67. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  68. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  69. c.ServeJSON()
  70. return
  71. }
  72. // 添加患者信息
  73. func (c *PatientController) Patient_Add() {
  74. T_number := c.GetString("T_number")
  75. T_name := c.GetString("T_name")
  76. T_age, _ := c.GetInt("T_age")
  77. T_tag := c.GetString("T_tag")
  78. T_illness, _ := c.GetInt("T_illness")
  79. T_surgical, _ := c.GetInt("T_surgical")
  80. T_phone := c.GetString("T_phone")
  81. T_notice_phone, _ := c.GetInt("T_notice_phone")
  82. T_notice_message, _ := c.GetInt("T_notice_message")
  83. T_notice_interval := c.GetString("T_notice_interval")
  84. T_record := c.GetString("T_record")
  85. var_ := Patient.Patient{
  86. T_uid: c.User.Id,
  87. T_number: T_number,
  88. T_name: T_name,
  89. T_age: T_age,
  90. T_tag: T_tag,
  91. T_illness: T_illness,
  92. T_surgical: T_surgical,
  93. T_phone: T_phone,
  94. T_notice_phone: T_notice_phone,
  95. T_notice_message: T_notice_message,
  96. T_notice_interval: T_notice_interval,
  97. T_record: T_record,
  98. T_State: 1,
  99. }
  100. T_record_list := strings.Split(strings.Trim(T_record, "|"), "|")
  101. var T_time string // 复诊时间
  102. var T_interval int // 复诊间隔
  103. if len(T_record_list) > 0 {
  104. temp := T_record_list[len(T_record_list)-1]
  105. T_time = strings.Split(temp, ",")[0]
  106. T_interval, _ = strconv.Atoi(strings.Split(temp, ",")[1])
  107. t, _ := lib.DateStrToTime(T_time)
  108. nextTime := t.AddDate(0, 0, T_interval)
  109. // 复诊状态 1正常 2超时 3结束
  110. var_.T_follow_up = 1
  111. if nextTime.Before(time.Now()) {
  112. var_.T_follow_up = 2
  113. }
  114. if T_interval == 0 {
  115. var_.T_follow_up = 3
  116. }
  117. var_.T_next_time = nextTime.Format("2006-01-02")
  118. var_.T_notice = 1
  119. }
  120. _, err := Patient.Add_Patient(var_)
  121. if err != nil {
  122. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. System.Add_UserLogs_T(c.User.T_uuid, "患者", "新增", var_)
  127. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  128. c.ServeJSON()
  129. return
  130. }
  131. // 修改患者信息
  132. func (c *PatientController) Patient_Edit() {
  133. T_uuid := c.GetString("T_uuid")
  134. T_number := c.GetString("T_number")
  135. T_name := c.GetString("T_name")
  136. T_age, _ := c.GetInt("T_age")
  137. T_tag := c.GetString("T_tag")
  138. T_illness, _ := c.GetInt("T_illness")
  139. T_surgical, _ := c.GetInt("T_surgical")
  140. T_phone := c.GetString("T_phone")
  141. T_notice_phone, _ := c.GetInt("T_notice_phone")
  142. T_notice_message, _ := c.GetInt("T_notice_message")
  143. T_notice_interval := c.GetString("T_notice_interval")
  144. T_record := c.GetString("T_record")
  145. var err error
  146. var patient Patient.Patient
  147. if len(T_uuid) == 0 {
  148. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. patient, err = Patient.Read_Patient_ByT_uuid(T_uuid)
  153. if err != nil {
  154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  155. c.ServeJSON()
  156. return
  157. }
  158. var cols []string
  159. if len(T_number) > 0 {
  160. patient.T_number = T_number
  161. cols = append(cols, "T_number")
  162. }
  163. if len(T_name) > 0 {
  164. patient.T_name = T_name
  165. cols = append(cols, "T_name")
  166. }
  167. if T_age > 0 {
  168. patient.T_age = T_age
  169. cols = append(cols, "T_age")
  170. }
  171. patient.T_tag = T_tag
  172. cols = append(cols, "T_tag")
  173. patient.T_illness = T_illness
  174. cols = append(cols, "T_illness")
  175. patient.T_surgical = T_surgical
  176. cols = append(cols, "T_surgical")
  177. if len(T_phone) > 0 {
  178. patient.T_phone = T_phone
  179. cols = append(cols, "T_phone")
  180. }
  181. patient.T_notice_phone = T_notice_phone
  182. cols = append(cols, "T_notice_phone")
  183. patient.T_notice_message = T_notice_message
  184. cols = append(cols, "T_notice_message")
  185. if len(T_notice_interval) > 0 {
  186. patient.T_notice_interval = T_notice_interval
  187. cols = append(cols, "T_notice_interval")
  188. }
  189. if len(T_record) > 0 {
  190. patient.T_record = T_record
  191. cols = append(cols, "T_record")
  192. }
  193. T_record_list := strings.Split(strings.Trim(T_record, "|"), "|")
  194. var T_time string // 复诊时间
  195. var T_interval int // 复诊间隔
  196. if len(T_record_list) > 0 {
  197. temp := T_record_list[len(T_record_list)-1]
  198. T_time = strings.Split(temp, ",")[0]
  199. T_interval, _ = strconv.Atoi(strings.Split(temp, ",")[1])
  200. t, _ := lib.DateStrToTime(T_time)
  201. nextTime := t.AddDate(0, 0, T_interval)
  202. nextTimeStr := nextTime.Format("2006-01-02")
  203. if nextTimeStr != patient.T_next_time {
  204. patient.T_follow_up = 1
  205. if nextTime.Before(time.Now()) {
  206. patient.T_follow_up = 2
  207. }
  208. if T_interval == 0 {
  209. patient.T_follow_up = 3
  210. }
  211. patient.T_next_time = nextTimeStr
  212. patient.T_notice = 1
  213. }
  214. }
  215. cols = append(cols, "T_next_time", "T_notice", "T_follow_up")
  216. if err = Patient.Update_Patient(patient, cols...); err != nil {
  217. c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
  218. c.ServeJSON()
  219. return
  220. }
  221. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", patient)
  222. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  223. c.ServeJSON()
  224. return
  225. }
  226. // 删除患者信息
  227. func (c *PatientController) Patient_Del() {
  228. T_uuid := c.GetString("T_uuid")
  229. if len(T_uuid) == 0 {
  230. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  231. c.ServeJSON()
  232. return
  233. }
  234. patient, err := Patient.Read_Patient_ByT_uuid(T_uuid)
  235. if err != nil {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  237. c.ServeJSON()
  238. return
  239. }
  240. if c.User.Id != 1 || c.User.Id != patient.T_uid {
  241. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. if err = Patient.Delete_Patient(patient); err != nil {
  246. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  247. c.ServeJSON()
  248. return
  249. }
  250. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  251. c.ServeJSON()
  252. return
  253. }
  254. func (c *PatientController) VoiceCall_Status() {
  255. // {"eventType":"callout","statusInfo":{"sessionId":"1201_14852_4294967295_20230529072219@callenabler245.huaweicaas.com","timestamp":"2023-05-29 07:22:19","caller":"+8651668971369","called":"+8618086869080"}}
  256. logs.Println("VoiceCall RequestBody-", string(c.Ctx.Input.RequestBody))
  257. type RequestBody struct {
  258. EventType string `json:"eventType"`
  259. StatusInfo struct {
  260. SessionId string `json:"sessionId"`
  261. Timestamp string `json:"timestamp"`
  262. Caller string `json:"caller"`
  263. Called string `json:"called"`
  264. StateCode int `json:"stateCode"`
  265. StateDesc string `json:"stateDesc"`
  266. } `json:"statusInfo"`
  267. }
  268. var body RequestBody
  269. data := c.Ctx.Input.RequestBody
  270. err := json.Unmarshal(data, &body)
  271. if err != nil {
  272. c.Data["json"] = lib.JSONS{Code: 202, Msg: "json.Unmarshal is err:" + err.Error()}
  273. c.ServeJSON()
  274. }
  275. if body.EventType == "disconnect" {
  276. r, err := Patient.Read_PatientSend_ByT_id(body.StatusInfo.SessionId)
  277. if err != nil {
  278. c.Data["json"] = lib.JSONS{Code: 202, Msg: "SessionId Err!"}
  279. c.ServeJSON()
  280. return
  281. }
  282. if body.StatusInfo.StateCode == 0 {
  283. r.T_State = 1
  284. }
  285. r.T_code = body.StatusInfo.StateCode
  286. r.T_remark = body.StatusInfo.StateDesc
  287. err = Patient.Update_PatientSend(r, "T_State", "T_remark", "T_code")
  288. if err != nil {
  289. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改发送状态失败!"}
  290. c.ServeJSON()
  291. return
  292. }
  293. }
  294. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  295. c.ServeJSON()
  296. return
  297. }
  298. func Cron_Patient() {
  299. //创建一个定时任务对象
  300. c := cron.New(cron.WithSeconds())
  301. //给对象增加定时任务
  302. //c.AddFunc("0 */1 * * * ?", Cron_Patient_ChangeFollowUp)
  303. c.AddFunc("@daily", Cron_Patient_ChangeFollowUp) // 修改复诊状态
  304. c.AddFunc("0 0 12 * * *", Cron_Patient_Notice) // 消息通知
  305. //启动定时任务
  306. c.Start()
  307. defer c.Stop()
  308. //查询语句,阻塞,让main函数不退出,保持程序运行
  309. select {}
  310. }
  311. // 修改患者复诊状态 每晚0点统计
  312. // 已通知并且下次复诊时间超过当期时间 已过期
  313. func Cron_Patient_ChangeFollowUp() {
  314. T_date := time.Now().Format("2006-01-02")
  315. logs.Info("开始处理" + T_date + "患者通知状态")
  316. // T_notice 通知状态 1待通知 2已通知
  317. // T_follow_up 复诊状态 1正常 2超时
  318. list, _ := Patient.Read_Patient_List(0, "", "", 0, 0, 0, 2, 1, 0, 0, 0, 9999)
  319. for _, v := range list {
  320. nextTime, _ := lib.DateStrToTime(v.T_next_time)
  321. if nextTime.Before(time.Now()) {
  322. var_ := Patient.Patient{Id: v.Id, T_follow_up: 2}
  323. if err := Patient.Update_Patient(var_, "T_follow_up"); err != nil {
  324. System.Add_SysLogs_T("复诊状态", "修改失败", var_)
  325. }
  326. }
  327. }
  328. }
  329. // 给患者发送消息提醒 每天8点
  330. // 发送成功将通知状态修改未已通知H
  331. func Cron_Patient_Notice() {
  332. T_date := time.Now().Format("2006-01-02")
  333. logs.Info("开始发送" + T_date + "患者通知")
  334. // T_notice 通知状态 1待通知 2已通知
  335. // T_follow_up 复诊状态 1正常 2超时 3结束
  336. now := time.Now()
  337. nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  338. userList, _ := Account.Read_User_List("", 0, 9999)
  339. for _, user := range userList {
  340. // T_arrears_notice 欠费通知 1继续通知 2终止通知
  341. // T_State 0删除 1启用 2停用
  342. // 欠费终止通知或者停用后,不再继续发送通知
  343. if (user.T_money <= 0 && user.T_arrears_notice == 2) || user.T_State == 2 {
  344. continue
  345. }
  346. patientList, _ := Patient.Read_Patient_List(user.Id, "", "", 0, 0, 0, 0, 1, 0, 0, 0, 9999)
  347. for _, v := range patientList {
  348. nextTime, _ := lib.DateStrToTime(v.T_next_time)
  349. var success bool
  350. // 提前1天
  351. if strings.Contains(v.T_notice_interval, "1") && nextTime.AddDate(0, 0, -1) == nowDate {
  352. success = Send_Notice(nextTime, user, v)
  353. }
  354. // 提前2天
  355. if strings.Contains(v.T_notice_interval, "2") && nextTime.AddDate(0, 0, -2) == nowDate {
  356. success = Send_Notice(nextTime, user, v)
  357. }
  358. // 提前3天
  359. if strings.Contains(v.T_notice_interval, "3") && nextTime.AddDate(0, 0, -3) == nowDate {
  360. success = Send_Notice(nextTime, user, v)
  361. }
  362. // 提前7天
  363. if strings.Contains(v.T_notice_interval, "7") && nextTime.AddDate(0, 0, -7) == nowDate {
  364. success = Send_Notice(nextTime, user, v)
  365. }
  366. if success == true {
  367. // 通知状态 1待通知 2已通知
  368. var_ := Patient.Patient{Id: v.Id, T_notice: 2}
  369. if err := Patient.Update_Patient(var_, "T_notice"); err != nil {
  370. System.Add_SysLogs_T("复诊通知", "修改失败", var_)
  371. }
  372. }
  373. }
  374. }
  375. }
  376. // 发送通知(短信、电话)
  377. func Send_Notice(nextTime time.Time, user Account.User_R, patient Patient.Patient_R) (Success bool) {
  378. //发送短信通知
  379. if patient.T_notice_message == 1 {
  380. res, err := http.SmsXSend(user.T_template_id, patient.T_phone, patient.T_name, nextTime.Format("2006年01月02日"))
  381. if err != nil {
  382. System.Add_SysLogs_T("复诊通知", "短信通知失败", patient)
  383. }
  384. // 保存短信发送记录
  385. smsSend := Patient.PatientSend{
  386. T_uid: user.Id,
  387. T_pid: patient.Id,
  388. T_phone: patient.T_phone,
  389. T_type: 1,
  390. T_id: res.Send_id,
  391. T_remark: res.Status,
  392. T_code: res.Fee,
  393. T_State: 1,
  394. }
  395. if res.Status == "error" {
  396. smsSend.T_State = 0
  397. }
  398. if res.Status == "success" {
  399. Success = true
  400. }
  401. _, err = Patient.Add_PatientSend(smsSend)
  402. if err != nil {
  403. System.Add_SysLogs_T("复诊通知", "添加发送记录失败", patient)
  404. }
  405. }
  406. if patient.T_notice_phone == 1 {
  407. playInfoList := http.GetPlayInfoList(conf.VoiceCall_Template, []string{user.T_user, patient.T_name, nextTime.Format("2006/01/02")})
  408. res, err := http.VoiceNotifyAPI(conf.VoiceCall_Phone, "+86"+patient.T_phone, playInfoList)
  409. if err != nil {
  410. System.Add_SysLogs_T("复诊通知", "电话通知失败", patient)
  411. }
  412. // 保存短信发送记录
  413. smsSend := Patient.PatientSend{
  414. T_uid: user.Id,
  415. T_pid: patient.Id,
  416. T_phone: patient.T_phone,
  417. T_type: 2,
  418. T_id: res.SessionId,
  419. T_remark: res.Resultdesc,
  420. T_State: 0,
  421. }
  422. if res.Resultcode != "0" {
  423. smsSend.T_State = 0
  424. }
  425. if res.Resultcode == "0" {
  426. Success = true
  427. }
  428. _, err = Patient.Add_PatientSend(smsSend)
  429. if err != nil {
  430. System.Add_SysLogs_T("复诊通知", "添加发送记录失败", patient)
  431. }
  432. }
  433. return Success
  434. }