ystx_service.go 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. type XmlKey struct {
  18. XMLName xml.Name `xml:"string"`
  19. StringName string `xml:"name,attr"`
  20. InnerText string `xml:",innerxml"`
  21. }
  22. // 获取艾迪康的key
  23. func GetAdKey(logid string, password string) (key string) {
  24. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
  25. apiurl := "logid=" + logid + "&password=" + password
  26. apiurl = url.PathEscape(apiurl)
  27. apiurl = Domain + apiurl
  28. xmlKey := XmlKey{}
  29. httplib.Get(apiurl).ToXML(&xmlKey)
  30. return xmlKey.InnerText
  31. }
  32. type ADString struct {
  33. XMLName xml.Name `xml:"string" json:"string"`
  34. NewDataSet rpnewDataSet `xml:"NewDataSet" json:"NewDataSet"`
  35. }
  36. type rpnewDataSet struct {
  37. ListTable []rplisttable `xml:"listtable" json:"listtable"`
  38. }
  39. type rplisttable struct {
  40. Id string `xml:"Id" json:"Id"`
  41. PatientName string `xml:"PatientName" json:"PatientName"`
  42. Bgrq string `xml:"Bgrq" json:"Bgrq"`
  43. Instrument string `xml:"Instrument" json:"Instrument"`
  44. LisDate string `xml:"LisDate" json:"LisDate"`
  45. Sampleid string `xml:"Sampleid" json:"Sampleid"`
  46. // ReportType string `xml:"ReportType" json:"ReportType"`
  47. // AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"`
  48. // CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
  49. // Repno string `xml:"Repno" json:"Repno"`
  50. // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"`
  51. // DownFlag string `xml:"DownFlag" json:"DownFlag"`
  52. // DownDate string `xml:"DownDate" json:"DownDate"`
  53. // Sjrq string `xml:"Sjrq" json:"Sjrq"`
  54. // Sjys string `xml:"Sjys" json:"Sjys"`
  55. // Brnl string `xml:"Brnl" json:"Brnl"`
  56. // Brxb string `xml:"Brxb" json:"Brxb"`
  57. // Bbzl string `xml:"bbzl" json:"bbzl"`
  58. // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"`
  59. // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"`
  60. // PageType string `xml:"PageType" json:"PageType"`
  61. // PatientNo string `xml:"PatientNo" json:"PatientNo"`
  62. // SfzId string `xml:"SfzId" json:"SfzId"`
  63. // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"`
  64. // Nationality string `xml:"Nationality" json:"Nationality"`
  65. // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"`
  66. // Str1 string `xml:"Str1" json:"Str1"`
  67. // Str2 string `xml:"Str2" json:"Str2"`
  68. // Str3 string `xml:"Str3" json:"Str3"`
  69. // Str4 string `xml:"Str4" json:"Str4"`
  70. // Str5 string `xml:"Str5" json:"Str5"`
  71. // TWBG string `xml:"TWBG" json:"TWBG"`
  72. }
  73. // 获取机构一段时间内检验检查列表
  74. func GetAdGetReportList(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADString) {
  75. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList?"
  76. apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
  77. apiurl = url.PathEscape(apiurl)
  78. apiurl = Domain + apiurl
  79. utils.InfoLog("url0: %v", apiurl)
  80. var xmlKey ADString
  81. xmlString, _ := httplib.Get(apiurl).String()
  82. xmlString = strings.Replace(xmlString, "&lt;", "<", -1)
  83. xmlString = strings.Replace(xmlString, "&gt;", ">", -1)
  84. xml.Unmarshal([]byte(xmlString), &xmlKey)
  85. return xmlKey
  86. }
  87. type adkCheck struct {
  88. ApplyId string `json:"applyid"`
  89. ReportNo string `json:"reportno"`
  90. PatName string `json:"patname"`
  91. Sex string `json:"sex"`
  92. Age string `json:"age"`
  93. DptName string `json:"dptname"`
  94. Testtype string `json:"testtype"`
  95. Testcode string `json:"testcode"`
  96. ExecuteTime string `json:"receivetime"`
  97. Checktime string `json:"checktime"`
  98. Checkcode string `json:"checkcode"`
  99. Result []adkResult `json:"result"`
  100. }
  101. func GetAdkInfo(key string, id string) (str []adkCheck, checkStr string) {
  102. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListById?"
  103. apiurl := "key=" + key + "&Id=" + id
  104. apiurl = url.PathEscape(apiurl)
  105. apiurl = Domain + apiurl
  106. utils.InfoLog("url1: %v", apiurl)
  107. xmlKey := XmlKey{}
  108. httplib.Get(apiurl).ToXML(&xmlKey)
  109. utils.InfoLog("url2: %v", xmlKey.InnerText)
  110. var checkJSON []adkCheck
  111. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  112. return checkJSON, xmlKey.InnerText
  113. }
  114. // 沅胜透析中心Lis同步
  115. func SyncLisYstx() (err error) {
  116. // 第一步:获取艾迪康下发的Key
  117. org_id := int64(9919)
  118. login := "R26028"
  119. password := "abc123"
  120. key := GetAdKey(login, password)
  121. // 第二步:获取上一次同步的时间点
  122. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  123. var sync_time int64
  124. if syncLastInfo.ID > 0 {
  125. sync_time = syncLastInfo.SyncTime + 86400
  126. } else {
  127. sync_time = 1577808000
  128. }
  129. nowTimes := time.Now().Unix()
  130. // syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
  131. if sync_time < nowTimes {
  132. for i := sync_time; i < nowTimes; i = i + 86400 {
  133. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  134. utils.InfoLog("tempsyncTimes:%v", tempsyncTimes)
  135. adString := GetAdGetReportList(key, tempsyncTimes, tempsyncTimes, "2", "2")
  136. listtable := adString.NewDataSet.ListTable
  137. utils.InfoLog("listtable: %v", listtable)
  138. if len(listtable) > 3 {
  139. // 第三步:根据检查列表获取的ID来获取检验检查详情
  140. for _, list := range listtable {
  141. listId := list.Id
  142. utils.InfoLog("listId: %v", listId)
  143. checkInfo, checkStr := GetAdkInfo(key, listId)
  144. utils.InfoLog("checkInfo: %v", checkInfo)
  145. utils.InfoLog("checkStr: %v", checkStr)
  146. for _, info := range checkInfo {
  147. // 根据姓名 去查询对应的patient_id
  148. patient, _ := GetPatientID(org_id, info.PatName)
  149. utils.InfoLog("patient: %v", patient)
  150. if patient.ID > 0 {
  151. middleReportHandle := models.MiddleReportHandle{
  152. OrgId: org_id,
  153. HisUserId: strconv.FormatInt(patient.ID, 10),
  154. ApplyNo: info.ApplyId,
  155. ReportId: info.ReportNo,
  156. Name: info.PatName,
  157. Sex: info.Sex,
  158. Age: info.Age,
  159. Dept: info.DptName,
  160. PatientId: strconv.FormatInt(patient.ID, 10),
  161. RequestTime: info.ExecuteTime,
  162. ExecuteTime: info.ExecuteTime,
  163. ReceiveTime: info.ExecuteTime,
  164. CheckTime: info.ExecuteTime,
  165. ReportInfo: checkStr,
  166. CreatedTime: time.Now().Unix(),
  167. UpdatedTime: time.Now().Unix(),
  168. }
  169. handleerr := CreateReportHandle(&middleReportHandle)
  170. if handleerr != nil {
  171. utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr)
  172. return
  173. }
  174. YsReportInfoHandle(info, org_id, patient.ID)
  175. }
  176. }
  177. }
  178. var syncInfo models.MiddleSyncInfo
  179. syncInfo.OrgId = org_id
  180. syncInfo.SyncTime = i
  181. syncInfo.SyncResultType = 1
  182. syncInfo.SyncRsultRemark = "同步成功"
  183. syncInfo.SyncTotalNum = 0
  184. syncInfo.SyncSuccessNum = 0
  185. syncInfo.SyncInfo = ""
  186. syncInfo.CreateTime = time.Now().Unix()
  187. syncInfo.UpdateTime = time.Now().Unix()
  188. cwderr := CreateSyncInfo(&syncInfo)
  189. if cwderr != nil {
  190. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  191. return
  192. }
  193. }
  194. }
  195. }
  196. return
  197. }
  198. // 根据姓名获取patient_id
  199. func GetPatientID(org_id int64, patient_name string) (patient models.Patients, err error) {
  200. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ? ", org_id, patient_name).First(&patient).Error
  201. return
  202. }
  203. func GetProjectInfo(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) {
  204. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error
  205. return
  206. }
  207. // 修改配置信息
  208. func UpdateInspectionReference(InspectionReference *models.MiddleInspectionReference) error {
  209. err := readMiddleDb.Model(&models.MiddleInspectionReference{}).Updates(&InspectionReference).Error
  210. return err
  211. }
  212. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  213. func GetYsProjectID(org_id int64, project_name string) (project_id int64, err error) {
  214. var inspection_reference models.MiddleInspectionReference
  215. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  216. if inspection_reference.ID > 0 {
  217. return inspection_reference.ProjectId, err
  218. } else {
  219. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  220. utils.InfoLog("inspection_reference: %v", inspection_reference)
  221. if inspection_reference.ProjectId > 0 {
  222. return inspection_reference.ProjectId + 1, err
  223. } else {
  224. return 38001, err
  225. }
  226. }
  227. }
  228. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  229. func GetGzjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  230. var inspection_reference models.MiddleInspectionReference
  231. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  232. if inspection_reference.ID > 0 {
  233. return inspection_reference.ProjectId, err
  234. } else {
  235. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  236. if inspection_reference.ProjectId > 0 {
  237. return inspection_reference.ProjectId + 1, err
  238. } else {
  239. return 116001, err
  240. }
  241. }
  242. }
  243. func YsReportInfoHandle(CheckInfo adkCheck, org_id int64, patientId int64) {
  244. // 1.先把所有的检查项弄出来
  245. for _, info := range CheckInfo.Result {
  246. // 根据project_id和item_id 判断该项是否存在
  247. curitemcode, _ := strconv.ParseInt(info.Itemcode, 10, 64)
  248. reference, _ := GetProjectInfo(curitemcode, org_id)
  249. utils.InfoLog("reference: %v", info)
  250. if reference.ID > 0 {
  251. if reference.ProjectId > 0 {
  252. LgthYsSync(info, reference.ProjectId, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  253. } else {
  254. project_id := int64(0)
  255. if len(info.Groupitemcode) > 0 {
  256. // 对没有project信息的项目进行修改
  257. project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
  258. reference.ProjectId = project_id
  259. reference.ProjectName = info.Groupitemcode
  260. reference.Project = info.Groupitemcode
  261. // 修改配置信息
  262. UpdateInspectionReference(&reference)
  263. }
  264. LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  265. }
  266. } else {
  267. project_id := int64(0)
  268. if len(info.Groupitemcode) > 0 {
  269. project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
  270. }
  271. LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  272. }
  273. }
  274. SyncToYstx()
  275. }
  276. type adkResult struct {
  277. Testno string `json:"testno"`
  278. Reportno string `json:"reportno"`
  279. Itemord string `json:"itemord"`
  280. Itemcode string `json:"itemcode"`
  281. Itemname string `json:"itemname"`
  282. ItemEname string `json:"itemename"`
  283. Result string `json:"result"`
  284. Units string `json:"units"`
  285. Testdate string `json:"testdate"`
  286. Eqpname string `json:"eqpname"`
  287. Groupitemcode string `json:"groupitemcode"`
  288. Groupitemname string `json:"groupitemname"`
  289. Limit string `json:"limit"`
  290. Resultstate string `json:"resultstate"`
  291. Criticalflag string `json:"criticalflag"`
  292. Itemename string `json:"itemename"`
  293. }
  294. // 从机构将数据同步到中间库
  295. func LgthYsSync(report adkResult, project_id int64, item_id int64, org_id int64, patient_id int64, receivetime string) (*models.MiddleInspectionReference, error) {
  296. tx := writeMiddleDb.Begin()
  297. var inspection models.MiddleInspection
  298. var inspection_reference models.MiddleInspectionReference
  299. recordDateStr := receivetime
  300. if len(receivetime) == 0 {
  301. recordDateStr = time.Now().Format("2006-01-02 15:04")
  302. }
  303. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  304. record_date := record_date_str.Unix()
  305. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  306. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  307. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  308. var total int
  309. var RangeOptions string
  310. var RangeMin string
  311. var RangeMax string
  312. // // 判断检查类型
  313. ItemType := 1
  314. Range := strings.Split(report.Limit, "-")
  315. if len(Range) > 1 {
  316. RangeMin = Range[0]
  317. RangeMax = Range[1]
  318. ItemType = 1
  319. } else {
  320. ItemType = 2
  321. RangeOptions = report.Limit
  322. }
  323. 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
  324. if inspection_reference.ID > 0 {
  325. ItemType = inspection_reference.RangeType
  326. }
  327. if total <= 0 {
  328. inspection_reference.OrgId = org_id
  329. inspection_reference.ProjectName = report.Groupitemcode
  330. inspection_reference.Project = report.Groupitemcode
  331. inspection_reference.ProjectId = project_id
  332. inspection_reference.ItemName = report.Itemname
  333. inspection_reference.ItemNameAddition = report.ItemEname
  334. inspection_reference.ItemId = item_id
  335. inspection_reference.RangeType = ItemType
  336. inspection_reference.RangeMin = RangeMin
  337. inspection_reference.RangeMax = RangeMax
  338. // inspection_reference.RangeValue = RangeValue
  339. inspection_reference.RangeOptions = RangeOptions
  340. inspection_reference.Unit = report.Units
  341. inspection_reference.Status = 1
  342. inspection_reference.CreatedTime = time.Now().Unix()
  343. inspection_reference.UpdatedTime = time.Now().Unix()
  344. inspection_reference.InspectDate = inspect_date
  345. inspection_reference.UTime = inspect_date
  346. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  347. if err != nil {
  348. tx.Rollback()
  349. }
  350. }
  351. var itotal int
  352. 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
  353. if itotal <= 0 {
  354. report.Result = strings.Replace(report.Result, "&gt;", ">", -1)
  355. report.Result = strings.Replace(report.Result, "&lt;", "<", -1)
  356. inspection.PatientId = patient_id
  357. inspection.OrgId = org_id
  358. inspection.ProjectId = project_id
  359. inspection.ItemName = inspection_reference.ItemName
  360. inspection.ProjectName = inspection_reference.ProjectName
  361. inspection.InspectType = ItemType
  362. inspection.ItemId = item_id
  363. inspection.InspectValue = report.Result
  364. inspection.InspectDate = inspect_date
  365. inspection.RecordDate = record_date
  366. inspection.InspectTips = report.Resultstate
  367. inspection.Status = 1
  368. inspection.CreatedTime = time.Now().Unix()
  369. inspection.UpdatedTime = time.Now().Unix()
  370. inspection.UTime = inspect_date
  371. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  372. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  373. if err != nil {
  374. tx.Rollback()
  375. }
  376. }
  377. // //处理检验检查操作表里同步条数和状态
  378. // cur_num := Head.ReportInfoItemHandleNum + 1
  379. // Head.ReportInfoItemHandleNum = cur_num
  380. // handleType := 2
  381. // if cur_num == Head.ReportInfoItemNum {
  382. // handleType = 1
  383. // }
  384. // middleReportHandle := models.MiddleReportHandle{
  385. // ID: Head.ID,
  386. // ReportInfoItemHandleNum: cur_num,
  387. // HandleType: int64(handleType),
  388. // UpdatedTime: time.Now().Unix(),
  389. // }
  390. // err = UpdateReportHandleListyOrgID(&middleReportHandle)
  391. // if err != nil {
  392. // tx.Rollback()
  393. // }
  394. // }
  395. tx.Commit()
  396. return nil, err
  397. }
  398. func SyncToYstx() {
  399. utils.TraceLog("检验检查同步任务开始执行")
  400. org_id := int64(9919)
  401. // 第一步:跟进org_id 去中间库查出需要同步的数据
  402. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  403. inspections, _ := GetSyncInspectionByOrgId(org_id)
  404. // 第二步:将数据同步到业务库
  405. if len(inspection_references) > 0 {
  406. for _, inspection_reference := range inspection_references {
  407. SyncInspectionReference(&inspection_reference)
  408. }
  409. }
  410. if len(inspections) > 0 {
  411. for _, inspection := range inspections {
  412. SyncInspection(&inspection)
  413. }
  414. }
  415. utils.SuccessLog("检验检查同步任务完成")
  416. }
  417. // 根据姓名获取patient_id
  418. func GetPatientGzjhList(org_id int64, schedulesTime int64) (schedules []*models.DialysisSchedule, err error) {
  419. err = readDb.Model(&models.DialysisSchedule{}).Where("user_org_id = ? and status = 1 and schedule_date = ?", org_id, schedulesTime).Preload("Patients", "status = 1").Find(&schedules).Error
  420. return
  421. }
  422. // 根据姓名获取admin_user_id
  423. func GetAdminUserId(org_id int64, doctor_name string) (doctor models.UserAdminRole, err error) {
  424. err = writeUserDb.Model(&models.UserAdminRole{}).Where("org_id = ? and status = 1 and user_name = ?", org_id, doctor_name).First(&doctor).Error
  425. return
  426. }
  427. func GetPatientGzjhByOrgId(org_id int64) (patientList []*models.Patients, err error) {
  428. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error
  429. return
  430. }
  431. type gzjhResult struct {
  432. ErrorCode int64 `json:"error_code"`
  433. Result []gzjhyz `json:"data"`
  434. }
  435. type gzjhyz struct {
  436. HisUserId string `json:"his_user_id"`
  437. AdviceId int64 `json:"advice_id"`
  438. UserName string `json:"user_name"`
  439. DoctorId string `json:"doctor_id"`
  440. AdviceType string `json:"advice_type"`
  441. StartTime string `json:"start_time"`
  442. AdviceName string `json:"advice_name"`
  443. AdviceDesc string `json:"advice_desc"`
  444. SingleDose string `json:"single_dose"`
  445. SingleDoseUnit string `json:"single_dose_unit"`
  446. PrescribingNumber string `json:"prescribing_number"`
  447. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  448. DeliveryWay string `json:"delivery_way"`
  449. ExecutionFrequency string `json:"execution_frequency"`
  450. State string `json:"state"`
  451. CTime string `json:"c_time"`
  452. UTime string `json:"u_time"`
  453. }
  454. func GetGzjhYz(sync_time string, his_user_id string) (str gzjhResult, checkStr string) {
  455. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryyzxx"
  456. rep := httplib.Post(apiurl)
  457. rep.Param("sync_time", sync_time)
  458. rep.Param("access_token", "")
  459. rep.Param("his_user_id", his_user_id)
  460. // str, _ := rep.String()
  461. xmlKey := XmlKey{}
  462. rep.ToXML(&xmlKey)
  463. var checkJSON gzjhResult
  464. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  465. return checkJSON, xmlKey.InnerText
  466. }
  467. func CreateDoctorAdviceHandle(advice *models.XtDoctorAdvice) error {
  468. err := writeDb.Create(&advice).Error
  469. return err
  470. }
  471. func GetAdviceBySyncAdviceId(orgId int64, sync_advice_id int64) (advice models.XtDoctorAdvice, err error) {
  472. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_advice_id = ?", orgId, sync_advice_id).First(&advice).Error
  473. return
  474. }
  475. // 广州暨华透析中心医嘱同步
  476. func SyncGzjhyz() (err error) {
  477. // 第一步:获取上次同步时间
  478. org_id := int64(10016)
  479. redisClient := RedisClient()
  480. defer redisClient.Close()
  481. sync_time, _ := redisClient.Get("gdjh_sync_time").Result()
  482. if len(sync_time) == 0 {
  483. sync_time = time.Now().Format("2006010215:04:05")
  484. }
  485. new_sync_time := time.Now().Format("2006010215:04:05")
  486. utils.InfoLog("new_sync_time:%v", new_sync_time)
  487. // 切换为当前时间
  488. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  489. // 第二步: 获取当天排班患者的住院号
  490. schedulesDateStr := time.Now().Format("2006-01-02")
  491. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  492. schedulesTime := schedulesDate.Unix()
  493. patient_zy_ids, _ := redisClient.Get("gdjh_patient_zy_ids").Result()
  494. if len(patient_zy_ids) == 0 {
  495. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  496. if len(schedules) > 0 {
  497. for _, item := range schedules {
  498. if len(item.Patients.AdmissionNumber) > 0 {
  499. if len(patient_zy_ids) == 0 {
  500. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  501. } else {
  502. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  503. }
  504. }
  505. }
  506. redisClient.Set("gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
  507. }
  508. }
  509. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  510. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  511. idArray := strings.Split(patient_zy_ids, ",")
  512. for _, idStr := range idArray {
  513. temp_id := strings.Split(idStr, "-")
  514. id := temp_id[0]
  515. patient_id := temp_id[1]
  516. gzjhyz, yzstr := GetGzjhYz(sync_time, id)
  517. utils.InfoLog("sync_time:%v", sync_time)
  518. utils.InfoLog("yzstr:%v", yzstr)
  519. if len(gzjhyz.Result) > 0 {
  520. timeLayout := "2006010215:04:05" //转化所需模板
  521. loc, _ := time.LoadLocation("Local") //重要:获取时区
  522. for _, yz := range gzjhyz.Result {
  523. // 根据姓名获取医生ID
  524. var doctor_id int64
  525. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + yz.DoctorId).Result()
  526. if len(temp_doctor_id) == 0 {
  527. doctor, _ := GetAdminUserId(org_id, yz.DoctorId)
  528. doctor_id = doctor.AdminUserId
  529. if doctor_id > 0 {
  530. redisClient.Set("gdjh_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
  531. } else {
  532. continue
  533. }
  534. } else {
  535. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  536. }
  537. theTime, _ := time.ParseInLocation(timeLayout, yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
  538. start_time := theTime.Unix()
  539. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  540. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  541. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  542. advice := models.XtDoctorAdvice{
  543. UserOrgId: org_id,
  544. PatientId: temp_patient_id,
  545. AdviceType: 2,
  546. AdviceDate: schedulesTime,
  547. StartTime: start_time,
  548. AdviceName: yz.AdviceName,
  549. AdviceDesc: yz.AdviceDesc,
  550. RecordDate: schedulesTime,
  551. SingleDose: SingleDose,
  552. SingleDoseUnit: yz.SingleDoseUnit,
  553. PrescribingNumber: PrescribingNumber,
  554. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  555. DeliveryWay: yz.DeliveryWay,
  556. ExecutionFrequency: yz.ExecutionFrequency,
  557. AdviceDoctor: doctor_id,
  558. CreatedTime: time.Now().Unix(),
  559. UpdatedTime: time.Now().Unix(),
  560. SyncAdviceId: yz.AdviceId,
  561. ExecutionState: 2,
  562. Status: 1,
  563. IsSync: 1,
  564. }
  565. utils.InfoLog("advice:%v", advice)
  566. // 根据同步来的 advice_id 来查询库里是否已经存在
  567. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  568. utils.InfoLog("advice_info:%v", advice_info)
  569. if advice_info.ID > 0 {
  570. continue
  571. } else {
  572. if len(yz.AdviceName) > 0 {
  573. handleerr := CreateDoctorAdviceHandle(&advice)
  574. if handleerr != nil {
  575. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  576. continue
  577. }
  578. } else {
  579. continue
  580. }
  581. }
  582. }
  583. }
  584. }
  585. return
  586. }
  587. type gzjhLisResult struct {
  588. ErrorCode int64 `json:"error_code"`
  589. Result []gzjhlis `json:"data"`
  590. }
  591. type gzjhlis struct {
  592. HisUserId string `json:"his_user_id"`
  593. PatientName string `json:"patient_name"`
  594. ProjectId string `json:"project_id"`
  595. ProjectName string `json:"project_name"`
  596. InspectDate string `json:"inspect_date"`
  597. ItemId string `json:"item_id"`
  598. ItemName string `json:"item_name"`
  599. ItemType string `json:"item_type"`
  600. ItemValue string `json:"item_value"`
  601. RangeMin string `json:"range_min"`
  602. RangeMax string `json:"range_max"`
  603. RangeValue string `json:"range_value"`
  604. RangeOptions string `json:"range_options"`
  605. Unit string `json:"unit"`
  606. State string `json:"state"`
  607. CTime string `json:"c_time"`
  608. UTime string `json:"u_time"`
  609. }
  610. func GetGzjhLis(sync_time string, his_user_id string) (str gzjhLisResult, checkStr string) {
  611. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryjyjcxx"
  612. rep := httplib.Post(apiurl)
  613. rep.Param("sync_time", sync_time)
  614. rep.Param("access_token", "")
  615. rep.Param("his_user_id", his_user_id)
  616. // str, _ := rep.String()
  617. xmlKey := XmlKey{}
  618. rep.ToXML(&xmlKey)
  619. var checkJSON gzjhLisResult
  620. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  621. return checkJSON, xmlKey.InnerText
  622. }
  623. // 广州暨华透析中心Lis同步
  624. func SyncGzjhLis() (err error) {
  625. org_id := int64(10016)
  626. // 第一步:获取上一次同步的时间点
  627. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  628. var sync_time int64
  629. if syncLastInfo.ID > 0 {
  630. sync_time = syncLastInfo.SyncTime + 86400
  631. } else {
  632. sync_time = 1577808000
  633. }
  634. // 第二步:获取所有患者的病历号
  635. patientList, _ := GetPatientGzjhByOrgId(org_id)
  636. if len(patientList) > 0 {
  637. for _, patient := range patientList {
  638. if len(patient.AdmissionNumber) > 0 {
  639. // 上次同步时间转换为接口需要的格式
  640. patient_id := patient.ID
  641. sync_time_temp := time.Unix(sync_time, 0)
  642. syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  643. gzjhLis, lisStr := GetGzjhLis(syncTimeStr, patient.IdCardNo)
  644. utils.InfoLog("yzstr:%v", lisStr)
  645. if len(gzjhLis.Result) > 0 {
  646. // timeLayout := "2006010215:04:05" // 转化所需模板
  647. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  648. for _, info := range gzjhLis.Result {
  649. // 根据project_id和item_id 判断该项是否存在
  650. project_id := int64(0)
  651. if len(info.ProjectId) > 0 {
  652. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  653. } else {
  654. continue
  655. }
  656. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  657. tx := writeMiddleDb.Begin()
  658. var inspection models.MiddleInspection
  659. var inspection_reference models.MiddleInspectionReference
  660. recordDateStr := info.InspectDate
  661. if len(info.InspectDate) == 0 {
  662. recordDateStr = time.Now().Format("2006-01-02 15:04")
  663. }
  664. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  665. record_date := record_date_str.Unix()
  666. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  667. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  668. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  669. var total int
  670. var RangeOptions string
  671. var RangeMin string
  672. var RangeMax string
  673. // // 判断检查类型
  674. ItemType, _ := strconv.Atoi(info.ItemType)
  675. Range := strings.Split(info.RangeValue, "~")
  676. if len(Range) > 1 {
  677. RangeMin = Range[0]
  678. RangeMax = Range[1]
  679. ItemType = 1
  680. } else {
  681. ItemType = 2
  682. RangeOptions = info.RangeOptions
  683. }
  684. 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
  685. if inspection_reference.ID > 0 {
  686. ItemType = inspection_reference.RangeType
  687. }
  688. if total <= 0 {
  689. inspection_reference.OrgId = org_id
  690. inspection_reference.ProjectName = info.ProjectName
  691. inspection_reference.Project = info.ProjectName
  692. inspection_reference.ProjectId = project_id
  693. inspection_reference.ItemName = info.ItemName
  694. inspection_reference.ItemNameAddition = info.ItemId
  695. inspection_reference.ItemId = item_id
  696. inspection_reference.RangeType = ItemType
  697. inspection_reference.RangeMin = RangeMin
  698. inspection_reference.RangeMax = RangeMax
  699. // inspection_reference.RangeValue = RangeValue
  700. inspection_reference.RangeOptions = RangeOptions
  701. inspection_reference.Unit = info.Unit
  702. inspection_reference.Status = 1
  703. inspection_reference.CreatedTime = time.Now().Unix()
  704. inspection_reference.UpdatedTime = time.Now().Unix()
  705. inspection_reference.InspectDate = inspect_date
  706. inspection_reference.UTime = inspect_date
  707. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  708. if err != nil {
  709. tx.Rollback()
  710. }
  711. }
  712. var itotal int
  713. 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
  714. if itotal <= 0 {
  715. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  716. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  717. inspection.PatientId = patient_id
  718. inspection.OrgId = org_id
  719. inspection.ProjectId = project_id
  720. inspection.ItemName = inspection_reference.ItemName
  721. inspection.ProjectName = inspection_reference.ProjectName
  722. inspection.InspectType = ItemType
  723. inspection.ItemId = item_id
  724. inspection.InspectValue = info.ItemValue
  725. inspection.InspectDate = inspect_date
  726. inspection.RecordDate = record_date
  727. // inspection.InspectTips = report.Resultstate
  728. inspection.Status = 1
  729. inspection.CreatedTime = time.Now().Unix()
  730. inspection.UpdatedTime = time.Now().Unix()
  731. inspection.UTime = inspect_date
  732. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  733. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  734. if err != nil {
  735. tx.Rollback()
  736. }
  737. }
  738. tx.Commit()
  739. }
  740. } else {
  741. continue
  742. }
  743. } else {
  744. continue
  745. }
  746. }
  747. }
  748. var syncInfo models.MiddleSyncInfo
  749. syncInfo.OrgId = org_id
  750. syncInfo.SyncTime = time.Now().Unix()
  751. syncInfo.SyncResultType = 1
  752. syncInfo.SyncRsultRemark = "同步成功"
  753. syncInfo.SyncTotalNum = 0
  754. syncInfo.SyncSuccessNum = 0
  755. syncInfo.SyncInfo = ""
  756. syncInfo.CreateTime = time.Now().Unix()
  757. syncInfo.UpdateTime = time.Now().Unix()
  758. cwderr := CreateSyncInfo(&syncInfo)
  759. if cwderr != nil {
  760. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  761. return
  762. }
  763. SyncToGzjhtx()
  764. return
  765. }
  766. func SyncToGzjhtx() {
  767. utils.TraceLog("检验检查同步任务开始执行")
  768. org_id := int64(10016)
  769. // 第一步:跟进org_id 去中间库查出需要同步的数据
  770. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  771. inspections, _ := GetSyncInspectionByOrgId(org_id)
  772. // 第二步:将数据同步到业务库
  773. if len(inspection_references) > 0 {
  774. for _, inspection_reference := range inspection_references {
  775. SyncInspectionReference(&inspection_reference)
  776. }
  777. }
  778. if len(inspections) > 0 {
  779. for _, inspection := range inspections {
  780. SyncInspection(&inspection)
  781. }
  782. }
  783. utils.SuccessLog("检验检查同步任务完成")
  784. }
  785. type hbdyyz struct {
  786. HisUserId string `json:"his_user_id"`
  787. UserName string `json:"user_name"`
  788. DoctorId int64 `json:"doctor_id"`
  789. DoctorName string `json:"Doctor_name"`
  790. AdviceId int64 `json:"advice_id"`
  791. MedListCodg string `json:"med_list_codg"`
  792. StartTime int64 `json:"start_time"`
  793. AdviceName string `json:"advice_name"`
  794. AdviceDesc string `json:"advice_desc"`
  795. SingleDose string `json:"single_dose"`
  796. SingleDoseUnit string `json:"single_dose_unit"`
  797. PrescribingNumber string `json:"prescribing_number"`
  798. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  799. DeliveryWay string `json:"delivery_way"`
  800. ExecutionFrequency string `json:"execution_frequency"`
  801. State int64 `json:"state"`
  802. CTime int64 `json:"create_time"`
  803. UTime int64 `json:"update_time"`
  804. }
  805. type hbdyHisResult struct {
  806. ErrorCode int64 `json:"error_code"`
  807. Result []hbdyyz `json:"data"`
  808. }
  809. func GetHbdyYz(sync_time string, his_user_id string) (str hbdyHisResult, checkStr string) {
  810. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_ADVICE"
  811. rep := httplib.Post(apiurl)
  812. rep.Param("sync_time", sync_time)
  813. rep.Param("his_user_id", his_user_id)
  814. strs, err := rep.String()
  815. if err != nil {
  816. utils.InfoLog("err:%v", err)
  817. }
  818. utils.InfoLog("str:%v", strs)
  819. var checkJSON hbdyHisResult
  820. json.Unmarshal([]byte(strs), &checkJSON)
  821. utils.InfoLog("checkJSON:%v", checkJSON)
  822. return checkJSON, strs
  823. }
  824. // 湖北监利大垸透析中心医嘱同步
  825. func SyncHbJldyyz() (err error) {
  826. // 第一步:获取上次同步时间
  827. org_id := int64(10101)
  828. redisClient := RedisClient()
  829. defer redisClient.Close()
  830. sync_time, _ := redisClient.Get("gdjh_sync_time1").Result()
  831. if len(sync_time) == 0 {
  832. sync_time = strconv.FormatInt(time.Now().Unix(), 10)
  833. utils.InfoLog("sync_time:%v", sync_time)
  834. // formatTimeStr := "2021-08-31 00:00"
  835. // checkDateUnix, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", sync_time)
  836. // sync_time = strconv.FormatInt(checkDateUnix.Unix(),10)
  837. }
  838. new_sync_time := time.Now().Unix()
  839. utils.InfoLog("new_sync_time:%v", new_sync_time)
  840. // 切换为当前时间
  841. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  842. // 第二步: 获取当天排班患者的住院号
  843. schedulesDateStr := time.Now().Format("2006-01-02")
  844. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  845. schedulesTime := schedulesDate.Unix()
  846. patient_zy_ids, _ := redisClient.Get("gdjh_patient_zy_ids").Result()
  847. if len(patient_zy_ids) == 0 {
  848. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  849. if len(schedules) > 0 {
  850. for _, item := range schedules {
  851. if len(patient_zy_ids) == 0 {
  852. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  853. } else {
  854. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  855. }
  856. }
  857. redisClient.Set("gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
  858. }
  859. }
  860. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  861. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  862. idArray := strings.Split(patient_zy_ids, ",")
  863. for _, idStr := range idArray {
  864. temp_id := strings.Split(idStr, "-")
  865. id := temp_id[0]
  866. patient_id := temp_id[1]
  867. hbdyyz, _ := GetHbdyYz(sync_time, id)
  868. if len(hbdyyz.Result) > 0 {
  869. for _, yz := range hbdyyz.Result {
  870. utils.InfoLog("yz:%v", yz)
  871. // 根据姓名获取医生ID
  872. var doctor_id int64
  873. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + strconv.FormatInt(yz.DoctorId, 10)).Result()
  874. if len(temp_doctor_id) == 0 {
  875. doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
  876. doctor_id = doctor.AdminUserId
  877. if doctor_id > 0 {
  878. redisClient.Set("gdjh_doctor_id_"+strconv.FormatInt(yz.DoctorId, 10), doctor_id, time.Minute*60*2)
  879. } else {
  880. continue
  881. }
  882. } else {
  883. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  884. }
  885. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  886. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  887. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  888. utils.InfoLog("PrescribingNumber:%v", PrescribingNumber)
  889. advice := models.XtDoctorAdvice{
  890. UserOrgId: org_id,
  891. PatientId: temp_patient_id,
  892. AdviceType: 2,
  893. AdviceDate: schedulesTime,
  894. StartTime: yz.StartTime,
  895. AdviceName: yz.AdviceName,
  896. AdviceDesc: yz.AdviceDesc,
  897. RecordDate: schedulesTime,
  898. SingleDose: SingleDose,
  899. SingleDoseUnit: yz.SingleDoseUnit,
  900. PrescribingNumber: PrescribingNumber,
  901. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  902. DeliveryWay: yz.DeliveryWay,
  903. ExecutionFrequency: yz.ExecutionFrequency,
  904. AdviceDoctor: doctor_id,
  905. CreatedTime: time.Now().Unix(),
  906. UpdatedTime: time.Now().Unix(),
  907. SyncAdviceId: yz.AdviceId,
  908. ExecutionState: 2,
  909. Status: 1,
  910. IsSync: 1,
  911. }
  912. utils.InfoLog("advice:%v", advice)
  913. // 根据同步来的 advice_id 来查询库里是否已经存在
  914. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  915. utils.InfoLog("advice_info:%v", advice_info)
  916. if advice_info.ID > 0 {
  917. continue
  918. } else {
  919. if yz.State == 1 {
  920. handleerr := CreateDoctorAdviceHandle(&advice)
  921. if handleerr != nil {
  922. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  923. continue
  924. }
  925. } else {
  926. continue
  927. }
  928. }
  929. }
  930. }
  931. }
  932. return
  933. }
  934. type hbdylis struct {
  935. HisUserId string `json:"his_user_id"`
  936. PatientName string `json:"patient_name"`
  937. ProjectId int64 `json:"project_id"`
  938. ProjectName string `json:"project_name"`
  939. InspectDate int64 `json:"inspect_date"`
  940. ItemId int64 `json:"item_id"`
  941. ItemName string `json:"item_name"`
  942. ItemType int64 `json:"item_type"`
  943. ItemValue string `json:"item_value"`
  944. RangeMin string `json:"range_min"`
  945. RangeMax string `json:"range_max"`
  946. RangeValue string `json:"range_value"`
  947. RangeOptions string `json:"range_options"`
  948. Unit string `json:"unit"`
  949. State int64 `json:"state"`
  950. CTime int64 `json:"c_time"`
  951. UTime int64 `json:"u_time"`
  952. }
  953. type hbdyLisResult struct {
  954. ErrorCode int64 `json:"error_code"`
  955. Result []hbdylis `json:"data"`
  956. }
  957. func GethbdyLis(sync_time string, his_user_id string) (str hbdyLisResult, checkStr string) {
  958. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_INSPECTION"
  959. rep := httplib.Post(apiurl)
  960. rep.Param("sync_time", sync_time)
  961. rep.Param("his_user_id", his_user_id)
  962. strs, err := rep.String()
  963. if err != nil {
  964. utils.InfoLog("err:%v", err)
  965. }
  966. utils.InfoLog("str:%v", strs)
  967. var checkJSON hbdyLisResult
  968. json.Unmarshal([]byte(strs), &checkJSON)
  969. // utils.InfoLog("checkJSON:%v", checkJSON)
  970. return checkJSON, strs
  971. }
  972. // 广州暨华透析中心Lis同步
  973. func SyncHbdyLis() (err error) {
  974. org_id := int64(10101)
  975. // 第一步:获取上一次同步的时间点
  976. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  977. var sync_time int64
  978. if syncLastInfo.ID > 0 {
  979. sync_time = syncLastInfo.SyncTime
  980. } else {
  981. sync_time = 1627747200
  982. }
  983. // 第二步:获取所有患者的病历号
  984. patientList, _ := GetPatientGzjhByOrgId(org_id)
  985. if len(patientList) > 0 {
  986. for _, patient := range patientList {
  987. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  988. // 上次同步时间转换为接口需要的格式
  989. patient_id := patient.ID
  990. // sync_time_temp := time.Unix(sync_time,0)
  991. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  992. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  993. // 获取会员号
  994. // utils.InfoLog("VipStr:%v", VipStr)
  995. gzjhLis, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  996. // if len(gzjhLis.Result) == 0 {
  997. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  998. // }
  999. // utils.InfoLog("yzstr:%v", gzjhLis)
  1000. if len(gzjhLis.Result) > 0 {
  1001. // timeLayout := "2006010215:04:05" // 转化所需模板
  1002. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1003. for _, info := range gzjhLis.Result {
  1004. utils.InfoLog("Result:%v", info)
  1005. // 根据project_id和item_id 判断该项是否存在
  1006. project_id := int64(0)
  1007. if info.ProjectId > 0 {
  1008. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1009. } else {
  1010. continue
  1011. }
  1012. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1013. tx := writeMiddleDb.Begin()
  1014. var inspection models.MiddleInspection
  1015. var inspection_reference models.MiddleInspectionReference
  1016. record_date := info.InspectDate
  1017. if info.InspectDate == 0 {
  1018. record_date = time.Now().Unix()
  1019. }
  1020. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1021. // record_date := record_date_str.Unix()
  1022. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1023. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1024. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1025. var total int
  1026. var RangeOptions string
  1027. var RangeMin string
  1028. var RangeMax string
  1029. // // 判断检查类型
  1030. ItemType := info.ItemType
  1031. Range := strings.Split(info.RangeValue, "-")
  1032. if len(Range) > 1 {
  1033. RangeMin = Range[0]
  1034. RangeMax = Range[1]
  1035. ItemType = 1
  1036. } else {
  1037. ItemType = 2
  1038. RangeOptions = info.RangeOptions
  1039. }
  1040. 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
  1041. // if inspection_reference.ID > 0 {
  1042. // ItemType = int64(inspection_reference.RangeType)
  1043. // }
  1044. if total <= 0 {
  1045. inspection_reference.OrgId = org_id
  1046. inspection_reference.ProjectName = info.ProjectName
  1047. inspection_reference.Project = info.ProjectName
  1048. inspection_reference.ProjectId = project_id
  1049. inspection_reference.ItemName = info.ItemName
  1050. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1051. inspection_reference.ItemId = item_id
  1052. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1053. inspection_reference.RangeMin = RangeMin
  1054. inspection_reference.RangeMax = RangeMax
  1055. // inspection_reference.RangeValue = RangeValue
  1056. inspection_reference.RangeOptions = RangeOptions
  1057. inspection_reference.Unit = info.Unit
  1058. inspection_reference.Status = 1
  1059. inspection_reference.CreatedTime = time.Now().Unix()
  1060. inspection_reference.UpdatedTime = time.Now().Unix()
  1061. inspection_reference.InspectDate = inspect_date
  1062. inspection_reference.UTime = inspect_date
  1063. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1064. if err != nil {
  1065. tx.Rollback()
  1066. }
  1067. }
  1068. var itotal int
  1069. 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
  1070. if itotal <= 0 {
  1071. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1072. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1073. inspection.PatientId = patient_id
  1074. inspection.OrgId = org_id
  1075. inspection.ProjectId = project_id
  1076. inspection.ItemName = inspection_reference.ItemName
  1077. inspection.ProjectName = inspection_reference.ProjectName
  1078. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1079. inspection.ItemId = item_id
  1080. inspection.InspectValue = info.ItemValue
  1081. inspection.InspectDate = inspect_date
  1082. inspection.RecordDate = record_date
  1083. // inspection.InspectTips = report.Resultstate
  1084. inspection.Status = 1
  1085. inspection.CreatedTime = time.Now().Unix()
  1086. inspection.UpdatedTime = time.Now().Unix()
  1087. inspection.UTime = inspect_date
  1088. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1089. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1090. if err != nil {
  1091. tx.Rollback()
  1092. }
  1093. }
  1094. tx.Commit()
  1095. }
  1096. }
  1097. VipStr := ""
  1098. VipArr := strings.Split(patient.DialysisNo, "(")
  1099. if len(VipArr) > 1 {
  1100. VipArrTwo := strings.Split(VipArr[1], ")")
  1101. VipStr = VipArrTwo[0]
  1102. gzjhLisbyVip, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), VipStr)
  1103. if len(gzjhLisbyVip.Result) > 0 {
  1104. // timeLayout := "2006010215:04:05" // 转化所需模板
  1105. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1106. for _, info := range gzjhLisbyVip.Result {
  1107. // utils.InfoLog("Result:%v", info)
  1108. // 根据project_id和item_id 判断该项是否存在
  1109. project_id := int64(0)
  1110. if info.ProjectId > 0 {
  1111. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1112. } else {
  1113. continue
  1114. }
  1115. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1116. tx := writeMiddleDb.Begin()
  1117. var inspection models.MiddleInspection
  1118. var inspection_reference models.MiddleInspectionReference
  1119. record_date := info.InspectDate
  1120. if info.InspectDate == 0 {
  1121. record_date = time.Now().Unix()
  1122. }
  1123. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1124. // record_date := record_date_str.Unix()
  1125. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1126. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1127. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1128. var total int
  1129. var RangeOptions string
  1130. var RangeMin string
  1131. var RangeMax string
  1132. // // 判断检查类型
  1133. ItemType := info.ItemType
  1134. Range := strings.Split(info.RangeValue, "-")
  1135. if len(Range) > 1 {
  1136. RangeMin = Range[0]
  1137. RangeMax = Range[1]
  1138. ItemType = 1
  1139. } else {
  1140. ItemType = 2
  1141. RangeOptions = info.RangeOptions
  1142. }
  1143. 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
  1144. if inspection_reference.ID > 0 {
  1145. ItemType = int64(inspection_reference.RangeType)
  1146. }
  1147. if total <= 0 {
  1148. inspection_reference.OrgId = org_id
  1149. inspection_reference.ProjectName = info.ProjectName
  1150. inspection_reference.Project = info.ProjectName
  1151. inspection_reference.ProjectId = project_id
  1152. inspection_reference.ItemName = info.ItemName
  1153. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1154. inspection_reference.ItemId = item_id
  1155. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1156. inspection_reference.RangeMin = RangeMin
  1157. inspection_reference.RangeMax = RangeMax
  1158. // inspection_reference.RangeValue = RangeValue
  1159. inspection_reference.RangeOptions = RangeOptions
  1160. inspection_reference.Unit = info.Unit
  1161. inspection_reference.Status = 1
  1162. inspection_reference.CreatedTime = time.Now().Unix()
  1163. inspection_reference.UpdatedTime = time.Now().Unix()
  1164. inspection_reference.InspectDate = inspect_date
  1165. inspection_reference.UTime = inspect_date
  1166. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1167. if err != nil {
  1168. tx.Rollback()
  1169. }
  1170. }
  1171. var itotal int
  1172. 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
  1173. if itotal <= 0 {
  1174. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1175. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1176. inspection.PatientId = patient_id
  1177. inspection.OrgId = org_id
  1178. inspection.ProjectId = project_id
  1179. inspection.ItemName = inspection_reference.ItemName
  1180. inspection.ProjectName = inspection_reference.ProjectName
  1181. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1182. inspection.ItemId = item_id
  1183. inspection.InspectValue = info.ItemValue
  1184. inspection.InspectDate = inspect_date
  1185. inspection.RecordDate = record_date
  1186. // inspection.InspectTips = report.Resultstate
  1187. inspection.Status = 1
  1188. inspection.CreatedTime = time.Now().Unix()
  1189. inspection.UpdatedTime = time.Now().Unix()
  1190. inspection.UTime = inspect_date
  1191. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1192. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1193. if err != nil {
  1194. tx.Rollback()
  1195. }
  1196. }
  1197. tx.Commit()
  1198. }
  1199. }
  1200. }
  1201. } else {
  1202. continue
  1203. }
  1204. }
  1205. }
  1206. var syncInfo models.MiddleSyncInfo
  1207. syncInfo.OrgId = org_id
  1208. syncInfo.SyncTime = time.Now().Unix()
  1209. syncInfo.SyncResultType = 1
  1210. syncInfo.SyncRsultRemark = "同步成功"
  1211. syncInfo.SyncTotalNum = 0
  1212. syncInfo.SyncSuccessNum = 0
  1213. syncInfo.SyncInfo = ""
  1214. syncInfo.CreateTime = time.Now().Unix()
  1215. syncInfo.UpdateTime = time.Now().Unix()
  1216. cwderr := CreateSyncInfo(&syncInfo)
  1217. if cwderr != nil {
  1218. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  1219. return
  1220. }
  1221. SyncToHbdytx()
  1222. return
  1223. }
  1224. func SyncToHbdytx() {
  1225. utils.TraceLog("检验检查同步任务开始执行")
  1226. org_id := int64(10101)
  1227. // 第一步:跟进org_id 去中间库查出需要同步的数据
  1228. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  1229. inspections, _ := GetSyncInspectionByOrgId(org_id)
  1230. // 第二步:将数据同步到业务库
  1231. if len(inspection_references) > 0 {
  1232. for _, inspection_reference := range inspection_references {
  1233. SyncInspectionReference(&inspection_reference)
  1234. }
  1235. }
  1236. if len(inspections) > 0 {
  1237. for _, inspection := range inspections {
  1238. SyncInspection(&inspection)
  1239. }
  1240. }
  1241. utils.SuccessLog("检验检查同步任务完成")
  1242. }
  1243. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1244. func GetBljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1245. var inspection_reference models.MiddleInspectionReference
  1246. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1247. if inspection_reference.ID > 0 {
  1248. return inspection_reference.ProjectId, err
  1249. } else {
  1250. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1251. if inspection_reference.ProjectId > 0 {
  1252. return inspection_reference.ProjectId + 1, err
  1253. } else {
  1254. return 5210138, err
  1255. }
  1256. }
  1257. }
  1258. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1259. func GetBlItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1260. var inspection_reference models.MiddleInspectionReference
  1261. 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
  1262. if inspection_reference.ID > 0 {
  1263. return inspection_reference.ItemId, err
  1264. } else {
  1265. 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
  1266. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1267. if inspection_reference.ItemId > 0 {
  1268. return inspection_reference.ItemId + 1, err
  1269. } else {
  1270. return project_id*10 + 1, err
  1271. }
  1272. }
  1273. }
  1274. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1275. func GetShjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1276. var inspection_reference models.MiddleInspectionReference
  1277. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1278. if inspection_reference.ID > 0 {
  1279. return inspection_reference.ProjectId, err
  1280. } else {
  1281. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1282. if inspection_reference.ProjectId > 0 {
  1283. return inspection_reference.ProjectId + 1, err
  1284. } else {
  1285. return 5210215, err
  1286. }
  1287. }
  1288. }
  1289. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1290. func GetShItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1291. var inspection_reference models.MiddleInspectionReference
  1292. 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
  1293. if inspection_reference.ID > 0 {
  1294. return inspection_reference.ItemId, err
  1295. } else {
  1296. 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
  1297. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1298. if inspection_reference.ItemId > 0 {
  1299. return inspection_reference.ItemId + 1, err
  1300. } else {
  1301. return project_id*10 + 1, err
  1302. }
  1303. }
  1304. }