inspection_service.go 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 CreatePatientInspection(inspectins []models.Inspection) (err error) {
  21. if len(inspectins) == 0 {
  22. err = errors.New("Inspections Cant be nil.")
  23. return
  24. }
  25. 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 "
  26. insertParams := make([]string, 0)
  27. insertData := make([]interface{}, 0)
  28. for _, inspectin := range inspectins {
  29. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  30. insertData = append(insertData, inspectin.PatientId)
  31. insertData = append(insertData, inspectin.OrgId)
  32. insertData = append(insertData, inspectin.ProjectId)
  33. insertData = append(insertData, inspectin.ItemId)
  34. insertData = append(insertData, inspectin.ItemName)
  35. insertData = append(insertData, inspectin.ProjectName)
  36. insertData = append(insertData, inspectin.InspectType)
  37. insertData = append(insertData, inspectin.InspectValue)
  38. insertData = append(insertData, inspectin.InspectDate)
  39. insertData = append(insertData, 1)
  40. insertData = append(insertData, inspectin.CreatedTime)
  41. insertData = append(insertData, inspectin.UpdatedTime)
  42. }
  43. thisSQL += strings.Join(insertParams, ", ")
  44. err = writeDb.Exec(thisSQL, insertData...).Error
  45. return
  46. }
  47. func EditPatientInspection(add []models.Inspection, edit []models.Inspection, noDeleteIDs []int64, patientId, orgId, projectId, date int64) (err error) {
  48. if len(add) == 0 && len(edit) == 0 && len(noDeleteIDs) == 0 {
  49. err = errors.New(" Cant be nil.")
  50. return
  51. }
  52. tx := writeDb.Begin()
  53. if len(noDeleteIDs) > 0 {
  54. 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
  55. if err != nil {
  56. tx.Rollback()
  57. return
  58. }
  59. }
  60. if len(edit) > 0 {
  61. for _, item := range edit {
  62. err = tx.Save(&item).Error
  63. if err != nil {
  64. tx.Rollback()
  65. return
  66. }
  67. }
  68. }
  69. if len(add) > 0 {
  70. 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 "
  71. insertParams := make([]string, 0)
  72. insertData := make([]interface{}, 0)
  73. for _, inspectin := range add {
  74. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  75. insertData = append(insertData, inspectin.PatientId)
  76. insertData = append(insertData, inspectin.OrgId)
  77. insertData = append(insertData, inspectin.ProjectId)
  78. insertData = append(insertData, inspectin.ItemId)
  79. insertData = append(insertData, inspectin.ItemName)
  80. insertData = append(insertData, inspectin.ProjectName)
  81. insertData = append(insertData, inspectin.InspectType)
  82. insertData = append(insertData, inspectin.InspectValue)
  83. insertData = append(insertData, inspectin.InspectDate)
  84. insertData = append(insertData, 1)
  85. insertData = append(insertData, inspectin.CreatedTime)
  86. insertData = append(insertData, inspectin.UpdatedTime)
  87. }
  88. thisSQL += strings.Join(insertParams, ", ")
  89. err = tx.Exec(thisSQL, insertData...).Error
  90. if err != nil {
  91. tx.Rollback()
  92. return
  93. }
  94. }
  95. tx.Commit()
  96. return
  97. }
  98. func DeletePatientInspection(orgId, patientId, projectId, date int64) (err error) {
  99. 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
  100. return
  101. }
  102. func GetPatientInspections(orgId, patientId, projectId, page int64) (inspections []*models.Inspection, total int64, date int64, err error) {
  103. var projectCount models.UserInspectionProjectCount
  104. 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
  105. if err != nil {
  106. return
  107. }
  108. total = projectCount.Count
  109. if total == 0 || page > total {
  110. return
  111. }
  112. var Id models.InspectionDate
  113. 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
  114. if err != nil {
  115. return
  116. }
  117. if Id.InspectDate == 0 {
  118. return
  119. }
  120. date = Id.InspectDate
  121. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  122. if err != nil {
  123. return
  124. }
  125. return
  126. }
  127. type DateViewModel struct {
  128. inspect_date int64 `gorm:"column:inspect_date" json:"inspect_date"`
  129. project_id int64 `gorm:"column:project_id" json:"project_id"`
  130. }
  131. func FindLastRecordDate(orgId int64, patientId int64, project_id int64) (inspections models.Inspection, err error) {
  132. 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
  133. return
  134. }
  135. func UpDateInfectiousRecordTime(orgId int64, patientId int64, time int64, remind_cycle int64) (err error) {
  136. 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
  137. return
  138. }
  139. func GetAllPatientInspection(orgId, patientId, page int64, projectId int64, start_time int64, end_time int64) (inspections []*models.Inspection, date int64, err error) {
  140. //var projectCount models.UserInspectionProjectCount
  141. //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
  142. //
  143. //if err != nil {
  144. // return
  145. //}
  146. //
  147. //total = projectCount.Count
  148. //if total == 0 || page > total {
  149. // return
  150. //}
  151. var Id models.InspectionDate
  152. 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
  153. if err != nil {
  154. return
  155. }
  156. if Id.InspectDate == 0 {
  157. return
  158. }
  159. date = Id.InspectDate
  160. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  161. if err != nil {
  162. return
  163. }
  164. return
  165. }
  166. func GetAllInspectionReference(orgId int64) (inspectionReference []*models.InspectionReference, err error) {
  167. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? AND status = 1", orgId).Find(&inspectionReference).Error
  168. return
  169. }