patient_dataconfig_api_controller.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/astaxie/beego"
  11. )
  12. func PatientDataConfigAPIControllerRegistRouters() {
  13. beego.Router("/api/patient/courses", &PatientDataConfigAPIController{}, "get:Courses")
  14. beego.Router("/api/patient/course/create", &PatientDataConfigAPIController{}, "post:CreateCourse")
  15. beego.Router("/api/patient/course/delete", &PatientDataConfigAPIController{}, "post:DeleteCourse")
  16. beego.Router("/api/patient/course/modify", &PatientDataConfigAPIController{}, "post:ModifyCourse")
  17. beego.Router("/api/patient/rescues", &PatientDataConfigAPIController{}, "get:Rescues")
  18. beego.Router("/api/patient/rescue/create", &PatientDataConfigAPIController{}, "post:CreateRescue")
  19. beego.Router("/api/patient/rescue/delete", &PatientDataConfigAPIController{}, "post:DeleteRescue")
  20. }
  21. type PatientDataConfigAPIController struct {
  22. BaseAuthAPIController
  23. }
  24. // /api/patient/courses [get]
  25. // @param patient_id:int
  26. // @param start_time:string (yyyy-MM-dd)
  27. // @param end_time:string (yyyy-MM-dd)
  28. func (this *PatientDataConfigAPIController) Courses() {
  29. patientID, _ := this.GetInt64("patient_id")
  30. startTimeYMDStr := this.GetString("start_time")
  31. endTimeYMDStr := this.GetString("end_time")
  32. if patientID <= 0 || len(startTimeYMDStr) == 0 || len(endTimeYMDStr) == 0 {
  33. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  34. return
  35. }
  36. endTimeYMDHmsStr := endTimeYMDStr + " 23:59:59"
  37. startTime, parseStartTimeErr := utils.ParseTimeStringToTime("2006-01-02", startTimeYMDStr)
  38. endTime, parseEndTimeErr := utils.ParseTimeStringToTime("2006-01-02 15:04:05", endTimeYMDHmsStr)
  39. if parseStartTimeErr != nil || parseEndTimeErr != nil {
  40. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  41. return
  42. }
  43. adminUserInfo := this.GetAdminUserInfo()
  44. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  45. if getPatientErr != nil {
  46. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  47. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  48. return
  49. } else if patient == nil {
  50. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  51. return
  52. }
  53. records, getRecordsErr := service.GetPatientCourseOfDisease(adminUserInfo.CurrentOrgId, patientID, startTime.Unix(), endTime.Unix())
  54. if getRecordsErr != nil {
  55. this.ErrorLog("获取患者病程记录失败:%v", getRecordsErr)
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  57. return
  58. }
  59. admins, getAllAdminsErr := service.GetAllAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  60. if getAllAdminsErr != nil {
  61. this.ErrorLog("获取所有管理员失败:%v", getAllAdminsErr)
  62. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  63. return
  64. }
  65. this.ServeSuccessJSON(map[string]interface{}{
  66. "records": records,
  67. "doctors": admins,
  68. })
  69. }
  70. // /api/patient/course/create [post]
  71. // @param patient_id:int
  72. // @param content:string
  73. func (this *PatientDataConfigAPIController) CreateCourse() {
  74. patientID, _ := this.GetInt64("patient_id")
  75. content := this.GetString("content")
  76. record_time_str := this.GetString("record_time")
  77. title := this.GetString("title")
  78. if patientID <= 0 || len(content) == 0 {
  79. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  80. return
  81. }
  82. if len(record_time_str) == 0 {
  83. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  84. return
  85. }
  86. checkDate, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", record_time_str)
  87. adminUserInfo := this.GetAdminUserInfo()
  88. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  89. if getPatientErr != nil {
  90. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  91. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  92. return
  93. } else if patient == nil {
  94. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  95. return
  96. }
  97. now := time.Now().Unix()
  98. record := models.PatientDiseaseCourse{
  99. OrgID: adminUserInfo.CurrentOrgId,
  100. PatientID: patientID,
  101. Recorder: adminUserInfo.AdminUser.Id,
  102. RecordTime: checkDate.Unix(),
  103. Content: content,
  104. Status: 1,
  105. CreateTime: now,
  106. ModifyTime: now,
  107. Title: title,
  108. }
  109. createErr := service.CreatePatientCourseOfDisease(&record)
  110. if createErr != nil {
  111. this.ErrorLog("创建患者病程记录失败:%v", createErr)
  112. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  113. return
  114. }
  115. this.ServeSuccessJSON(map[string]interface{}{
  116. "record": record,
  117. })
  118. }
  119. // /api/patient/course/delete
  120. // @param patient_id:int
  121. // @param ids:string 一个或多个record_id以逗号相隔 ("1,3,7")
  122. func (this *PatientDataConfigAPIController) DeleteCourse() {
  123. patientID, _ := this.GetInt64("patient_id")
  124. idsStr := this.GetString("ids")
  125. if patientID <= 0 || len(idsStr) == 0 {
  126. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  127. return
  128. }
  129. adminUserInfo := this.GetAdminUserInfo()
  130. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  131. if getPatientErr != nil {
  132. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  133. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  134. return
  135. } else if patient == nil {
  136. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  137. return
  138. }
  139. recordIDStrs := strings.Split(idsStr, ",")
  140. recordIDs := make([]int64, 0, len(recordIDStrs))
  141. for _, idStr := range recordIDStrs {
  142. id, parseErr := strconv.Atoi(idStr)
  143. if parseErr != nil {
  144. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  145. return
  146. }
  147. recordIDs = append(recordIDs, int64(id))
  148. }
  149. deleteErr := service.DeletePatientCoursesInBatch(adminUserInfo.CurrentOrgId, patientID, recordIDs)
  150. if deleteErr != nil {
  151. this.ErrorLog("删除患者病程记录失败:%v", deleteErr)
  152. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  153. return
  154. }
  155. this.ServeSuccessJSON(nil)
  156. }
  157. // /api/patient/rescues [get]
  158. // @param patient_id:int
  159. // @param start_time:string (yyyy-MM-dd)
  160. // @param end_time:string (yyyy-MM-dd)
  161. func (this *PatientDataConfigAPIController) Rescues() {
  162. patientID, _ := this.GetInt64("patient_id")
  163. startTimeYMDStr := this.GetString("start_time")
  164. endTimeYMDStr := this.GetString("end_time")
  165. if patientID <= 0 || len(startTimeYMDStr) == 0 || len(endTimeYMDStr) == 0 {
  166. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  167. return
  168. }
  169. endTimeYMDHmsStr := endTimeYMDStr + " 23:59:59"
  170. startTime, parseStartTimeErr := utils.ParseTimeStringToTime("2006-01-02", startTimeYMDStr)
  171. endTime, parseEndTimeErr := utils.ParseTimeStringToTime("2006-01-02 15:04:05", endTimeYMDHmsStr)
  172. if parseStartTimeErr != nil || parseEndTimeErr != nil {
  173. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  174. return
  175. }
  176. adminUserInfo := this.GetAdminUserInfo()
  177. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  178. if getPatientErr != nil {
  179. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  180. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  181. return
  182. } else if patient == nil {
  183. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  184. return
  185. }
  186. records, getRecordsErr := service.GetPatientRescueRecords(adminUserInfo.CurrentOrgId, patientID, startTime.Unix(), endTime.Unix())
  187. if getRecordsErr != nil {
  188. this.ErrorLog("获取患者抢救记录失败:%v", getRecordsErr)
  189. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  190. return
  191. }
  192. admins, getAllAdminsErr := service.GetAllAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  193. if getAllAdminsErr != nil {
  194. this.ErrorLog("获取所有管理员失败:%v", getAllAdminsErr)
  195. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  196. return
  197. }
  198. this.ServeSuccessJSON(map[string]interface{}{
  199. "records": records,
  200. "doctors": admins,
  201. })
  202. }
  203. // /api/patient/rescue/create [post]
  204. // @param patient_id:int
  205. // @param content:string
  206. func (this *PatientDataConfigAPIController) CreateRescue() {
  207. patientID, _ := this.GetInt64("patient_id")
  208. content := this.GetString("content")
  209. if patientID <= 0 || len(content) == 0 {
  210. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  211. return
  212. }
  213. adminUserInfo := this.GetAdminUserInfo()
  214. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  215. if getPatientErr != nil {
  216. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  217. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  218. return
  219. } else if patient == nil {
  220. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  221. return
  222. }
  223. now := time.Now().Unix()
  224. record := models.PatientRescueRecord{
  225. OrgID: adminUserInfo.CurrentOrgId,
  226. PatientID: patientID,
  227. Recorder: adminUserInfo.AdminUser.Id,
  228. RecordTime: now,
  229. Content: content,
  230. Status: 1,
  231. CreateTime: now,
  232. ModifyTime: now,
  233. }
  234. createErr := service.CreatePatientRescueRecord(&record)
  235. if createErr != nil {
  236. this.ErrorLog("创建患者抢救记录失败:%v", createErr)
  237. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  238. return
  239. }
  240. this.ServeSuccessJSON(map[string]interface{}{
  241. "record": record,
  242. })
  243. }
  244. // /api/patient/rescue/delete
  245. // @param patient_id:int
  246. // @param ids:string 一个或多个record_id以逗号相隔 ("1,3,7")
  247. func (this *PatientDataConfigAPIController) DeleteRescue() {
  248. patientID, _ := this.GetInt64("patient_id")
  249. idsStr := this.GetString("ids")
  250. if patientID <= 0 || len(idsStr) == 0 {
  251. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  252. return
  253. }
  254. adminUserInfo := this.GetAdminUserInfo()
  255. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  256. if getPatientErr != nil {
  257. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  258. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  259. return
  260. } else if patient == nil {
  261. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  262. return
  263. }
  264. recordIDStrs := strings.Split(idsStr, ",")
  265. recordIDs := make([]int64, 0, len(recordIDStrs))
  266. for _, idStr := range recordIDStrs {
  267. id, parseErr := strconv.Atoi(idStr)
  268. if parseErr != nil {
  269. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  270. return
  271. }
  272. recordIDs = append(recordIDs, int64(id))
  273. }
  274. deleteErr := service.DeletePatientResuceRecordsInBatch(adminUserInfo.CurrentOrgId, patientID, recordIDs)
  275. if deleteErr != nil {
  276. this.ErrorLog("删除患者抢救记录失败:%v", deleteErr)
  277. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  278. return
  279. }
  280. this.ServeSuccessJSON(nil)
  281. }
  282. func (this *PatientDataConfigAPIController) ModifyCourse() {
  283. patientID, _ := this.GetInt64("patient_id")
  284. content := this.GetString("content")
  285. id, _ := this.GetInt64("id", 0)
  286. record_time_str := this.GetString("record_time")
  287. title := this.GetString("title")
  288. if patientID <= 0 || len(content) == 0 || id == 0 || len(record_time_str) == 0 {
  289. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  290. return
  291. }
  292. checkDate, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", record_time_str)
  293. adminUserInfo := this.GetAdminUserInfo()
  294. patient, getPatientErr := service.GetPatientByID(adminUserInfo.CurrentOrgId, patientID)
  295. if getPatientErr != nil {
  296. this.ErrorLog("获取患者信息失败:%v", getPatientErr)
  297. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  298. return
  299. } else if patient == nil {
  300. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  301. return
  302. }
  303. now := time.Now().Unix()
  304. record := models.PatientDiseaseCourse{
  305. ID: id,
  306. OrgID: adminUserInfo.CurrentOrgId,
  307. PatientID: patientID,
  308. Recorder: adminUserInfo.AdminUser.Id,
  309. Content: content,
  310. Status: 1,
  311. CreateTime: now,
  312. ModifyTime: now,
  313. Title: title,
  314. RecordTime: checkDate.Unix(),
  315. }
  316. createErr := service.ModifyPatientCourses(&record)
  317. if createErr != nil {
  318. this.ErrorLog("创建患者抢救记录失败:%v", createErr)
  319. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  320. return
  321. }
  322. this.ServeSuccessJSON(map[string]interface{}{
  323. "record": record,
  324. })
  325. }