auto_create_week_disinfection.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "Xcx_New/utils"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "github.com/robfig/cron"
  8. "strconv"
  9. "time"
  10. )
  11. // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  12. var createWeekDisinfectionCronJob *cron.Cron
  13. func init() {
  14. createWeekDisinfectionCronJob = cron.New()
  15. //spec := "0 0 23 * * ?" // 每天23点执行一次
  16. spec := "0 55 23 * * ?" // 每天23点55执行一次
  17. //spec := "0 0 5 * * ?" // 每天23点55执行一次
  18. //spec := "0 */1 * * * ?" // 每1分钟执行一次
  19. createWeekDisinfectionCronJob.AddFunc(spec, func() {
  20. AutoCreateWeekDisinfectionJob()
  21. })
  22. }
  23. func BeginAutoCreateWeekDisinfectionJob() {
  24. createWeekDisinfectionCronJob.Start()
  25. }
  26. func AutoCreateWeekDisinfectionJob() {
  27. timeStr := time.Now().Format("2006-01-02")
  28. timeLayout := "2006-01-02 15:04:05"
  29. fmt.Println("timeStr:", timeStr)
  30. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, timeStr+" 00:00:00")
  31. timenow := timeStringToTime.Unix()
  32. fmt.Println("timenow是什么", timenow)
  33. //查询当天排班所有机构
  34. org, errs := GetAllOrgID(timenow)
  35. fmt.Print("错误是什么", errs)
  36. //fmt.Println("查询有机构失败", err)
  37. for _, item := range org {
  38. //查询机构是否开启自动使用登记
  39. remander, errs := GetRemanderDatas(item.UserOrgId)
  40. fmt.Print("查询自动使用登记错误", errs)
  41. //开启
  42. if remander.IsRun == 1 {
  43. fmt.Println("机构id", item.UserOrgId)
  44. //查询当天机构的所有下机病人id
  45. order, _ := GetAllPatientByOrgID(item.UserOrgId, timenow)
  46. for _, it := range order {
  47. //fmt.Println("病人", it.PatientId)
  48. //查询病人信息
  49. patients, err := GetPatientInformation(it.PatientId, it.UserOrgId)
  50. fmt.Println("查询病人信息报错", err)
  51. var con = ""
  52. if patients.IsInfectious == 0 {
  53. con = ""
  54. }
  55. if patients.IsInfectious == 1 {
  56. con = "无"
  57. }
  58. if patients.IsInfectious == 2 {
  59. con = "有"
  60. }
  61. //fmt.Print("con------------------------", con)
  62. // fmt.Print("传染病", patients.IsInfectious)
  63. //查询病人的班次
  64. //schedules, _ := GetAllPatientSchedule(it.PatientId, it.UserOrgId,timenow)
  65. schedules, _ := GetAllPatientSchedule(it.PatientId, it.UserOrgId, timenow)
  66. fmt.Println("班次", schedules.ScheduleType)
  67. //查询病人今日的透析模式
  68. prescription, _ := GetDialysisPrescription(it.PatientId, it.UserOrgId, timenow)
  69. fmt.Println("透析模式", prescription.ModeId)
  70. //查询病人今日透后评估数据
  71. dislysis, err := GetAssessmentAfterDissData(it.PatientId, it.UserOrgId, timenow)
  72. fmt.Println("透后评估错误", err)
  73. fmt.Print("透析时长", dislysis.ActualTreatmentHour+'h'+dislysis.ActualTreatmentMinute+'m'+'i'+'n')
  74. // //根据床位号获取设备id
  75. addmacher, _ := GetEquimentID(schedules.BedId, it.UserOrgId)
  76. //unitype, err := strconv.ParseInt(addmacher.UnitType, 10, 64)
  77. fmt.Println(" 设备id", addmacher.ID)
  78. //根据设备id获取设备类型
  79. //equimentname, _ := GetAddmacher(addmacher.ID, it.UserOrgId)
  80. fmt.Println("班次", it.SchedualType)
  81. //查看该设备是否有消毒计划,无怎返回,有则添加
  82. plan, errplan := GetDisinfection(addmacher.UnitType, it.SchedualType, it.UserOrgId)
  83. fmt.Print("plan", plan)
  84. fmt.Println("错误是什么", errplan)
  85. if errplan == gorm.ErrRecordNotFound {
  86. fmt.Println("该设备无消毒计划")
  87. return
  88. } else if errplan == nil {
  89. //fmt.Println("消毒时长", plan.DisinfecTime)
  90. //fmt.Println("基表消毒方式", plan.Way)
  91. //fmt.Println("基表消毒液", plan.MachineDisinfectant)
  92. //fmt.Println("液路消毒方式", plan.DisinfectanWay)
  93. //fmt.Println("液路消毒液", plan.Disinfectant)
  94. //fmt.Println("上机时间", it.StartTime)
  95. //fmt.Println("下机时间", it.EndTime)
  96. //fmt.Println("传染病", con)
  97. //fmt.Println("体重减少", strconv.FormatFloat(dislysis.WeightLoss, 'f', -1, 64))
  98. //fmt.Println("时间超滤量", strconv.FormatFloat(dislysis.ActualUltrafiltration, 'f', -1, 64))
  99. //fmt.Println("透析时长", strconv.FormatInt(dislysis.ActualTreatmentHour, 10))
  100. //查询该病人是否有记录,无记录的则添加
  101. information, errcode := GetInfor(it.PatientId, timenow, it.UserOrgId, it.SchedualType)
  102. //fmt.Println("errcode是什么", errcode)
  103. fmt.Println("information", information)
  104. if errcode == gorm.ErrRecordNotFound {
  105. information := models.DeviceInformation{
  106. Date: timenow,
  107. Zone: schedules.PartitionId,
  108. Class: it.SchedualType,
  109. BedNumber: schedules.BedId,
  110. PatientId: schedules.PatientId,
  111. DialysisMode: prescription.ModeId,
  112. LongTime: strconv.FormatInt(plan.DisinfecTime, 10),
  113. Disinfection: 1,
  114. DialysisConcentration: 1,
  115. DisinfectionStatus: 1,
  116. Move: 1,
  117. UserOrgId: it.UserOrgId,
  118. DisinfectType: plan.Way, //基表消毒方式
  119. DisinfectantType: plan.MachineDisinfectant, //基表消毒液
  120. FluidPath: plan.DisinfectanWay, //液路消毒方式
  121. Disinfectant: plan.Disinfectant, //液路消毒液
  122. Ctime: time.Now().Unix(),
  123. Status: 1,
  124. SignName: it.FinishNurse,
  125. EquimentId: addmacher.ID,
  126. DisinfectionResidue: 2,
  127. Bed: addmacher.BedNumber,
  128. StartTime: it.StartTime,
  129. EndTime: it.EndTime,
  130. Contagion: con,
  131. WeightLoss: dislysis.WeightLoss,
  132. Hyperfiltratio: dislysis.ActualUltrafiltration,
  133. DialysisHour: strconv.FormatInt(dislysis.ActualTreatmentHour, 10),
  134. MachineRun: 1,
  135. }
  136. err := CreateInformation(&information)
  137. fmt.Println("报错", err)
  138. } else if errcode == nil {
  139. } else {
  140. }
  141. } else {
  142. return
  143. }
  144. }
  145. }
  146. //关闭
  147. if remander.IsRun == 2 {
  148. fmt.Print("已关闭")
  149. }
  150. }
  151. }
  152. //
  153. //func GetAllOrgID(status int64) (org []*models.Org, err error) {
  154. //
  155. // err = UserReadDB().Where("status = ?", status).Find(&org).Error
  156. //
  157. // return org, err
  158. //}
  159. //查询当天有排班的所有机构
  160. func GetAllOrgID(time int64) (schedule []*models.XtSchedule, err error) {
  161. err = XTReadDB().Raw("select user_org_id,id,partition_id,bed_id,patient_id,schedule_date,schedule_type,schedule_week,mode_id from xt_schedule where schedule_date = ? group by user_org_id", time).Scan(&schedule).Error
  162. return schedule, err
  163. }
  164. func GetAllPatientByOrgID(orgid int64, timenow int64) (order []*models.DialysisOrder, err error) {
  165. err = XTReadDB().Where("user_org_id = ? AND dialysis_date = ? AND stage = 2 AND status = ?", orgid, timenow, 1).Find(&order).Error
  166. return order, err
  167. }
  168. func CreateInformation(information *models.DeviceInformation) error {
  169. err := UserWriteDB().Create(&information).Error
  170. return err
  171. }
  172. func GetAllPatientSchedule(patientid int64, orgID int64, timenow int64) (models.Schedule, error) {
  173. schedules := models.Schedule{}
  174. err := XTReadDB().Where("patient_id = ? AND user_org_id = ? AND schedule_date = ?", patientid, orgID, timenow).Find(&schedules).Error
  175. return schedules, err
  176. }
  177. func GetInfor(patientid int64, timenow int64, orgid int64, class int64) (*models.DeviceInformation, error) {
  178. var pre models.DeviceInformation
  179. var err error
  180. err = readUserDb.Model(&pre).Where("patient_id = ? AND date = ? AND user_org_id = ? AND class = ? AND status = 1", patientid, timenow, orgid, class).Find(&pre).Error
  181. if err == gorm.ErrRecordNotFound {
  182. return nil, err
  183. }
  184. if err != nil {
  185. return nil, err
  186. }
  187. return &pre, nil
  188. }
  189. func GetDisinfection(equitType int64, scheduleType int64, orgID int64) (*models.DevicePlan, error) {
  190. var plan models.DevicePlan
  191. var err error
  192. err = readUserDb.Model(&plan).Where("device_type = ? AND classtime = ? AND user_org_id = ? AND status = 1", equitType, scheduleType, orgID).Find(&plan).Error
  193. if err == gorm.ErrRecordNotFound {
  194. return nil, err
  195. }
  196. if err != nil {
  197. return nil, err
  198. }
  199. return &plan, nil
  200. }
  201. func GetDialysisPrescription(patientid int64, orgID int64, timenow int64) (models.DialysisPrescription, error) {
  202. prescription := models.DialysisPrescription{}
  203. err := XTReadDB().Model(&prescription).Where("patient_id = ? AND user_org_id = ? AND record_date = ?", patientid, orgID, timenow).Find(&prescription).Error
  204. return prescription, err
  205. }
  206. func GetAssessmentAfterDissData(patientid int64, orgID int64, timenow int64) (models.XtAssessmentAfterDislysis, error) {
  207. dislysis := models.XtAssessmentAfterDislysis{}
  208. err := XTReadDB().Where("patient_id = ? AND user_org_id = ? AND assessment_date = ?", patientid, orgID, timenow).Find(&dislysis).Error
  209. return dislysis, err
  210. }
  211. func GetEquimentID(bedId int64, orgID int64) (models.DeviceAddmacher, error) {
  212. addmacher := models.DeviceAddmacher{}
  213. err := readUserDb.Model(&addmacher).Where("bed_id = ? AND user_org_id = ? AND status = ?", bedId, orgID, 1).Find(&addmacher).Error
  214. return addmacher, err
  215. }
  216. func GetAddmacher(equitmentid int64, orgid int64) (models.DeviceEquimentname, error) {
  217. equimentname := models.DeviceEquimentname{}
  218. err := readUserDb.Model(&equimentname).Where("equitment_id = ? AND user_org_id = ? AND status = 1", equitmentid, orgid).Find(&equimentname).Error
  219. return equimentname, err
  220. }
  221. func GetPatientInformation(id int64, orgid int64) (models.Patients, error) {
  222. patients := models.Patients{}
  223. err := XTReadDB().Where("id = ? AND user_org_id = ?", id, orgid).Find(&patients).Error
  224. return patients, err
  225. }
  226. func GetRemanderDatas(orgid int64) (remander models.DeviceRemander, err error) {
  227. err = UserReadDB().Model(&models.DeviceRemander{}).Where("user_org_id = ? and status = 1", orgid).Find(&remander).Error
  228. return
  229. }