ystx_service.go 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 int64, his_user_id string) (str gzjhResult, checkStr string) {
  455. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryyzxx"
  456. rep := httplib.Get(apiurl)
  457. rep.Param("sync_time", strconv.FormatInt(sync_time, 10))
  458. rep.Param("access_token", "0")
  459. rep.Param("his_user_id", his_user_id)
  460. utils.InfoLog(strconv.FormatInt(sync_time, 10))
  461. utils.InfoLog(his_user_id)
  462. // str, _ := rep.String()
  463. xmlKey := XmlKey{}
  464. rep.ToXML(&xmlKey)
  465. var checkJSON gzjhResult
  466. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  467. return checkJSON, xmlKey.InnerText
  468. }
  469. func CreateDoctorAdviceHandle(advice *models.XtDoctorAdvice) error {
  470. err := writeDb.Create(&advice).Error
  471. return err
  472. }
  473. func GetAdviceBySyncAdviceId(orgId int64, sync_advice_id int64) (advice models.XtDoctorAdvice, err error) {
  474. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_advice_id = ?", orgId, sync_advice_id).First(&advice).Error
  475. return
  476. }
  477. // 广州暨华透析中心医嘱同步
  478. func SyncGzjhyz() (err error) {
  479. // 第一步:获取上次同步时间
  480. org_id := int64(10015)
  481. redisClient := RedisClient()
  482. defer redisClient.Close()
  483. sync_time_tt, _ := redisClient.Get("gdjh_sync_time").Result()
  484. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  485. if sync_time == 0 {
  486. sync_time = time.Now().Unix()
  487. }
  488. new_sync_time := time.Now().Unix()
  489. utils.InfoLog("new_sync_time:%v", new_sync_time)
  490. // 切换为当前时间
  491. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  492. // 第二步: 获取当天排班患者的住院号
  493. schedulesDateStr := time.Now().Format("2006-01-02")
  494. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  495. schedulesTime := schedulesDate.Unix()
  496. var patient_zy_ids string
  497. // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
  498. // if len(patient_zy_ids) == 0 {
  499. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  500. if len(schedules) > 0 {
  501. for _, item := range schedules {
  502. // if len(item.Patients.AdmissionNumber) > 0 {
  503. if len(patient_zy_ids) == 0 {
  504. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  505. } else {
  506. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  507. }
  508. // }
  509. }
  510. // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
  511. }
  512. // }
  513. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  514. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  515. idArray := strings.Split(patient_zy_ids, ",")
  516. for _, idStr := range idArray {
  517. temp_id := strings.Split(idStr, "-")
  518. id := temp_id[0]
  519. patient_id := temp_id[1]
  520. admission_number := temp_id[2]
  521. gzjhyz, yzstr := GetGzjhYz(sync_time, id)
  522. if len(gzjhyz.Result) == 0 && len(admission_number) > 0 {
  523. utils.InfoLog("admission_number:%v", admission_number)
  524. gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
  525. }
  526. utils.InfoLog("sync_time:%v", sync_time)
  527. utils.InfoLog("yzstr:%v", yzstr)
  528. if len(gzjhyz.Result) > 0 {
  529. timeLayout := "2006010215:04:05" //转化所需模板
  530. loc, _ := time.LoadLocation("Local") //重要:获取时区
  531. for _, yz := range gzjhyz.Result {
  532. // 根据姓名获取医生ID
  533. var doctor_id int64
  534. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + yz.DoctorId).Result()
  535. if len(temp_doctor_id) == 0 {
  536. doctor, _ := GetAdminUserId(org_id, yz.DoctorId)
  537. doctor_id = doctor.AdminUserId
  538. if doctor_id > 0 {
  539. redisClient.Set("gdjh_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
  540. } else {
  541. continue
  542. }
  543. } else {
  544. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  545. }
  546. theTime, _ := time.ParseInLocation(timeLayout, yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
  547. start_time := theTime.Unix()
  548. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  549. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  550. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  551. advice := models.XtDoctorAdvice{
  552. UserOrgId: org_id,
  553. PatientId: temp_patient_id,
  554. AdviceType: 2,
  555. AdviceDate: schedulesTime,
  556. StartTime: start_time,
  557. AdviceName: yz.AdviceName,
  558. AdviceDesc: yz.AdviceDesc,
  559. RecordDate: schedulesTime,
  560. SingleDose: SingleDose,
  561. SingleDoseUnit: yz.SingleDoseUnit,
  562. PrescribingNumber: PrescribingNumber,
  563. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  564. DeliveryWay: yz.DeliveryWay,
  565. ExecutionFrequency: yz.ExecutionFrequency,
  566. AdviceDoctor: doctor_id,
  567. CreatedTime: time.Now().Unix(),
  568. UpdatedTime: time.Now().Unix(),
  569. SyncAdviceId: yz.AdviceId,
  570. ExecutionState: 2,
  571. Status: 1,
  572. IsSync: 1,
  573. }
  574. utils.InfoLog("advice:%v", advice)
  575. // 根据同步来的 advice_id 来查询库里是否已经存在
  576. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  577. utils.InfoLog("advice_info:%v", advice_info)
  578. if advice_info.ID > 0 {
  579. key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
  580. //清空key 值
  581. redisClient.Set(key, "", time.Second)
  582. keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
  583. redisClient.Set(keyOne, "", time.Second)
  584. continue
  585. } else {
  586. if len(yz.AdviceName) > 0 {
  587. handleerr := CreateDoctorAdviceHandle(&advice)
  588. if handleerr != nil {
  589. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  590. continue
  591. }
  592. } else {
  593. continue
  594. }
  595. }
  596. }
  597. }
  598. }
  599. return
  600. }
  601. type gzjhLisResult struct {
  602. ErrorCode int64 `json:"error_code"`
  603. Result []gzjhlis `json:"data"`
  604. }
  605. type gzjhlis struct {
  606. HisUserId string `json:"his_user_id"`
  607. PatientName string `json:"patient_name"`
  608. ProjectId string `json:"project_id"`
  609. ProjectName string `json:"project_name"`
  610. InspectDate string `json:"inspect_date"`
  611. ItemId string `json:"item_id"`
  612. ItemName string `json:"item_name"`
  613. ItemType string `json:"item_type"`
  614. ItemValue string `json:"item_value"`
  615. RangeMin string `json:"range_min"`
  616. RangeMax string `json:"range_max"`
  617. RangeValue string `json:"range_value"`
  618. RangeOptions string `json:"range_options"`
  619. Unit string `json:"unit"`
  620. State string `json:"state"`
  621. CTime string `json:"c_time"`
  622. UTime string `json:"u_time"`
  623. }
  624. func GetGzjhLis(sync_time string, his_user_id string) (str gzjhLisResult, checkStr string) {
  625. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryjyjcxx"
  626. rep := httplib.Post(apiurl)
  627. rep.Param("sync_time", sync_time)
  628. rep.Param("access_token", "")
  629. rep.Param("his_user_id", his_user_id)
  630. // str, _ := rep.String()
  631. xmlKey := XmlKey{}
  632. rep.ToXML(&xmlKey)
  633. var checkJSON gzjhLisResult
  634. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  635. return checkJSON, xmlKey.InnerText
  636. }
  637. // 广州暨华透析中心Lis同步
  638. func SyncGzjhLis() (err error) {
  639. org_id := int64(10016)
  640. // 第一步:获取上一次同步的时间点
  641. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  642. var sync_time int64
  643. if syncLastInfo.ID > 0 {
  644. sync_time = syncLastInfo.SyncTime + 86400
  645. } else {
  646. sync_time = 1577808000
  647. }
  648. // 第二步:获取所有患者的病历号
  649. patientList, _ := GetPatientGzjhByOrgId(org_id)
  650. if len(patientList) > 0 {
  651. for _, patient := range patientList {
  652. if len(patient.AdmissionNumber) > 0 {
  653. // 上次同步时间转换为接口需要的格式
  654. patient_id := patient.ID
  655. sync_time_temp := time.Unix(sync_time, 0)
  656. syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  657. gzjhLis, lisStr := GetGzjhLis(syncTimeStr, patient.IdCardNo)
  658. utils.InfoLog("yzstr:%v", lisStr)
  659. if len(gzjhLis.Result) > 0 {
  660. // timeLayout := "2006010215:04:05" // 转化所需模板
  661. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  662. for _, info := range gzjhLis.Result {
  663. // 根据project_id和item_id 判断该项是否存在
  664. project_id := int64(0)
  665. if len(info.ProjectId) > 0 {
  666. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  667. } else {
  668. continue
  669. }
  670. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  671. tx := writeMiddleDb.Begin()
  672. var inspection models.MiddleInspection
  673. var inspection_reference models.MiddleInspectionReference
  674. recordDateStr := info.InspectDate
  675. if len(info.InspectDate) == 0 {
  676. recordDateStr = time.Now().Format("2006-01-02 15:04")
  677. }
  678. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  679. record_date := record_date_str.Unix()
  680. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  681. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  682. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  683. var total int
  684. var RangeOptions string
  685. var RangeMin string
  686. var RangeMax string
  687. // // 判断检查类型
  688. ItemType, _ := strconv.Atoi(info.ItemType)
  689. Range := strings.Split(info.RangeValue, "~")
  690. if len(Range) > 1 {
  691. RangeMin = Range[0]
  692. RangeMax = Range[1]
  693. ItemType = 1
  694. } else {
  695. ItemType = 2
  696. RangeOptions = info.RangeOptions
  697. }
  698. 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
  699. if inspection_reference.ID > 0 {
  700. ItemType = inspection_reference.RangeType
  701. }
  702. if total <= 0 {
  703. inspection_reference.OrgId = org_id
  704. inspection_reference.ProjectName = info.ProjectName
  705. inspection_reference.Project = info.ProjectName
  706. inspection_reference.ProjectId = project_id
  707. inspection_reference.ItemName = info.ItemName
  708. inspection_reference.ItemNameAddition = info.ItemId
  709. inspection_reference.ItemId = item_id
  710. inspection_reference.RangeType = ItemType
  711. inspection_reference.RangeMin = RangeMin
  712. inspection_reference.RangeMax = RangeMax
  713. // inspection_reference.RangeValue = RangeValue
  714. inspection_reference.RangeOptions = RangeOptions
  715. inspection_reference.Unit = info.Unit
  716. inspection_reference.Status = 1
  717. inspection_reference.CreatedTime = time.Now().Unix()
  718. inspection_reference.UpdatedTime = time.Now().Unix()
  719. inspection_reference.InspectDate = inspect_date
  720. inspection_reference.UTime = inspect_date
  721. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  722. if err != nil {
  723. tx.Rollback()
  724. }
  725. }
  726. var itotal int
  727. 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
  728. if itotal <= 0 {
  729. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  730. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  731. inspection.PatientId = patient_id
  732. inspection.OrgId = org_id
  733. inspection.ProjectId = project_id
  734. inspection.ItemName = inspection_reference.ItemName
  735. inspection.ProjectName = inspection_reference.ProjectName
  736. inspection.InspectType = ItemType
  737. inspection.ItemId = item_id
  738. inspection.InspectValue = info.ItemValue
  739. inspection.InspectDate = inspect_date
  740. inspection.RecordDate = record_date
  741. // inspection.InspectTips = report.Resultstate
  742. inspection.Status = 1
  743. inspection.CreatedTime = time.Now().Unix()
  744. inspection.UpdatedTime = time.Now().Unix()
  745. inspection.UTime = inspect_date
  746. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  747. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  748. if err != nil {
  749. tx.Rollback()
  750. }
  751. }
  752. tx.Commit()
  753. }
  754. } else {
  755. continue
  756. }
  757. } else {
  758. continue
  759. }
  760. }
  761. }
  762. var syncInfo models.MiddleSyncInfo
  763. syncInfo.OrgId = org_id
  764. syncInfo.SyncTime = time.Now().Unix()
  765. syncInfo.SyncResultType = 1
  766. syncInfo.SyncRsultRemark = "同步成功"
  767. syncInfo.SyncTotalNum = 0
  768. syncInfo.SyncSuccessNum = 0
  769. syncInfo.SyncInfo = ""
  770. syncInfo.CreateTime = time.Now().Unix()
  771. syncInfo.UpdateTime = time.Now().Unix()
  772. cwderr := CreateSyncInfo(&syncInfo)
  773. if cwderr != nil {
  774. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  775. return
  776. }
  777. SyncToGzjhtx()
  778. return
  779. }
  780. func SyncToGzjhtx() {
  781. utils.TraceLog("检验检查同步任务开始执行")
  782. org_id := int64(10016)
  783. // 第一步:跟进org_id 去中间库查出需要同步的数据
  784. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  785. inspections, _ := GetSyncInspectionByOrgId(org_id)
  786. // 第二步:将数据同步到业务库
  787. if len(inspection_references) > 0 {
  788. for _, inspection_reference := range inspection_references {
  789. SyncInspectionReference(&inspection_reference)
  790. }
  791. }
  792. if len(inspections) > 0 {
  793. for _, inspection := range inspections {
  794. SyncInspection(&inspection)
  795. }
  796. }
  797. utils.SuccessLog("检验检查同步任务完成")
  798. }
  799. type hbdyyz struct {
  800. HisUserId string `json:"his_user_id"`
  801. UserName string `json:"user_name"`
  802. DoctorId int64 `json:"doctor_id"`
  803. DoctorName string `json:"Doctor_name"`
  804. AdviceId int64 `json:"advice_id"`
  805. MedListCodg string `json:"med_list_codg"`
  806. StartTime int64 `json:"start_time"`
  807. AdviceName string `json:"advice_name"`
  808. AdviceDesc string `json:"advice_desc"`
  809. SingleDose string `json:"single_dose"`
  810. SingleDoseUnit string `json:"single_dose_unit"`
  811. PrescribingNumber string `json:"prescribing_number"`
  812. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  813. DeliveryWay string `json:"delivery_way"`
  814. ExecutionFrequency string `json:"execution_frequency"`
  815. State int64 `json:"state"`
  816. CTime int64 `json:"create_time"`
  817. UTime int64 `json:"update_time"`
  818. }
  819. type hbdyHisResult struct {
  820. ErrorCode int64 `json:"error_code"`
  821. Result []hbdyyz `json:"data"`
  822. }
  823. func GetHbdyYz(sync_time string, his_user_id string) (str hbdyHisResult, checkStr string) {
  824. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_ADVICE"
  825. rep := httplib.Post(apiurl)
  826. rep.Param("sync_time", sync_time)
  827. rep.Param("his_user_id", his_user_id)
  828. strs, err := rep.String()
  829. if err != nil {
  830. utils.InfoLog("err:%v", err)
  831. }
  832. utils.InfoLog("str:%v", strs)
  833. var checkJSON hbdyHisResult
  834. json.Unmarshal([]byte(strs), &checkJSON)
  835. utils.InfoLog("checkJSON:%v", checkJSON)
  836. return checkJSON, strs
  837. }
  838. // 湖北监利大垸透析中心医嘱同步
  839. func SyncHbJldyyz() (err error) {
  840. // 第一步:获取上次同步时间
  841. org_id := int64(10101)
  842. redisClient := RedisClient()
  843. defer redisClient.Close()
  844. sync_time, _ := redisClient.Get("gdjh_sync_time1").Result()
  845. if len(sync_time) == 0 {
  846. sync_time = strconv.FormatInt(time.Now().Unix(), 10)
  847. utils.InfoLog("sync_time:%v", sync_time)
  848. // formatTimeStr := "2021-08-31 00:00"
  849. // checkDateUnix, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", sync_time)
  850. // sync_time = strconv.FormatInt(checkDateUnix.Unix(),10)
  851. }
  852. new_sync_time := time.Now().Unix()
  853. utils.InfoLog("new_sync_time:%v", new_sync_time)
  854. // 切换为当前时间
  855. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  856. // 第二步: 获取当天排班患者的住院号
  857. schedulesDateStr := time.Now().Format("2006-01-02")
  858. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  859. schedulesTime := schedulesDate.Unix()
  860. patient_zy_ids, _ := redisClient.Get("gdjh_patient_zy_ids").Result()
  861. if len(patient_zy_ids) == 0 {
  862. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  863. if len(schedules) > 0 {
  864. for _, item := range schedules {
  865. if len(patient_zy_ids) == 0 {
  866. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  867. } else {
  868. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  869. }
  870. }
  871. redisClient.Set("gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
  872. }
  873. }
  874. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  875. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  876. idArray := strings.Split(patient_zy_ids, ",")
  877. for _, idStr := range idArray {
  878. temp_id := strings.Split(idStr, "-")
  879. id := temp_id[0]
  880. patient_id := temp_id[1]
  881. hbdyyz, _ := GetHbdyYz(sync_time, id)
  882. if len(hbdyyz.Result) > 0 {
  883. for _, yz := range hbdyyz.Result {
  884. utils.InfoLog("yz:%v", yz)
  885. // 根据姓名获取医生ID
  886. var doctor_id int64
  887. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + strconv.FormatInt(yz.DoctorId, 10)).Result()
  888. if len(temp_doctor_id) == 0 {
  889. doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
  890. doctor_id = doctor.AdminUserId
  891. if doctor_id > 0 {
  892. redisClient.Set("gdjh_doctor_id_"+strconv.FormatInt(yz.DoctorId, 10), doctor_id, time.Minute*60*2)
  893. } else {
  894. continue
  895. }
  896. } else {
  897. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  898. }
  899. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  900. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  901. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  902. utils.InfoLog("PrescribingNumber:%v", PrescribingNumber)
  903. advice := models.XtDoctorAdvice{
  904. UserOrgId: org_id,
  905. PatientId: temp_patient_id,
  906. AdviceType: 2,
  907. AdviceDate: schedulesTime,
  908. StartTime: yz.StartTime,
  909. AdviceName: yz.AdviceName,
  910. AdviceDesc: yz.AdviceDesc,
  911. RecordDate: schedulesTime,
  912. SingleDose: SingleDose,
  913. SingleDoseUnit: yz.SingleDoseUnit,
  914. PrescribingNumber: PrescribingNumber,
  915. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  916. DeliveryWay: yz.DeliveryWay,
  917. ExecutionFrequency: yz.ExecutionFrequency,
  918. AdviceDoctor: doctor_id,
  919. CreatedTime: time.Now().Unix(),
  920. UpdatedTime: time.Now().Unix(),
  921. SyncAdviceId: yz.AdviceId,
  922. ExecutionState: 2,
  923. Status: 1,
  924. IsSync: 1,
  925. }
  926. utils.InfoLog("advice:%v", advice)
  927. // 根据同步来的 advice_id 来查询库里是否已经存在
  928. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  929. utils.InfoLog("advice_info:%v", advice_info)
  930. if advice_info.ID > 0 {
  931. continue
  932. } else {
  933. if yz.State == 1 {
  934. handleerr := CreateDoctorAdviceHandle(&advice)
  935. if handleerr != nil {
  936. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  937. continue
  938. }
  939. } else {
  940. continue
  941. }
  942. }
  943. }
  944. }
  945. }
  946. return
  947. }
  948. type hbdylis struct {
  949. HisUserId string `json:"his_user_id"`
  950. PatientName string `json:"patient_name"`
  951. ProjectId int64 `json:"project_id"`
  952. ProjectName string `json:"project_name"`
  953. InspectDate int64 `json:"inspect_date"`
  954. ItemId int64 `json:"item_id"`
  955. ItemName string `json:"item_name"`
  956. ItemType int64 `json:"item_type"`
  957. ItemValue string `json:"item_value"`
  958. RangeMin string `json:"range_min"`
  959. RangeMax string `json:"range_max"`
  960. RangeValue string `json:"range_value"`
  961. RangeOptions string `json:"range_options"`
  962. Unit string `json:"unit"`
  963. State int64 `json:"state"`
  964. CTime int64 `json:"c_time"`
  965. UTime int64 `json:"u_time"`
  966. }
  967. type hbdyLisResult struct {
  968. ErrorCode int64 `json:"error_code"`
  969. Result []hbdylis `json:"data"`
  970. }
  971. func GethbdyLis(sync_time string, his_user_id string) (str hbdyLisResult, checkStr string) {
  972. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_INSPECTION"
  973. rep := httplib.Post(apiurl)
  974. rep.Param("sync_time", sync_time)
  975. rep.Param("his_user_id", his_user_id)
  976. strs, err := rep.String()
  977. if err != nil {
  978. utils.InfoLog("err:%v", err)
  979. }
  980. utils.InfoLog("str:%v", strs)
  981. var checkJSON hbdyLisResult
  982. json.Unmarshal([]byte(strs), &checkJSON)
  983. // utils.InfoLog("checkJSON:%v", checkJSON)
  984. return checkJSON, strs
  985. }
  986. // 湖北监利大院透析中心Lis同步
  987. func SyncHbdyLis() (err error) {
  988. org_id := int64(10101)
  989. // 第一步:获取上一次同步的时间点
  990. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  991. var sync_time int64
  992. if syncLastInfo.ID > 0 {
  993. sync_time = syncLastInfo.SyncTime
  994. } else {
  995. sync_time = 1627747200
  996. }
  997. // 第二步:获取所有患者的病历号
  998. patientList, _ := GetPatientGzjhByOrgId(org_id)
  999. if len(patientList) > 0 {
  1000. for _, patient := range patientList {
  1001. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  1002. // 上次同步时间转换为接口需要的格式
  1003. patient_id := patient.ID
  1004. // sync_time_temp := time.Unix(sync_time,0)
  1005. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  1006. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  1007. // 获取会员号
  1008. // utils.InfoLog("VipStr:%v", VipStr)
  1009. gzjhLis, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  1010. // if len(gzjhLis.Result) == 0 {
  1011. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  1012. // }
  1013. // utils.InfoLog("yzstr:%v", gzjhLis)
  1014. if len(gzjhLis.Result) > 0 {
  1015. // timeLayout := "2006010215:04:05" // 转化所需模板
  1016. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1017. for _, info := range gzjhLis.Result {
  1018. utils.InfoLog("Result:%v", info)
  1019. // 根据project_id和item_id 判断该项是否存在
  1020. project_id := int64(0)
  1021. if info.ProjectId > 0 {
  1022. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1023. } else {
  1024. continue
  1025. }
  1026. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1027. tx := writeMiddleDb.Begin()
  1028. var inspection models.MiddleInspection
  1029. var inspection_reference models.MiddleInspectionReference
  1030. record_date := info.InspectDate
  1031. if info.InspectDate == 0 {
  1032. record_date = time.Now().Unix()
  1033. }
  1034. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1035. // record_date := record_date_str.Unix()
  1036. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1037. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1038. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1039. var total int
  1040. var RangeOptions string
  1041. var RangeMin string
  1042. var RangeMax string
  1043. // // 判断检查类型
  1044. ItemType := info.ItemType
  1045. Range := strings.Split(info.RangeValue, "-")
  1046. if len(Range) > 1 {
  1047. RangeMin = Range[0]
  1048. RangeMax = Range[1]
  1049. ItemType = 1
  1050. } else {
  1051. ItemType = 2
  1052. RangeOptions = info.RangeOptions
  1053. }
  1054. 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
  1055. // if inspection_reference.ID > 0 {
  1056. // ItemType = int64(inspection_reference.RangeType)
  1057. // }
  1058. if total <= 0 {
  1059. inspection_reference.OrgId = org_id
  1060. inspection_reference.ProjectName = info.ProjectName
  1061. inspection_reference.Project = info.ProjectName
  1062. inspection_reference.ProjectId = project_id
  1063. inspection_reference.ItemName = info.ItemName
  1064. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1065. inspection_reference.ItemId = item_id
  1066. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1067. inspection_reference.RangeMin = RangeMin
  1068. inspection_reference.RangeMax = RangeMax
  1069. // inspection_reference.RangeValue = RangeValue
  1070. inspection_reference.RangeOptions = RangeOptions
  1071. inspection_reference.Unit = info.Unit
  1072. inspection_reference.Status = 1
  1073. inspection_reference.CreatedTime = time.Now().Unix()
  1074. inspection_reference.UpdatedTime = time.Now().Unix()
  1075. inspection_reference.InspectDate = inspect_date
  1076. inspection_reference.UTime = inspect_date
  1077. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1078. if err != nil {
  1079. tx.Rollback()
  1080. }
  1081. }
  1082. var itotal int
  1083. 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
  1084. if itotal <= 0 {
  1085. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1086. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1087. inspection.PatientId = patient_id
  1088. inspection.OrgId = org_id
  1089. inspection.ProjectId = project_id
  1090. inspection.ItemName = inspection_reference.ItemName
  1091. inspection.ProjectName = inspection_reference.ProjectName
  1092. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1093. inspection.ItemId = item_id
  1094. inspection.InspectValue = info.ItemValue
  1095. inspection.InspectDate = inspect_date
  1096. inspection.RecordDate = record_date
  1097. // inspection.InspectTips = report.Resultstate
  1098. inspection.Status = 1
  1099. inspection.CreatedTime = time.Now().Unix()
  1100. inspection.UpdatedTime = time.Now().Unix()
  1101. inspection.UTime = inspect_date
  1102. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1103. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1104. if err != nil {
  1105. tx.Rollback()
  1106. }
  1107. }
  1108. tx.Commit()
  1109. }
  1110. }
  1111. VipStr := ""
  1112. VipArr := strings.Split(patient.DialysisNo, "(")
  1113. if len(VipArr) > 1 {
  1114. VipArrTwo := strings.Split(VipArr[1], ")")
  1115. VipStr = VipArrTwo[0]
  1116. gzjhLisbyVip, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), VipStr)
  1117. if len(gzjhLisbyVip.Result) > 0 {
  1118. // timeLayout := "2006010215:04:05" // 转化所需模板
  1119. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1120. for _, info := range gzjhLisbyVip.Result {
  1121. // utils.InfoLog("Result:%v", info)
  1122. // 根据project_id和item_id 判断该项是否存在
  1123. project_id := int64(0)
  1124. if info.ProjectId > 0 {
  1125. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1126. } else {
  1127. continue
  1128. }
  1129. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1130. tx := writeMiddleDb.Begin()
  1131. var inspection models.MiddleInspection
  1132. var inspection_reference models.MiddleInspectionReference
  1133. record_date := info.InspectDate
  1134. if info.InspectDate == 0 {
  1135. record_date = time.Now().Unix()
  1136. }
  1137. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1138. // record_date := record_date_str.Unix()
  1139. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1140. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1141. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1142. var total int
  1143. var RangeOptions string
  1144. var RangeMin string
  1145. var RangeMax string
  1146. // // 判断检查类型
  1147. ItemType := info.ItemType
  1148. Range := strings.Split(info.RangeValue, "-")
  1149. if len(Range) > 1 {
  1150. RangeMin = Range[0]
  1151. RangeMax = Range[1]
  1152. ItemType = 1
  1153. } else {
  1154. ItemType = 2
  1155. RangeOptions = info.RangeOptions
  1156. }
  1157. 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
  1158. if inspection_reference.ID > 0 {
  1159. ItemType = int64(inspection_reference.RangeType)
  1160. }
  1161. if total <= 0 {
  1162. inspection_reference.OrgId = org_id
  1163. inspection_reference.ProjectName = info.ProjectName
  1164. inspection_reference.Project = info.ProjectName
  1165. inspection_reference.ProjectId = project_id
  1166. inspection_reference.ItemName = info.ItemName
  1167. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1168. inspection_reference.ItemId = item_id
  1169. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1170. inspection_reference.RangeMin = RangeMin
  1171. inspection_reference.RangeMax = RangeMax
  1172. // inspection_reference.RangeValue = RangeValue
  1173. inspection_reference.RangeOptions = RangeOptions
  1174. inspection_reference.Unit = info.Unit
  1175. inspection_reference.Status = 1
  1176. inspection_reference.CreatedTime = time.Now().Unix()
  1177. inspection_reference.UpdatedTime = time.Now().Unix()
  1178. inspection_reference.InspectDate = inspect_date
  1179. inspection_reference.UTime = inspect_date
  1180. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1181. if err != nil {
  1182. tx.Rollback()
  1183. }
  1184. }
  1185. var itotal int
  1186. 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
  1187. if itotal <= 0 {
  1188. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1189. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1190. inspection.PatientId = patient_id
  1191. inspection.OrgId = org_id
  1192. inspection.ProjectId = project_id
  1193. inspection.ItemName = inspection_reference.ItemName
  1194. inspection.ProjectName = inspection_reference.ProjectName
  1195. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1196. inspection.ItemId = item_id
  1197. inspection.InspectValue = info.ItemValue
  1198. inspection.InspectDate = inspect_date
  1199. inspection.RecordDate = record_date
  1200. // inspection.InspectTips = report.Resultstate
  1201. inspection.Status = 1
  1202. inspection.CreatedTime = time.Now().Unix()
  1203. inspection.UpdatedTime = time.Now().Unix()
  1204. inspection.UTime = inspect_date
  1205. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1206. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1207. if err != nil {
  1208. tx.Rollback()
  1209. }
  1210. }
  1211. tx.Commit()
  1212. }
  1213. }
  1214. }
  1215. } else {
  1216. continue
  1217. }
  1218. }
  1219. }
  1220. var syncInfo models.MiddleSyncInfo
  1221. syncInfo.OrgId = org_id
  1222. syncInfo.SyncTime = time.Now().Unix()
  1223. syncInfo.SyncResultType = 1
  1224. syncInfo.SyncRsultRemark = "同步成功"
  1225. syncInfo.SyncTotalNum = 0
  1226. syncInfo.SyncSuccessNum = 0
  1227. syncInfo.SyncInfo = ""
  1228. syncInfo.CreateTime = time.Now().Unix()
  1229. syncInfo.UpdateTime = time.Now().Unix()
  1230. cwderr := CreateSyncInfo(&syncInfo)
  1231. if cwderr != nil {
  1232. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  1233. return
  1234. }
  1235. SyncToHbdytx()
  1236. return
  1237. }
  1238. func SyncToHbdytx() {
  1239. utils.TraceLog("检验检查同步任务开始执行")
  1240. org_id := int64(10101)
  1241. // 第一步:跟进org_id 去中间库查出需要同步的数据
  1242. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  1243. inspections, _ := GetSyncInspectionByOrgId(org_id)
  1244. // 第二步:将数据同步到业务库
  1245. if len(inspection_references) > 0 {
  1246. for _, inspection_reference := range inspection_references {
  1247. SyncInspectionReference(&inspection_reference)
  1248. }
  1249. }
  1250. if len(inspections) > 0 {
  1251. for _, inspection := range inspections {
  1252. SyncInspection(&inspection)
  1253. }
  1254. }
  1255. utils.SuccessLog("检验检查同步任务完成")
  1256. }
  1257. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1258. func GetBljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1259. var inspection_reference models.MiddleInspectionReference
  1260. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1261. if inspection_reference.ID > 0 {
  1262. return inspection_reference.ProjectId, err
  1263. } else {
  1264. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1265. if inspection_reference.ProjectId > 0 {
  1266. return inspection_reference.ProjectId + 1, err
  1267. } else {
  1268. return 5210138, err
  1269. }
  1270. }
  1271. }
  1272. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1273. func GetBlItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1274. var inspection_reference models.MiddleInspectionReference
  1275. 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
  1276. if inspection_reference.ID > 0 {
  1277. return inspection_reference.ItemId, err
  1278. } else {
  1279. 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
  1280. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1281. if inspection_reference.ItemId > 0 {
  1282. return inspection_reference.ItemId + 1, err
  1283. } else {
  1284. return project_id*10 + 1, err
  1285. }
  1286. }
  1287. }
  1288. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1289. func GetShjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1290. var inspection_reference models.MiddleInspectionReference
  1291. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1292. if inspection_reference.ID > 0 {
  1293. return inspection_reference.ProjectId, err
  1294. } else {
  1295. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1296. if inspection_reference.ProjectId > 0 {
  1297. return inspection_reference.ProjectId + 1, err
  1298. } else {
  1299. return 5210215, err
  1300. }
  1301. }
  1302. }
  1303. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1304. func GetShItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1305. var inspection_reference models.MiddleInspectionReference
  1306. 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
  1307. if inspection_reference.ID > 0 {
  1308. return inspection_reference.ItemId, err
  1309. } else {
  1310. 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
  1311. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1312. if inspection_reference.ItemId > 0 {
  1313. return inspection_reference.ItemId + 1, err
  1314. } else {
  1315. return project_id*10 + 1, err
  1316. }
  1317. }
  1318. }