inspection_service.go 9.0KB

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