auto_create_week_disinfection.go 9.8KB

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