ystx_service.go 68KB

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