patient_dataconfig_api_controller.go 12KB

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