lgty_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. "encoding/json"
  7. _ "encoding/json"
  8. _ "fmt"
  9. "github.com/astaxie/beego/httplib"
  10. _ "github.com/jinzhu/gorm"
  11. "net/url"
  12. _ "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 龙岗二院Lis同步
  17. func SyncLisLgty() (err error) {
  18. // 第一步:获取龙岗二院所有病人
  19. org_id := int64(9598)
  20. patientlist, patientError := GetUserInfoByOrgID(org_id)
  21. if patientError != nil {
  22. utils.ErrorLog("获取龙岗二院患者信息失败:%v", patientError)
  23. return
  24. }
  25. if len(patientlist) > 0 {
  26. // 第二步:根据病人的住院号来查询上次同步时间
  27. for _, patient := range patientlist {
  28. //if len(patient.AdmissionNumber) <= 0 {
  29. // continue
  30. //}
  31. syncLastInfo, _ := GetSyncTimeByOrgID(org_id, patient.IdCardNo)
  32. var sync_time int64
  33. if syncLastInfo.ID > 0 {
  34. sync_time = syncLastInfo.SyncTime
  35. } else {
  36. inspectionLastInfo, _ := GetInspectionLastInfoByOrgID(org_id, patient.IdCardNo)
  37. if inspectionLastInfo.ID > 0 {
  38. sync_time = inspectionLastInfo.CreatedTime
  39. } else {
  40. sync_time = 1672502400
  41. }
  42. }
  43. syncTimes := time.Unix(sync_time, 0).Format("2006-01-02 15:04")
  44. utils.InfoLog("syncTimes:%v", syncTimes)
  45. nowTimes := time.Now().Format("2006-01-02 15:04")
  46. // 第三步:跟进上次同步时间和指定的门诊号,获取上次同步到现在指定人的检验检查记录
  47. lisHead, HeadString := GetHead(patient.IdCardNo, syncTimes, nowTimes)
  48. var resultCount int64
  49. // 第四步,将同步过来的头部信息存到中间库的检验检查操作表中
  50. if len(lisHead) > 0 {
  51. resultCount = 0
  52. for _, head := range lisHead {
  53. // if len(head.Item) <= 0 {
  54. // continue
  55. // }
  56. if len(head.ReportId) > 1 {
  57. if len(head.Item) == 0 {
  58. head.Item = "肾功三项"
  59. }
  60. middleReportHandle := models.MiddleReportHandle{
  61. OrgId: org_id,
  62. HisUserId: patient.IdCardNo,
  63. ApplyNo: head.ApplyNo,
  64. ReportId: head.ReportId,
  65. Name: head.Name,
  66. Sex: head.Sex,
  67. Age: head.Age,
  68. Dept: head.Dept,
  69. PatientId: patient.IdCardNo,
  70. RequestTime: head.RequestTime,
  71. ExecuteTime: head.ExecuteTime,
  72. ReceiveTime: head.ReceiveTime,
  73. Item: head.Item,
  74. CheckTime: head.CheckTime,
  75. PatientType: head.PatientType,
  76. CreatedTime: time.Now().Unix(),
  77. UpdatedTime: time.Now().Unix(),
  78. }
  79. handleerr := CreateReportHandle(&middleReportHandle)
  80. if handleerr != nil {
  81. utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr)
  82. return
  83. }
  84. // // reportInfos,ReportString := GetReport(head.ReportId)
  85. // HeadString = HeadString + " " + ReportString
  86. // // 获取该项的检验检查project_id,如果没有,则创建一个
  87. // project_id,_ := GetProjectID(org_id,head.Item)
  88. // if len(reportInfos) > 2 {
  89. // // 将同步过来的检验检查数据存到中间库
  90. // for _,reportInfo := range reportInfos{
  91. // if len(reportInfo.ItemName) <= 0 {
  92. // continue
  93. // }
  94. // // 获取该小项的检验检查item_id,如果没有,则创建一个
  95. // item_id,_ := GetItemID(org_id,head.Item,reportInfo.ItemName,project_id)
  96. // _,_,syerr := LgthSync(head,reportInfo,project_id,item_id,org_id,patient.ID,patient.AdmissionNumber)
  97. // if syerr == nil {
  98. // resultCount = resultCount + 1
  99. // }
  100. // }
  101. // }
  102. }
  103. }
  104. var syncInfo models.MiddleSyncInfo
  105. syncInfo.OrgId = org_id
  106. syncInfo.HisUserId = patient.IdCardNo
  107. syncInfo.SyncTime = time.Now().Unix()
  108. syncInfo.SyncResultType = 1
  109. syncInfo.SyncRsultRemark = "同步成功"
  110. syncInfo.SyncTotalNum = resultCount
  111. syncInfo.SyncSuccessNum = 0
  112. syncInfo.SyncInfo = HeadString
  113. syncInfo.CreateTime = time.Now().Unix()
  114. syncInfo.UpdateTime = time.Now().Unix()
  115. cwderr := CreateSyncInfo(&syncInfo)
  116. if cwderr != nil {
  117. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  118. return
  119. }
  120. } else {
  121. // 未同步到检验检查头部信息
  122. var syncInfo models.MiddleSyncInfo
  123. syncInfo.OrgId = org_id
  124. syncInfo.HisUserId = patient.IdCardNo
  125. syncInfo.SyncTime = time.Now().Unix()
  126. syncInfo.SyncResultType = 1
  127. syncInfo.SyncRsultRemark = "该患者这个时间段没有任何检验检查数据"
  128. syncInfo.CreateTime = time.Now().Unix()
  129. syncInfo.UpdateTime = time.Now().Unix()
  130. serr := CreateSyncInfo(&syncInfo)
  131. if serr != nil {
  132. utils.ErrorLog("创建同步信息失败:%v", serr)
  133. return
  134. }
  135. continue
  136. }
  137. }
  138. }
  139. // 第五步,查询检验检查操作表里状态为0的数据,并将该对应报告的检验检查详情数据请求回来并存入该条数据里的report_info字段
  140. ReportInfoHandle(org_id)
  141. return
  142. }
  143. func ReportInfoHandle(org_id int64) {
  144. // 1.从检验检查表中查询操作状态为0的数据
  145. report_handle_list, _ := GetReportHandleListyOrgID(org_id)
  146. if len(report_handle_list) > 0 {
  147. for _, report_handle := range report_handle_list {
  148. // 2. 根据report_id获取报告详情并进行相关处理
  149. reportInfos, ReportString := GetReport(report_handle.ReportId)
  150. if len(reportInfos) > 0 {
  151. report_info_item_num := len(reportInfos)
  152. // 将检验检查详情,检验检查小项条数更新到操作表数据中
  153. middleReportHandle := models.MiddleReportHandle{
  154. ID: report_handle.ID,
  155. ReportInfo: ReportString,
  156. ReportInfoItemNum: int64(report_info_item_num),
  157. UpdatedTime: time.Now().Unix(),
  158. }
  159. err := UpdateReportHandleListyOrgID(&middleReportHandle)
  160. if err != nil {
  161. utils.ErrorLog("创建同步信息失败:%v", err)
  162. //return
  163. }
  164. report_handle.ReportInfoItemNum = int64(report_info_item_num)
  165. project_id, _ := GetProjectID(org_id, report_handle.Item)
  166. patient, perr := GetUserInfoByAdmission(report_handle.PatientId, org_id)
  167. if perr != nil {
  168. utils.ErrorLog("查询病人信息失败:%v", perr)
  169. //return
  170. }
  171. if len(reportInfos) > 0 {
  172. // 将同步过来的检验检查数据存到中间库
  173. for _, reportInfo := range reportInfos {
  174. if len(reportInfo.ItemName) <= 0 {
  175. continue
  176. }
  177. // 获取该小项的检验检查item_id,如果没有,则创建一个
  178. item_id, _ := GetItemID(org_id, report_handle.Item, reportInfo.ItemName, project_id)
  179. _, _, syerr := LgthSync(&report_handle, reportInfo, project_id, item_id, org_id, patient.ID, patient.AdmissionNumber)
  180. if syerr != nil {
  181. utils.ErrorLog("创建同步信息失败:%v", syerr)
  182. return
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. SyncToLgey()
  190. }
  191. func SyncToLgey() {
  192. utils.TraceLog("检验检查同步任务开始执行")
  193. org_id := int64(9598)
  194. // 第一步:跟进org_id 去中间库查出需要同步的数据
  195. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  196. inspections, _ := GetSyncInspectionByOrgId(org_id)
  197. // 第二步:将数据同步到业务库
  198. if len(inspection_references) > 0 {
  199. for _, inspection_reference := range inspection_references {
  200. SyncInspectionReference(&inspection_reference)
  201. }
  202. }
  203. if len(inspections) > 0 {
  204. for _, inspection := range inspections {
  205. SyncInspection(&inspection)
  206. }
  207. }
  208. utils.SuccessLog("检验检查同步任务完成")
  209. }
  210. // 从机构将数据同步到中间库
  211. func LgthSync(Head *models.MiddleReportHandle, report ReportInfo, project_id int64, item_id int64, org_id int64, patient_id int64, his_user_id string) (*models.MiddleInspectionReference, *models.MiddleInspection, error) {
  212. tx := writeMiddleDb.Begin()
  213. var inspection models.MiddleInspection
  214. var inspection_reference models.MiddleInspectionReference
  215. recordDateStr := Head.CheckTime
  216. utils.InfoLog("打印测试: %v", recordDateStr)
  217. if len(Head.CheckTime) == 0 {
  218. recordDateStr = time.Now().Format("2006-01-02 15:04")
  219. } else {
  220. //dateTempArrOne := strings.Split(recordDateStr, "T")
  221. dateTempArrTwo := strings.Split(recordDateStr, ".")
  222. recordDateStr = dateTempArrTwo[0]
  223. }
  224. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  225. record_date := record_date_str.Unix()
  226. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  227. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  228. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  229. var total int
  230. var itotal int
  231. var RangeOptions string
  232. var RangeMin string
  233. var RangeMax string
  234. // // 判断检查类型
  235. ItemType := 1
  236. Range := strings.Split(report.Reference, "~")
  237. if len(Range) > 1 {
  238. RangeMin = Range[0]
  239. RangeMax = Range[1]
  240. ItemType = 1
  241. } else {
  242. ItemType = 2
  243. RangeOptions = report.Reference
  244. }
  245. 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
  246. if inspection_reference.ID > 0 {
  247. ItemType = inspection_reference.RangeType
  248. }
  249. if total <= 0 && len(Head.Item) > 0 {
  250. inspection_reference.OrgId = org_id
  251. inspection_reference.ProjectName = Head.Item
  252. inspection_reference.Project = Head.Item
  253. inspection_reference.ProjectId = project_id
  254. inspection_reference.ItemName = report.ItemName
  255. inspection_reference.ItemId = item_id
  256. inspection_reference.RangeType = ItemType
  257. inspection_reference.RangeMin = RangeMin
  258. inspection_reference.RangeMax = RangeMax
  259. // inspection_reference.RangeValue = RangeValue
  260. inspection_reference.RangeOptions = RangeOptions
  261. inspection_reference.Unit = report.Unit
  262. inspection_reference.Status = 1
  263. inspection_reference.CreatedTime = time.Now().Unix()
  264. inspection_reference.UpdatedTime = time.Now().Unix()
  265. inspection_reference.InspectDate = inspect_date
  266. inspection_reference.UTime = inspect_date
  267. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  268. if err != nil {
  269. tx.Rollback()
  270. }
  271. }
  272. 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, record_date, patient_id).Find(&inspection).Count(&itotal).Error
  273. if itotal <= 0 {
  274. inspection.PatientId = patient_id
  275. inspection.OrgId = org_id
  276. inspection.ProjectId = project_id
  277. inspection.ItemName = report.ItemName
  278. inspection.ProjectName = Head.Item
  279. inspection.InspectType = ItemType
  280. inspection.ItemId = item_id
  281. inspection.InspectValue = report.Result
  282. inspection.InspectDate = inspect_date
  283. inspection.RecordDate = record_date
  284. inspection.Status = 1
  285. inspection.CreatedTime = time.Now().Unix()
  286. inspection.UpdatedTime = time.Now().Unix()
  287. inspection.UTime = inspect_date
  288. inspection.HisUserId = his_user_id
  289. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  290. if err != nil {
  291. tx.Rollback()
  292. }
  293. //处理检验检查操作表里同步条数和状态
  294. cur_num := Head.ReportInfoItemHandleNum + 1
  295. Head.ReportInfoItemHandleNum = cur_num
  296. handleType := 2
  297. if cur_num == Head.ReportInfoItemNum {
  298. handleType = 1
  299. }
  300. middleReportHandle := models.MiddleReportHandle{
  301. ID: Head.ID,
  302. ReportInfoItemHandleNum: cur_num,
  303. HandleType: int64(handleType),
  304. UpdatedTime: time.Now().Unix(),
  305. }
  306. err = UpdateReportHandleListyOrgID(&middleReportHandle)
  307. if err != nil {
  308. tx.Rollback()
  309. }
  310. }
  311. tx.Commit()
  312. return nil, nil, err
  313. }
  314. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  315. func GetItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  316. var inspection_reference models.MiddleInspectionReference
  317. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  318. if inspection_reference.ID > 0 {
  319. return inspection_reference.ItemId, err
  320. } else {
  321. 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
  322. utils.InfoLog("inspection_reference: %v", inspection_reference)
  323. if inspection_reference.ItemId > 0 {
  324. return inspection_reference.ItemId + 1, err
  325. } else {
  326. return project_id*1000 + 1, err
  327. }
  328. }
  329. }
  330. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  331. func GetProjectID(org_id int64, project_name string) (project_id int64, err error) {
  332. var inspection_reference models.MiddleInspectionReference
  333. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  334. if inspection_reference.ID > 0 {
  335. return inspection_reference.ProjectId, err
  336. } else {
  337. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  338. utils.InfoLog("inspection_reference: %v", inspection_reference)
  339. if inspection_reference.ProjectId > 0 {
  340. return inspection_reference.ProjectId + 1, err
  341. } else {
  342. return 18001, err
  343. }
  344. }
  345. }
  346. // 根据机构ID获取该机构所有病人的admission_number (住院号)
  347. func GetUserInfoByOrgID(org_id int64) (patients []models.Patients, err error) {
  348. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
  349. return
  350. }
  351. // 根据机构ID获取该机构所有病人的admission_number (住院号)
  352. func GetUserInfoByAdmission(admission_number string, org_id int64) (patients models.Patients, err error) {
  353. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and id_card_no = ? and status = 1", org_id, admission_number).First(&patients).Error
  354. return
  355. }
  356. type HeadInfo struct {
  357. ApplyNo string `json:"applyNo"`
  358. ReportId string `json:"reportId"`
  359. Name string `json:"name"`
  360. Sex string `json:"sex"`
  361. Age string `json:"age"`
  362. Dept string `json:"dept"`
  363. BedNo string `json:"bedNo"`
  364. PatientId string `json:"patientId"`
  365. PatientPid string `json:"patientPid"`
  366. SampleNo float64 `json:"sampleNo"`
  367. RequestTime string `json:"requestTime"`
  368. ExecuteTime string `json:"executeTime"`
  369. ReceiveTime string `json:"receiveTime"`
  370. Item string `json:"item"`
  371. CheckTime string `json:"checkTime"`
  372. PatientType string `json:"patientType"`
  373. RequestTimes string `json:"request_time"`
  374. }
  375. type ReportInfo struct {
  376. ReportId string `json:"reportId"`
  377. ItemCode string `json:"itemCode"`
  378. ItemName string `json:"itemName"`
  379. StdCode string `json:"stdCode"`
  380. Result string `json:"result"`
  381. Unit string `json:"unit"`
  382. SampleNo float64 `json:"sampleNo"`
  383. Reference string `json:"reference"`
  384. Prompt string `json:"prompt"`
  385. }
  386. func GetHead(patient_id string, start_time string, end_time string) (head []HeadInfo, str string) {
  387. Domain := "http://www.szlg2h.cn:9991/xuetoushi/head/getHead?"
  388. apiurl := "identity_Card=" + patient_id + "&check_time_start=" + start_time + "&check_time_end=" + end_time
  389. apiurl = url.PathEscape(apiurl)
  390. apiurl = Domain + apiurl
  391. utils.InfoLog("url: %v", apiurl)
  392. var respJSON []HeadInfo
  393. httplib.Get(apiurl).ToJSON(&respJSON)
  394. jsonStr, _ := json.Marshal(respJSON)
  395. utils.InfoLog("请求患者检查头部数据接口返回数据: %v", string(jsonStr))
  396. return respJSON, string(jsonStr)
  397. }
  398. func GetReport(report_id string) (head []ReportInfo, str string) {
  399. Domain := "http://www.szlg2h.cn:9991/xuetoushi/detail/getDetail?"
  400. apiurl := "report_id=" + report_id
  401. apiurl = url.PathEscape(apiurl)
  402. apiurl = Domain + apiurl
  403. var respJSON []ReportInfo
  404. httplib.Get(apiurl).ToJSON(&respJSON)
  405. jsonStr, _ := json.Marshal(respJSON)
  406. utils.InfoLog("请求患者检查详情数据接口返回数据: %v", string(jsonStr))
  407. return respJSON, string(jsonStr)
  408. }