gzky_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. "encoding/json"
  7. _ "encoding/json"
  8. "encoding/xml"
  9. _ "fmt"
  10. "github.com/astaxie/beego/httplib"
  11. _ "github.com/jinzhu/gorm"
  12. "net/url"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // 广州康允血液透析中心 检验检查对接
  18. type XmlKeyGzky struct {
  19. XMLName xml.Name `xml:"string"`
  20. StringName string `xml:"name,attr"`
  21. InnerText string `xml:",innerxml"`
  22. }
  23. // 获取艾迪康的key
  24. func GetAdKeyGzky(logid string, password string) (key string) {
  25. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
  26. apiurl := "logid=" + logid + "&password=" + password
  27. apiurl = url.PathEscape(apiurl)
  28. apiurl = Domain + apiurl
  29. XmlKeyGzky := XmlKeyGzky{}
  30. httplib.Get(apiurl).ToXML(&XmlKeyGzky)
  31. return XmlKeyGzky.InnerText
  32. }
  33. type ADStringGzky struct {
  34. XMLName xml.Name `xml:"string" json:"string"`
  35. NewDataSetGzky rpnewDataSetGzky `xml:"NewDataSet" json:"NewDataSet"`
  36. }
  37. type rpnewDataSetGzky struct {
  38. ListTableGzky []rplisttableGzky `xml:"listtable" json:"listtable"`
  39. }
  40. type rplisttableGzky struct {
  41. Id string `xml:"Id" json:"Id"`
  42. ReportType string `xml:"ReportType" json:"ReportType"`
  43. AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"`
  44. PatientName string `xml:"PatientName" json:"PatientName"`
  45. Bgrq string `xml:"Bgrq" json:"Bgrq"`
  46. Instrument string `xml:"Instrument" json:"Instrument"`
  47. LisDate string `xml:"LisDate" json:"LisDate"`
  48. Sampleid string `xml:"Sampleid" json:"Sampleid"`
  49. CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
  50. Repno string `xml:"Repno" json:"Repno"`
  51. // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"`
  52. // DownFlag string `xml:"DownFlag" json:"DownFlag"`
  53. // DownDate string `xml:"DownDate" json:"DownDate"`
  54. Sjrq string `xml:"Sjrq" json:"Sjrq"`
  55. // Sjys string `xml:"Sjys" json:"Sjys"`
  56. // Brnl string `xml:"Brnl" json:"Brnl"`
  57. // Brxb string `xml:"Brxb" json:"Brxb"`
  58. Bbzl string `xml:"bbzl" json:"bbzl"`
  59. // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"`
  60. // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"`
  61. // PageType string `xml:"PageType" json:"PageType"`
  62. PatientNo string `xml:"PatientNo" json:"PatientNo"`
  63. // SfzId string `xml:"SfzId" json:"SfzId"`
  64. // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"`
  65. // Nationality string `xml:"Nationality" json:"Nationality"`
  66. // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"`
  67. // Str1 string `xml:"Str1" json:"Str1"`
  68. // Str2 string `xml:"Str2" json:"Str2"`
  69. // Str3 string `xml:"Str3" json:"Str3"`
  70. // Str4 string `xml:"Str4" json:"Str4"`
  71. // Str5 string `xml:"Str5" json:"Str5"`
  72. // TWBG string `xml:"TWBG" json:"TWBG"`
  73. }
  74. // 获取机构一段时间内检验检查列表
  75. func GetAdGetReportListGzky(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADStringGzky) {
  76. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList"
  77. //apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
  78. //apiurl = url.PathEscape(apiurl)
  79. //apiurl = Domain + apiurl
  80. //utils.InfoLog("url0: %v", apiurl)
  81. var xmlKey ADStringGzky
  82. req := httplib.Post(Domain)
  83. req.Param("Key", Key)
  84. req.Param("BeginDateTime", BeginDateTime)
  85. req.Param("EndDateTime", EndDateTime)
  86. req.Param("TypeDateTime", TypeDateTime)
  87. req.Param("AgainFlag", AgainFlag)
  88. xmlString, _ := req.String()
  89. xmlString = strings.Replace(xmlString, "&lt;", "<", -1)
  90. xmlString = strings.Replace(xmlString, "&gt;", ">", -1)
  91. xml.Unmarshal([]byte(xmlString), &xmlKey)
  92. return xmlKey
  93. }
  94. type AdkCheckGzky struct {
  95. Cgbg AdkSResultGzky `json:"常规报告"`
  96. }
  97. type AdkSResultGzky struct {
  98. Item []AdkItemGzky `json:"item"`
  99. }
  100. type AdkItemGzky struct {
  101. Adiconbarcode string `json:"AdiconBarcode"`
  102. Patientname string `json:"PatientName"`
  103. Sex string `json:"Sex"`
  104. Age string `json:"Age"`
  105. Reportdate string `json:"ReportDate"`
  106. Itemcode string `json:"ItemCode"`
  107. ItemnameCn string `json:"ItemName_CN"`
  108. ItemnameEn string `json:"ItemName_EN"`
  109. Result string `json:"Result"`
  110. Resulthint string `json:"ResultHint"`
  111. Resultreference string `json:"ResultReference"`
  112. Resultunit string `json:"ResultUnit"`
  113. Zhmc string `json:"zhmc"`
  114. }
  115. func GetAdkInfoGzky(key string, id string) (str AdkCheckGzky, checkStr string) {
  116. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListByAdiconBarocde?"
  117. //apiurl := "key=" + key + "&AdiconBarocde=" + id
  118. //apiurl = url.PathEscape(apiurl)
  119. //apiurl = Domain + apiurl
  120. //utils.InfoLog("url1: %v", apiurl)
  121. xmlKey := XmlKeyGzky{}
  122. rep := httplib.Post(Domain)
  123. rep.Param("Key", key)
  124. rep.Param("AdiconBarcode", id)
  125. rep.ToXML(&xmlKey)
  126. utils.InfoLog("InnerText: %v", xmlKey.InnerText)
  127. var checkJSON AdkCheckGzky
  128. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  129. return checkJSON, xmlKey.InnerText
  130. }
  131. // 广州康允透析中心Lis同步
  132. func SyncLisGzky(flag int64) (err error) {
  133. // 第一步:获取艾迪康下发的Key
  134. org_id := int64(10567)
  135. login := "RH0395"
  136. password := "abc123456"
  137. key := GetAdKeyGzky(login, password)
  138. utils.InfoLog("key:%v", key)
  139. // 第二步:获取上一次同步的时间点
  140. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  141. var sync_time int64
  142. if syncLastInfo.ID > 0 {
  143. sync_time = syncLastInfo.SyncTime - 60*60*24*3
  144. } else {
  145. sync_time = 1730390400
  146. }
  147. if flag > 0 {
  148. sync_time = 1730390400
  149. }
  150. nowTimes := time.Now().Unix()
  151. // syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
  152. if sync_time < nowTimes {
  153. for i := sync_time; i < nowTimes; i = i + 86400 {
  154. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  155. adString := GetAdGetReportListGzky(key, tempsyncTimes, tempsyncTimes, "2", "1")
  156. listtable := adString.NewDataSetGzky.ListTableGzky
  157. if len(listtable) >= 1 {
  158. // 第三步:根据检查列表获取的ID来获取检验检查详情
  159. for _, list := range listtable {
  160. listId := list.AdiconBarcode
  161. checkInfo, _ := GetAdkInfoGzky(key, listId)
  162. //utils.InfoLog("checkStr: %v", checkStr)
  163. for _, info := range checkInfo.Cgbg.Item {
  164. // 根据姓名 去查询对应的patient_id
  165. patient, _ := GetPatientID(org_id, info.Patientname)
  166. utils.InfoLog("patient: %v", patient)
  167. if patient.ID > 0 {
  168. GzkyReportInfoHandle(info, org_id, patient.ID)
  169. }
  170. }
  171. }
  172. var syncInfo models.MiddleSyncInfo
  173. syncInfo.OrgId = org_id
  174. syncInfo.SyncTime = i
  175. syncInfo.SyncResultType = 1
  176. syncInfo.SyncRsultRemark = "同步成功"
  177. syncInfo.SyncTotalNum = 0
  178. syncInfo.SyncSuccessNum = 0
  179. syncInfo.SyncInfo = ""
  180. syncInfo.CreateTime = time.Now().Unix()
  181. syncInfo.UpdateTime = time.Now().Unix()
  182. cwderr := CreateSyncInfo(&syncInfo)
  183. if cwderr != nil {
  184. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  185. return
  186. }
  187. }
  188. }
  189. }
  190. SyncToGzky()
  191. TreatSgnForGzky(10567)
  192. return
  193. }
  194. func SyncToGzky() {
  195. utils.TraceLog("检验检查同步任务开始执行")
  196. org_id := int64(10567)
  197. // 第一步:跟进org_id 去中间库查出需要同步的数据
  198. //inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  199. inspections, _ := GetSyncInspectionByOrgId(org_id)
  200. // 第二步:将数据同步到业务库
  201. //if len(inspection_references) > 0 {
  202. // for _, inspection_reference := range inspection_references {
  203. // SyncInspectionReference(&inspection_reference)
  204. // }
  205. //}
  206. if len(inspections) > 0 {
  207. for _, inspection := range inspections {
  208. if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  209. SyncGzkyInspection(&inspection)
  210. }
  211. }
  212. }
  213. utils.SuccessLog("检验检查同步任务完成")
  214. }
  215. // 从机构将数据同步到中间库
  216. func SyncGzkyInspection(data *models.MiddleInspection) error {
  217. tx := writeDb.Begin()
  218. var inspection models.Inspection
  219. var total int
  220. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error
  221. if total <= 0 {
  222. inspection.OrgId = data.OrgId
  223. inspection.PatientId = data.PatientId
  224. inspection.ProjectName = data.ProjectName
  225. // inspection.Project = data.ProjectName
  226. inspection.ProjectId = data.SysProjectId
  227. inspection.ItemName = data.ItemName
  228. inspection.ItemId = data.SysItemId
  229. inspection.InspectType = int64(data.InspectType)
  230. inspection.InspectValue = data.InspectValue
  231. inspection.InspectTips = data.InspectTips
  232. inspection.InspectDate = data.RecordDate
  233. inspection.Status = 1
  234. inspection.CreatedTime = time.Now().Unix()
  235. inspection.UpdatedTime = time.Now().Unix()
  236. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  237. if err != nil {
  238. tx.Rollback()
  239. }
  240. data.IsSync = 1
  241. data.SyncId = inspection.ID
  242. data.UpdatedTime = time.Now().Unix()
  243. ierr := writeMiddleDb.Save(&data).Error
  244. if ierr != nil {
  245. tx.Rollback()
  246. }
  247. }
  248. tx.Commit()
  249. return err
  250. }
  251. // 同步到正式库后,确认透前肾功能和透后肾功能
  252. // 计算KT/V 和 URR
  253. func TreatSgnForGzky(org_id int64) error {
  254. tx := writeDb.Begin()
  255. var inspection []models.Inspection
  256. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = 15 and sgn_treat = 0 and status = 1", org_id).Find(&inspection).Error
  257. if len(inspection) > 0 {
  258. for _, insp := range inspection {
  259. var inspectionInfo models.Inspection
  260. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date = ? and sgn_treat = 0 and status = 1", org_id, insp.PatientId, insp.ItemId, insp.InspectDate).First(&inspectionInfo).Error
  261. if inspectionInfo.ID > 0 {
  262. // 查询前面两天是否有数据,有的话,当前数据有透后数据
  263. startData := inspectionInfo.InspectDate - 60*60*48
  264. var inspectionBefore models.Inspection
  265. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date >= ? and inspect_date < ? and sgn_treat = 0 and status = 1", org_id, insp.PatientId, insp.ItemId, startData, insp.InspectDate).First(&inspectionBefore).Error
  266. if inspectionBefore.ID > 0 {
  267. inspectionBefore.SgnTreat = 1
  268. inspectionBefore.UpdatedTime = time.Now().Unix()
  269. err = writeDb.Save(&inspectionBefore).Error
  270. if err != nil {
  271. tx.Rollback()
  272. }
  273. insp.ProjectId = 8060
  274. switch insp.ItemId {
  275. case 37: //尿素
  276. insp.ItemId = 8061
  277. break
  278. case 38: //尿酸
  279. insp.ItemId = 10834
  280. break
  281. case 42: //β2-微球蛋白
  282. insp.ItemId = 10835
  283. break
  284. case 115: //肌酐
  285. insp.ItemId = 8060
  286. break
  287. }
  288. insp.UpdatedTime = time.Now().Unix()
  289. insp.SgnTreat = 1
  290. err = writeDb.Save(&insp).Error
  291. if err != nil {
  292. tx.Rollback()
  293. }
  294. } else {
  295. // 前面两天没有查到数据,则可能当前数据是透前数据,后面两天查询到的数据是透后数据
  296. endData := inspectionInfo.InspectDate + 60*60*48
  297. var inspectionEnd models.Inspection
  298. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date > ? and inspect_date <= ? and sgn_treat = 0 and status = 1", org_id, insp.PatientId, insp.ItemId, insp.InspectDate, endData).First(&inspectionEnd).Error
  299. if inspectionEnd.ID > 0 {
  300. insp.SgnTreat = 1
  301. insp.UpdatedTime = time.Now().Unix()
  302. err = writeDb.Save(&insp).Error
  303. if err != nil {
  304. tx.Rollback()
  305. }
  306. inspectionEnd.ProjectId = 8060
  307. switch inspectionEnd.ItemId {
  308. case 37: //尿素
  309. inspectionEnd.ItemId = 8061
  310. break
  311. case 38: //尿酸
  312. inspectionEnd.ItemId = 10834
  313. break
  314. case 42: //β2-微球蛋白
  315. inspectionEnd.ItemId = 10835
  316. break
  317. case 115: //肌酐
  318. inspectionEnd.ItemId = 8060
  319. break
  320. }
  321. inspectionEnd.UpdatedTime = time.Now().Unix()
  322. inspectionEnd.SgnTreat = 1
  323. err = writeDb.Save(&inspectionEnd).Error
  324. if err != nil {
  325. tx.Rollback()
  326. }
  327. } else {
  328. // 如果前面两天和后面两天都没有数据,则直接改变状体
  329. insp.SgnTreat = 1
  330. insp.UpdatedTime = time.Now().Unix()
  331. err = writeDb.Save(&insp).Error
  332. if err != nil {
  333. tx.Rollback()
  334. }
  335. }
  336. }
  337. } else {
  338. continue
  339. }
  340. }
  341. }
  342. tx.Commit()
  343. return err
  344. }
  345. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  346. func GetGzkyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  347. var inspection_reference models.MiddleInspectionReference
  348. 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
  349. if inspection_reference.ID > 0 {
  350. return inspection_reference.ItemId, err
  351. } else {
  352. 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
  353. utils.InfoLog("inspection_reference: %v", inspection_reference)
  354. if inspection_reference.ItemId > 0 {
  355. return inspection_reference.ItemId + 1, err
  356. } else {
  357. return project_id*100 + 1, err
  358. }
  359. }
  360. }
  361. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  362. func GetGzkyProjectID(org_id int64, project_name string) (project_id int64, err error) {
  363. var inspection_reference models.MiddleInspectionReference
  364. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  365. if inspection_reference.ID > 0 {
  366. return inspection_reference.ProjectId, err
  367. } else {
  368. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  369. if inspection_reference.ProjectId > 0 {
  370. return inspection_reference.ProjectId + 1, err
  371. } else {
  372. return 1056701, err
  373. }
  374. }
  375. }
  376. func GzkyReportInfoHandle(CheckInfo AdkItemGzky, org_id int64, patientId int64) {
  377. tx := writeMiddleDb.Begin()
  378. var total int
  379. var RangeMin string
  380. var RangeMax string
  381. timstr := strings.Split(CheckInfo.Reportdate, ".")
  382. recordDateStr := timstr[0]
  383. if len(recordDateStr) == 0 {
  384. recordDateStr = time.Now().Format("2006-01-02 15:04")
  385. }
  386. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  387. record_date := record_date_str.Unix()
  388. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  389. // // 判断检查类型
  390. ItemType := 1
  391. Range := strings.Split(CheckInfo.Resultreference, "-")
  392. if len(Range) > 1 {
  393. RangeMin = Range[0]
  394. RangeMax = Range[1]
  395. ItemType = 1
  396. } else {
  397. ItemType = 2
  398. }
  399. var inspection models.MiddleInspection
  400. var inspection_reference models.MiddleInspectionReference
  401. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_code = ? and status = 1", org_id, CheckInfo.Zhmc, CheckInfo.Itemcode).Find(&inspection_reference).Count(&total).Error
  402. if inspection_reference.ID > 0 {
  403. ItemType = inspection_reference.RangeType
  404. }
  405. if total <= 0 {
  406. // 如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息
  407. project_id, _ := GetChydProjectID(org_id, CheckInfo.Zhmc)
  408. item_id, _ := GetChydItemID(org_id, CheckInfo.Zhmc, CheckInfo.ItemnameCn, project_id)
  409. inspection_reference.OrgId = org_id
  410. inspection_reference.ProjectName = CheckInfo.Zhmc
  411. inspection_reference.Project = CheckInfo.Zhmc
  412. inspection_reference.ProjectId = project_id
  413. inspection_reference.ItemName = CheckInfo.ItemnameCn
  414. inspection_reference.ItemNameAddition = CheckInfo.ItemnameEn
  415. inspection_reference.ItemId = item_id
  416. inspection_reference.ItemCode = CheckInfo.Itemcode
  417. inspection_reference.RangeType = ItemType
  418. inspection_reference.RangeMin = RangeMin
  419. inspection_reference.RangeMax = RangeMax
  420. // inspection_reference.RangeValue = RangeValue
  421. //inspection_reference.RangeOptions = RangeOptions
  422. inspection_reference.Unit = CheckInfo.Resultunit
  423. inspection_reference.Status = 1
  424. inspection_reference.CreatedTime = time.Now().Unix()
  425. inspection_reference.UpdatedTime = time.Now().Unix()
  426. inspection_reference.InspectDate = inspect_date
  427. inspection_reference.UTime = inspect_date
  428. inspection_reference.Adiconbarcode = CheckInfo.Adiconbarcode
  429. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  430. if err != nil {
  431. tx.Rollback()
  432. }
  433. }
  434. var itotal int
  435. 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, inspection_reference.ProjectId, inspection_reference.ItemId, record_date, patientId).Find(&inspection).Count(&itotal).Error
  436. if itotal <= 0 {
  437. inspection.PatientId = patientId
  438. inspection.OrgId = org_id
  439. inspection.ProjectId = inspection_reference.ProjectId
  440. inspection.ItemName = inspection_reference.ItemName
  441. inspection.ProjectName = inspection_reference.ProjectName
  442. inspection.InspectType = ItemType
  443. inspection.ItemId = inspection_reference.ItemId
  444. inspection.InspectValue = CheckInfo.Result
  445. inspection.InspectDate = inspect_date
  446. inspection.RecordDate = record_date
  447. //inspection.InspectTips = result.AntiCode
  448. inspection.Status = 1
  449. inspection.CreatedTime = time.Now().Unix()
  450. inspection.UpdatedTime = time.Now().Unix()
  451. inspection.SysProjectId = inspection_reference.XtProjectId
  452. inspection.SysItemId = inspection_reference.XtItemId
  453. inspection.UTime = inspect_date
  454. inspection.HisUserId = strconv.FormatInt(patientId, 10)
  455. inspection.Adiconbarcode = CheckInfo.Adiconbarcode
  456. if inspection_reference.ItemId == 103180107 || inspection_reference.ItemId == 103180301 {
  457. if strings.Index(CheckInfo.Result, "阳性") != -1 {
  458. inspection.InspectValue = "阳性"
  459. } else {
  460. inspection.InspectValue = "阴性"
  461. }
  462. }
  463. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  464. if err != nil {
  465. tx.Rollback()
  466. }
  467. }
  468. tx.Commit()
  469. }
  470. //
  471. //type gzkylis struct {
  472. // Finputdate string `gorm:"column:F_InputDate" json:"F_InputDate"`
  473. // Fname string `gorm:"column:F_Name" json:"F_Name"`
  474. // Fsex string `gorm:"column:F_Sex" json:"F_Sex"`
  475. // Fage string `gorm:"column:F_Age" json:"F_Age"`
  476. // Fhospnaturalitem string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem"`
  477. // Fhospnaturalitemname string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName"`
  478. // Fhospitem string `gorm:"column:F_HospItem" json:"F_HospItem"`
  479. // Fhospitemname string `gorm:"column:F_HospItemName" json:"F_HospItemName"`
  480. // Fresult string `gorm:"column:F_Result" json:"F_Result"`
  481. // Funit string `gorm:"column:F_Unit" json:"F_Unit"`
  482. // Fhint string `gorm:"column:F_Hint" json:"F_Hint"`
  483. // Freference string `gorm:"column:F_Reference" json:"F_Reference"`
  484. // Frecordtime string `gorm:"column:F_RecordTime" json:"F_RecordTime"`
  485. //}
  486. //
  487. //func (gzkylis) TableName() string {
  488. // return "V_KM_LIS_Result"
  489. //}
  490. //
  491. //func GetGzkyLis(rdb *gorm.DB, synctime string) (record []*gzkylis, err error) {
  492. // err = rdb.Model(&gzkylis{}).Where("F_RecordTime >= ? ", synctime).Find(&record).Error
  493. // return
  494. //}
  495. //
  496. //// 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  497. //func GetGzkyProjectID(org_id int64, project_name string) (project_id int64, err error) {
  498. // var inspection_reference models.MiddleInspectionReference
  499. // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? ", org_id, project_name).First(&inspection_reference).Error
  500. // if inspection_reference.ID > 0 {
  501. // return inspection_reference.ProjectId, err
  502. // } else {
  503. // err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  504. // if inspection_reference.ProjectId > 0 {
  505. // return inspection_reference.ProjectId + 1, err
  506. // } else {
  507. // return 1056701, err
  508. // }
  509. // }
  510. //}
  511. //
  512. //// 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  513. //func GetGzkyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  514. // var inspection_reference models.MiddleInspectionReference
  515. // 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
  516. // if inspection_reference.ID > 0 {
  517. // return inspection_reference.ItemId, err
  518. // } else {
  519. // 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
  520. // if inspection_reference.ItemId > 0 {
  521. // return inspection_reference.ItemId + 1, err
  522. // } else {
  523. // return project_id*100 + 1, err
  524. // }
  525. // }
  526. //}
  527. //
  528. //// 汕头三优血液透析中心
  529. //func SyncGzkyLis() (err error) {
  530. // org_id := int64(10567)
  531. //
  532. // org := &models.DataUploadConfig{
  533. // OrgId: org_id,
  534. // DbHost: "127.0.0.1",
  535. // DbPort: "1433",
  536. // DbPass: "1Q2W3e4r!@#$",
  537. // DbUser: "kyy",
  538. // DbName: "kmdb",
  539. // }
  540. //
  541. // orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  542. // if err != nil {
  543. // utils.ErrorLog("创建数据库连接失败:%v", err)
  544. // return
  545. // }
  546. //
  547. // // var sync_time int64
  548. // // sync_time = 1627747200
  549. //
  550. // // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  551. // // utils.InfoLog("IdCardNo:%v",scpaLis)
  552. //
  553. // // 第一步:获取上一次同步的时间点
  554. // syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  555. // var sync_time int64
  556. // if syncLastInfo.ID > 0 {
  557. // sync_time = syncLastInfo.SyncTime
  558. // } else {
  559. // sync_time = 1685548800
  560. // }
  561. //
  562. // SyncTime := time.Unix(sync_time, 0)
  563. // syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
  564. //
  565. // fmt.Println(SyncTime)
  566. // // 第二步:获取时间内所有检验结果
  567. // gzkyLis, _ := GetGzkyLis(orgDb, syncTimeStr)
  568. //
  569. // if len(gzkyLis) > 0 {
  570. // for _, LisInfo := range gzkyLis {
  571. // utils.InfoLog("LisInfo:%v", LisInfo)
  572. // utils.InfoLog("患者姓名:%v", LisInfo.Fname)
  573. // brxm := LisInfo.Fname
  574. // // 根据姓名获取患者ID
  575. // patient, _ := GetUserInfoByName(org_id, brxm)
  576. // if patient.ID > 0 {
  577. // project_id, _ := GetGzkyProjectID(org_id, LisInfo.Fhospnaturalitemname)
  578. // item_id, _ := GetGzkyItemID(org_id, LisInfo.Fhospnaturalitemname, LisInfo.Fhospitemname, project_id)
  579. // tx := writeMiddleDb.Begin()
  580. // var inspection models.MiddleInspection
  581. // var inspection_reference models.MiddleInspectionReference
  582. //
  583. // tempTime := strings.Split(LisInfo.Frecordtime, "T")
  584. // utils.InfoLog("tempTime1:%v", tempTime[0])
  585. // utils.InfoLog("tempTime2:%v", tempTime[1])
  586. // tempTime1 := strings.Split(tempTime[1], "Z")
  587. // tempRecord := tempTime[0] + " " + tempTime1[0]
  588. //
  589. // utils.InfoLog("tempTime3:%v", tempRecord)
  590. //
  591. // recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
  592. // utils.InfoLog("tempTime4:%v", recordTime)
  593. // inspect_date := recordTime.Format("2006-01-02 15:04")
  594. // recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  595. // recordTimeInt := recordTimeStr.Unix()
  596. // utils.InfoLog("tempTime5:%v", recordTimeInt)
  597. // utils.InfoLog("tempTime6:%v", recordTimeStr)
  598. //
  599. // var total int
  600. // var RangeOptions string
  601. // var RangeMin string
  602. // var RangeMax string
  603. // var ItemType int
  604. // typeFlag := strings.Contains(LisInfo.Freference, "阴性")
  605. // if typeFlag {
  606. // ItemType = 2
  607. // RangeMin = LisInfo.Freference
  608. // RangeOptions = LisInfo.Fhint
  609. // } else {
  610. // Range := strings.Split(LisInfo.Freference, "-")
  611. // if len(Range) > 1 {
  612. // RangeMin = Range[0]
  613. // RangeMax = Range[1]
  614. // ItemType = 1
  615. // } else {
  616. // ItemType = 2
  617. // RangeMin = LisInfo.Freference
  618. // }
  619. // RangeOptions = LisInfo.Fhint
  620. // }
  621. //
  622. // 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
  623. // if total <= 0 {
  624. // inspection_reference.OrgId = org_id
  625. // inspection_reference.ProjectName = LisInfo.Fhospnaturalitemname
  626. // inspection_reference.Project = LisInfo.Fhospnaturalitem
  627. // inspection_reference.ProjectId = project_id
  628. // inspection_reference.ItemName = LisInfo.Fhospitemname
  629. // inspection_reference.ItemNameAddition = LisInfo.Fhospitem
  630. // inspection_reference.ItemId = item_id
  631. // inspection_reference.RangeType = ItemType
  632. // inspection_reference.RangeMin = RangeMin
  633. // inspection_reference.RangeMax = RangeMax
  634. // inspection_reference.RangeValue = LisInfo.Freference
  635. // inspection_reference.RangeOptions = RangeOptions
  636. // inspection_reference.Unit = LisInfo.Funit
  637. // inspection_reference.Status = 1
  638. // inspection_reference.CreatedTime = time.Now().Unix()
  639. // inspection_reference.UpdatedTime = time.Now().Unix()
  640. // inspection_reference.InspectDate = tempRecord
  641. // inspection_reference.UTime = tempRecord
  642. // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  643. // if err != nil {
  644. // tx.Rollback()
  645. // }
  646. // }
  647. // var itotal int
  648. // 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, patient.ID).Find(&inspection).Count(&itotal).Error
  649. // if itotal <= 0 {
  650. // // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  651. // // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  652. // inspection.PatientId = patient.ID
  653. // inspection.OrgId = org_id
  654. // inspection.ProjectId = project_id
  655. // inspection.ItemName = inspection_reference.ItemName
  656. // inspection.ProjectName = inspection_reference.ProjectName
  657. // inspection.InspectType = ItemType
  658. // inspection.ItemId = item_id
  659. // inspection.InspectValue = LisInfo.Fresult
  660. // inspection.InspectDate = inspect_date
  661. // inspection.RecordDate = recordTimeInt
  662. // inspection.InspectTips = LisInfo.Fhint
  663. // inspection.Status = 1
  664. // inspection.CreatedTime = time.Now().Unix()
  665. // inspection.UpdatedTime = time.Now().Unix()
  666. // // inspection.UTime = record_date.Format(timeLayout)
  667. // // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  668. // inspection.SysProjectId = inspection_reference.XtProjectId
  669. // inspection.SysItemId = inspection_reference.XtItemId
  670. // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  671. // if err != nil {
  672. // tx.Rollback()
  673. // }
  674. // }
  675. // tx.Commit()
  676. // } else {
  677. // continue
  678. // }
  679. // }
  680. // }
  681. // var syncInfo models.MiddleSyncInfo
  682. // syncInfo.OrgId = org_id
  683. // syncInfo.SyncTime = time.Now().Unix()
  684. // syncInfo.SyncResultType = 1
  685. // syncInfo.SyncRsultRemark = "同步成功"
  686. // syncInfo.SyncTotalNum = 0
  687. // syncInfo.SyncSuccessNum = 0
  688. // syncInfo.SyncInfo = ""
  689. // syncInfo.CreateTime = time.Now().Unix()
  690. // syncInfo.UpdateTime = time.Now().Unix()
  691. //
  692. // cwderr := CreateSyncInfo(&syncInfo)
  693. // if cwderr != nil {
  694. // utils.ErrorLog("创建同步信息失败:%v", cwderr)
  695. // return
  696. // }
  697. // SyncToGzkytx()
  698. // return
  699. //}
  700. //
  701. //// func deleteMid(tx *gorm.DB) (err error) {
  702. //// err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  703. //// "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  704. //// "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  705. //// "WHERE t1.user_org_id = t2.orgid)tmp " +
  706. //// "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  707. //// "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  708. //// return
  709. //// }
  710. //
  711. //func SyncToGzkytx() {
  712. //
  713. // utils.TraceLog("检验检查同步任务开始执行")
  714. // org_id := int64(10567)
  715. //
  716. // // 第一步:跟进org_id 去中间库查出需要同步的数据
  717. // //inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  718. // inspections, _ := GetSyncInspectionByOrgId(org_id)
  719. //
  720. // // 第二步:将数据同步到业务库
  721. // //if len(inspection_references) > 0 {
  722. // // for _, inspection_reference := range inspection_references {
  723. // // SyncInspectionReference(&inspection_reference)
  724. // // }
  725. // //}
  726. //
  727. // if len(inspections) > 0 {
  728. // for _, inspection := range inspections {
  729. // if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  730. // SyncGzkyInspection(&inspection)
  731. // }
  732. // }
  733. // }
  734. // utils.SuccessLog("检验检查同步任务完成")
  735. //}
  736. //
  737. //// 从机构将数据同步到中间库
  738. //func SyncGzkyInspection(data *models.MiddleInspection) error {
  739. // tx := writeDb.Begin()
  740. // var inspection models.Inspection
  741. // var total int
  742. // err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error
  743. // if total <= 0 {
  744. //
  745. // inspection.OrgId = data.OrgId
  746. // inspection.PatientId = data.PatientId
  747. // inspection.ProjectName = data.ProjectName
  748. // // inspection.Project = data.ProjectName
  749. // inspection.ProjectId = data.SysProjectId
  750. // inspection.ItemName = data.ItemName
  751. // inspection.ItemId = data.SysItemId
  752. // inspection.InspectType = int64(data.InspectType)
  753. // inspection.InspectValue = data.InspectValue
  754. // inspection.InspectTips = data.InspectTips
  755. // inspection.InspectDate = data.RecordDate
  756. // inspection.Status = 1
  757. // inspection.CreatedTime = time.Now().Unix()
  758. // inspection.UpdatedTime = time.Now().Unix()
  759. //
  760. // err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  761. // if err != nil {
  762. // tx.Rollback()
  763. // }
  764. //
  765. // data.IsSync = 1
  766. // data.SyncId = inspection.ID
  767. // data.UpdatedTime = time.Now().Unix()
  768. // ierr := writeMiddleDb.Save(&data).Error
  769. // if ierr != nil {
  770. // tx.Rollback()
  771. // }
  772. // }
  773. // tx.Commit()
  774. // return err
  775. //}