inspection_service.go 8.6KB

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