inspection_service.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "strings"
  6. "time"
  7. )
  8. func GetInspectionReference(orgId int64, inspect_type int64) (reference []*models.InspectionReference, err error) {
  9. if inspect_type == 0 {
  10. var count int
  11. err = readDb.Model(&models.InspectionReference{}).Where("org_id=? and status=1 and inspection_type = 0", orgId).Count(&count).Error
  12. if count > 0 {
  13. err = readDb.Model(&models.InspectionReference{}).Where("org_id=? and status=1 and inspection_type = 0", orgId).Order("project_id,created_time").Find(&reference).Error
  14. } else {
  15. err = readDb.Model(&models.InspectionReference{}).Where("org_id=0 and status=1 and inspection_type = 0").Order("project_id,created_time").Find(&reference).Error
  16. }
  17. } else {
  18. var count int
  19. err = readDb.Model(&models.InspectionReference{}).Where("org_id=? and status=1 and inspection_type = ?", orgId, inspect_type).Count(&count).Error
  20. if count > 0 {
  21. err = readDb.Model(&models.InspectionReference{}).Where("org_id=? and status=1 and inspection_type = ?", orgId, inspect_type).Order("project_id,created_time").Find(&reference).Error
  22. } else {
  23. err = readDb.Model(&models.InspectionReference{}).Where("org_id=0 and status=1 and inspection_type = ?", inspect_type).Order("project_id,created_time").Find(&reference).Error
  24. }
  25. }
  26. return
  27. }
  28. func GetPatientInspectionProjectCount(orgId, patientId int64) (projectCounts []*models.UserInspectionProjectCount, err error) {
  29. 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
  30. return
  31. }
  32. func GetPatientInspectionByDate(orgId, patientId, date, projectId int64) (inspections []*models.Inspection, err error) {
  33. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and org_id=? and project_id =? and inspect_date =? and status=1", patientId, orgId, projectId, date).Order("created_time desc").Find(&inspections).Error
  34. return
  35. }
  36. func GetAdvices(orgid int64, patientid int64, recorddate int64) (advcie []*models.DoctorAdvice, err error) {
  37. err = XTReadDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and status =1", patientid, orgid, recorddate).Find(&advcie).Error
  38. return
  39. }
  40. func GetAdvicesByPrescription(orgid int64, patientid int64, recorddate int64) (models.DoctorAdvice, error) {
  41. advice := models.DoctorAdvice{}
  42. 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
  43. return advice, err
  44. }
  45. func UpdateDoctorAdvieById(id int64, prescription_number float64) (models.DoctorAdvice, error) {
  46. advice := models.DoctorAdvice{}
  47. err := XTWriteDB().Model(&advice).Where("id=? and status= 1", id).Updates(map[string]interface{}{"prescribing_number": prescription_number}).Error
  48. return advice, err
  49. }
  50. func UpdateDoctorAdvieByIdOne(id int64, prescription_number float64, single_dose float64) (models.DoctorAdvice, error) {
  51. advice := models.DoctorAdvice{}
  52. err := XTWriteDB().Model(&advice).Where("id=? and status= 1 ", id).Updates(map[string]interface{}{"prescribing_number": prescription_number, "single_dose": single_dose}).Error
  53. return advice, err
  54. }
  55. func CreatePatientInspection(inspectins []models.Inspection) (err error) {
  56. if len(inspectins) == 0 {
  57. err = errors.New("Inspections Cant be nil.")
  58. return
  59. }
  60. 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 "
  61. insertParams := make([]string, 0)
  62. insertData := make([]interface{}, 0)
  63. for _, inspectin := range inspectins {
  64. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  65. insertData = append(insertData, inspectin.PatientId)
  66. insertData = append(insertData, inspectin.OrgId)
  67. insertData = append(insertData, inspectin.ProjectId)
  68. insertData = append(insertData, inspectin.ItemId)
  69. insertData = append(insertData, inspectin.ItemName)
  70. insertData = append(insertData, inspectin.ProjectName)
  71. insertData = append(insertData, inspectin.InspectType)
  72. insertData = append(insertData, inspectin.InspectValue)
  73. insertData = append(insertData, inspectin.InspectDate)
  74. insertData = append(insertData, 1)
  75. insertData = append(insertData, inspectin.CreatedTime)
  76. insertData = append(insertData, inspectin.UpdatedTime)
  77. }
  78. thisSQL += strings.Join(insertParams, ", ")
  79. err = writeDb.Exec(thisSQL, insertData...).Error
  80. return
  81. }
  82. func SavePatientInspection(inspectin models.Inspection) (err error) {
  83. err = writeDb.Save(&inspectin).Error
  84. return
  85. }
  86. func DeleteInspectionTwo(id int64) {
  87. err = writeDb.Model(&models.Inspection{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  88. return
  89. }
  90. func EditPatientInspection(add []models.Inspection, edit []models.Inspection, noDeleteIDs []int64, patientId, orgId, projectId, date int64) (err error) {
  91. if len(add) == 0 && len(edit) == 0 && len(noDeleteIDs) == 0 {
  92. err = errors.New(" Cant be nil.")
  93. return
  94. }
  95. tx := writeDb.Begin()
  96. if len(noDeleteIDs) > 0 {
  97. 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
  98. if err != nil {
  99. tx.Rollback()
  100. return
  101. }
  102. }
  103. if len(edit) > 0 {
  104. for _, item := range edit {
  105. err = tx.Save(&item).Error
  106. if err != nil {
  107. tx.Rollback()
  108. return
  109. }
  110. }
  111. }
  112. if len(add) > 0 {
  113. 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 "
  114. insertParams := make([]string, 0)
  115. insertData := make([]interface{}, 0)
  116. for _, inspectin := range add {
  117. insertParams = append(insertParams, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  118. insertData = append(insertData, inspectin.PatientId)
  119. insertData = append(insertData, inspectin.OrgId)
  120. insertData = append(insertData, inspectin.ProjectId)
  121. insertData = append(insertData, inspectin.ItemId)
  122. insertData = append(insertData, inspectin.ItemName)
  123. insertData = append(insertData, inspectin.ProjectName)
  124. insertData = append(insertData, inspectin.InspectType)
  125. insertData = append(insertData, inspectin.InspectValue)
  126. insertData = append(insertData, inspectin.InspectDate)
  127. insertData = append(insertData, 1)
  128. insertData = append(insertData, inspectin.CreatedTime)
  129. insertData = append(insertData, inspectin.UpdatedTime)
  130. }
  131. thisSQL += strings.Join(insertParams, ", ")
  132. err = tx.Exec(thisSQL, insertData...).Error
  133. if err != nil {
  134. tx.Rollback()
  135. return
  136. }
  137. }
  138. tx.Commit()
  139. return
  140. }
  141. func DeletePatientInspection(orgId, patientId, projectId, date int64) (err error) {
  142. 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
  143. return
  144. }
  145. func GetPatientInspections(orgId, patientId, projectId, page int64) (inspections []*models.Inspection, total int64, date int64, err error) {
  146. var projectCount models.UserInspectionProjectCount
  147. 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").Order("inspect_date desc").Scan(&projectCount).Error
  148. if err != nil {
  149. return
  150. }
  151. total = projectCount.Count
  152. if total == 0 || page > total {
  153. return
  154. }
  155. var Id models.InspectionDate
  156. 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
  157. if err != nil {
  158. return
  159. }
  160. if Id.InspectDate == 0 {
  161. return
  162. }
  163. date = Id.InspectDate
  164. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  165. if err != nil {
  166. return
  167. }
  168. return
  169. }
  170. type DateViewModel struct {
  171. inspect_date int64 `gorm:"column:inspect_date" json:"inspect_date"`
  172. project_id int64 `gorm:"column:project_id" json:"project_id"`
  173. }
  174. func FindLastRecordDate(orgId int64, patientId int64, project_id int64) (inspections models.Inspection, err error) {
  175. 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
  176. return
  177. }
  178. func UpDateInfectiousRecordTime(orgId int64, patientId int64, time int64, remind_cycle int64) (err error) {
  179. 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
  180. return
  181. }
  182. func CreateRemind(remind models.XtCheckRemind) (err error) {
  183. err = writeDb.Create(&remind).Error
  184. return
  185. }
  186. func UpDateCheckRemindRecordTime(orgId int64, patientId int64, time int64, project_id int64, remind_cycle int64) (err error) {
  187. err = writeDb.Model(&models.XtCheckRemind{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? and project_id = ?", orgId, patientId, project_id).Updates(map[string]interface{}{"last_remind_date": time, "remind_cycle": remind_cycle}).Error
  188. return
  189. }
  190. func UpDateCheckRemindRecordTimeTwo(orgId int64, patientId int64, time int64, project_id int64) (err error) {
  191. err = writeDb.Model(&models.XtCheckRemind{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? and project_id = ?", orgId, patientId, project_id).Updates(map[string]interface{}{"last_remind_date": time}).Error
  192. return
  193. }
  194. func GetCheckRemindRecordTime(orgId int64, patientId int64, project_id int64) (remind models.XtCheckRemind, err error) {
  195. err = writeDb.Model(&models.XtCheckRemind{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? and project_id = ?", orgId, patientId, project_id).First(&remind).Error
  196. return
  197. }
  198. func GetAllPatientInspection(orgId, patientId, page int64, projectId int64, start_time int64, end_time int64) (inspections []*models.Inspection, date int64, err error) {
  199. //var projectCount models.UserInspectionProjectCount
  200. //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
  201. //
  202. //if err != nil {
  203. // return
  204. //}
  205. //
  206. //total = projectCount.Count
  207. //if total == 0 || page > total {
  208. // return
  209. //}
  210. var Id models.InspectionDate
  211. 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
  212. if err != nil {
  213. return
  214. }
  215. if Id.InspectDate == 0 {
  216. return
  217. }
  218. date = Id.InspectDate
  219. inspections, err = GetPatientInspectionByDate(orgId, patientId, Id.InspectDate, projectId)
  220. if err != nil {
  221. return
  222. }
  223. return
  224. }
  225. func GetAllInspectionReference(orgId int64) (inspectionReference []*models.InspectionReference, err error) {
  226. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? AND status = 1", orgId).Find(&inspectionReference).Error
  227. return
  228. }
  229. func GetInspectionReferenceByOrgId(orgid int64) (insepciton []*models.XtInspectionReference, err error) {
  230. err = readDb.Model(&insepciton).Where("org_id = ? and status = 1 and project_name = '传染病检查'", orgid).Find(&insepciton).Error
  231. return insepciton, err
  232. }
  233. func GetInspectionReferenceTwo(project_id int64) (reference []*models.InspectionReference, err error) {
  234. err = readDb.Model(&models.InspectionReference{}).Where("project_id = ? and status = 1", project_id).Order("project_id").Find(&reference).Error
  235. return
  236. }
  237. func GetInspectionReferenceThree(project_id int64, item_id int64) (reference models.InspectionReference, err error) {
  238. err = readDb.Model(&models.InspectionReference{}).Where("project_id = ? and id = ? and status = 1 and org_id = 0", project_id, item_id).Order("project_id").Find(&reference).Error
  239. return
  240. }
  241. func GetInspectionTwo(startime int64, endtime int64, orgid int64, projectid int64, itemid int64) (inspection []*models.Inspection, total int64, err error) {
  242. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  243. db = db.Where("project_id = ? and item_id = ?", projectid, itemid)
  244. if startime > 0 {
  245. db = db.Where("x.inspect_date >= ?", startime)
  246. }
  247. if endtime > 0 {
  248. db = db.Where("x.inspect_date<=?", endtime)
  249. }
  250. if orgid > 0 {
  251. db = db.Where("x.org_id = ?", orgid)
  252. }
  253. if projectid > 0 {
  254. db = db.Where("x.project_id = ?", projectid)
  255. }
  256. err = db.Count(&total).Order("x.inspect_date desc").Group("x.id").
  257. Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.created_time").Find(&inspection).Error
  258. return inspection, total, err
  259. }
  260. // 查出日期差小于3天的数据
  261. func GetAllInspectionRemind(org_id int64, patient_id int64) (check []models.XtCheckRemind, err error) {
  262. err = readDb.Model(&models.XtCheckRemind{}).Preload("InspectionReference", "status = 1").Where("patient_id = ? and status = 1 and user_org_id = ? and DATEDIFF(FROM_UNIXTIME(last_remind_date),NOW()) < 3 and last_remind_date <> 0", patient_id, org_id).Find(&check).Error
  263. return
  264. }