ystx_service.go 66KB

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