ystx_service.go 68KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 := "Abc123456@"
  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. SyncToGzczjhtx()
  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. // 根据姓名获取patient_id
  471. func GetPatientGzjhList(org_id int64, schedulesTime int64) (schedules []*models.DialysisSchedule, err error) {
  472. 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
  473. return
  474. }
  475. // 根据姓名获取admin_user_id
  476. func GetAdminUserId(org_id int64, doctor_name string) (doctor models.UserAdminRole, err error) {
  477. err = writeUserDb.Model(&models.UserAdminRole{}).Where("org_id = ? and status = 1 and user_name = ?", org_id, doctor_name).First(&doctor).Error
  478. return
  479. }
  480. func GetPatientGzjhByOrgId(org_id int64) (patientList []*models.Patients, err error) {
  481. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error
  482. return
  483. }
  484. type gzjhResult struct {
  485. ErrorCode int64 `json:"error_code"`
  486. Result []gzjhyz `json:"data"`
  487. }
  488. type gzjhyz struct {
  489. HisUserId string `json:"his_user_id"`
  490. AdviceId int64 `json:"advice_id"`
  491. UserName string `json:"user_name"`
  492. DoctorId string `json:"doctor_id"`
  493. AdviceType string `json:"advice_type"`
  494. StartTime string `json:"start_time"`
  495. AdviceName string `json:"advice_name"`
  496. AdviceDesc string `json:"advice_desc"`
  497. SingleDose string `json:"single_dose"`
  498. SingleDoseUnit string `json:"single_dose_unit"`
  499. PrescribingNumber string `json:"prescribing_number"`
  500. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  501. DeliveryWay string `json:"delivery_way"`
  502. ExecutionFrequency string `json:"execution_frequency"`
  503. State string `json:"state"`
  504. CTime string `json:"c_time"`
  505. UTime string `json:"u_time"`
  506. }
  507. func GetGzjhYz(sync_time int64, his_user_id string) (str gzjhResult, checkStr string) {
  508. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryyzxx"
  509. rep := httplib.Get(apiurl)
  510. rep.Param("sync_time", strconv.FormatInt(sync_time, 10))
  511. rep.Param("access_token", "0")
  512. rep.Param("his_user_id", his_user_id)
  513. utils.InfoLog(strconv.FormatInt(sync_time, 10))
  514. utils.InfoLog(his_user_id)
  515. // str, _ := rep.String()
  516. xmlKey := XmlKey{}
  517. rep.ToXML(&xmlKey)
  518. var checkJSON gzjhResult
  519. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  520. return checkJSON, xmlKey.InnerText
  521. }
  522. func CreateDoctorAdviceHandle(advice *models.XtDoctorAdvice) error {
  523. err := writeDb.Create(&advice).Error
  524. return err
  525. }
  526. func GetAdviceBySyncAdviceId(orgId int64, sync_advice_id int64) (advice models.XtDoctorAdvice, err error) {
  527. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_advice_id = ?", orgId, sync_advice_id).First(&advice).Error
  528. return
  529. }
  530. func GetAdviceBySyncAdviceIdTwo(orgId int64, sync_advice_id string) (advice models.XtDoctorAdvice, err error) {
  531. err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_id = ?", orgId, sync_advice_id).First(&advice).Error
  532. return
  533. }
  534. func HttpGetTfData(id_card_no string, advice_time string) (string, string) {
  535. inputData := make(map[string]interface{})
  536. inputData["病人来源"] = "1"
  537. inputData["身份证号"] = id_card_no
  538. inputData["开嘱时间"] = advice_time
  539. var inputLog string
  540. bytesData, err := json.Marshal(inputData)
  541. inputLog = string(bytesData)
  542. fmt.Println(string(bytesData))
  543. if err != nil {
  544. fmt.Println(err.Error())
  545. return err.Error(), ""
  546. }
  547. reader := bytes.NewReader(bytesData)
  548. var url string
  549. gdyb_url := "http://hip.zptfyy.com/esb/listener/getVaf"
  550. url = gdyb_url
  551. //url := "http://igb.hsa.gdgov.cn/ebus/gdyb_inf/poc/hsa/hgs/1101"
  552. request, err := http.NewRequest("POST", url, reader)
  553. if err != nil {
  554. fmt.Println(err.Error())
  555. return err.Error(), ""
  556. }
  557. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  558. request.Header.Set("code", "xt")
  559. client := http.Client{}
  560. resp, err := client.Do(request)
  561. if err != nil {
  562. fmt.Println(err.Error())
  563. return err.Error(), ""
  564. }
  565. respBytes, err := ioutil.ReadAll(resp.Body)
  566. if err != nil {
  567. fmt.Println(err.Error())
  568. return err.Error(), ""
  569. }
  570. fmt.Println(string(respBytes))
  571. str := string(respBytes)
  572. return str, inputLog
  573. }
  574. //func GettfYz(sync_time int64, id_card_no string, advice_time string) (str gzjhResult, checkStr string) {
  575. //
  576. // api := "http://10.129.128.51:8803/esb/listener/getVaf" + "?病人来源=" + "1" + "&身份证号=" + "350623196810011858" + "&开嘱时间=" + "2022-03-30 01:47:51"
  577. // resp, requestErr := http.Get(api)
  578. // fmt.Println(requestErr)
  579. // defer resp.Body.Close()
  580. // body, ioErr := ioutil.ReadAll(resp.Body)
  581. // fmt.Println(ioErr)
  582. // fmt.Println(body)
  583. //
  584. // //apiurl := "http://10.129.128.51:8803/esb/listener/getVaf"
  585. // //rep := httplib.Get(apiurl)
  586. // //rep.Param("病人来源", strconv.FormatInt(1, 10))
  587. // //rep.Param("身份证号", id_card_no)
  588. // //rep.Param("开嘱时间", advice_time)
  589. // //
  590. // //xmlKey := XmlKey{}
  591. // //rep.ToXML(&xmlKey)
  592. // //
  593. // //var checkJSON gzjhResult
  594. // //json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  595. // return nil, ""
  596. //}
  597. // 广州暨华透析中心医嘱同步
  598. func SyncGzjhyz() (err error) {
  599. // 第一步:获取上次同步时间
  600. org_id := int64(10015)
  601. redisClient := RedisClient()
  602. defer redisClient.Close()
  603. sync_time_tt, _ := redisClient.Get("gdjh_sync_time").Result()
  604. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  605. if sync_time == 0 {
  606. sync_time = time.Now().Unix()
  607. }
  608. new_sync_time := time.Now().Unix()
  609. utils.InfoLog("new_sync_time:%v", new_sync_time)
  610. // 切换为当前时间
  611. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  612. // 第二步: 获取当天排班患者的住院号
  613. schedulesDateStr := time.Now().Format("2006-01-02")
  614. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  615. schedulesTime := schedulesDate.Unix()
  616. var patient_zy_ids []string
  617. // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
  618. // if len(patient_zy_ids) == 0 {
  619. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  620. if len(schedules) > 0 {
  621. for _, item := range schedules {
  622. fmt.Println(item)
  623. // if len(item.Patients.AdmissionNumber) > 0 {
  624. if len(patient_zy_ids) == 0 {
  625. //patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  626. } else {
  627. //patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
  628. }
  629. // }
  630. }
  631. // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
  632. }
  633. // }
  634. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  635. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  636. idArray := strings.Split("", ",")
  637. for _, idStr := range idArray {
  638. temp_id := strings.Split(idStr, "-")
  639. id := temp_id[0]
  640. patient_id := temp_id[1]
  641. admission_number := temp_id[2]
  642. gzjhyz, yzstr := GetGzjhYz(sync_time, id)
  643. if len(gzjhyz.Result) == 0 && len(admission_number) > 0 {
  644. utils.InfoLog("admission_number:%v", admission_number)
  645. gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
  646. }
  647. utils.InfoLog("sync_time:%v", sync_time)
  648. utils.InfoLog("yzstr:%v", yzstr)
  649. if len(gzjhyz.Result) > 0 {
  650. timeLayout := "2006010215:04:05" //转化所需模板
  651. loc, _ := time.LoadLocation("Local") //重要:获取时区
  652. for _, yz := range gzjhyz.Result {
  653. // 根据姓名获取医生ID
  654. var doctor_id int64
  655. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + yz.DoctorId).Result()
  656. if len(temp_doctor_id) == 0 {
  657. doctor, _ := GetAdminUserId(org_id, yz.DoctorId)
  658. doctor_id = doctor.AdminUserId
  659. if doctor_id > 0 {
  660. redisClient.Set("gdjh_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
  661. } else {
  662. continue
  663. }
  664. } else {
  665. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  666. }
  667. theTime, _ := time.ParseInLocation(timeLayout, yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
  668. start_time := theTime.Unix()
  669. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  670. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  671. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  672. advice := models.XtDoctorAdvice{
  673. UserOrgId: org_id,
  674. PatientId: temp_patient_id,
  675. AdviceType: 2,
  676. AdviceDate: schedulesTime,
  677. StartTime: start_time,
  678. AdviceName: yz.AdviceName,
  679. AdviceDesc: yz.AdviceDesc,
  680. RecordDate: schedulesTime,
  681. SingleDose: SingleDose,
  682. SingleDoseUnit: yz.SingleDoseUnit,
  683. PrescribingNumber: PrescribingNumber,
  684. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  685. DeliveryWay: yz.DeliveryWay,
  686. ExecutionFrequency: yz.ExecutionFrequency,
  687. AdviceDoctor: doctor_id,
  688. CreatedTime: time.Now().Unix(),
  689. UpdatedTime: time.Now().Unix(),
  690. SyncAdviceId: yz.AdviceId,
  691. ExecutionState: 2,
  692. Status: 1,
  693. IsSync: 1,
  694. }
  695. utils.InfoLog("advice:%v", advice)
  696. // 根据同步来的 advice_id 来查询库里是否已经存在
  697. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  698. utils.InfoLog("advice_info:%v", advice_info)
  699. if advice_info.ID > 0 {
  700. key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
  701. //清空key 值
  702. redisClient.Set(key, "", time.Second)
  703. keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
  704. redisClient.Set(keyOne, "", time.Second)
  705. continue
  706. } else {
  707. if len(yz.AdviceName) > 0 {
  708. handleerr := CreateDoctorAdviceHandle(&advice)
  709. if handleerr != nil {
  710. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  711. continue
  712. }
  713. } else {
  714. continue
  715. }
  716. }
  717. }
  718. }
  719. }
  720. return
  721. }
  722. type gzjhLisResult struct {
  723. ErrorCode int64 `json:"error_code"`
  724. Result []gzjhlis `json:"data"`
  725. }
  726. type gzjhlis struct {
  727. HisUserId string `json:"his_user_id"`
  728. PatientName string `json:"patient_name"`
  729. ProjectId string `json:"project_id"`
  730. ProjectName string `json:"project_name"`
  731. InspectDate string `json:"inspect_date"`
  732. ItemId string `json:"item_id"`
  733. ItemName string `json:"item_name"`
  734. ItemType string `json:"item_type"`
  735. ItemValue string `json:"item_value"`
  736. RangeMin string `json:"range_min"`
  737. RangeMax string `json:"range_max"`
  738. RangeValue string `json:"range_value"`
  739. RangeOptions string `json:"range_options"`
  740. Unit string `json:"unit"`
  741. State string `json:"state"`
  742. CTime string `json:"c_time"`
  743. UTime string `json:"u_time"`
  744. }
  745. func GetGzjhLis(sync_time string, his_user_id string) (str gzjhLisResult, checkStr string) {
  746. apiurl := "http://219.135.166.106:8067/WebService.asmx/queryjyjcxx"
  747. rep := httplib.Post(apiurl)
  748. rep.Param("sync_time", sync_time)
  749. rep.Param("access_token", "")
  750. rep.Param("his_user_id", his_user_id)
  751. // str, _ := rep.String()
  752. xmlKey := XmlKey{}
  753. rep.ToXML(&xmlKey)
  754. var checkJSON gzjhLisResult
  755. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  756. return checkJSON, xmlKey.InnerText
  757. }
  758. // 广州暨华透析中心Lis同步
  759. func SyncGzjhLis() (err error) {
  760. org_id := int64(10016)
  761. // 第一步:获取上一次同步的时间点
  762. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  763. var sync_time int64
  764. if syncLastInfo.ID > 0 {
  765. sync_time = syncLastInfo.SyncTime + 86400
  766. } else {
  767. sync_time = 1577808000
  768. }
  769. // 第二步:获取所有患者的病历号
  770. patientList, _ := GetPatientGzjhByOrgId(org_id)
  771. if len(patientList) > 0 {
  772. for _, patient := range patientList {
  773. if len(patient.AdmissionNumber) > 0 {
  774. // 上次同步时间转换为接口需要的格式
  775. patient_id := patient.ID
  776. sync_time_temp := time.Unix(sync_time, 0)
  777. syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  778. gzjhLis, lisStr := GetGzjhLis(syncTimeStr, patient.IdCardNo)
  779. utils.InfoLog("yzstr:%v", lisStr)
  780. if len(gzjhLis.Result) > 0 {
  781. // timeLayout := "2006010215:04:05" // 转化所需模板
  782. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  783. for _, info := range gzjhLis.Result {
  784. // 根据project_id和item_id 判断该项是否存在
  785. project_id := int64(0)
  786. if len(info.ProjectId) > 0 {
  787. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  788. } else {
  789. continue
  790. }
  791. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  792. tx := writeMiddleDb.Begin()
  793. var inspection models.MiddleInspection
  794. var inspection_reference models.MiddleInspectionReference
  795. recordDateStr := info.InspectDate
  796. if len(info.InspectDate) == 0 {
  797. recordDateStr = time.Now().Format("2006-01-02 15:04")
  798. }
  799. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  800. record_date := record_date_str.Unix()
  801. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  802. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  803. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  804. var total int
  805. var RangeOptions string
  806. var RangeMin string
  807. var RangeMax string
  808. // // 判断检查类型
  809. ItemType, _ := strconv.Atoi(info.ItemType)
  810. Range := strings.Split(info.RangeValue, "~")
  811. if len(Range) > 1 {
  812. RangeMin = Range[0]
  813. RangeMax = Range[1]
  814. ItemType = 1
  815. } else {
  816. ItemType = 2
  817. RangeOptions = info.RangeOptions
  818. }
  819. 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
  820. if inspection_reference.ID > 0 {
  821. ItemType = inspection_reference.RangeType
  822. }
  823. if total <= 0 {
  824. inspection_reference.OrgId = org_id
  825. inspection_reference.ProjectName = info.ProjectName
  826. inspection_reference.Project = info.ProjectName
  827. inspection_reference.ProjectId = project_id
  828. inspection_reference.ItemName = info.ItemName
  829. inspection_reference.ItemNameAddition = info.ItemId
  830. inspection_reference.ItemId = item_id
  831. inspection_reference.RangeType = ItemType
  832. inspection_reference.RangeMin = RangeMin
  833. inspection_reference.RangeMax = RangeMax
  834. // inspection_reference.RangeValue = RangeValue
  835. inspection_reference.RangeOptions = RangeOptions
  836. inspection_reference.Unit = info.Unit
  837. inspection_reference.Status = 1
  838. inspection_reference.CreatedTime = time.Now().Unix()
  839. inspection_reference.UpdatedTime = time.Now().Unix()
  840. inspection_reference.InspectDate = inspect_date
  841. inspection_reference.UTime = inspect_date
  842. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  843. if err != nil {
  844. tx.Rollback()
  845. }
  846. }
  847. var itotal int
  848. 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
  849. if itotal <= 0 {
  850. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  851. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  852. inspection.PatientId = patient_id
  853. inspection.OrgId = org_id
  854. inspection.ProjectId = project_id
  855. inspection.ItemName = inspection_reference.ItemName
  856. inspection.ProjectName = inspection_reference.ProjectName
  857. inspection.InspectType = ItemType
  858. inspection.ItemId = item_id
  859. inspection.InspectValue = info.ItemValue
  860. inspection.InspectDate = inspect_date
  861. inspection.RecordDate = record_date
  862. // inspection.InspectTips = report.Resultstate
  863. inspection.Status = 1
  864. inspection.CreatedTime = time.Now().Unix()
  865. inspection.UpdatedTime = time.Now().Unix()
  866. inspection.UTime = inspect_date
  867. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  868. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  869. if err != nil {
  870. tx.Rollback()
  871. }
  872. }
  873. tx.Commit()
  874. }
  875. } else {
  876. continue
  877. }
  878. } else {
  879. continue
  880. }
  881. }
  882. }
  883. var syncInfo models.MiddleSyncInfo
  884. syncInfo.OrgId = org_id
  885. syncInfo.SyncTime = time.Now().Unix()
  886. syncInfo.SyncResultType = 1
  887. syncInfo.SyncRsultRemark = "同步成功"
  888. syncInfo.SyncTotalNum = 0
  889. syncInfo.SyncSuccessNum = 0
  890. syncInfo.SyncInfo = ""
  891. syncInfo.CreateTime = time.Now().Unix()
  892. syncInfo.UpdateTime = time.Now().Unix()
  893. cwderr := CreateSyncInfo(&syncInfo)
  894. if cwderr != nil {
  895. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  896. return
  897. }
  898. SyncToGzjhtx()
  899. return
  900. }
  901. func SyncToGzjhtx() {
  902. utils.TraceLog("检验检查同步任务开始执行")
  903. org_id := int64(10016)
  904. // 第一步:跟进org_id 去中间库查出需要同步的数据
  905. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  906. inspections, _ := GetSyncInspectionByOrgId(org_id)
  907. // 第二步:将数据同步到业务库
  908. if len(inspection_references) > 0 {
  909. for _, inspection_reference := range inspection_references {
  910. SyncInspectionReference(&inspection_reference)
  911. }
  912. }
  913. if len(inspections) > 0 {
  914. for _, inspection := range inspections {
  915. SyncInspection(&inspection)
  916. }
  917. }
  918. utils.SuccessLog("检验检查同步任务完成")
  919. }
  920. type hbdyyz struct {
  921. HisUserId string `json:"his_user_id"`
  922. UserName string `json:"user_name"`
  923. DoctorId int64 `json:"doctor_id"`
  924. DoctorName string `json:"Doctor_name"`
  925. AdviceId int64 `json:"advice_id"`
  926. MedListCodg string `json:"med_list_codg"`
  927. StartTime int64 `json:"start_time"`
  928. AdviceName string `json:"advice_name"`
  929. AdviceDesc string `json:"advice_desc"`
  930. SingleDose string `json:"single_dose"`
  931. SingleDoseUnit string `json:"single_dose_unit"`
  932. PrescribingNumber string `json:"prescribing_number"`
  933. PrescribingNumberUnit string `json:"prescribing_number_unit"`
  934. DeliveryWay string `json:"delivery_way"`
  935. ExecutionFrequency string `json:"execution_frequency"`
  936. State int64 `json:"state"`
  937. CTime int64 `json:"create_time"`
  938. UTime int64 `json:"update_time"`
  939. }
  940. type hbdyHisResult struct {
  941. ErrorCode int64 `json:"error_code"`
  942. Result []hbdyyz `json:"data"`
  943. }
  944. func GetHbdyYz(sync_time string, his_user_id string) (str hbdyHisResult, checkStr string) {
  945. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_ADVICE"
  946. rep := httplib.Post(apiurl)
  947. rep.Param("sync_time", sync_time)
  948. rep.Param("his_user_id", his_user_id)
  949. strs, err := rep.String()
  950. if err != nil {
  951. utils.InfoLog("err:%v", err)
  952. }
  953. utils.InfoLog("str:%v", strs)
  954. var checkJSON hbdyHisResult
  955. json.Unmarshal([]byte(strs), &checkJSON)
  956. utils.InfoLog("checkJSON:%v", checkJSON)
  957. return checkJSON, strs
  958. }
  959. // 湖北监利大垸透析中心医嘱同步
  960. func SyncHbJldyyz() (err error) {
  961. // 第一步:获取上次同步时间
  962. org_id := int64(10101)
  963. redisClient := RedisClient()
  964. defer redisClient.Close()
  965. sync_time, _ := redisClient.Get("gdjh_sync_time1").Result()
  966. if len(sync_time) == 0 {
  967. sync_time = strconv.FormatInt(time.Now().Unix(), 10)
  968. utils.InfoLog("sync_time:%v", sync_time)
  969. // formatTimeStr := "2021-08-31 00:00"
  970. // checkDateUnix, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", sync_time)
  971. // sync_time = strconv.FormatInt(checkDateUnix.Unix(),10)
  972. }
  973. new_sync_time := time.Now().Unix()
  974. utils.InfoLog("new_sync_time:%v", new_sync_time)
  975. // 切换为当前时间
  976. redisClient.Set("gdjh_sync_time", new_sync_time, time.Minute*60*24)
  977. // 第二步: 获取当天排班患者的住院号
  978. schedulesDateStr := time.Now().Format("2006-01-02")
  979. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  980. schedulesTime := schedulesDate.Unix()
  981. patient_zy_ids, _ := redisClient.Get("gdjh_patient_zy_ids").Result()
  982. if len(patient_zy_ids) == 0 {
  983. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  984. if len(schedules) > 0 {
  985. for _, item := range schedules {
  986. if len(patient_zy_ids) == 0 {
  987. patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  988. } else {
  989. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
  990. }
  991. }
  992. redisClient.Set("gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
  993. }
  994. }
  995. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  996. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  997. idArray := strings.Split(patient_zy_ids, ",")
  998. for _, idStr := range idArray {
  999. temp_id := strings.Split(idStr, "-")
  1000. id := temp_id[0]
  1001. patient_id := temp_id[1]
  1002. hbdyyz, _ := GetHbdyYz(sync_time, id)
  1003. if len(hbdyyz.Result) > 0 {
  1004. for _, yz := range hbdyyz.Result {
  1005. utils.InfoLog("yz:%v", yz)
  1006. // 根据姓名获取医生ID
  1007. var doctor_id int64
  1008. temp_doctor_id, _ := redisClient.Get("gdjh_doctor_id_" + strconv.FormatInt(yz.DoctorId, 10)).Result()
  1009. if len(temp_doctor_id) == 0 {
  1010. doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
  1011. doctor_id = doctor.AdminUserId
  1012. if doctor_id > 0 {
  1013. redisClient.Set("gdjh_doctor_id_"+strconv.FormatInt(yz.DoctorId, 10), doctor_id, time.Minute*60*2)
  1014. } else {
  1015. continue
  1016. }
  1017. } else {
  1018. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  1019. }
  1020. temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
  1021. SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
  1022. PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
  1023. utils.InfoLog("PrescribingNumber:%v", PrescribingNumber)
  1024. advice := models.XtDoctorAdvice{
  1025. UserOrgId: org_id,
  1026. PatientId: temp_patient_id,
  1027. AdviceType: 2,
  1028. AdviceDate: schedulesTime,
  1029. StartTime: yz.StartTime,
  1030. AdviceName: yz.AdviceName,
  1031. AdviceDesc: yz.AdviceDesc,
  1032. RecordDate: schedulesTime,
  1033. SingleDose: SingleDose,
  1034. SingleDoseUnit: yz.SingleDoseUnit,
  1035. PrescribingNumber: PrescribingNumber,
  1036. PrescribingNumberUnit: yz.PrescribingNumberUnit,
  1037. DeliveryWay: yz.DeliveryWay,
  1038. ExecutionFrequency: yz.ExecutionFrequency,
  1039. AdviceDoctor: doctor_id,
  1040. CreatedTime: time.Now().Unix(),
  1041. UpdatedTime: time.Now().Unix(),
  1042. SyncAdviceId: yz.AdviceId,
  1043. ExecutionState: 2,
  1044. Status: 1,
  1045. IsSync: 1,
  1046. }
  1047. utils.InfoLog("advice:%v", advice)
  1048. // 根据同步来的 advice_id 来查询库里是否已经存在
  1049. advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
  1050. utils.InfoLog("advice_info:%v", advice_info)
  1051. if advice_info.ID > 0 {
  1052. continue
  1053. } else {
  1054. if yz.State == 1 {
  1055. handleerr := CreateDoctorAdviceHandle(&advice)
  1056. if handleerr != nil {
  1057. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  1058. continue
  1059. }
  1060. } else {
  1061. continue
  1062. }
  1063. }
  1064. }
  1065. }
  1066. }
  1067. return
  1068. }
  1069. type hbdylis struct {
  1070. HisUserId string `json:"his_user_id"`
  1071. PatientName string `json:"patient_name"`
  1072. ProjectId int64 `json:"project_id"`
  1073. ProjectName string `json:"project_name"`
  1074. InspectDate int64 `json:"inspect_date"`
  1075. ItemId int64 `json:"item_id"`
  1076. ItemName string `json:"item_name"`
  1077. ItemType int64 `json:"item_type"`
  1078. ItemValue string `json:"item_value"`
  1079. RangeMin string `json:"range_min"`
  1080. RangeMax string `json:"range_max"`
  1081. RangeValue string `json:"range_value"`
  1082. RangeOptions string `json:"range_options"`
  1083. Unit string `json:"unit"`
  1084. State int64 `json:"state"`
  1085. CTime int64 `json:"c_time"`
  1086. UTime int64 `json:"u_time"`
  1087. }
  1088. type hbdyLisResult struct {
  1089. ErrorCode int64 `json:"error_code"`
  1090. Result []hbdylis `json:"data"`
  1091. }
  1092. func GethbdyLis(sync_time string, his_user_id string) (str hbdyLisResult, checkStr string) {
  1093. apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_INSPECTION"
  1094. rep := httplib.Post(apiurl)
  1095. rep.Param("sync_time", sync_time)
  1096. rep.Param("his_user_id", his_user_id)
  1097. strs, err := rep.String()
  1098. if err != nil {
  1099. utils.InfoLog("err:%v", err)
  1100. }
  1101. utils.InfoLog("str:%v", strs)
  1102. var checkJSON hbdyLisResult
  1103. json.Unmarshal([]byte(strs), &checkJSON)
  1104. // utils.InfoLog("checkJSON:%v", checkJSON)
  1105. return checkJSON, strs
  1106. }
  1107. // 湖北监利大院透析中心Lis同步
  1108. func SyncHbdyLis() (err error) {
  1109. org_id := int64(10101)
  1110. // 第一步:获取上一次同步的时间点
  1111. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  1112. var sync_time int64
  1113. if syncLastInfo.ID > 0 {
  1114. sync_time = syncLastInfo.SyncTime
  1115. } else {
  1116. sync_time = 1627747200
  1117. }
  1118. // 第二步:获取所有患者的病历号
  1119. patientList, _ := GetPatientGzjhByOrgId(org_id)
  1120. if len(patientList) > 0 {
  1121. for _, patient := range patientList {
  1122. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  1123. // 上次同步时间转换为接口需要的格式
  1124. patient_id := patient.ID
  1125. // sync_time_temp := time.Unix(sync_time,0)
  1126. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  1127. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  1128. // 获取会员号
  1129. // utils.InfoLog("VipStr:%v", VipStr)
  1130. gzjhLis, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  1131. // if len(gzjhLis.Result) == 0 {
  1132. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  1133. // }
  1134. // utils.InfoLog("yzstr:%v", gzjhLis)
  1135. if len(gzjhLis.Result) > 0 {
  1136. // timeLayout := "2006010215:04:05" // 转化所需模板
  1137. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1138. for _, info := range gzjhLis.Result {
  1139. utils.InfoLog("Result:%v", info)
  1140. // 根据project_id和item_id 判断该项是否存在
  1141. project_id := int64(0)
  1142. if info.ProjectId > 0 {
  1143. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1144. } else {
  1145. continue
  1146. }
  1147. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1148. tx := writeMiddleDb.Begin()
  1149. var inspection models.MiddleInspection
  1150. var inspection_reference models.MiddleInspectionReference
  1151. record_date := info.InspectDate
  1152. if info.InspectDate == 0 {
  1153. record_date = time.Now().Unix()
  1154. }
  1155. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1156. // record_date := record_date_str.Unix()
  1157. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1158. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1159. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1160. var total int
  1161. var RangeOptions string
  1162. var RangeMin string
  1163. var RangeMax string
  1164. // // 判断检查类型
  1165. ItemType := info.ItemType
  1166. Range := strings.Split(info.RangeValue, "-")
  1167. if len(Range) > 1 {
  1168. RangeMin = Range[0]
  1169. RangeMax = Range[1]
  1170. ItemType = 1
  1171. } else {
  1172. ItemType = 2
  1173. RangeOptions = info.RangeOptions
  1174. }
  1175. 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
  1176. // if inspection_reference.ID > 0 {
  1177. // ItemType = int64(inspection_reference.RangeType)
  1178. // }
  1179. if total <= 0 {
  1180. inspection_reference.OrgId = org_id
  1181. inspection_reference.ProjectName = info.ProjectName
  1182. inspection_reference.Project = info.ProjectName
  1183. inspection_reference.ProjectId = project_id
  1184. inspection_reference.ItemName = info.ItemName
  1185. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1186. inspection_reference.ItemId = item_id
  1187. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1188. inspection_reference.RangeMin = RangeMin
  1189. inspection_reference.RangeMax = RangeMax
  1190. // inspection_reference.RangeValue = RangeValue
  1191. inspection_reference.RangeOptions = RangeOptions
  1192. inspection_reference.Unit = info.Unit
  1193. inspection_reference.Status = 1
  1194. inspection_reference.CreatedTime = time.Now().Unix()
  1195. inspection_reference.UpdatedTime = time.Now().Unix()
  1196. inspection_reference.InspectDate = inspect_date
  1197. inspection_reference.UTime = inspect_date
  1198. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1199. if err != nil {
  1200. tx.Rollback()
  1201. }
  1202. }
  1203. var itotal int
  1204. 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
  1205. if itotal <= 0 {
  1206. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1207. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1208. inspection.PatientId = patient_id
  1209. inspection.OrgId = org_id
  1210. inspection.ProjectId = project_id
  1211. inspection.ItemName = inspection_reference.ItemName
  1212. inspection.ProjectName = inspection_reference.ProjectName
  1213. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1214. inspection.ItemId = item_id
  1215. inspection.InspectValue = info.ItemValue
  1216. inspection.InspectDate = inspect_date
  1217. inspection.RecordDate = record_date
  1218. // inspection.InspectTips = report.Resultstate
  1219. inspection.Status = 1
  1220. inspection.CreatedTime = time.Now().Unix()
  1221. inspection.UpdatedTime = time.Now().Unix()
  1222. inspection.UTime = inspect_date
  1223. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1224. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1225. if err != nil {
  1226. tx.Rollback()
  1227. }
  1228. }
  1229. tx.Commit()
  1230. }
  1231. }
  1232. VipStr := ""
  1233. VipArr := strings.Split(patient.DialysisNo, "(")
  1234. if len(VipArr) > 1 {
  1235. VipArrTwo := strings.Split(VipArr[1], ")")
  1236. VipStr = VipArrTwo[0]
  1237. gzjhLisbyVip, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), VipStr)
  1238. if len(gzjhLisbyVip.Result) > 0 {
  1239. // timeLayout := "2006010215:04:05" // 转化所需模板
  1240. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  1241. for _, info := range gzjhLisbyVip.Result {
  1242. // utils.InfoLog("Result:%v", info)
  1243. // 根据project_id和item_id 判断该项是否存在
  1244. project_id := int64(0)
  1245. if info.ProjectId > 0 {
  1246. project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
  1247. } else {
  1248. continue
  1249. }
  1250. item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
  1251. tx := writeMiddleDb.Begin()
  1252. var inspection models.MiddleInspection
  1253. var inspection_reference models.MiddleInspectionReference
  1254. record_date := info.InspectDate
  1255. if info.InspectDate == 0 {
  1256. record_date = time.Now().Unix()
  1257. }
  1258. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  1259. // record_date := record_date_str.Unix()
  1260. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  1261. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  1262. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  1263. var total int
  1264. var RangeOptions string
  1265. var RangeMin string
  1266. var RangeMax string
  1267. // // 判断检查类型
  1268. ItemType := info.ItemType
  1269. Range := strings.Split(info.RangeValue, "-")
  1270. if len(Range) > 1 {
  1271. RangeMin = Range[0]
  1272. RangeMax = Range[1]
  1273. ItemType = 1
  1274. } else {
  1275. ItemType = 2
  1276. RangeOptions = info.RangeOptions
  1277. }
  1278. 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
  1279. if inspection_reference.ID > 0 {
  1280. ItemType = int64(inspection_reference.RangeType)
  1281. }
  1282. if total <= 0 {
  1283. inspection_reference.OrgId = org_id
  1284. inspection_reference.ProjectName = info.ProjectName
  1285. inspection_reference.Project = info.ProjectName
  1286. inspection_reference.ProjectId = project_id
  1287. inspection_reference.ItemName = info.ItemName
  1288. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  1289. inspection_reference.ItemId = item_id
  1290. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1291. inspection_reference.RangeMin = RangeMin
  1292. inspection_reference.RangeMax = RangeMax
  1293. // inspection_reference.RangeValue = RangeValue
  1294. inspection_reference.RangeOptions = RangeOptions
  1295. inspection_reference.Unit = info.Unit
  1296. inspection_reference.Status = 1
  1297. inspection_reference.CreatedTime = time.Now().Unix()
  1298. inspection_reference.UpdatedTime = time.Now().Unix()
  1299. inspection_reference.InspectDate = inspect_date
  1300. inspection_reference.UTime = inspect_date
  1301. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  1302. if err != nil {
  1303. tx.Rollback()
  1304. }
  1305. }
  1306. var itotal int
  1307. 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
  1308. if itotal <= 0 {
  1309. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  1310. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  1311. inspection.PatientId = patient_id
  1312. inspection.OrgId = org_id
  1313. inspection.ProjectId = project_id
  1314. inspection.ItemName = inspection_reference.ItemName
  1315. inspection.ProjectName = inspection_reference.ProjectName
  1316. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  1317. inspection.ItemId = item_id
  1318. inspection.InspectValue = info.ItemValue
  1319. inspection.InspectDate = inspect_date
  1320. inspection.RecordDate = record_date
  1321. // inspection.InspectTips = report.Resultstate
  1322. inspection.Status = 1
  1323. inspection.CreatedTime = time.Now().Unix()
  1324. inspection.UpdatedTime = time.Now().Unix()
  1325. inspection.UTime = inspect_date
  1326. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  1327. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  1328. if err != nil {
  1329. tx.Rollback()
  1330. }
  1331. }
  1332. tx.Commit()
  1333. }
  1334. }
  1335. }
  1336. } else {
  1337. continue
  1338. }
  1339. }
  1340. }
  1341. var syncInfo models.MiddleSyncInfo
  1342. syncInfo.OrgId = org_id
  1343. syncInfo.SyncTime = time.Now().Unix()
  1344. syncInfo.SyncResultType = 1
  1345. syncInfo.SyncRsultRemark = "同步成功"
  1346. syncInfo.SyncTotalNum = 0
  1347. syncInfo.SyncSuccessNum = 0
  1348. syncInfo.SyncInfo = ""
  1349. syncInfo.CreateTime = time.Now().Unix()
  1350. syncInfo.UpdateTime = time.Now().Unix()
  1351. cwderr := CreateSyncInfo(&syncInfo)
  1352. if cwderr != nil {
  1353. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  1354. return
  1355. }
  1356. SyncToHbdytx()
  1357. return
  1358. }
  1359. func SyncToHbdytx() {
  1360. utils.TraceLog("检验检查同步任务开始执行")
  1361. org_id := int64(10101)
  1362. // 第一步:跟进org_id 去中间库查出需要同步的数据
  1363. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  1364. inspections, _ := GetSyncInspectionByOrgId(org_id)
  1365. // 第二步:将数据同步到业务库
  1366. if len(inspection_references) > 0 {
  1367. for _, inspection_reference := range inspection_references {
  1368. SyncInspectionReference(&inspection_reference)
  1369. }
  1370. }
  1371. if len(inspections) > 0 {
  1372. for _, inspection := range inspections {
  1373. SyncInspection(&inspection)
  1374. }
  1375. }
  1376. utils.SuccessLog("检验检查同步任务完成")
  1377. }
  1378. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1379. func GetBljhProjectID(org_id int64, project_name string) (project_id int64, sys_project_id int64, err error) {
  1380. var inspection_reference models.MiddleInspectionReference
  1381. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1382. if inspection_reference.ID > 0 {
  1383. return inspection_reference.ProjectId, inspection_reference.XtProjectId, err
  1384. } else {
  1385. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1386. if inspection_reference.ProjectId > 0 {
  1387. return inspection_reference.ProjectId + 1, 0, err
  1388. } else {
  1389. return 5210138, 0, err
  1390. }
  1391. }
  1392. }
  1393. //// 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1394. //func GetBlItemIDTwo(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, sys_project_id int64, err error) {
  1395. // var inspection_reference models.MiddleInspectionReference
  1396. // 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
  1397. // if inspection_reference.ID > 0 {
  1398. // return inspection_reference.ItemId, err
  1399. // } else {
  1400. // 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
  1401. // utils.InfoLog("inspection_reference: %v", inspection_reference)
  1402. // if inspection_reference.ItemId > 0 {
  1403. // return inspection_reference.ItemId + 1, err
  1404. // } else {
  1405. // return project_id*10 + 1, err
  1406. // }
  1407. // }
  1408. //}
  1409. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1410. func GetBlItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, sys_item_id int64, err error) {
  1411. var inspection_reference models.MiddleInspectionReference
  1412. 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
  1413. if inspection_reference.ID > 0 {
  1414. return inspection_reference.ItemId, inspection_reference.XtItemId, err
  1415. } else {
  1416. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and xt_project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
  1417. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1418. if inspection_reference.ItemId > 0 {
  1419. return inspection_reference.ItemId + 1, 0, err
  1420. } else {
  1421. return project_id*10 + 1, 0, err
  1422. }
  1423. }
  1424. }
  1425. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1426. func GetShjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1427. var inspection_reference models.MiddleInspectionReference
  1428. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1429. if inspection_reference.ID > 0 {
  1430. return inspection_reference.ProjectId, err
  1431. } else {
  1432. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1433. if inspection_reference.ProjectId > 0 {
  1434. return inspection_reference.ProjectId + 1, err
  1435. } else {
  1436. return 5210215, err
  1437. }
  1438. }
  1439. }
  1440. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  1441. func GetSzbljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
  1442. var inspection_reference models.MiddleInspectionReference
  1443. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  1444. if inspection_reference.ID > 0 {
  1445. return inspection_reference.ProjectId, err
  1446. } else {
  1447. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  1448. if inspection_reference.ProjectId > 0 {
  1449. return inspection_reference.ProjectId + 1, err
  1450. } else {
  1451. return 1013800, err
  1452. }
  1453. }
  1454. }
  1455. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  1456. func GetShItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1457. var inspection_reference models.MiddleInspectionReference
  1458. 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
  1459. if inspection_reference.ID > 0 {
  1460. return inspection_reference.ItemId, err
  1461. } else {
  1462. 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
  1463. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1464. if inspection_reference.ItemId > 0 {
  1465. return inspection_reference.ItemId + 1, err
  1466. } else {
  1467. return project_id*10 + 1, err
  1468. }
  1469. }
  1470. }
  1471. func Synctfyz() (err error) {
  1472. // 第一步:获取上次同步时间
  1473. org_id := int64(10330)
  1474. redisClient := RedisClient()
  1475. defer redisClient.Close()
  1476. sync_time_tt, _ := redisClient.Get("tf_sync_time").Result()
  1477. sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
  1478. if sync_time == 0 {
  1479. sync_time = time.Now().Unix()
  1480. }
  1481. new_sync_time := time.Now().Unix()
  1482. utils.InfoLog("new_sync_time:%v", new_sync_time)
  1483. // 切换为当前时间
  1484. redisClient.Set("tf_sync_time", new_sync_time, time.Minute*60*24)
  1485. // 第二步: 获取当天排班患者的住院号
  1486. schedulesDateStr := time.Now().Format("2006-01-02")
  1487. schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
  1488. schedulesTime := schedulesDate.Unix()
  1489. var patient_zy_ids string
  1490. // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
  1491. // if len(patient_zy_ids) == 0 {
  1492. schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
  1493. if len(schedules) > 0 {
  1494. for _, item := range schedules {
  1495. // if len(item.Patients.AdmissionNumber) > 0 {
  1496. if len(patient_zy_ids) == 0 {
  1497. patient_zy_ids = item.Patients.IdCardNo
  1498. } else {
  1499. patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo
  1500. }
  1501. // }
  1502. }
  1503. // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
  1504. }
  1505. // }
  1506. utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
  1507. // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
  1508. idArray := strings.Split(patient_zy_ids, ",")
  1509. for _, idStr := range idArray {
  1510. //temp_id := strings.Split(idStr, "-")
  1511. id := idStr
  1512. printInfo, _ := GetPatientsInfoByIDCardNo(id, org_id)
  1513. timeNow := time.Unix(sync_time, 0) //2017-08-30 16:19:19 +0800 CST
  1514. timeString := timeNow.Format("2006-01-02 15:04:05") //2015-06-15 08:52:32
  1515. result, requestLog := HttpGetTfData(id, timeString)
  1516. saveLog(result, requestLog, "tf", "tf", "tf")
  1517. //fmt.Println(result)
  1518. //fmt.Println(requestLog)
  1519. var res RequestResult
  1520. if err := json.Unmarshal([]byte(result), &res); err != nil {
  1521. utils.ErrorLog("解析失败:%v", err)
  1522. }
  1523. fmt.Println(res)
  1524. //saveLog(result, requestLog, "tf", "tf", "tf")
  1525. //
  1526. //if len(gzjhyz.Result) == 0 && len(admission_number) > 0 {
  1527. // utils.InfoLog("admission_number:%v", admission_number)
  1528. // gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
  1529. //}
  1530. //utils.InfoLog("sync_time:%v", sync_time)
  1531. //utils.InfoLog("yzstr:%v", yzstr)
  1532. if len(res.Result) > 0 {
  1533. //timeLayout := "2006010215:04:05" //转化所需模板
  1534. //loc, _ := time.LoadLocation("Local") //重要:获取时区
  1535. for _, yz := range res.Result {
  1536. // 根据姓名获取医生ID
  1537. var doctor_id int64
  1538. //doctor_id, _ = strconv.ParseInt(yz.AdviceDoctorId, 10, 64)
  1539. temp_doctor_id, _ := redisClient.Get("tf_doctor_id_" + yz.Doctor).Result()
  1540. if len(temp_doctor_id) == 0 {
  1541. doctor, _ := GetAdminUserId(org_id, yz.Doctor)
  1542. doctor_id = doctor.AdminUserId
  1543. if doctor_id > 0 {
  1544. redisClient.Set("tf_doctor_id_"+yz.Doctor, doctor_id, time.Minute*60*2)
  1545. } else {
  1546. continue
  1547. }
  1548. } else {
  1549. doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
  1550. }
  1551. //theTime, _ := time.ParseInLocation(timeLayout, , loc) //使用模板在对应时区转化为time.time类型
  1552. start_time := yz.AdviceTime / 1000
  1553. fmt.Println(start_time)
  1554. temp_patient_id := printInfo.ID
  1555. SingleDose := yz.DanLiang
  1556. PrescribingNumber := yz.Shuliang
  1557. advice := models.XtDoctorAdvice{
  1558. UserOrgId: org_id,
  1559. PatientId: temp_patient_id,
  1560. AdviceType: 2,
  1561. AdviceDate: schedulesTime,
  1562. StartTime: start_time,
  1563. AdviceName: yz.Advice,
  1564. //AdviceDesc: yz.AdviceDesc,
  1565. RecordDate: schedulesTime,
  1566. SingleDose: SingleDose,
  1567. PrescribingNumber: PrescribingNumber,
  1568. DeliveryWay: yz.GeiYaoTuJing,
  1569. ExecutionFrequency: yz.ZhiXingPinCi,
  1570. AdviceDoctor: doctor_id,
  1571. CreatedTime: time.Now().Unix(),
  1572. UpdatedTime: time.Now().Unix(),
  1573. SyncAdviceId: 0,
  1574. ExecutionState: 2,
  1575. Status: 1,
  1576. IsSync: 1,
  1577. SyncId: strconv.FormatInt(yz.AdviceId, 10),
  1578. }
  1579. utils.InfoLog("advice:%v", advice)
  1580. // 根据同步来的 advice_id 来查询库里是否已经存在
  1581. advice_info, _ := GetAdviceBySyncAdviceIdTwo(org_id, strconv.FormatInt(yz.AdviceId, 10))
  1582. utils.InfoLog("advice_info:%v", advice_info)
  1583. if advice_info.ID > 0 {
  1584. key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
  1585. //清空key 值
  1586. redisClient.Set(key, "", time.Second)
  1587. keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
  1588. redisClient.Set(keyOne, "", time.Second)
  1589. continue
  1590. } else {
  1591. if len(yz.Advice) > 0 {
  1592. handleerr := CreateDoctorAdviceHandle(&advice)
  1593. if handleerr != nil {
  1594. utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
  1595. continue
  1596. }
  1597. } else {
  1598. continue
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. return
  1605. }
  1606. //
  1607. func saveLog(result string, request string, infno string, desc string, org_name string) {
  1608. //org_id, _ := beego.AppConfig.Int64("org_id")
  1609. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  1610. dir := org_name + "日志"
  1611. utils.Mkdir(dir)
  1612. month := time.Unix(1557042972, 0).Format("1")
  1613. year := time.Now().Format("2006")
  1614. month = time.Now().Format("01")
  1615. day := time.Now().Format("02")
  1616. hour := time.Now().Format("15")
  1617. min := time.Now().Format("04")
  1618. sec := time.Now().Format("05")
  1619. result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  1620. file := org_name + "_" + year + month + day + "_log"
  1621. file_name := file + ".txt"
  1622. file_path := org_name + "日志" + "/" + file_name
  1623. exist, _ := utils.PathExists(file_path)
  1624. if exist { //存在
  1625. fmt.Println("存在")
  1626. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  1627. if err != nil {
  1628. fmt.Println("read fail")
  1629. }
  1630. content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result
  1631. n, _ := f.Seek(0, 2)
  1632. _, err = f.WriteAt([]byte(content), n)
  1633. } else { //不存在
  1634. fmt.Println("文件不存在,创建文件")
  1635. f, err := os.Create(org_name + "日志" + "/" + file_name)
  1636. defer f.Close()
  1637. if err != nil {
  1638. } else {
  1639. _, err = f.Write([]byte("记录日志"))
  1640. }
  1641. }
  1642. }
  1643. func GetSzblItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  1644. var inspection_reference models.MiddleInspectionReference
  1645. 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
  1646. if inspection_reference.ID > 0 {
  1647. return inspection_reference.ItemId, err
  1648. } else {
  1649. 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
  1650. utils.InfoLog("inspection_reference: %v", inspection_reference)
  1651. if inspection_reference.ItemId > 0 {
  1652. return inspection_reference.ItemId + 1, err
  1653. } else {
  1654. return project_id*100 + 1, err
  1655. }
  1656. }
  1657. }