inspection_service.go 9.0KB

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