Patient.go 13 KB

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