inspection_service.go 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "strings"
  6. "time"
  7. )
  8. func GetInspectionReference(orgId int64) (reference []*models.InspectionReference, err error) {
  9. err = readDb.Model(&models.InspectionReference{}).Where("(org_id=0 OR org_id=?) and status=1", orgId).Order("project_id").Find(&reference).Error
  10. return
  11. }
  12. func GetPatientInspectionProjectCount(orgId, patientId int64) (projectCounts []*models.UserInspectionProjectCount, err error) {
  13. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and status=1", patientId, orgId).Select("count(distinct inspect_date) as count, project_id, patient_id").Group("project_id").Scan(&projectCounts).Error
  14. return
  15. }
  16. func GetPatientInspectionByDate(orgId, patientId, date, projectId int64) (inspections []*models.Inspection, err error) {
  17. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id =? and inspect_date =? and status=1", patientId, orgId, projectId, date).Find(&inspections).Error
  18. return
  19. }
  20. func GetAdvices(orgid int64, patientid int64, recorddate int64) (advcie []*models.DoctorAdvice, err error) {
  21. err = XTReadDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and status =1", patientid, orgid, recorddate).Find(&advcie).Error
  22. return
  23. }
  24. func CreatePatientInspection(inspectins []models.Inspection) (err error) {
  25. if len(inspectins) == 0 {
  26. err = errors.New("Inspections Cant be nil.")
  27. return
  28. }
  29. thisSQL := "INSERT INTO xt_inspection (patient_id, org_id, project_id, item_id, item_name, project_name, inspect_type, inspect_value, inspect_date, status, created_time, updated_time) VALUES "
  30. insertParams := make([]string, 0)
  31. insertData := make([]interface{}, 0)
  32. for _, inspectin := range inspectins {
  33. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  34. insertData = append(insertData, inspectin.PatientId)
  35. insertData = append(insertData, inspectin.OrgId)
  36. insertData = append(insertData, inspectin.ProjectId)
  37. insertData = append(insertData, inspectin.ItemId)
  38. insertData = append(insertData, inspectin.ItemName)
  39. insertData = append(insertData, inspectin.ProjectName)
  40. insertData = append(insertData, inspectin.InspectType)
  41. insertData = append(insertData, inspectin.InspectValue)
  42. insertData = append(insertData, inspectin.InspectDate)
  43. insertData = append(insertData, 1)
  44. insertData = append(insertData, inspectin.CreatedTime)
  45. insertData = append(insertData, inspectin.UpdatedTime)
  46. }
  47. thisSQL += strings.Join(insertParams, ", ")
  48. err = writeDb.Exec(thisSQL, insertData...).Error
  49. return
  50. }
  51. func EditPatientInspection(add []models.Inspection, edit []models.Inspection, noDeleteIDs []int64, patientId, orgId, projectId, date int64) (err error) {
  52. if len(add) == 0 && len(edit) == 0 && len(noDeleteIDs) == 0 {
  53. err = errors.New(" Cant be nil.")
  54. return
  55. }
  56. tx := writeDb.Begin()
  57. if len(noDeleteIDs) > 0 {
  58. err = tx.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id =? and inspect_date =? and id NOT IN (?) and status=1", patientId, orgId, projectId, date, noDeleteIDs).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  59. if err != nil {
  60. tx.Rollback()
  61. return
  62. }
  63. }
  64. if len(edit) > 0 {
  65. for _, item := range edit {
  66. err = tx.Save(&item).Error
  67. if err != nil {
  68. tx.Rollback()
  69. return
  70. }
  71. }
  72. }
  73. if len(add) > 0 {
  74. thisSQL := "INSERT INTO xt_inspection (patient_id, org_id, project_id, item_id, item_name, project_name, inspect_type, inspect_value, inspect_date, status, created_time, updated_time) VALUES "
  75. insertParams := make([]string, 0)
  76. insertData := make([]interface{}, 0)
  77. for _, inspectin := range add {
  78. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  79. insertData = append(insertData, inspectin.PatientId)
  80. insertData = append(insertData, inspectin.OrgId)
  81. insertData = append(insertData, inspectin.ProjectId)
  82. insertData = append(insertData, inspectin.ItemId)
  83. insertData = append(insertData, inspectin.ItemName)
  84. insertData = append(insertData, inspectin.ProjectName)
  85. insertData = append(insertData, inspectin.InspectType)
  86. insertData = append(insertData, inspectin.InspectValue)
  87. insertData = append(insertData, inspectin.InspectDate)
  88. insertData = append(insertData, 1)
  89. insertData = append(insertData, inspectin.CreatedTime)
  90. insertData = append(insertData, inspectin.UpdatedTime)
  91. }
  92. thisSQL += strings.Join(insertParams, ", ")
  93. err = tx.Exec(thisSQL, insertData...).Error
  94. if err != nil {
  95. tx.Rollback()
  96. return
  97. }
  98. }
  99. tx.Commit()
  100. return
  101. }
  102. func DeletePatientInspection(orgId, patientId, projectId, date int64) (err error) {
  103. err = writeDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id =? and inspect_date =? and status=1", patientId, orgId, projectId, date).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  104. return
  105. }
  106. func GetPatientInspections(orgId, patientId, projectId, page int64) (inspections []*models.Inspection, total int64, date int64, err error) {
  107. var projectCount models.UserInspectionProjectCount
  108. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id=? and status=1", patientId, orgId, projectId).Select("count(distinct inspect_date) as count, project_id, patient_id").Scan(&projectCount).Error
  109. if err != nil {
  110. return
  111. }
  112. total = projectCount.Count
  113. if total == 0 || page > total {
  114. return
  115. }
  116. var Id models.InspectionDate
  117. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id=? and status=1", patientId, orgId, projectId).Select("inspect_date").Group("inspect_date").Order("inspect_date desc").Offset(page - 1).Limit(1).Scan(&Id).Error
  118. if err != nil {
  119. return
  120. }
  121. if Id.InspectDate == 0 {
  122. return
  123. }
  124. date = Id.InspectDate
  125. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  126. if err != nil {
  127. return
  128. }
  129. return
  130. }
  131. type DateViewModel struct {
  132. inspect_date int64 `gorm:"column:inspect_date" json:"inspect_date"`
  133. project_id int64 `gorm:"column:project_id" json:"project_id"`
  134. }
  135. func FindLastRecordDate(orgId int64, patientId int64, project_id int64) (inspections models.Inspection, err error) {
  136. err = readDb.Raw("SELECT max(inspect_date) as inspect_date,project_id FROM `xt_inspection` WHERE (status = 1 AND org_id = ? AND project_id = ? AND patient_id = ?) ORDER BY `xt_inspection`.`id` ASC LIMIT 1", orgId, project_id, patientId).Scan(&inspections).Error
  137. return
  138. }
  139. func UpDateInfectiousRecordTime(orgId int64, patientId int64, time int64, remind_cycle int64) (err error) {
  140. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgId, patientId).Updates(map[string]interface{}{"infectious_next_record_time": time, "remind_cycle": remind_cycle}).Error
  141. return
  142. }
  143. func GetAllPatientInspection(orgId, patientId, page int64, projectId int64, start_time int64, end_time int64) (inspections []*models.Inspection, date int64, err error) {
  144. //var projectCount models.UserInspectionProjectCount
  145. //err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id in (?) and status=1", patientId, orgId,projectId).Select("count(distinct inspect_date) as count, project_id, patient_id").Scan(&projectCount).Error
  146. //
  147. //if err != nil {
  148. // return
  149. //}
  150. //
  151. //total = projectCount.Count
  152. //if total == 0 || page > total {
  153. // return
  154. //}
  155. var Id models.InspectionDate
  156. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id=? and status=1 and inspect_date >= ? and inspect_date <= ?", patientId, orgId, projectId, start_time, end_time).Select("inspect_date").Group("inspect_date").Order("inspect_date desc").Offset(page - 1).Limit(1).Scan(&Id).Error
  157. if err != nil {
  158. return
  159. }
  160. if Id.InspectDate == 0 {
  161. return
  162. }
  163. date = Id.InspectDate
  164. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  165. if err != nil {
  166. return
  167. }
  168. return
  169. }
  170. func GetAllInspectionReference(orgId int64) (inspectionReference []*models.InspectionReference, err error) {
  171. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? AND status = 1", orgId).Find(&inspectionReference).Error
  172. return
  173. }