ystx_service.go 68KB

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