inspection_service.go 9.6KB

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