ystx_service.go 67KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. "bytes"
  7. "encoding/json"
  8. _ "encoding/json"
  9. "encoding/xml"
  10. "fmt"
  11. _ "fmt"
  12. "github.com/astaxie/beego/httplib"
  13. _ "github.com/jinzhu/gorm"
  14. "io/ioutil"
  15. "net/http"
  16. "net/url"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type RequestResult struct {
  23. Msg string `json:"msg"`
  24. Result []struct {
  25. AdviceId int64 `json:"医嘱ID"`
  26. OtherId int64 `json:"相关ID"`
  27. Source int `json:"病人来源"`
  28. PatientId int64 `json:"病人ID"`
  29. JzId int64 `json:"就诊ID"`
  30. BabyId int `json:"婴儿ID"`
  31. PatientDepartmentId int `json:"病人科室ID"`
  32. OrderId int `json:"次序"`
  33. AdviceStatus int `json:"医嘱状态"`
  34. AdviceType int `json:"医嘱类型"`
  35. DiagnosisType string `json:"诊疗类型"`
  36. DiagnosisItemId int `json:"诊疗项目ID"`
  37. CheckMethd string `json:"检查方法"`
  38. ChargeItemId int `json:"收费项目ID"`
  39. Day int `json:"天数"`
  40. JiLiang float64 `json:"剂量"`
  41. YongLiang string `json:"用量"`
  42. DanLiang float64 `json:"单量"`
  43. Shuliang float64 `json:"数量"`
  44. Advice string `json:"医嘱"`
  45. GeiYaoTuJing string `json:"给药途径"`
  46. ShuoMing string `json:"说明"`
  47. ZhiXingKeShiId int `json:"执行科室ID"`
  48. ZhiXingKeShi string `json:"执行科室"`
  49. PN string `json:"P(+)或N(-)表示阳性或阴性"`
  50. ZhiXingPinCi string `json:"执行频次"`
  51. PinLvCiShu int `json:"频率次数"`
  52. PinLvJianGe int `json:"频率间隔"`
  53. JianGeDanWei string `json:"间隔单位"`
  54. ZhiXingShiJianFangAn string `json:"执行时间方案"`
  55. JiJiaTeXing int `json:"计价特性"`
  56. ZhiXingXingZhi int `json:"执行性质"`
  57. ZhiXingBiaoZhi int `json:"执行标记"`
  58. KeFouFenLing int `json:"可否分零"`
  59. JinJiBiaoZhi int `json:"紧急标志"`
  60. KaiShiZhiXingShiJian int64 `json:"开始执行时间"`
  61. ZhiXingZhongZhiShiJian int64 `json:"执行终止时间"`
  62. ShangCiZhiXingShiJian int64 `json:"上次执行时间"`
  63. KaiZhuKeShiId int `json:"开嘱科室ID"`
  64. DoctorCode string `json:"医师编码"`
  65. Doctor string `json:"开嘱医师"`
  66. AdviceTime int64 `json:"开嘱时间"`
  67. StopTime int64 `json:"停嘱时间"`
  68. ApplyId int `json:"申请ID"`
  69. UploadLable int `json:"上传标志"`
  70. CheckResult int `json:"审查结果"`
  71. NoPrint int `json:"忽略打印"`
  72. Summary string `json:"摘要"`
  73. AdviceOrderId int64 `json:"医嘱单id"`
  74. GeiYaoKeShi int `json:"给药科室"`
  75. GeiYaoXingZhi int `json:"给药性质"`
  76. GroupNo int `json:"组号"`
  77. Speed string `json:"滴速"`
  78. ShouRiCiShu float64 `json:"首日次数"`
  79. MoRiCiShu float64 `json:"末日次数"`
  80. AdviceDoctorId int `json:"开嘱医师ID"`
  81. AdviceNurseId int `json:"开嘱护士ID"`
  82. OperaId int `json:"操作员ID"`
  83. Opera string `json:"操作员"`
  84. CheckTime int64 `json:"审核时间"`
  85. ReplyAdviceTime int64 `json:"重整医嘱时间"`
  86. } `json:"result"`
  87. Code string `json:"code"`
  88. }
  89. type XmlKey struct {
  90. XMLName xml.Name `xml:"string"`
  91. StringName string `xml:"name,attr"`
  92. InnerText string `xml:",innerxml"`
  93. }
  94. // 获取艾迪康的key
  95. func GetAdKey(logid string, password string) (key string) {
  96. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
  97. apiurl := "logid=" + logid + "&password=" + password
  98. apiurl = url.PathEscape(apiurl)
  99. apiurl = Domain + apiurl
  100. xmlKey := XmlKey{}
  101. httplib.Get(apiurl).ToXML(&xmlKey)
  102. return xmlKey.InnerText
  103. }
  104. type ADString struct {
  105. XMLName xml.Name `xml:"string" json:"string"`
  106. NewDataSet rpnewDataSet `xml:"NewDataSet" json:"NewDataSet"`
  107. }
  108. type rpnewDataSet struct {
  109. ListTable []rplisttable `xml:"listtable" json:"listtable"`
  110. }
  111. type rplisttable struct {
  112. Id string `xml:"Id" json:"Id"`
  113. PatientName string `xml:"PatientName" json:"PatientName"`
  114. Bgrq string `xml:"Bgrq" json:"Bgrq"`
  115. Instrument string `xml:"Instrument" json:"Instrument"`
  116. LisDate string `xml:"LisDate" json:"LisDate"`
  117. Sampleid string `xml:"Sampleid" json:"Sampleid"`
  118. // ReportType string `xml:"ReportType" json:"ReportType"`
  119. // AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"`
  120. // CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
  121. // Repno string `xml:"Repno" json:"Repno"`
  122. // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"`
  123. // DownFlag string `xml:"DownFlag" json:"DownFlag"`
  124. // DownDate string `xml:"DownDate" json:"DownDate"`
  125. // Sjrq string `xml:"Sjrq" json:"Sjrq"`
  126. // Sjys string `xml:"Sjys" json:"Sjys"`
  127. // Brnl string `xml:"Brnl" json:"Brnl"`
  128. // Brxb string `xml:"Brxb" json:"Brxb"`
  129. // Bbzl string `xml:"bbzl" json:"bbzl"`
  130. // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"`
  131. // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"`
  132. // PageType string `xml:"PageType" json:"PageType"`
  133. // PatientNo string `xml:"PatientNo" json:"PatientNo"`
  134. // SfzId string `xml:"SfzId" json:"SfzId"`
  135. // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"`
  136. // Nationality string `xml:"Nationality" json:"Nationality"`
  137. // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"`
  138. // Str1 string `xml:"Str1" json:"Str1"`
  139. // Str2 string `xml:"Str2" json:"Str2"`
  140. // Str3 string `xml:"Str3" json:"Str3"`
  141. // Str4 string `xml:"Str4" json:"Str4"`
  142. // Str5 string `xml:"Str5" json:"Str5"`
  143. // TWBG string `xml:"TWBG" json:"TWBG"`
  144. }
  145. // 获取机构一段时间内检验检查列表
  146. func GetAdGetReportList(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADString) {
  147. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList?"
  148. apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
  149. apiurl = url.PathEscape(apiurl)
  150. apiurl = Domain + apiurl
  151. utils.InfoLog("url0: %v", apiurl)
  152. var xmlKey ADString
  153. xmlString, _ := httplib.Get(apiurl).String()
  154. xmlString = strings.Replace(xmlString, "&lt;", "<", -1)
  155. xmlString = strings.Replace(xmlString, "&gt;", ">", -1)
  156. xml.Unmarshal([]byte(xmlString), &xmlKey)
  157. return xmlKey
  158. }
  159. type adkCheck struct {
  160. ApplyId string `json:"applyid"`
  161. ReportNo string `json:"reportno"`
  162. PatName string `json:"patname"`
  163. Sex string `json:"sex"`
  164. Age string `json:"age"`
  165. DptName string `json:"dptname"`
  166. Testtype string `json:"testtype"`
  167. Testcode string `json:"testcode"`
  168. ExecuteTime string `json:"receivetime"`
  169. Checktime string `json:"checktime"`
  170. Checkcode string `json:"checkcode"`
  171. Result []adkResult `json:"result"`
  172. }
  173. func GetAdkInfo(key string, id string) (str []adkCheck, checkStr string) {
  174. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListById?"
  175. apiurl := "key=" + key + "&Id=" + id
  176. apiurl = url.PathEscape(apiurl)
  177. apiurl = Domain + apiurl
  178. utils.InfoLog("url1: %v", apiurl)
  179. xmlKey := XmlKey{}
  180. httplib.Get(apiurl).ToXML(&xmlKey)
  181. utils.InfoLog("url2: %v", xmlKey.InnerText)
  182. var checkJSON []adkCheck
  183. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  184. return checkJSON, xmlKey.InnerText
  185. }
  186. // 沅胜透析中心Lis同步
  187. func SyncLisYstx() (err error) {
  188. // 第一步:获取艾迪康下发的Key
  189. org_id := int64(9919)
  190. login := "R26028"
  191. password := "abc123"
  192. key := GetAdKey(login, password)
  193. // 第二步:获取上一次同步的时间点
  194. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  195. var sync_time int64
  196. if syncLastInfo.ID > 0 {
  197. sync_time = syncLastInfo.SyncTime + 86400
  198. } else {
  199. sync_time = 1577808000
  200. }
  201. nowTimes := time.Now().Unix()
  202. // syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
  203. if sync_time < nowTimes {
  204. for i := sync_time; i < nowTimes; i = i + 86400 {
  205. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  206. utils.InfoLog("tempsyncTimes:%v", tempsyncTimes)
  207. adString := GetAdGetReportList(key, tempsyncTimes, tempsyncTimes, "2", "2")
  208. listtable := adString.NewDataSet.ListTable
  209. utils.InfoLog("listtable: %v", listtable)
  210. if len(listtable) > 3 {
  211. // 第三步:根据检查列表获取的ID来获取检验检查详情
  212. for _, list := range listtable {
  213. listId := list.Id
  214. utils.InfoLog("listId: %v", listId)
  215. checkInfo, checkStr := GetAdkInfo(key, listId)
  216. utils.InfoLog("checkInfo: %v", checkInfo)
  217. utils.InfoLog("checkStr: %v", checkStr)
  218. for _, info := range checkInfo {
  219. // 根据姓名 去查询对应的patient_id
  220. patient, _ := GetPatientID(org_id, info.PatName)
  221. utils.InfoLog("patient: %v", patient)
  222. if patient.ID > 0 {
  223. middleReportHandle := models.MiddleReportHandle{
  224. OrgId: org_id,
  225. HisUserId: strconv.FormatInt(patient.ID, 10),
  226. ApplyNo: info.ApplyId,
  227. ReportId: info.ReportNo,
  228. Name: info.PatName,
  229. Sex: info.Sex,
  230. Age: info.Age,
  231. Dept: info.DptName,
  232. PatientId: strconv.FormatInt(patient.ID, 10),
  233. RequestTime: info.ExecuteTime,
  234. ExecuteTime: info.ExecuteTime,
  235. ReceiveTime: info.ExecuteTime,
  236. CheckTime: info.ExecuteTime,
  237. ReportInfo: checkStr,
  238. CreatedTime: time.Now().Unix(),
  239. UpdatedTime: time.Now().Unix(),
  240. }
  241. handleerr := CreateReportHandle(&middleReportHandle)
  242. if handleerr != nil {
  243. utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr)
  244. return
  245. }
  246. YsReportInfoHandle(info, org_id, patient.ID)
  247. }
  248. }
  249. }
  250. var syncInfo models.MiddleSyncInfo
  251. syncInfo.OrgId = org_id
  252. syncInfo.SyncTime = i
  253. syncInfo.SyncResultType = 1
  254. syncInfo.SyncRsultRemark = "同步成功"
  255. syncInfo.SyncTotalNum = 0
  256. syncInfo.SyncSuccessNum = 0
  257. syncInfo.SyncInfo = ""
  258. syncInfo.CreateTime = time.Now().Unix()
  259. syncInfo.UpdateTime = time.Now().Unix()
  260. cwderr := CreateSyncInfo(&syncInfo)
  261. if cwderr != nil {
  262. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  263. return
  264. }
  265. }
  266. }
  267. }
  268. return
  269. }
  270. // 根据姓名获取patient_id
  271. func GetPatientID(org_id int64, patient_name string) (patient models.Patients, err error) {
  272. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ? ", org_id, patient_name).First(&patient).Error
  273. return
  274. }
  275. func GetProjectInfo(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) {
  276. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error
  277. return
  278. }
  279. // 修改配置信息
  280. func UpdateInspectionReference(InspectionReference *models.MiddleInspectionReference) error {
  281. err := readMiddleDb.Model(&models.MiddleInspectionReference{}).Updates(&InspectionReference).Error
  282. return err
  283. }
  284. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  285. func GetYsProjectID(org_id int64, project_name string) (project_id int64, err error) {
  286. var inspection_reference models.MiddleInspectionReference
  287. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  288. if inspection_reference.ID > 0 {
  289. return inspection_reference.ProjectId, err
  290. } else {
  291. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  292. utils.InfoLog("inspection_reference: %v", inspection_reference)
  293. if inspection_reference.ProjectId > 0 {
  294. return inspection_reference.ProjectId + 1, err
  295. } else {
  296. return 38001, err
  297. }
  298. }
  299. }
  300. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  301. func GetGzjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  302. var inspection_reference models.MiddleInspectionReference
  303. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  304. if inspection_reference.ID > 0 {
  305. return inspection_reference.ProjectId, err
  306. } else {
  307. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  308. if inspection_reference.ProjectId > 0 {
  309. return inspection_reference.ProjectId + 1, err
  310. } else {
  311. return 116001, err
  312. }
  313. }
  314. }
  315. func YsReportInfoHandle(CheckInfo adkCheck, org_id int64, patientId int64) {
  316. // 1.先把所有的检查项弄出来
  317. for _, info := range CheckInfo.Result {
  318. // 根据project_id和item_id 判断该项是否存在
  319. curitemcode, _ := strconv.ParseInt(info.Itemcode, 10, 64)
  320. reference, _ := GetProjectInfo(curitemcode, org_id)
  321. utils.InfoLog("reference: %v", info)
  322. if reference.ID > 0 {
  323. if reference.ProjectId > 0 {
  324. LgthYsSync(info, reference.ProjectId, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  325. } else {
  326. project_id := int64(0)
  327. if len(info.Groupitemcode) > 0 {
  328. // 对没有project信息的项目进行修改
  329. project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
  330. reference.ProjectId = project_id
  331. reference.ProjectName = info.Groupitemcode
  332. reference.Project = info.Groupitemcode
  333. // 修改配置信息
  334. UpdateInspectionReference(&reference)
  335. }
  336. LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  337. }
  338. } else {
  339. project_id := int64(0)
  340. if len(info.Groupitemcode) > 0 {
  341. project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
  342. }
  343. LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
  344. }
  345. }
  346. SyncToYstx()
  347. }
  348. type adkResult struct {
  349. Testno string `json:"testno"`
  350. Reportno string `json:"reportno"`
  351. Itemord string `json:"itemord"`
  352. Itemcode string `json:"itemcode"`
  353. Itemname string `json:"itemname"`
  354. ItemEname string `json:"itemename"`
  355. Result string `json:"result"`
  356. Units string `json:"units"`
  357. Testdate string `json:"testdate"`
  358. Eqpname string `json:"eqpname"`
  359. Groupitemcode string `json:"groupitemcode"`
  360. Groupitemname string `json:"groupitemname"`
  361. Limit string `json:"limit"`
  362. Resultstate string `json:"resultstate"`
  363. Criticalflag string `json:"criticalflag"`
  364. Itemename string `json:"itemename"`
  365. }
  366. // 从机构将数据同步到中间库
  367. func LgthYsSync(report adkResult, project_id int64, item_id int64, org_id int64, patient_id int64, receivetime string) (*models.MiddleInspectionReference, error) {
  368. tx := writeMiddleDb.Begin()
  369. var inspection models.MiddleInspection
  370. var inspection_reference models.MiddleInspectionReference
  371. recordDateStr := receivetime
  372. if len(receivetime) == 0 {
  373. recordDateStr = time.Now().Format("2006-01-02 15:04")
  374. }
  375. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  376. record_date := record_date_str.Unix()
  377. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  378. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  379. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  380. var total int
  381. var RangeOptions string
  382. var RangeMin string
  383. var RangeMax string
  384. // // 判断检查类型
  385. ItemType := 1
  386. Range := strings.Split(report.Limit, "-")
  387. if len(Range) > 1 {
  388. RangeMin = Range[0]
  389. RangeMax = Range[1]
  390. ItemType = 1
  391. } else {
  392. ItemType = 2
  393. RangeOptions = report.Limit
  394. }
  395. 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
  396. if inspection_reference.ID > 0 {
  397. ItemType = inspection_reference.RangeType
  398. }
  399. if total <= 0 {
  400. inspection_reference.OrgId = org_id
  401. inspection_reference.ProjectName = report.Groupitemcode
  402. inspection_reference.Project = report.Groupitemcode
  403. inspection_reference.ProjectId = project_id
  404. inspection_reference.ItemName = report.Itemname
  405. inspection_reference.ItemNameAddition = report.ItemEname
  406. inspection_reference.ItemId = item_id
  407. inspection_reference.RangeType = ItemType
  408. inspection_reference.RangeMin = RangeMin
  409. inspection_reference.RangeMax = RangeMax
  410. // inspection_reference.RangeValue = RangeValue
  411. inspection_reference.RangeOptions = RangeOptions
  412. inspection_reference.Unit = report.Units
  413. inspection_reference.Status = 1
  414. inspection_reference.CreatedTime = time.Now().Unix()
  415. inspection_reference.UpdatedTime = time.Now().Unix()
  416. inspection_reference.InspectDate = inspect_date
  417. inspection_reference.UTime = inspect_date
  418. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  419. if err != nil {
  420. tx.Rollback()
  421. }
  422. }
  423. var itotal int
  424. 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
  425. if itotal <= 0 {
  426. report.Result = strings.Replace(report.Result, "&gt;", ">", -1)
  427. report.Result = strings.Replace(report.Result, "&lt;", "<", -1)
  428. inspection.PatientId = patient_id
  429. inspection.OrgId = org_id
  430. inspection.ProjectId = project_id
  431. inspection.ItemName = inspection_reference.ItemName
  432. inspection.ProjectName = inspection_reference.ProjectName
  433. inspection.InspectType = ItemType
  434. inspection.ItemId = item_id
  435. inspection.InspectValue = report.Result
  436. inspection.InspectDate = inspect_date
  437. inspection.RecordDate = record_date
  438. inspection.InspectTips = report.Resultstate
  439. inspection.Status = 1
  440. inspection.CreatedTime = time.Now().Unix()
  441. inspection.UpdatedTime = time.Now().Unix()
  442. inspection.UTime = inspect_date
  443. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  444. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  445. if err != nil {
  446. tx.Rollback()
  447. }
  448. }
  449. // //处理检验检查操作表里同步条数和状态
  450. // cur_num := Head.ReportInfoItemHandleNum + 1
  451. // Head.ReportInfoItemHandleNum = cur_num
  452. // handleType := 2
  453. // if cur_num == Head.ReportInfoItemNum {
  454. // handleType = 1
  455. // }
  456. // middleReportHandle := models.MiddleReportHandle{
  457. // ID: Head.ID,
  458. // ReportInfoItemHandleNum: cur_num,
  459. // HandleType: int64(handleType),
  460. // UpdatedTime: time.Now().Unix(),
  461. // }
  462. // err = UpdateReportHandleListyOrgID(&middleReportHandle)
  463. // if err != nil {
  464. // tx.Rollback()
  465. // }
  466. // }
  467. tx.Commit()
  468. return nil, err
  469. }
  470. func SyncToYstx() {
  471. utils.TraceLog("检验检查同步任务开始执行")
  472. org_id := int64(9919)
  473. // 第一步:跟进org_id 去中间库查出需要同步的数据
  474. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  475. inspections, _ := GetSyncInspectionByOrgId(org_id)
  476. // 第二步:将数据同步到业务库
  477. if len(inspection_references) > 0 {
  478. for _, inspection_reference := range inspection_references {
  479. SyncInspectionReference(&inspection_reference)
  480. }
  481. }
  482. if len(inspections) > 0 {
  483. for _, inspection := range inspections {
  484. SyncInspection(&inspection)
  485. }
  486. }
  487. utils.SuccessLog("检验检查同步任务完成")
  488. }
  489. // 根据姓名获取patient_id
  490. func GetPatientGzjhList(org_id int64, schedulesTime int64) (schedules []*models.DialysisSchedule, err error) {
  491. 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
  492. return
  493. }
  494. // 根据姓名获取admin_user_id
  495. func GetAdminUserId(org_id int64, doctor_name string) (doctor models.UserAdminRole, err error) {
  496. err = writeUserDb.Model(&models.UserAdminRole{}).Where("org_id = ? and status = 1 and user_name = ?", org_id, doctor_name).First(&doctor).Error
  497. return
  498. }
  499. func GetPatientGzjhByOrgId(org_id int64) (patientList []*models.Patients, err error) {
  500. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error
  501. return
  502. }
  503. type gzjhResult struct {
  504. ErrorCode int64 `json:"error_code"`
  505. Result []gzjhyz `json:"data"`
  506. }
  507. type gzjhyz struct {
  508. HisUserId string `json:"his_user_id"`
  509. AdviceId int64 `json:"advice_id"`
  510. UserName string `json:"user_name"`
  511. DoctorId string `json:"doctor_id"`
  512. AdviceType string `json:"advice_type"`
  513. StartTime string `json:"start_time"`
  514. AdviceName string `json:"advice_name"`
  515. AdviceDesc string `json:"advice_desc"`
  516. SingleDose string `json:"single_dose"`
  517. SingleDoseUnit string `json:"single_dose_unit"`
  518. PrescribingNumber string `json:"prescribing_number"`
  519. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  520. DeliveryWay string `json:"delivery_way"`
  521. ExecutionFrequency string `json:"execution_frequency"`
  522. State string `json:"state"`
  523. CTime string `json:"c_time"`
  524. UTime string `json:"u_time"`
  525. }
  526. func GetGzjhYz(sync_time int64, his_user_id string) (str gzjhResult, checkStr string) {
  527. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryyzxx"
  528. rep := httplib.Get(apiurl)
  529. rep.Param("sync_time", strconv.FormatInt(sync_time, 10))
  530. rep.Param("access_token", "0")
  531. rep.Param("his_user_id", his_user_id)
  532. utils.InfoLog(strconv.FormatInt(sync_time, 10))
  533. utils.InfoLog(his_user_id)
  534. // str, _ := rep.String()
  535. xmlKey := XmlKey{}
  536. rep.ToXML(&xmlKey)
  537. var checkJSON gzjhResult
  538. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  539. return checkJSON, xmlKey.InnerText
  540. }
  541. func CreateDoctorAdviceHandle(advice *models.XtDoctorAdvice) error {
  542. err := writeDb.Create(&advice).Error
  543. return err
  544. }
  545. func GetAdviceBySyncAdviceId(orgId int64, sync_advice_id int64) (advice models.XtDoctorAdvice, err error) {
  546. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_advice_id = ?", orgId, sync_advice_id).First(&advice).Error
  547. return
  548. }
  549. func GetAdviceBySyncAdviceIdTwo(orgId int64, sync_advice_id string) (advice models.XtDoctorAdvice, err error) {
  550. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_id = ?", orgId, sync_advice_id).First(&advice).Error
  551. return
  552. }
  553. func HttpGetTfData(id_card_no string, advice_time string) (string, string) {
  554. inputData := make(map[string]interface{})
  555. inputData["病人来源"] = "1"
  556. inputData["身份证号"] = id_card_no
  557. inputData["开嘱时间"] = advice_time
  558. var inputLog string
  559. bytesData, err := json.Marshal(inputData)
  560. inputLog = string(bytesData)
  561. fmt.Println(string(bytesData))
  562. if err != nil {
  563. fmt.Println(err.Error())
  564. return err.Error(), ""
  565. }
  566. reader := bytes.NewReader(bytesData)
  567. var url string
  568. gdyb_url := "http://hip.zptfyy.com/esb/listener/getVaf"
  569. url = gdyb_url
  570. //url := "http://igb.hsa.gdgov.cn/ebus/gdyb_inf/poc/hsa/hgs/1101"
  571. request, err := http.NewRequest("POST", url, reader)
  572. if err != nil {
  573. fmt.Println(err.Error())
  574. return err.Error(), ""
  575. }
  576. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  577. request.Header.Set("code", "xt")
  578. client := http.Client{}
  579. resp, err := client.Do(request)
  580. if err != nil {
  581. fmt.Println(err.Error())
  582. return err.Error(), ""
  583. }
  584. respBytes, err := ioutil.ReadAll(resp.Body)
  585. if err != nil {
  586. fmt.Println(err.Error())
  587. return err.Error(), ""
  588. }
  589. fmt.Println(string(respBytes))
  590. str := string(respBytes)
  591. return str, inputLog
  592. }
  593. //func GettfYz(sync_time int64, id_card_no string, advice_time string) (str gzjhResult, checkStr string) {
  594. //
  595. // api := "http://10.129.128.51:8803/esb/listener/getVaf" + "?病人来源=" + "1" + "&身份证号=" + "350623196810011858" + "&开嘱时间=" + "2022-03-30 01:47:51"
  596. // resp, requestErr := http.Get(api)
  597. // fmt.Println(requestErr)
  598. // defer resp.Body.Close()
  599. // body, ioErr := ioutil.ReadAll(resp.Body)
  600. // fmt.Println(ioErr)
  601. // fmt.Println(body)
  602. //
  603. // //apiurl := "http://10.129.128.51:8803/esb/listener/getVaf"
  604. // //rep := httplib.Get(apiurl)
  605. // //rep.Param("病人来源", strconv.FormatInt(1, 10))
  606. // //rep.Param("身份证号", id_card_no)
  607. // //rep.Param("开嘱时间", advice_time)
  608. // //
  609. // //xmlKey := XmlKey{}
  610. // //rep.ToXML(&xmlKey)
  611. // //
  612. // //var checkJSON gzjhResult
  613. // //json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  614. // return nil, ""
  615. //}
  616. // 广州暨华透析中心医嘱同步
  617. func SyncGzjhyz() (err error) {
  618. // 第一步:获取上次同步时间
  619. org_id := int64(10015)
  620. redisClient := RedisClient()
  621. defer redisClient.Close()
  622. sync_time_tt, _ := redisClient.Get("gdjh_sync_time").Result()
  623. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  624. if sync_time == 0 {
  625. sync_time = time.Now().Unix()
  626. }
  627. new_sync_time := time.Now().Unix()
  628. utils.InfoLog("new_sync_time:%v", new_sync_time)
  629. // 切换为当前时间
  630. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  631. // 第二步: 获取当天排班患者的住院号
  632. schedulesDateStr := time.Now().Format("2006-01-02")
  633. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  634. schedulesTime := schedulesDate.Unix()
  635. var patient_zy_ids []string
  636. // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
  637. // if len(patient_zy_ids) == 0 {
  638. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  639. if len(schedules) > 0 {
  640. for _, item := range schedules {
  641. fmt.Println(item)
  642. // if len(item.Patients.AdmissionNumber) > 0 {
  643. if len(patient_zy_ids) == 0 {
  644. //patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  645. } else {
  646. //patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  647. }
  648. // }
  649. }
  650. // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
  651. }
  652. // }
  653. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  654. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  655. idArray := strings.Split("", ",")
  656. for _, idStr := range idArray {
  657. temp_id := strings.Split(idStr, "-")
  658. id := temp_id[0]
  659. patient_id := temp_id[1]
  660. admission_number := temp_id[2]
  661. gzjhyz, yzstr := GetGzjhYz(sync_time, id)
  662. if len(gzjhyz.Result) == 0 && len(admission_number) > 0 {
  663. utils.InfoLog("admission_number:%v", admission_number)
  664. gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
  665. }
  666. utils.InfoLog("sync_time:%v", sync_time)
  667. utils.InfoLog("yzstr:%v", yzstr)
  668. if len(gzjhyz.Result) > 0 {
  669. timeLayout := "2006010215:04:05" //转化所需模板
  670. loc, _ := time.LoadLocation("Local") //重要:获取时区
  671. for _, yz := range gzjhyz.Result {
  672. // 根据姓名获取医生ID
  673. var doctor_id int64
  674. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + yz.DoctorId).Result()
  675. if len(temp_doctor_id) == 0 {
  676. doctor, _ := GetAdminUserId(org_id, yz.DoctorId)
  677. doctor_id = doctor.AdminUserId
  678. if doctor_id > 0 {
  679. redisClient.Set("gdjh_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
  680. } else {
  681. continue
  682. }
  683. } else {
  684. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  685. }
  686. theTime, _ := time.ParseInLocation(timeLayout, yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
  687. start_time := theTime.Unix()
  688. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  689. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  690. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  691. advice := models.XtDoctorAdvice{
  692. UserOrgId: org_id,
  693. PatientId: temp_patient_id,
  694. AdviceType: 2,
  695. AdviceDate: schedulesTime,
  696. StartTime: start_time,
  697. AdviceName: yz.AdviceName,
  698. AdviceDesc: yz.AdviceDesc,
  699. RecordDate: schedulesTime,
  700. SingleDose: SingleDose,
  701. SingleDoseUnit: yz.SingleDoseUnit,
  702. PrescribingNumber: PrescribingNumber,
  703. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  704. DeliveryWay: yz.DeliveryWay,
  705. ExecutionFrequency: yz.ExecutionFrequency,
  706. AdviceDoctor: doctor_id,
  707. CreatedTime: time.Now().Unix(),
  708. UpdatedTime: time.Now().Unix(),
  709. SyncAdviceId: yz.AdviceId,
  710. ExecutionState: 2,
  711. Status: 1,
  712. IsSync: 1,
  713. }
  714. utils.InfoLog("advice:%v", advice)
  715. // 根据同步来的 advice_id 来查询库里是否已经存在
  716. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  717. utils.InfoLog("advice_info:%v", advice_info)
  718. if advice_info.ID > 0 {
  719. key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
  720. //清空key 值
  721. redisClient.Set(key, "", time.Second)
  722. keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
  723. redisClient.Set(keyOne, "", time.Second)
  724. continue
  725. } else {
  726. if len(yz.AdviceName) > 0 {
  727. handleerr := CreateDoctorAdviceHandle(&advice)
  728. if handleerr != nil {
  729. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  730. continue
  731. }
  732. } else {
  733. continue
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return
  740. }
  741. type gzjhLisResult struct {
  742. ErrorCode int64 `json:"error_code"`
  743. Result []gzjhlis `json:"data"`
  744. }
  745. type gzjhlis struct {
  746. HisUserId string `json:"his_user_id"`
  747. PatientName string `json:"patient_name"`
  748. ProjectId string `json:"project_id"`
  749. ProjectName string `json:"project_name"`
  750. InspectDate string `json:"inspect_date"`
  751. ItemId string `json:"item_id"`
  752. ItemName string `json:"item_name"`
  753. ItemType string `json:"item_type"`
  754. ItemValue string `json:"item_value"`
  755. RangeMin string `json:"range_min"`
  756. RangeMax string `json:"range_max"`
  757. RangeValue string `json:"range_value"`
  758. RangeOptions string `json:"range_options"`
  759. Unit string `json:"unit"`
  760. State string `json:"state"`
  761. CTime string `json:"c_time"`
  762. UTime string `json:"u_time"`
  763. }
  764. func GetGzjhLis(sync_time string, his_user_id string) (str gzjhLisResult, checkStr string) {
  765. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryjyjcxx"
  766. rep := httplib.Post(apiurl)
  767. rep.Param("sync_time", sync_time)
  768. rep.Param("access_token", "")
  769. rep.Param("his_user_id", his_user_id)
  770. // str, _ := rep.String()
  771. xmlKey := XmlKey{}
  772. rep.ToXML(&xmlKey)
  773. var checkJSON gzjhLisResult
  774. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  775. return checkJSON, xmlKey.InnerText
  776. }
  777. // 广州暨华透析中心Lis同步
  778. func SyncGzjhLis() (err error) {
  779. org_id := int64(10016)
  780. // 第一步:获取上一次同步的时间点
  781. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  782. var sync_time int64
  783. if syncLastInfo.ID > 0 {
  784. sync_time = syncLastInfo.SyncTime + 86400
  785. } else {
  786. sync_time = 1577808000
  787. }
  788. // 第二步:获取所有患者的病历号
  789. patientList, _ := GetPatientGzjhByOrgId(org_id)
  790. if len(patientList) > 0 {
  791. for _, patient := range patientList {
  792. if len(patient.AdmissionNumber) > 0 {
  793. // 上次同步时间转换为接口需要的格式
  794. patient_id := patient.ID
  795. sync_time_temp := time.Unix(sync_time, 0)
  796. syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  797. gzjhLis, lisStr := GetGzjhLis(syncTimeStr, patient.IdCardNo)
  798. utils.InfoLog("yzstr:%v", lisStr)
  799. if len(gzjhLis.Result) > 0 {
  800. // timeLayout := "2006010215:04:05" // 转化所需模板
  801. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  802. for _, info := range gzjhLis.Result {
  803. // 根据project_id和item_id 判断该项是否存在
  804. project_id := int64(0)
  805. if len(info.ProjectId) > 0 {
  806. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  807. } else {
  808. continue
  809. }
  810. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  811. tx := writeMiddleDb.Begin()
  812. var inspection models.MiddleInspection
  813. var inspection_reference models.MiddleInspectionReference
  814. recordDateStr := info.InspectDate
  815. if len(info.InspectDate) == 0 {
  816. recordDateStr = time.Now().Format("2006-01-02 15:04")
  817. }
  818. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  819. record_date := record_date_str.Unix()
  820. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  821. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  822. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  823. var total int
  824. var RangeOptions string
  825. var RangeMin string
  826. var RangeMax string
  827. // // 判断检查类型
  828. ItemType, _ := strconv.Atoi(info.ItemType)
  829. Range := strings.Split(info.RangeValue, "~")
  830. if len(Range) > 1 {
  831. RangeMin = Range[0]
  832. RangeMax = Range[1]
  833. ItemType = 1
  834. } else {
  835. ItemType = 2
  836. RangeOptions = info.RangeOptions
  837. }
  838. 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
  839. if inspection_reference.ID > 0 {
  840. ItemType = inspection_reference.RangeType
  841. }
  842. if total <= 0 {
  843. inspection_reference.OrgId = org_id
  844. inspection_reference.ProjectName = info.ProjectName
  845. inspection_reference.Project = info.ProjectName
  846. inspection_reference.ProjectId = project_id
  847. inspection_reference.ItemName = info.ItemName
  848. inspection_reference.ItemNameAddition = info.ItemId
  849. inspection_reference.ItemId = item_id
  850. inspection_reference.RangeType = ItemType
  851. inspection_reference.RangeMin = RangeMin
  852. inspection_reference.RangeMax = RangeMax
  853. // inspection_reference.RangeValue = RangeValue
  854. inspection_reference.RangeOptions = RangeOptions
  855. inspection_reference.Unit = info.Unit
  856. inspection_reference.Status = 1
  857. inspection_reference.CreatedTime = time.Now().Unix()
  858. inspection_reference.UpdatedTime = time.Now().Unix()
  859. inspection_reference.InspectDate = inspect_date
  860. inspection_reference.UTime = inspect_date
  861. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  862. if err != nil {
  863. tx.Rollback()
  864. }
  865. }
  866. var itotal int
  867. 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
  868. if itotal <= 0 {
  869. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  870. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  871. inspection.PatientId = patient_id
  872. inspection.OrgId = org_id
  873. inspection.ProjectId = project_id
  874. inspection.ItemName = inspection_reference.ItemName
  875. inspection.ProjectName = inspection_reference.ProjectName
  876. inspection.InspectType = ItemType
  877. inspection.ItemId = item_id
  878. inspection.InspectValue = info.ItemValue
  879. inspection.InspectDate = inspect_date
  880. inspection.RecordDate = record_date
  881. // inspection.InspectTips = report.Resultstate
  882. inspection.Status = 1
  883. inspection.CreatedTime = time.Now().Unix()
  884. inspection.UpdatedTime = time.Now().Unix()
  885. inspection.UTime = inspect_date
  886. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  887. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  888. if err != nil {
  889. tx.Rollback()
  890. }
  891. }
  892. tx.Commit()
  893. }
  894. } else {
  895. continue
  896. }
  897. } else {
  898. continue
  899. }
  900. }
  901. }
  902. var syncInfo models.MiddleSyncInfo
  903. syncInfo.OrgId = org_id
  904. syncInfo.SyncTime = time.Now().Unix()
  905. syncInfo.SyncResultType = 1
  906. syncInfo.SyncRsultRemark = "同步成功"
  907. syncInfo.SyncTotalNum = 0
  908. syncInfo.SyncSuccessNum = 0
  909. syncInfo.SyncInfo = ""
  910. syncInfo.CreateTime = time.Now().Unix()
  911. syncInfo.UpdateTime = time.Now().Unix()
  912. cwderr := CreateSyncInfo(&syncInfo)
  913. if cwderr != nil {
  914. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  915. return
  916. }
  917. SyncToGzjhtx()
  918. return
  919. }
  920. func SyncToGzjhtx() {
  921. utils.TraceLog("检验检查同步任务开始执行")
  922. org_id := int64(10016)
  923. // 第一步:跟进org_id 去中间库查出需要同步的数据
  924. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  925. inspections, _ := GetSyncInspectionByOrgId(org_id)
  926. // 第二步:将数据同步到业务库
  927. if len(inspection_references) > 0 {
  928. for _, inspection_reference := range inspection_references {
  929. SyncInspectionReference(&inspection_reference)
  930. }
  931. }
  932. if len(inspections) > 0 {
  933. for _, inspection := range inspections {
  934. SyncInspection(&inspection)
  935. }
  936. }
  937. utils.SuccessLog("检验检查同步任务完成")
  938. }
  939. type hbdyyz struct {
  940. HisUserId string `json:"his_user_id"`
  941. UserName string `json:"user_name"`
  942. DoctorId int64 `json:"doctor_id"`
  943. DoctorName string `json:"Doctor_name"`
  944. AdviceId int64 `json:"advice_id"`
  945. MedListCodg string `json:"med_list_codg"`
  946. StartTime int64 `json:"start_time"`
  947. AdviceName string `json:"advice_name"`
  948. AdviceDesc string `json:"advice_desc"`
  949. SingleDose string `json:"single_dose"`
  950. SingleDoseUnit string `json:"single_dose_unit"`
  951. PrescribingNumber string `json:"prescribing_number"`
  952. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  953. DeliveryWay string `json:"delivery_way"`
  954. ExecutionFrequency string `json:"execution_frequency"`
  955. State int64 `json:"state"`
  956. CTime int64 `json:"create_time"`
  957. UTime int64 `json:"update_time"`
  958. }
  959. type hbdyHisResult struct {
  960. ErrorCode int64 `json:"error_code"`
  961. Result []hbdyyz `json:"data"`
  962. }
  963. func GetHbdyYz(sync_time string, his_user_id string) (str hbdyHisResult, checkStr string) {
  964. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_ADVICE"
  965. rep := httplib.Post(apiurl)
  966. rep.Param("sync_time", sync_time)
  967. rep.Param("his_user_id", his_user_id)
  968. strs, err := rep.String()
  969. if err != nil {
  970. utils.InfoLog("err:%v", err)
  971. }
  972. utils.InfoLog("str:%v", strs)
  973. var checkJSON hbdyHisResult
  974. json.Unmarshal([]byte(strs), &checkJSON)
  975. utils.InfoLog("checkJSON:%v", checkJSON)
  976. return checkJSON, strs
  977. }
  978. // 湖北监利大垸透析中心医嘱同步
  979. func SyncHbJldyyz() (err error) {
  980. // 第一步:获取上次同步时间
  981. org_id := int64(10101)
  982. redisClient := RedisClient()
  983. defer redisClient.Close()
  984. sync_time, _ := redisClient.Get("gdjh_sync_time1").Result()
  985. if len(sync_time) == 0 {
  986. sync_time = strconv.FormatInt(time.Now().Unix(), 10)
  987. utils.InfoLog("sync_time:%v", sync_time)
  988. // formatTimeStr := "2021-08-31 00:00"
  989. // checkDateUnix, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", sync_time)
  990. // sync_time = strconv.FormatInt(checkDateUnix.Unix(),10)
  991. }
  992. new_sync_time := time.Now().Unix()
  993. utils.InfoLog("new_sync_time:%v", new_sync_time)
  994. // 切换为当前时间
  995. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  996. // 第二步: 获取当天排班患者的住院号
  997. schedulesDateStr := time.Now().Format("2006-01-02")
  998. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  999. schedulesTime := schedulesDate.Unix()
  1000. patient_zy_ids, _ := redisClient.Get("gdjh_patient_zy_ids").Result()
  1001. if len(patient_zy_ids) == 0 {
  1002. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  1003. if len(schedules) > 0 {
  1004. for _, item := range schedules {
  1005. if len(patient_zy_ids) == 0 {
  1006. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  1007. } else {
  1008. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  1009. }
  1010. }
  1011. redisClient.Set("gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
  1012. }
  1013. }
  1014. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  1015. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  1016. idArray := strings.Split(patient_zy_ids, ",")
  1017. for _, idStr := range idArray {
  1018. temp_id := strings.Split(idStr, "-")
  1019. id := temp_id[0]
  1020. patient_id := temp_id[1]
  1021. hbdyyz, _ := GetHbdyYz(sync_time, id)
  1022. if len(hbdyyz.Result) > 0 {
  1023. for _, yz := range hbdyyz.Result {
  1024. utils.InfoLog("yz:%v", yz)
  1025. // 根据姓名获取医生ID
  1026. var doctor_id int64
  1027. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + strconv.FormatInt(yz.DoctorId, 10)).Result()
  1028. if len(temp_doctor_id) == 0 {
  1029. doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
  1030. doctor_id = doctor.AdminUserId
  1031. if doctor_id > 0 {
  1032. redisClient.Set("gdjh_doctor_id_"+strconv.FormatInt(yz.DoctorId, 10), doctor_id, time.Minute*60*2)
  1033. } else {
  1034. continue
  1035. }
  1036. } else {
  1037. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  1038. }
  1039. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  1040. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  1041. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  1042. utils.InfoLog("PrescribingNumber:%v", PrescribingNumber)
  1043. advice := models.XtDoctorAdvice{
  1044. UserOrgId: org_id,
  1045. PatientId: temp_patient_id,
  1046. AdviceType: 2,
  1047. AdviceDate: schedulesTime,
  1048. StartTime: yz.StartTime,
  1049. AdviceName: yz.AdviceName,
  1050. AdviceDesc: yz.AdviceDesc,
  1051. RecordDate: schedulesTime,
  1052. SingleDose: SingleDose,
  1053. SingleDoseUnit: yz.SingleDoseUnit,
  1054. PrescribingNumber: PrescribingNumber,
  1055. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  1056. DeliveryWay: yz.DeliveryWay,
  1057. ExecutionFrequency: yz.ExecutionFrequency,
  1058. AdviceDoctor: doctor_id,
  1059. CreatedTime: time.Now().Unix(),
  1060. UpdatedTime: time.Now().Unix(),
  1061. SyncAdviceId: yz.AdviceId,
  1062. ExecutionState: 2,
  1063. Status: 1,
  1064. IsSync: 1,
  1065. }
  1066. utils.InfoLog("advice:%v", advice)
  1067. // 根据同步来的 advice_id 来查询库里是否已经存在
  1068. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  1069. utils.InfoLog("advice_info:%v", advice_info)
  1070. if advice_info.ID > 0 {
  1071. continue
  1072. } else {
  1073. if yz.State == 1 {
  1074. handleerr := CreateDoctorAdviceHandle(&advice)
  1075. if handleerr != nil {
  1076. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  1077. continue
  1078. }
  1079. } else {
  1080. continue
  1081. }
  1082. }
  1083. }
  1084. }
  1085. }
  1086. return
  1087. }
  1088. type hbdylis struct {
  1089. HisUserId string `json:"his_user_id"`
  1090. PatientName string `json:"patient_name"`
  1091. ProjectId int64 `json:"project_id"`
  1092. ProjectName string `json:"project_name"`
  1093. InspectDate int64 `json:"inspect_date"`
  1094. ItemId int64 `json:"item_id"`
  1095. ItemName string `json:"item_name"`
  1096. ItemType int64 `json:"item_type"`
  1097. ItemValue string `json:"item_value"`
  1098. RangeMin string `json:"range_min"`
  1099. RangeMax string `json:"range_max"`
  1100. RangeValue string `json:"range_value"`
  1101. RangeOptions string `json:"range_options"`
  1102. Unit string `json:"unit"`
  1103. State int64 `json:"state"`
  1104. CTime int64 `json:"c_time"`
  1105. UTime int64 `json:"u_time"`
  1106. }
  1107. type hbdyLisResult struct {
  1108. ErrorCode int64 `json:"error_code"`
  1109. Result []hbdylis `json:"data"`
  1110. }
  1111. func GethbdyLis(sync_time string, his_user_id string) (str hbdyLisResult, checkStr string) {
  1112. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_INSPECTION"
  1113. rep := httplib.Post(apiurl)
  1114. rep.Param("sync_time", sync_time)
  1115. rep.Param("his_user_id", his_user_id)
  1116. strs, err := rep.String()
  1117. if err != nil {
  1118. utils.InfoLog("err:%v", err)
  1119. }
  1120. utils.InfoLog("str:%v", strs)
  1121. var checkJSON hbdyLisResult
  1122. json.Unmarshal([]byte(strs), &checkJSON)
  1123. // utils.InfoLog("checkJSON:%v", checkJSON)
  1124. return checkJSON, strs
  1125. }
  1126. // 湖北监利大院透析中心Lis同步
  1127. func SyncHbdyLis() (err error) {
  1128. org_id := int64(10101)
  1129. // 第一步:获取上一次同步的时间点
  1130. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  1131. var sync_time int64
  1132. if syncLastInfo.ID > 0 {
  1133. sync_time = syncLastInfo.SyncTime
  1134. } else {
  1135. sync_time = 1627747200
  1136. }
  1137. // 第二步:获取所有患者的病历号
  1138. patientList, _ := GetPatientGzjhByOrgId(org_id)
  1139. if len(patientList) > 0 {
  1140. for _, patient := range patientList {
  1141. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  1142. // 上次同步时间转换为接口需要的格式
  1143. patient_id := patient.ID
  1144. // sync_time_temp := time.Unix(sync_time,0)
  1145. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  1146. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  1147. // 获取会员号
  1148. // utils.InfoLog("VipStr:%v", VipStr)
  1149. gzjhLis, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  1150. // if len(gzjhLis.Result) == 0 {
  1151. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  1152. // }
  1153. // utils.InfoLog("yzstr:%v", gzjhLis)
  1154. if len(gzjhLis.Result) > 0 {
  1155. // timeLayout := "2006010215:04:05" // 转化所需模板
  1156. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1157. for _, info := range gzjhLis.Result {
  1158. utils.InfoLog("Result:%v", info)
  1159. // 根据project_id和item_id 判断该项是否存在
  1160. project_id := int64(0)
  1161. if info.ProjectId > 0 {
  1162. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1163. } else {
  1164. continue
  1165. }
  1166. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1167. tx := writeMiddleDb.Begin()
  1168. var inspection models.MiddleInspection
  1169. var inspection_reference models.MiddleInspectionReference
  1170. record_date := info.InspectDate
  1171. if info.InspectDate == 0 {
  1172. record_date = time.Now().Unix()
  1173. }
  1174. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1175. // record_date := record_date_str.Unix()
  1176. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1177. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1178. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1179. var total int
  1180. var RangeOptions string
  1181. var RangeMin string
  1182. var RangeMax string
  1183. // // 判断检查类型
  1184. ItemType := info.ItemType
  1185. Range := strings.Split(info.RangeValue, "-")
  1186. if len(Range) > 1 {
  1187. RangeMin = Range[0]
  1188. RangeMax = Range[1]
  1189. ItemType = 1
  1190. } else {
  1191. ItemType = 2
  1192. RangeOptions = info.RangeOptions
  1193. }
  1194. 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
  1195. // if inspection_reference.ID > 0 {
  1196. // ItemType = int64(inspection_reference.RangeType)
  1197. // }
  1198. if total <= 0 {
  1199. inspection_reference.OrgId = org_id
  1200. inspection_reference.ProjectName = info.ProjectName
  1201. inspection_reference.Project = info.ProjectName
  1202. inspection_reference.ProjectId = project_id
  1203. inspection_reference.ItemName = info.ItemName
  1204. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1205. inspection_reference.ItemId = item_id
  1206. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1207. inspection_reference.RangeMin = RangeMin
  1208. inspection_reference.RangeMax = RangeMax
  1209. // inspection_reference.RangeValue = RangeValue
  1210. inspection_reference.RangeOptions = RangeOptions
  1211. inspection_reference.Unit = info.Unit
  1212. inspection_reference.Status = 1
  1213. inspection_reference.CreatedTime = time.Now().Unix()
  1214. inspection_reference.UpdatedTime = time.Now().Unix()
  1215. inspection_reference.InspectDate = inspect_date
  1216. inspection_reference.UTime = inspect_date
  1217. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1218. if err != nil {
  1219. tx.Rollback()
  1220. }
  1221. }
  1222. var itotal int
  1223. 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
  1224. if itotal <= 0 {
  1225. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1226. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1227. inspection.PatientId = patient_id
  1228. inspection.OrgId = org_id
  1229. inspection.ProjectId = project_id
  1230. inspection.ItemName = inspection_reference.ItemName
  1231. inspection.ProjectName = inspection_reference.ProjectName
  1232. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1233. inspection.ItemId = item_id
  1234. inspection.InspectValue = info.ItemValue
  1235. inspection.InspectDate = inspect_date
  1236. inspection.RecordDate = record_date
  1237. // inspection.InspectTips = report.Resultstate
  1238. inspection.Status = 1
  1239. inspection.CreatedTime = time.Now().Unix()
  1240. inspection.UpdatedTime = time.Now().Unix()
  1241. inspection.UTime = inspect_date
  1242. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1243. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1244. if err != nil {
  1245. tx.Rollback()
  1246. }
  1247. }
  1248. tx.Commit()
  1249. }
  1250. }
  1251. VipStr := ""
  1252. VipArr := strings.Split(patient.DialysisNo, "(")
  1253. if len(VipArr) > 1 {
  1254. VipArrTwo := strings.Split(VipArr[1], ")")
  1255. VipStr = VipArrTwo[0]
  1256. gzjhLisbyVip, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), VipStr)
  1257. if len(gzjhLisbyVip.Result) > 0 {
  1258. // timeLayout := "2006010215:04:05" // 转化所需模板
  1259. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1260. for _, info := range gzjhLisbyVip.Result {
  1261. // utils.InfoLog("Result:%v", info)
  1262. // 根据project_id和item_id 判断该项是否存在
  1263. project_id := int64(0)
  1264. if info.ProjectId > 0 {
  1265. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1266. } else {
  1267. continue
  1268. }
  1269. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1270. tx := writeMiddleDb.Begin()
  1271. var inspection models.MiddleInspection
  1272. var inspection_reference models.MiddleInspectionReference
  1273. record_date := info.InspectDate
  1274. if info.InspectDate == 0 {
  1275. record_date = time.Now().Unix()
  1276. }
  1277. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1278. // record_date := record_date_str.Unix()
  1279. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1280. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1281. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1282. var total int
  1283. var RangeOptions string
  1284. var RangeMin string
  1285. var RangeMax string
  1286. // // 判断检查类型
  1287. ItemType := info.ItemType
  1288. Range := strings.Split(info.RangeValue, "-")
  1289. if len(Range) > 1 {
  1290. RangeMin = Range[0]
  1291. RangeMax = Range[1]
  1292. ItemType = 1
  1293. } else {
  1294. ItemType = 2
  1295. RangeOptions = info.RangeOptions
  1296. }
  1297. 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
  1298. if inspection_reference.ID > 0 {
  1299. ItemType = int64(inspection_reference.RangeType)
  1300. }
  1301. if total <= 0 {
  1302. inspection_reference.OrgId = org_id
  1303. inspection_reference.ProjectName = info.ProjectName
  1304. inspection_reference.Project = info.ProjectName
  1305. inspection_reference.ProjectId = project_id
  1306. inspection_reference.ItemName = info.ItemName
  1307. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1308. inspection_reference.ItemId = item_id
  1309. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1310. inspection_reference.RangeMin = RangeMin
  1311. inspection_reference.RangeMax = RangeMax
  1312. // inspection_reference.RangeValue = RangeValue
  1313. inspection_reference.RangeOptions = RangeOptions
  1314. inspection_reference.Unit = info.Unit
  1315. inspection_reference.Status = 1
  1316. inspection_reference.CreatedTime = time.Now().Unix()
  1317. inspection_reference.UpdatedTime = time.Now().Unix()
  1318. inspection_reference.InspectDate = inspect_date
  1319. inspection_reference.UTime = inspect_date
  1320. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1321. if err != nil {
  1322. tx.Rollback()
  1323. }
  1324. }
  1325. var itotal int
  1326. 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
  1327. if itotal <= 0 {
  1328. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1329. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1330. inspection.PatientId = patient_id
  1331. inspection.OrgId = org_id
  1332. inspection.ProjectId = project_id
  1333. inspection.ItemName = inspection_reference.ItemName
  1334. inspection.ProjectName = inspection_reference.ProjectName
  1335. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1336. inspection.ItemId = item_id
  1337. inspection.InspectValue = info.ItemValue
  1338. inspection.InspectDate = inspect_date
  1339. inspection.RecordDate = record_date
  1340. // inspection.InspectTips = report.Resultstate
  1341. inspection.Status = 1
  1342. inspection.CreatedTime = time.Now().Unix()
  1343. inspection.UpdatedTime = time.Now().Unix()
  1344. inspection.UTime = inspect_date
  1345. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1346. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1347. if err != nil {
  1348. tx.Rollback()
  1349. }
  1350. }
  1351. tx.Commit()
  1352. }
  1353. }
  1354. }
  1355. } else {
  1356. continue
  1357. }
  1358. }
  1359. }
  1360. var syncInfo models.MiddleSyncInfo
  1361. syncInfo.OrgId = org_id
  1362. syncInfo.SyncTime = time.Now().Unix()
  1363. syncInfo.SyncResultType = 1
  1364. syncInfo.SyncRsultRemark = "同步成功"
  1365. syncInfo.SyncTotalNum = 0
  1366. syncInfo.SyncSuccessNum = 0
  1367. syncInfo.SyncInfo = ""
  1368. syncInfo.CreateTime = time.Now().Unix()
  1369. syncInfo.UpdateTime = time.Now().Unix()
  1370. cwderr := CreateSyncInfo(&syncInfo)
  1371. if cwderr != nil {
  1372. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  1373. return
  1374. }
  1375. SyncToHbdytx()
  1376. return
  1377. }
  1378. func SyncToHbdytx() {
  1379. utils.TraceLog("检验检查同步任务开始执行")
  1380. org_id := int64(10101)
  1381. // 第一步:跟进org_id 去中间库查出需要同步的数据
  1382. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  1383. inspections, _ := GetSyncInspectionByOrgId(org_id)
  1384. // 第二步:将数据同步到业务库
  1385. if len(inspection_references) > 0 {
  1386. for _, inspection_reference := range inspection_references {
  1387. SyncInspectionReference(&inspection_reference)
  1388. }
  1389. }
  1390. if len(inspections) > 0 {
  1391. for _, inspection := range inspections {
  1392. SyncInspection(&inspection)
  1393. }
  1394. }
  1395. utils.SuccessLog("检验检查同步任务完成")
  1396. }
  1397. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1398. func GetBljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1399. var inspection_reference models.MiddleInspectionReference
  1400. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1401. if inspection_reference.ID > 0 {
  1402. return inspection_reference.ProjectId, err
  1403. } else {
  1404. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1405. if inspection_reference.ProjectId > 0 {
  1406. return inspection_reference.ProjectId + 1, err
  1407. } else {
  1408. return 5210138, err
  1409. }
  1410. }
  1411. }
  1412. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1413. func GetBlItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1414. var inspection_reference models.MiddleInspectionReference
  1415. 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
  1416. if inspection_reference.ID > 0 {
  1417. return inspection_reference.ItemId, err
  1418. } else {
  1419. 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
  1420. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1421. if inspection_reference.ItemId > 0 {
  1422. return inspection_reference.ItemId + 1, err
  1423. } else {
  1424. return project_id*10 + 1, err
  1425. }
  1426. }
  1427. }
  1428. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1429. func GetShjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1430. var inspection_reference models.MiddleInspectionReference
  1431. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1432. if inspection_reference.ID > 0 {
  1433. return inspection_reference.ProjectId, err
  1434. } else {
  1435. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1436. if inspection_reference.ProjectId > 0 {
  1437. return inspection_reference.ProjectId + 1, err
  1438. } else {
  1439. return 5210215, err
  1440. }
  1441. }
  1442. }
  1443. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1444. func GetSzbljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1445. var inspection_reference models.MiddleInspectionReference
  1446. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1447. if inspection_reference.ID > 0 {
  1448. return inspection_reference.ProjectId, err
  1449. } else {
  1450. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1451. if inspection_reference.ProjectId > 0 {
  1452. return inspection_reference.ProjectId + 1, err
  1453. } else {
  1454. return 1013800, err
  1455. }
  1456. }
  1457. }
  1458. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1459. func GetShItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1460. var inspection_reference models.MiddleInspectionReference
  1461. 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
  1462. if inspection_reference.ID > 0 {
  1463. return inspection_reference.ItemId, err
  1464. } else {
  1465. 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
  1466. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1467. if inspection_reference.ItemId > 0 {
  1468. return inspection_reference.ItemId + 1, err
  1469. } else {
  1470. return project_id*10 + 1, err
  1471. }
  1472. }
  1473. }
  1474. func Synctfyz() (err error) {
  1475. // 第一步:获取上次同步时间
  1476. org_id := int64(10330)
  1477. redisClient := RedisClient()
  1478. defer redisClient.Close()
  1479. sync_time_tt, _ := redisClient.Get("tf_sync_time").Result()
  1480. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  1481. if sync_time == 0 {
  1482. sync_time = time.Now().Unix()
  1483. }
  1484. new_sync_time := time.Now().Unix()
  1485. utils.InfoLog("new_sync_time:%v", new_sync_time)
  1486. // 切换为当前时间
  1487. redisClient.Set("tf_sync_time", new_sync_time, time.Minute*60*24)
  1488. // 第二步: 获取当天排班患者的住院号
  1489. schedulesDateStr := time.Now().Format("2006-01-02")
  1490. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  1491. schedulesTime := schedulesDate.Unix()
  1492. var patient_zy_ids string
  1493. // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
  1494. // if len(patient_zy_ids) == 0 {
  1495. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  1496. if len(schedules) > 0 {
  1497. for _, item := range schedules {
  1498. // if len(item.Patients.AdmissionNumber) > 0 {
  1499. if len(patient_zy_ids) == 0 {
  1500. patient_zy_ids = item.Patients.IdCardNo
  1501. } else {
  1502. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo
  1503. }
  1504. // }
  1505. }
  1506. // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
  1507. }
  1508. // }
  1509. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  1510. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  1511. idArray := strings.Split(patient_zy_ids, ",")
  1512. for _, idStr := range idArray {
  1513. //temp_id := strings.Split(idStr, "-")
  1514. id := idStr
  1515. printInfo, _ := GetPatientsInfoByIDCardNo(id, org_id)
  1516. timeNow := time.Unix(sync_time, 0) //2017-08-30 16:19:19 +0800 CST
  1517. timeString := timeNow.Format("2006-01-02 15:04:05") //2015-06-15 08:52:32
  1518. result, requestLog := HttpGetTfData(id, timeString)
  1519. saveLog(result, requestLog, "tf", "tf", "tf")
  1520. //fmt.Println(result)
  1521. //fmt.Println(requestLog)
  1522. var res RequestResult
  1523. if err := json.Unmarshal([]byte(result), &res); err != nil {
  1524. utils.ErrorLog("解析失败:%v", err)
  1525. }
  1526. fmt.Println(res)
  1527. //saveLog(result, requestLog, "tf", "tf", "tf")
  1528. //
  1529. //if len(gzjhyz.Result) == 0 && len(admission_number) > 0 {
  1530. // utils.InfoLog("admission_number:%v", admission_number)
  1531. // gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
  1532. //}
  1533. //utils.InfoLog("sync_time:%v", sync_time)
  1534. //utils.InfoLog("yzstr:%v", yzstr)
  1535. if len(res.Result) > 0 {
  1536. //timeLayout := "2006010215:04:05" //转化所需模板
  1537. //loc, _ := time.LoadLocation("Local") //重要:获取时区
  1538. for _, yz := range res.Result {
  1539. // 根据姓名获取医生ID
  1540. var doctor_id int64
  1541. //doctor_id, _ = strconv.ParseInt(yz.AdviceDoctorId, 10, 64)
  1542. temp_doctor_id, _ := redisClient.Get("tf_doctor_id_" + yz.Doctor).Result()
  1543. if len(temp_doctor_id) == 0 {
  1544. doctor, _ := GetAdminUserId(org_id, yz.Doctor)
  1545. doctor_id = doctor.AdminUserId
  1546. if doctor_id > 0 {
  1547. redisClient.Set("tf_doctor_id_"+yz.Doctor, doctor_id, time.Minute*60*2)
  1548. } else {
  1549. continue
  1550. }
  1551. } else {
  1552. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  1553. }
  1554. //theTime, _ := time.ParseInLocation(timeLayout, , loc) //使用模板在对应时区转化为time.time类型
  1555. start_time := yz.AdviceTime / 1000
  1556. fmt.Println(start_time)
  1557. temp_patient_id := printInfo.ID
  1558. SingleDose := yz.DanLiang
  1559. PrescribingNumber := yz.Shuliang
  1560. advice := models.XtDoctorAdvice{
  1561. UserOrgId: org_id,
  1562. PatientId: temp_patient_id,
  1563. AdviceType: 2,
  1564. AdviceDate: schedulesTime,
  1565. StartTime: start_time,
  1566. AdviceName: yz.Advice,
  1567. //AdviceDesc: yz.AdviceDesc,
  1568. RecordDate: schedulesTime,
  1569. SingleDose: SingleDose,
  1570. PrescribingNumber: PrescribingNumber,
  1571. DeliveryWay: yz.GeiYaoTuJing,
  1572. ExecutionFrequency: yz.ZhiXingPinCi,
  1573. AdviceDoctor: doctor_id,
  1574. CreatedTime: time.Now().Unix(),
  1575. UpdatedTime: time.Now().Unix(),
  1576. SyncAdviceId: 0,
  1577. ExecutionState: 2,
  1578. Status: 1,
  1579. IsSync: 1,
  1580. SyncId: strconv.FormatInt(yz.AdviceId, 10),
  1581. }
  1582. utils.InfoLog("advice:%v", advice)
  1583. // 根据同步来的 advice_id 来查询库里是否已经存在
  1584. advice_info, _ := GetAdviceBySyncAdviceIdTwo(org_id, strconv.FormatInt(yz.AdviceId,10))
  1585. utils.InfoLog("advice_info:%v", advice_info)
  1586. if advice_info.ID > 0 {
  1587. key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
  1588. //清空key 值
  1589. redisClient.Set(key, "", time.Second)
  1590. keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
  1591. redisClient.Set(keyOne, "", time.Second)
  1592. continue
  1593. } else {
  1594. if len(yz.Advice) > 0 {
  1595. handleerr := CreateDoctorAdviceHandle(&advice)
  1596. if handleerr != nil {
  1597. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  1598. continue
  1599. }
  1600. } else {
  1601. continue
  1602. }
  1603. }
  1604. }
  1605. }
  1606. }
  1607. return
  1608. }
  1609. //
  1610. func saveLog(result string, request string, infno string, desc string, org_name string) {
  1611. //org_id, _ := beego.AppConfig.Int64("org_id")
  1612. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  1613. dir := org_name + "日志"
  1614. utils.Mkdir(dir)
  1615. month := time.Unix(1557042972, 0).Format("1")
  1616. year := time.Now().Format("2006")
  1617. month = time.Now().Format("01")
  1618. day := time.Now().Format("02")
  1619. hour := time.Now().Format("15")
  1620. min := time.Now().Format("04")
  1621. sec := time.Now().Format("05")
  1622. result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  1623. file := org_name + "_" + year + month + day + "_log"
  1624. file_name := file + ".txt"
  1625. file_path := org_name + "日志" + "/" + file_name
  1626. exist, _ := utils.PathExists(file_path)
  1627. if exist { //存在
  1628. fmt.Println("存在")
  1629. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  1630. if err != nil {
  1631. fmt.Println("read fail")
  1632. }
  1633. content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result
  1634. n, _ := f.Seek(0, 2)
  1635. _, err = f.WriteAt([]byte(content), n)
  1636. } else { //不存在
  1637. fmt.Println("文件不存在,创建文件")
  1638. f, err := os.Create(org_name + "日志" + "/" + file_name)
  1639. defer f.Close()
  1640. if err != nil {
  1641. } else {
  1642. _, err = f.Write([]byte("记录日志"))
  1643. }
  1644. }
  1645. }
  1646. func GetSzblItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1647. var inspection_reference models.MiddleInspectionReference
  1648. 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
  1649. if inspection_reference.ID > 0 {
  1650. return inspection_reference.ItemId, err
  1651. } else {
  1652. 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
  1653. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1654. if inspection_reference.ItemId > 0 {
  1655. return inspection_reference.ItemId + 1, err
  1656. } else {
  1657. return project_id*100 + 1, err
  1658. }
  1659. }
  1660. }