sclsyyyy_service.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "encoding/json"
  6. _ "fmt"
  7. oracle "github.com/dzwvip/gorm-oracle"
  8. "gorm.io/gorm"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // 四川乐山友谊医院 HIS医嘱同步+LIS检验结果同步
  14. type scyyyylis struct {
  15. HisUserId string `gorm:"column:HIS_USER_ID" json:"HIS_USER_ID"`
  16. PatientName string `gorm:"column:PATIENT_NAME" json:"PATIENT_NAME"`
  17. ProjectId string `gorm:"column:PROJECT_ID" json:"PROJECT_ID"`
  18. ProjectName string `gorm:"column:PROJECT_NAME" json:"PROJECT_NAME"`
  19. InspectDate string `gorm:"column:INSPECT_DATE" json:"INSPECT_DATE"`
  20. ItemId string `gorm:"column:ITEM_ID" json:"ITEM_ID"`
  21. ItemName string `gorm:"column:ITEM_NAME" json:"ITEM_NAME"`
  22. ItemType int64 `gorm:"column:ITEM_TYPE" json:"ITEM_TYPE"`
  23. ItemValue string `gorm:"column:ITEM_VALUE" json:"ITEM_VALUE"`
  24. RangeMin string `gorm:"column:RANGE_MIN" json:"RANGE_MIN"`
  25. RangeMax string `gorm:"column:RANGE_MAX" json:"RANGE_MAX"`
  26. RangeValue string `gorm:"column:RANGE_VALUE" json:"RANGE_VALUE"`
  27. RangeOptions string `gorm:"column:RANGE_OPTIONS" json:"RANGE_OPTIONS"`
  28. Unit string `gorm:"column:UNIT" json:"UNIT"`
  29. State int64 `gorm:"column:STATE" json:"STATE"`
  30. CTime string `gorm:"column:C_TIME" json:"C_TIME"`
  31. UTime string `gorm:"column:U_TIME" json:"U_TIME"`
  32. }
  33. func (scyyyylis) TableName() string {
  34. return "yyyyxthy"
  35. }
  36. func GetSclsyyyyLis(rdb *gorm.DB, c_time string) (record []*scyyyylis, err error) {
  37. err = rdb.Model(&scyyyylis{}).Where("C_TIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')", c_time).Find(&record).Error
  38. return
  39. }
  40. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  41. func GetScyyyyProjectID(org_id int64, project_name string, item_name string) (project_id int64, err error) {
  42. var inspection_reference models.MiddleInspectionReference
  43. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error
  44. if inspection_reference.ID > 0 {
  45. return inspection_reference.ProjectId, err
  46. } else {
  47. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", org_id, project_name).First(&inspection_reference).Error
  48. if inspection_reference.ID > 0 {
  49. return inspection_reference.ProjectId, err
  50. } else {
  51. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  52. if inspection_reference.ProjectId > 0 {
  53. return inspection_reference.ProjectId + 1, err
  54. } else {
  55. return 1067701, err
  56. }
  57. }
  58. }
  59. }
  60. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  61. func GetScyyyyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  62. var inspection_reference models.MiddleInspectionReference
  63. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  64. if inspection_reference.ID > 0 {
  65. return inspection_reference.ItemId, err
  66. } else {
  67. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
  68. if inspection_reference.ItemId > 0 {
  69. return inspection_reference.ItemId + 1, err
  70. } else {
  71. return project_id*100 + 1, err
  72. }
  73. }
  74. }
  75. // 四川乐山友谊医院Lis同步
  76. func SyncScyyyyLis() (err error) {
  77. org_id := int64(10677)
  78. orgDb, err := gorm.Open(oracle.Open("oracle://zjybase:zjybase@10.0.0.248:1521/orcl"), &gorm.Config{})
  79. if err != nil {
  80. utils.InfoLog("创建数据库连接失败:%v", err)
  81. }
  82. // 第一步:获取上一次同步的时间点
  83. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  84. var sync_time int64
  85. if syncLastInfo.ID > 0 {
  86. sync_time = syncLastInfo.SyncTime
  87. } else {
  88. sync_time = 1704038400 // 2024-01-01
  89. }
  90. // 第二步:获取时间内所有检验结果
  91. sync_time_temp := time.Unix(sync_time, 0)
  92. startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  93. yyyyLis, _ := GetSclsyyyyLis(orgDb, startTimeStr)
  94. if len(yyyyLis) > 0 {
  95. for _, LisInfo := range yyyyLis {
  96. var patientInfo models.Patients
  97. // 根据身份证号获取患者信息
  98. if len(LisInfo.HisUserId) > 0 {
  99. patientInfo, _ = GetPatientsInfoByIDCardNo(LisInfo.HisUserId, org_id)
  100. if patientInfo.ID <= 0 {
  101. continue
  102. }
  103. } else {
  104. patientInfo, _ = GetPatientsInfoByName(LisInfo.PatientName, org_id)
  105. if patientInfo.ID <= 0 {
  106. continue
  107. }
  108. }
  109. timeLayout := "2006-01-02 15:04"
  110. loc, _ := time.LoadLocation("Local")
  111. timstr := strings.Split(LisInfo.InspectDate, "T")
  112. timstem := strings.Split(timstr[1], "+")
  113. tempRecord := timstr[0] + " " + timstem[0]
  114. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  115. recordTimeInt := recordTime.Unix()
  116. recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
  117. project_id, _ := GetScyyyyProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName)
  118. item_id, _ := GetScyyyyItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id)
  119. tx := writeMiddleDb.Begin()
  120. var inspection models.MiddleInspection
  121. var inspection_reference models.MiddleInspectionReference
  122. var total int
  123. var RangeOptions string
  124. var RangeMin string
  125. var RangeMax string
  126. var ItemType int
  127. Range := strings.Split(LisInfo.RangeValue, "--")
  128. if len(Range) > 1 {
  129. RangeMin = Range[0]
  130. RangeMax = Range[1]
  131. ItemType = 1
  132. } else {
  133. ItemType = 2
  134. RangeOptions = LisInfo.RangeValue
  135. }
  136. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
  137. if total <= 0 {
  138. inspection_reference.OrgId = org_id
  139. inspection_reference.ProjectName = LisInfo.ProjectName
  140. inspection_reference.Project = LisInfo.ProjectName
  141. inspection_reference.ProjectId = project_id
  142. inspection_reference.ItemName = LisInfo.ItemName
  143. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  144. inspection_reference.ItemId = item_id
  145. inspection_reference.RangeType = ItemType
  146. inspection_reference.RangeMin = RangeMin
  147. inspection_reference.RangeMax = RangeMax
  148. // inspection_reference.RangeValue = RangeValue
  149. inspection_reference.RangeOptions = RangeOptions
  150. inspection_reference.Unit = LisInfo.Unit
  151. inspection_reference.Status = 1
  152. inspection_reference.CreatedTime = time.Now().Unix()
  153. inspection_reference.UpdatedTime = time.Now().Unix()
  154. inspection_reference.InspectDate = recordTimeStr
  155. inspection_reference.UTime = recordTimeStr
  156. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  157. if err != nil {
  158. tx.Rollback()
  159. }
  160. }
  161. var itotal int
  162. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, recordTimeStr, patientInfo.ID).Find(&inspection).Count(&itotal).Error
  163. if itotal <= 0 {
  164. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  165. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  166. inspection.PatientId = patientInfo.ID
  167. inspection.OrgId = org_id
  168. inspection.ProjectId = project_id
  169. inspection.ItemName = inspection_reference.ItemName
  170. inspection.ProjectName = inspection_reference.ProjectName
  171. inspection.InspectType = ItemType
  172. inspection.ItemId = item_id
  173. inspection.InspectValue = LisInfo.ItemValue
  174. inspection.InspectDate = recordTimeStr
  175. inspection.RecordDate = recordTimeInt
  176. // inspection.InspectTips = report.Resultstate
  177. inspection.Status = 1
  178. inspection.CreatedTime = time.Now().Unix()
  179. inspection.UpdatedTime = time.Now().Unix()
  180. // inspection.UTime = record_date.Format(timeLayout)
  181. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  182. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  183. if err != nil {
  184. tx.Rollback()
  185. }
  186. }
  187. tx.Commit()
  188. }
  189. var syncInfo models.MiddleSyncInfo
  190. syncInfo.OrgId = org_id
  191. syncInfo.SyncTime = time.Now().Unix()
  192. syncInfo.SyncResultType = 1
  193. syncInfo.SyncRsultRemark = "同步成功"
  194. syncInfo.SyncTotalNum = 0
  195. syncInfo.SyncSuccessNum = 0
  196. syncInfo.SyncInfo = ""
  197. syncInfo.CreateTime = time.Now().Unix()
  198. syncInfo.UpdateTime = time.Now().Unix()
  199. cwderr := CreateSyncInfo(&syncInfo)
  200. if cwderr != nil {
  201. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  202. return
  203. }
  204. }
  205. SyncToSclsyyyy()
  206. return
  207. }
  208. func SyncToSclsyyyy() {
  209. utils.TraceLog("检验检查同步任务开始执行")
  210. org_id := int64(10677)
  211. // 第一步:跟进org_id 去中间库查出需要同步的数据
  212. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  213. inspections, _ := GetSyncInspectionByOrgId(org_id)
  214. // 第二步:将数据同步到业务库
  215. if len(inspection_references) > 0 {
  216. for _, inspection_reference := range inspection_references {
  217. SyncInspectionReference(&inspection_reference)
  218. }
  219. }
  220. if len(inspections) > 0 {
  221. for _, inspection := range inspections {
  222. SyncInspection(&inspection)
  223. }
  224. }
  225. utils.SuccessLog("检验检查同步任务完成")
  226. }
  227. type yyyyxtyz struct {
  228. HisUserId string `gorm:"column:HIS_USER_ID" json:"HIS_USER_ID" form:"HIS_USER_ID"`
  229. UserName string `gorm:"column:USER_NAME" json:"USER_NAME" form:"USER_NAME"`
  230. DoctorId string `gorm:"column:DOCTOR_ID" json:"DOCTOR_ID" form:"DOCTOR_ID"`
  231. DoctorName string `gorm:"column:DOCTOR_NAME" json:"DOCTOR_NAME" form:"DOCTOR_NAME"`
  232. AdviceId int64 `gorm:"column:ADVICE_ID" json:"ADVICE_ID" form:"ADVICE_ID"`
  233. MedListCodg string `gorm:"column:MED_LIST_CODG" json:"MED_LIST_CODG" form:"MED_LIST_CODG"`
  234. StartTime string `gorm:"column:START_TIME" json:"START_TIME" form:"START_TIME"`
  235. AdviceName string `gorm:"column:ADVICE_NAME" json:"ADVICE_NAME" form:"ADVICE_NAME"`
  236. AdviceDesc string `gorm:"column:ADVICE_DESC" json:"ADVICE_DESC" form:"ADVICE_DESC"`
  237. SingleDose string `gorm:"column:SINGLE_DOSE" json:"SINGLE_DOSE" form:"SINGLE_DOSE"`
  238. SingleDoseUnit string `gorm:"column:SINGLE_DOSE_UNIT" json:"SINGLE_DOSE_UNIT" form:"SINGLE_DOSE_UNIT"`
  239. PrescribingNumber string `gorm:"column:PRESCRIBING_NUMBER" json:"PRESCRIBING_NUMBER" form:"PRESCRIBING_NUMBER"`
  240. PrescribingNumberUnit string `gorm:"column:PRESCRIBING_NUMBER_UNIT" json:"PRESCRIBING_NUMBER_UNIT" form:"PRESCRIBING_NUMBER_UNIT"`
  241. DeliveryWay string `gorm:"column:DELIVERY_WAY" json:"DELIVERY_WAY" form:"DELIVERY_WAY"`
  242. ExecutionFrequency string `gorm:"column:EXECUTION_FREQUENCY" json:"EXECUTION_FREQUENCY" form:"EXECUTION_FREQUENCY"`
  243. State int64 `gorm:"column:STATE" json:"STATE" form:"STATE"`
  244. CTime string `gorm:"column:CREATE_TIME" json:"CREATE_TIME" form:"CREATE_TIME"`
  245. UTime string `gorm:"column:UPDATE_TIME" json:"UPDATE_TIME" form:"UPDATE_TIME"`
  246. }
  247. func (yyyyxtyz) TableName() string {
  248. return "yyyyxtyz"
  249. }
  250. func GetSclsyyyyyz(rdb *gorm.DB, update_time string) (record []*yyyyxtyz, err error) {
  251. err = rdb.Model(&yyyyxtyz{}).Where("update_time >= ?", update_time).Find(&record).Error
  252. return
  253. }
  254. func GetAdviceBySyncAdviceIdForScyyyy(orgId int64, sync_advice_id int64, advice_date int64, patient_id int64) (advice models.XtDoctorAdvice, err error) {
  255. err = readDb.Model(&models.XtDoctorAdvice{}).Where(" user_org_id = ? AND advice_date = ? and patient_id = ? AND sync_advice_id = ? AND status = 1 ", orgId, advice_date, patient_id, sync_advice_id).First(&advice).Error
  256. return
  257. }
  258. // 根据姓名获取patient_id
  259. func GetSchedulesByPatientID(org_id int64, schedulesTime int64, patient_id int64) (schedules models.DialysisSchedule, err error) {
  260. err = readDb.Model(&models.DialysisSchedule{}).Where("user_org_id = ? and schedule_date = ? and patient_id = ? and status = 1", org_id, schedulesTime, patient_id).First(&schedules).Error
  261. return
  262. }
  263. // 四川乐山友谊医院医嘱同步
  264. func SyncSclsyyyyyz(orgDb *gorm.DB) (err error) {
  265. // 第一步:获取上次同步时间
  266. org_id := int64(10677)
  267. redisClient := RedisClient()
  268. defer redisClient.Close()
  269. sync_time_tt, _ := redisClient.Get("sclsyyyy_sync_time").Result()
  270. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  271. if sync_time == 0 {
  272. sync_time = time.Now().Unix() - 60*60*10
  273. }
  274. // 需要把时间戳转换为日期格式
  275. syncTimeStr := time.Unix(sync_time, 0).Format("2006-01-02 15:04:05")
  276. new_sync_time := time.Now().Unix() - 60*3
  277. schedulesDateStr := time.Now().Format("2006-01-02")
  278. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  279. schedulesTime := schedulesDate.Unix()
  280. utils.InfoLog("syncTimeStr:%v", syncTimeStr)
  281. utils.InfoLog("schedulesTime:%v", schedulesTime)
  282. // 切换为当前时间
  283. redisClient.Set("sclsyyyy_sync_time", new_sync_time, time.Minute*60*24)
  284. // 第二步: 获取该时间段内有更新的医嘱
  285. yzList, _ := GetSclsyyyyyz(orgDb, syncTimeStr)
  286. if len(yzList) > 0 {
  287. loc, _ := time.LoadLocation("Local") //重要:获取时区
  288. for _, yz := range yzList {
  289. if yz.DeliveryWay == "口服" {
  290. continue
  291. }
  292. var dflag int64
  293. dflag = 0
  294. utils.InfoLog("AdviceName:%v", yz.AdviceName)
  295. if strings.Contains(yz.AdviceName, "注射") {
  296. dflag = 1
  297. }
  298. if strings.Contains(yz.AdviceName, "血液") {
  299. dflag = 1
  300. }
  301. if strings.Contains(yz.AdviceName, "低分子肝素") {
  302. dflag = 1
  303. }
  304. if strings.Contains(yz.AdviceName, "左卡") {
  305. dflag = 1
  306. }
  307. if strings.Contains(yz.AdviceName, "促红素") {
  308. dflag = 1
  309. }
  310. if strings.Contains(yz.AdviceName, "奈莫司他") {
  311. dflag = 1
  312. }
  313. if strings.Contains(yz.AdviceName, "葡萄糖") {
  314. dflag = 1
  315. }
  316. if strings.Contains(yz.AdviceName, "吸氧") {
  317. dflag = 1
  318. }
  319. if strings.Contains(yz.AdviceName, "抢救") {
  320. dflag = 1
  321. }
  322. if strings.Contains(yz.AdviceName, "电脑血糖") {
  323. dflag = 1
  324. }
  325. if strings.Contains(yz.AdviceName, "血液透析") {
  326. yz.SingleDose = ""
  327. yz.SingleDoseUnit = ""
  328. yz.PrescribingNumber = "1"
  329. yz.PrescribingNumberUnit = "次"
  330. }
  331. utils.InfoLog("dflag:%v", dflag)
  332. if dflag == 0 {
  333. continue
  334. }
  335. var patientInfo models.Patients
  336. // 根据身份证号获取患者信息
  337. if len(yz.HisUserId) > 0 {
  338. patientInfo, _ = GetPatientsInfoByIDCardNo(yz.HisUserId, org_id)
  339. if patientInfo.ID <= 0 {
  340. continue
  341. }
  342. } else {
  343. patientInfo, _ = GetPatientsInfoByName(yz.UserName, org_id)
  344. if patientInfo.ID <= 0 {
  345. continue
  346. }
  347. }
  348. // 查询该患者当天是否有排班信息
  349. theTime, _ := time.ParseInLocation("2006-01-02 15:04", yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
  350. start_time := theTime.Unix()
  351. schedulesInfo, _ := GetSchedulesByPatientID(org_id, schedulesTime, patientInfo.ID)
  352. if schedulesInfo.ID > 0 {
  353. start_time = theTime.Unix()
  354. } else {
  355. continue
  356. }
  357. // 根据同步来的 advice_id 来查询库里是否已经存在
  358. advice_info, _ := GetAdviceBySyncAdviceIdForScyyyy(org_id, yz.AdviceId, schedulesTime, patientInfo.ID)
  359. if advice_info.ID > 0 {
  360. continue
  361. } else {
  362. // 根据姓名获取医生ID
  363. var doctor_id int64
  364. temp_doctor_id, _ := redisClient.Get("sclsyyyy_doctor_id_" + yz.DoctorId).Result()
  365. if len(temp_doctor_id) == 0 {
  366. doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
  367. doctor_id = doctor.AdminUserId
  368. if doctor_id > 0 {
  369. redisClient.Set("sclsyyyy_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
  370. } else {
  371. continue
  372. }
  373. } else {
  374. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  375. }
  376. // 根据排班班次,固定医嘱时间
  377. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  378. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  379. advice := models.XtDoctorAdvice{
  380. UserOrgId: org_id,
  381. PatientId: patientInfo.ID,
  382. AdviceType: 2,
  383. AdviceDate: schedulesTime,
  384. StartTime: start_time,
  385. AdviceName: yz.AdviceName,
  386. AdviceDesc: yz.AdviceDesc,
  387. RecordDate: schedulesTime,
  388. SingleDose: SingleDose,
  389. SingleDoseUnit: yz.SingleDoseUnit,
  390. PrescribingNumber: PrescribingNumber,
  391. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  392. DeliveryWay: yz.DeliveryWay,
  393. ExecutionFrequency: yz.ExecutionFrequency,
  394. AdviceDoctor: doctor_id,
  395. CreatedTime: time.Now().Unix(),
  396. UpdatedTime: time.Now().Unix(),
  397. SyncAdviceId: yz.AdviceId,
  398. ExecutionState: 2,
  399. Status: 1,
  400. IsSync: 1,
  401. }
  402. utils.InfoLog("advice:%v", advice)
  403. if len(yz.AdviceName) > 0 {
  404. handleerr := CreateDoctorAdviceHandle(&advice)
  405. if handleerr != nil {
  406. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  407. continue
  408. }
  409. } else {
  410. continue
  411. }
  412. }
  413. }
  414. }
  415. return
  416. }