patient_dataconfig_api_controller.go 12KB

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