lgty_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _"strconv"
  6. "strings"
  7. "encoding/json"
  8. "time"
  9. "net/url"
  10. _"IC/utils"
  11. _"fmt"
  12. _"encoding/json"
  13. _"github.com/jinzhu/gorm"
  14. "github.com/astaxie/beego/httplib"
  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.AdmissionNumber)
  32. var sync_time int64
  33. if syncLastInfo.ID > 0 {
  34. sync_time = syncLastInfo.SyncTime
  35. } else {
  36. inspectionLastInfo,_ := GetInspectionLastInfoByOrgID(org_id,patient.AdmissionNumber)
  37. if inspectionLastInfo.ID > 0 {
  38. sync_time = inspectionLastInfo.CreatedTime
  39. } else {
  40. sync_time = 0
  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.AdmissionNumber,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.AdmissionNumber,
  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: head.PatientId,
  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.AdmissionNumber
  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.AdmissionNumber
  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.HisUserId,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. if len(Head.CheckTime) == 0 {
  217. recordDateStr = time.Now().Format("2006-01-02 15:04")
  218. } else {
  219. dateTempArrOne := strings.Split(recordDateStr,"T")
  220. dateTempArrTwo := strings.Split(dateTempArrOne[1],".")
  221. recordDateStr = dateTempArrOne[0] + " " + dateTempArrTwo[0]
  222. }
  223. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  224. record_date := record_date_str.Unix()
  225. inspect_date := time.Unix(record_date,0).Format("2006-01-02 15:04")
  226. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  227. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  228. var total int
  229. var itotal int
  230. var RangeOptions string
  231. var RangeMin string
  232. var RangeMax string
  233. // // 判断检查类型
  234. ItemType := 1
  235. Range := strings.Split(report.Reference,"~")
  236. if len(Range) > 1 {
  237. RangeMin = Range[0]
  238. RangeMax = Range[1]
  239. ItemType = 1
  240. } else {
  241. ItemType = 2
  242. RangeOptions = report.Reference
  243. }
  244. 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
  245. if inspection_reference.ID > 0 {
  246. ItemType = inspection_reference.RangeType
  247. }
  248. if total <= 0 && len(Head.Item) > 0 {
  249. inspection_reference.OrgId = org_id
  250. inspection_reference.ProjectName = Head.Item
  251. inspection_reference.Project = Head.Item
  252. inspection_reference.ProjectId = project_id
  253. inspection_reference.ItemName = report.ItemName
  254. inspection_reference.ItemId = item_id
  255. inspection_reference.RangeType = ItemType
  256. inspection_reference.RangeMin = RangeMin
  257. inspection_reference.RangeMax = RangeMax
  258. // inspection_reference.RangeValue = RangeValue
  259. inspection_reference.RangeOptions = RangeOptions
  260. inspection_reference.Unit = report.Unit
  261. inspection_reference.Status = 1
  262. inspection_reference.CreatedTime = time.Now().Unix()
  263. inspection_reference.UpdatedTime = time.Now().Unix()
  264. inspection_reference.InspectDate = inspect_date
  265. inspection_reference.UTime = inspect_date
  266. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  267. if err != nil {
  268. tx.Rollback()
  269. }
  270. }
  271. 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
  272. if itotal <= 0 {
  273. inspection.PatientId = patient_id
  274. inspection.OrgId = org_id
  275. inspection.ProjectId = project_id
  276. inspection.ItemName = report.ItemName
  277. inspection.ProjectName = Head.Item
  278. inspection.InspectType = ItemType
  279. inspection.ItemId = item_id
  280. inspection.InspectValue = report.Result
  281. inspection.InspectDate = inspect_date
  282. inspection.RecordDate = record_date
  283. inspection.Status = 1
  284. inspection.CreatedTime = time.Now().Unix()
  285. inspection.UpdatedTime = time.Now().Unix()
  286. inspection.UTime = inspect_date
  287. inspection.HisUserId = his_user_id
  288. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  289. if err != nil {
  290. tx.Rollback()
  291. }
  292. //处理检验检查操作表里同步条数和状态
  293. cur_num := Head.ReportInfoItemHandleNum + 1
  294. Head.ReportInfoItemHandleNum = cur_num
  295. handleType := 2
  296. if cur_num == Head.ReportInfoItemNum {
  297. handleType = 1
  298. }
  299. middleReportHandle := models.MiddleReportHandle{
  300. ID: Head.ID,
  301. ReportInfoItemHandleNum: cur_num,
  302. HandleType: int64(handleType),
  303. UpdatedTime: time.Now().Unix(),
  304. }
  305. err = UpdateReportHandleListyOrgID(&middleReportHandle)
  306. if err != nil {
  307. tx.Rollback()
  308. }
  309. }
  310. tx.Commit()
  311. return nil,nil,err
  312. }
  313. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  314. func GetItemID(org_id int64,project_name string,item_name string,project_id int64) (item_id int64 ,err error) {
  315. var inspection_reference models.MiddleInspectionReference
  316. 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
  317. if inspection_reference.ID > 0 {
  318. return inspection_reference.ItemId,err
  319. } else {
  320. 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
  321. utils.InfoLog("inspection_reference: %v",inspection_reference)
  322. if inspection_reference.ItemId > 0 {
  323. return inspection_reference.ItemId + 1,err
  324. } else {
  325. return project_id * 1000 + 1 ,err
  326. }
  327. }
  328. }
  329. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  330. func GetProjectID(org_id int64,project_name string) (project_id int64 ,err error) {
  331. var inspection_reference models.MiddleInspectionReference
  332. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id,project_name).First(&inspection_reference).Error
  333. if inspection_reference.ID > 0 {
  334. return inspection_reference.ProjectId,err
  335. } else {
  336. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  337. utils.InfoLog("inspection_reference: %v",inspection_reference)
  338. if inspection_reference.ProjectId > 0 {
  339. return inspection_reference.ProjectId + 1,err
  340. } else {
  341. return 18001,err
  342. }
  343. }
  344. }
  345. // 根据机构ID获取该机构所有病人的admission_number (住院号)
  346. func GetUserInfoByOrgID(org_id int64) (patients []models.Patients ,err error) {
  347. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
  348. return
  349. }
  350. // 根据机构ID获取该机构所有病人的admission_number (住院号)
  351. func GetUserInfoByAdmission(admission_number string,org_id int64) (patients models.Patients ,err error) {
  352. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and admission_number = ? and status = 1", org_id,admission_number).First(&patients).Error
  353. return
  354. }
  355. type HeadInfo struct {
  356. ApplyNo string `json:"applyNo"`
  357. ReportId string `json:"reportId"`
  358. Name string `json:"name"`
  359. Sex string `json:"sex"`
  360. Age string `json:"age"`
  361. Dept string `json:"dept"`
  362. BedNo string `json:"bedNo"`
  363. PatientId string `json:"patientId"`
  364. PatientPid string `json:"patientPid"`
  365. SampleNo float64 `json:"sampleNo"`
  366. RequestTime string `json:"requestTime"`
  367. ExecuteTime string `json:"executeTime"`
  368. ReceiveTime string `json:"receiveTime"`
  369. Item string `json:"item"`
  370. CheckTime string `json:"checkTime"`
  371. PatientType string `json:"patientType"`
  372. RequestTimes string `json:"request_time"`
  373. }
  374. type ReportInfo struct {
  375. ReportId string `json:"reportId"`
  376. ItemCode string `json:"itemCode"`
  377. ItemName string `json:"itemName"`
  378. StdCode string `json:"stdCode"`
  379. Result string `json:"result"`
  380. Unit string `json:"unit"`
  381. SampleNo float64 `json:"sampleNo"`
  382. Reference string `json:"reference"`
  383. Prompt string `json:"prompt"`
  384. }
  385. func GetHead(patient_id string,start_time string,end_time string)(head []HeadInfo,str string){
  386. Domain := "http://www.szlg2h.cn:9991/xuetoushi/head/getHead?"
  387. apiurl := "patient_id=" + patient_id + "&check_time_start=" + start_time + "&check_time_end=" + end_time
  388. apiurl = url.PathEscape(apiurl)
  389. apiurl = Domain + apiurl
  390. utils.InfoLog("url: %v",apiurl)
  391. var respJSON []HeadInfo
  392. httplib.Get(apiurl).ToJSON(&respJSON)
  393. jsonStr, _ := json.Marshal(respJSON)
  394. utils.InfoLog("请求患者检查头部数据接口返回数据: %v",string(jsonStr))
  395. return respJSON,string(jsonStr)
  396. }
  397. func GetReport(report_id string)(head []ReportInfo,str string){
  398. Domain := "http://www.szlg2h.cn:9991/xuetoushi/detail/getDetail?"
  399. apiurl := "report_id=" + report_id
  400. apiurl = url.PathEscape(apiurl)
  401. apiurl = Domain + apiurl
  402. var respJSON []ReportInfo
  403. httplib.Get(apiurl).ToJSON(&respJSON)
  404. jsonStr, _ := json.Marshal(respJSON)
  405. utils.InfoLog("请求患者检查详情数据接口返回数据: %v",string(jsonStr))
  406. return respJSON,string(jsonStr)
  407. }