wfnfm_service.go 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. _"net/http"
  12. _"io/ioutil"
  13. _ "github.com/jinzhu/gorm"
  14. "strconv"
  15. _"strings"
  16. "time"
  17. "github.com/jinzhu/gorm"
  18. )
  19. type AdminUserList struct {
  20. Id int64 `json:"id"`
  21. Name string `json:"name"`
  22. UserType int64 `json:"user_type"`
  23. Status int64 `json:"status"`
  24. }
  25. func GetAllAdminUsers(orgId, appid int64) (list []*AdminUserList, err error) {
  26. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.org_id=? and uar.app_id =? and ua.status=1", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type,uar.status").Scan(&list).Error
  27. return
  28. }
  29. func GetPatientInformation(id int64, orgid int64) (models.Patients, error) {
  30. patients := models.Patients{}
  31. err := XTReadDB().Where("id = ? AND user_org_id = ?", id, orgid).Find(&patients).Error
  32. return patients, err
  33. }
  34. func GetAllPatientByOrgID(orgid int64, timenow int64) (order []*models.DialysisOrder, err error) {
  35. err = XTReadDB().Where("user_org_id = ? AND dialysis_date = ? AND stage = 2 AND status = ?", orgid, timenow, 1).Find(&order).Error
  36. return order, err
  37. }
  38. //透析处方
  39. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  40. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  41. return
  42. }
  43. //透前评估
  44. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  45. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  46. return
  47. }
  48. //透后评估
  49. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  50. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  51. return
  52. }
  53. //透析监测
  54. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  55. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  56. return
  57. }
  58. //临时医嘱
  59. func FindDoctorAdviceOrderById(orgID int64,patientId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  60. err = readDb.Model(&models.DoctorAdvice{}).
  61. Where("user_org_id=? and status=1 and record_date = ? and advice_type = 2 and patient_id = ?", orgID, recordDate,patientId).
  62. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  63. Find(&patient).Error
  64. return
  65. }
  66. type DataUploadConfig struct {
  67. DbHost string `gorm:"column:db_host" json:"db_host"`
  68. DbPort string `gorm:"column:db_port" json:"db_port"`
  69. DbUser string `gorm:"column:db_user" json:"db_user"`
  70. DbPass string `gorm:"column:db_pass" json:"db_pass"`
  71. DbName string `gorm:"column:db_name" json:"db_name"`
  72. }
  73. func BatchCreateRecord(ps *models.EmrBloodDialyRecord, rdb *gorm.DB) (err error) {
  74. err = rdb.Create(&ps).Error
  75. return err
  76. }
  77. func BatchCreateDetail(ps *models.EmrBloodDialyDetail, rdb *gorm.DB) (err error) {
  78. err = rdb.Create(&ps).Error
  79. return err
  80. }
  81. func BatchCreateOrder(ps *models.EmrBloodDialyOrder, rdb *gorm.DB) (err error) {
  82. err = rdb.Create(&ps).Error
  83. return err
  84. }
  85. // 潍坊内分泌医院 同步健康数据上报
  86. func EmrBloodDiaylOrder() {
  87. org := &DataUploadConfig{
  88. DbHost: "127.0.0.1",
  89. DbPort: "1433",
  90. DbPass: "Aa123456",
  91. DbUser: "xtzk",
  92. DbName: "wfnfm",
  93. }
  94. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  95. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  96. if err != nil {
  97. utils.ErrorLog("创建数据库连接失败:%v", err)
  98. return
  99. }
  100. // 第二步:获取上一次同步的时间点
  101. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(1041401)
  102. var sync_time int64
  103. if syncLastInfo.ID > 0 {
  104. sync_time = syncLastInfo.SyncTime + 86400
  105. } else {
  106. sync_time = 1664553600
  107. }
  108. adminUser, _ := GetAllAdminUsers(10414, 15102)
  109. nowTimes := time.Now().Unix()
  110. if sync_time < nowTimes {
  111. for i := sync_time; i < nowTimes; i = i + 86400 {
  112. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  113. timeLayout := "2006-01-02 15:04:05"
  114. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, tempsyncTimes+" 00:00:00")
  115. timenow := timeStringToTime.Unix()
  116. orders,_ := GetAllPatientByOrgID(10414,timenow)
  117. if len(orders) > 0 {
  118. for _, order := range orders {
  119. //查询病人信息
  120. patients, _ := GetPatientInformation(order.PatientId, 10414)
  121. Records, _ := FindDoctorAdviceOrderById(10414,order.PatientId,timenow)
  122. if len(Records) > 0 {
  123. for _,record := range Records {
  124. emrDetail := &models.EmrBloodDialyOrder{
  125. ReqNo : patients.DialysisNo,
  126. OrderType : "1",
  127. OrderDate : time.Unix(record.StartTime, 0).Format("2006-01-02 15:04:05"),
  128. OrderName : record.AdviceName,
  129. Dosage : strconv.FormatFloat(record.PrescribingNumber, 'f', 1, 32)+record.PrescribingNumberUnit,
  130. Usage : record.DeliveryWay,
  131. ExecDate : time.Unix(record.ExecutionTime, 0).Format("2006-01-02 15:04:05"),
  132. Orglevel : "二级",
  133. Submitdate : time.Unix(record.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  134. Recordstate : "0",
  135. Fectchdate : time.Unix(order.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  136. OrganizationCode : "",
  137. OrganizationName : "",
  138. DistrictCode : "370724",
  139. DistrictName : "临朐县",
  140. Uploadmark : "0",
  141. }
  142. for _, admin := range adminUser {
  143. if record.AdviceDoctor == admin.Id {
  144. emrDetail.DoctorName = admin.Name
  145. }
  146. if record.ExecutionStaff == admin.Id{
  147. emrDetail.NurseName = admin.Name
  148. }
  149. }
  150. currentYear := time.Now().Year() //当前年
  151. emrDetail.Businessyear = int64(currentYear)
  152. BatchCreateOrder(emrDetail,orgDb)
  153. }
  154. }
  155. }
  156. var syncInfo models.MiddleSyncInfo
  157. syncInfo.OrgId = 1041401
  158. syncInfo.SyncTime = i
  159. syncInfo.SyncResultType = 1
  160. syncInfo.SyncRsultRemark = "同步成功"
  161. syncInfo.SyncTotalNum = 0
  162. syncInfo.SyncSuccessNum = 0
  163. syncInfo.SyncInfo = ""
  164. syncInfo.CreateTime = time.Now().Unix()
  165. syncInfo.UpdateTime = time.Now().Unix()
  166. cwderr := CreateSyncInfo(&syncInfo)
  167. if cwderr != nil {
  168. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  169. return
  170. }
  171. }
  172. }
  173. }
  174. // 第四步:关闭数据库连接
  175. CloseDB(orgDb)
  176. }
  177. }
  178. // 潍坊内分泌医院 同步健康数据上报
  179. func EmrBloodDiaylDetail() {
  180. org := &DataUploadConfig{
  181. DbHost: "localhost",
  182. DbPort: "1433",
  183. DbPass: "Aa123456",
  184. DbUser: "xtzk",
  185. DbName: "wfnfm",
  186. }
  187. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  188. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  189. if err != nil {
  190. utils.ErrorLog("创建数据库连接失败:%v", err)
  191. return
  192. }
  193. // 第二步:获取上一次同步的时间点
  194. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(1041402)
  195. var sync_time int64
  196. if syncLastInfo.ID > 0 {
  197. sync_time = syncLastInfo.SyncTime + 86400
  198. } else {
  199. sync_time = 1664553600
  200. }
  201. nowTimes := time.Now().Unix()
  202. if sync_time < nowTimes {
  203. for i := sync_time; i < nowTimes; i = i + 86400 {
  204. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  205. timeLayout := "2006-01-02 15:04:05"
  206. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, tempsyncTimes+" 00:00:00")
  207. timenow := timeStringToTime.Unix()
  208. orders,_ := GetAllPatientByOrgID(10414,timenow)
  209. if len(orders) > 0 {
  210. for _, order := range orders {
  211. //查询病人信息
  212. patients, _ := GetPatientInformation(order.PatientId, 10414)
  213. Records, _ := FindAllMonitorRecord(10414, order.PatientId, timenow)
  214. if len(Records) > 0 {
  215. for _,record := range Records {
  216. emrDetail := &models.EmrBloodDialyDetail{
  217. ReqNo : patients.DialysisNo,
  218. Patientname : patients.Name,
  219. Idcard : patients.IdCardNo,
  220. Hospitalcode : "MA3R6UFL9",
  221. Hospitalname : "潍坊内分泌与代谢病医院",
  222. BdDate : time.Unix(record.MonitoringDate, 0).Format("2006-01-02") + " " + record.MonitoringTime ,
  223. BdBp : strconv.FormatFloat(record.SystolicBloodPressure, 'f', 1, 32) + "/" + strconv.FormatFloat(record.DiastolicBloodPressure, 'f', 1, 32),
  224. BdT : record.DialysateTemperature,
  225. BdRate : record.PulseFrequency,
  226. BdFlow : record.BloodFlowVolume,
  227. BdVbp : record.VenousPressure,
  228. BdTmp : record.TransmembranePressure,
  229. BdUfvLimit : record.UltrafiltrationVolume,
  230. Orglevel : "二级",
  231. Submitdate : time.Unix(record.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  232. Recordstate : "0",
  233. Fectchdate : time.Unix(order.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  234. OrganizationCode : "",
  235. OrganizationName : "",
  236. DistrictCode : "370724",
  237. DistrictName : "临朐县",
  238. Uploadmark : "0",
  239. }
  240. currentYear := time.Now().Year() //当前年
  241. emrDetail.Businessyear = int64(currentYear)
  242. BatchCreateDetail(emrDetail,orgDb)
  243. }
  244. }
  245. }
  246. var syncInfo models.MiddleSyncInfo
  247. syncInfo.OrgId = 1041402
  248. syncInfo.SyncTime = i
  249. syncInfo.SyncResultType = 1
  250. syncInfo.SyncRsultRemark = "同步成功"
  251. syncInfo.SyncTotalNum = 0
  252. syncInfo.SyncSuccessNum = 0
  253. syncInfo.SyncInfo = ""
  254. syncInfo.CreateTime = time.Now().Unix()
  255. syncInfo.UpdateTime = time.Now().Unix()
  256. cwderr := CreateSyncInfo(&syncInfo)
  257. if cwderr != nil {
  258. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  259. return
  260. }
  261. }
  262. }
  263. }
  264. // 第四步:关闭数据库连接
  265. CloseDB(orgDb)
  266. }
  267. }
  268. // 潍坊内分泌医院 同步健康数据上报
  269. func EmrBloodDiaylRecord() {
  270. org := &DataUploadConfig{
  271. DbHost: "localhost",
  272. DbPort: "1433",
  273. DbPass: "Aa123456",
  274. DbUser: "xtzk",
  275. DbName: "wfnfm",
  276. }
  277. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  278. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  279. if err != nil {
  280. utils.ErrorLog("创建数据库连接失败:%v", err)
  281. return
  282. }
  283. adminUser, _ := GetAllAdminUsers(10414, 15102)
  284. // 第二步:获取上一次同步的时间点
  285. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(1041403)
  286. var sync_time int64
  287. if syncLastInfo.ID > 0 {
  288. sync_time = syncLastInfo.SyncTime + 86400
  289. } else {
  290. sync_time = 1664553600
  291. }
  292. nowTimes := time.Now().Unix()
  293. if sync_time < nowTimes {
  294. for i := sync_time; i < nowTimes; i = i + 86400 {
  295. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  296. timeLayout := "2006-01-02 15:04:05"
  297. timeStringToTime, _ := utils.ParseTimeStringToTime(timeLayout, tempsyncTimes+" 00:00:00")
  298. timenow := timeStringToTime.Unix()
  299. orders,_ := GetAllPatientByOrgID(10414,timenow)
  300. if len(orders) > 0 {
  301. for _, order := range orders {
  302. //查询病人信息
  303. patients, _ := GetPatientInformation(order.PatientId, 10414)
  304. //透析处方
  305. dialysisPrescription, _ := FindPatientPrescribeById(10414, order.PatientId, timenow)
  306. //透前评估
  307. PredialysisEvaluation, _ := FindPredialysisEvaluationById(10414, order.PatientId, timenow)
  308. //透后评估
  309. AssessmentAfterDislysis, _ := FindAssessmentAfterDislysisById(10414, order.PatientId, timenow)
  310. emrRecord := &models.EmrBloodDialyRecord{
  311. ReqNo : patients.DialysisNo,
  312. Patientname : patients.Name,
  313. Idcard : patients.IdCardNo,
  314. Hospitalcode : "MA3R6UFL9",
  315. Hospitalname : "潍坊内分泌与代谢病医院",
  316. AdmissionId : patients.IdCardNo,
  317. BdDate : time.Unix(order.DialysisDate, 0).Format("2006-01-02"),
  318. BdMatno : dialysisPrescription.DialyzerPerfusionApparatus,
  319. Temperature : PredialysisEvaluation.Temperature,
  320. PulseRate : PredialysisEvaluation.PulseFrequency,
  321. BreathingRate : PredialysisEvaluation.BreathingRate,
  322. PressureH : PredialysisEvaluation.SystolicBloodPressure,
  323. PressureL : PredialysisEvaluation.DiastolicBloodPressure,
  324. UfvLimit : dialysisPrescription.DewaterAmount*1000,
  325. FirstAnticoagulant : dialysisPrescription.AnticoagulantShouji,
  326. BdHours : AssessmentAfterDislysis.ActualTreatmentHour,
  327. BdType : dialysisPrescription.ModeId,
  328. BdT : 36.5,
  329. AnticoagulantTotal : dialysisPrescription.AnticoagulantZongliang,
  330. BdCruorContiton : "2",
  331. PatientWeight : PredialysisEvaluation.DryWeight,
  332. WeightBf : PredialysisEvaluation.WeightBefore,
  333. WeightAf : AssessmentAfterDislysis.WeightAfter,
  334. WeightBalan : PredialysisEvaluation.WeightBefore - PredialysisEvaluation.WeightBefore,
  335. Vascular : "2",
  336. Orglevel : "二级",
  337. Submitdate : time.Unix(order.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  338. Recordstate : "0",
  339. Fectchdate : time.Unix(order.CreatedTime, 0).Format("2006-01-02 15:04:05"),
  340. OrganizationCode : "",
  341. OrganizationName : "",
  342. DistrictCode : "370724",
  343. DistrictName : "临朐县",
  344. Uploadmark : "0",
  345. }
  346. for _, admin := range adminUser {
  347. if dialysisPrescription.Creater == admin.Id {
  348. emrRecord.DoctorName = admin.Name
  349. }
  350. if order.StartNurse == admin.Id{
  351. emrRecord.OperName = admin.Name
  352. emrRecord.PrimaryName = admin.Name
  353. }
  354. }
  355. if(patients.Gender == 1){
  356. emrRecord.SexCode = "1"
  357. emrRecord.SexName = "男"
  358. }
  359. if(patients.Gender == 2){
  360. emrRecord.SexCode = "2"
  361. emrRecord.SexName = "女"
  362. }
  363. currentYear := time.Now().Year() //当前年
  364. birthYear := time.Unix(patients.Birthday, 0).Year()
  365. emrRecord.Businessyear = int64(currentYear)
  366. emrRecord.Age = int64(currentYear) - int64(birthYear)
  367. switch dialysisPrescription.Anticoagulant {
  368. case 1:
  369. emrRecord.Anticoagulant = "无肝素"
  370. break
  371. case 2:
  372. emrRecord.Anticoagulant = "普通肝素"
  373. break
  374. case 3:
  375. emrRecord.Anticoagulant = "低分子肝素"
  376. break
  377. case 4:
  378. emrRecord.Anticoagulant = "阿加曲班"
  379. break
  380. case 5:
  381. emrRecord.Anticoagulant = "枸橼酸钠"
  382. break
  383. case 6:
  384. emrRecord.Anticoagulant = "低分子肝素钙"
  385. break
  386. case 7:
  387. emrRecord.Anticoagulant = "低分子肝素钠"
  388. break
  389. }
  390. switch dialysisPrescription.ModeId {
  391. case 1:
  392. emrRecord.BdTypeName = "HD"
  393. break
  394. case 2:
  395. emrRecord.BdTypeName = "HDF"
  396. break
  397. case 3:
  398. emrRecord.BdTypeName = "HD+HP"
  399. break
  400. case 4:
  401. emrRecord.BdTypeName = "HP"
  402. break
  403. case 5:
  404. emrRecord.BdTypeName = "HF"
  405. break
  406. case 6:
  407. emrRecord.BdTypeName = "SCUF"
  408. break
  409. case 7:
  410. emrRecord.BdTypeName = "IUF"
  411. break
  412. case 8:
  413. emrRecord.BdTypeName = "HFHD"
  414. break
  415. case 9:
  416. emrRecord.BdTypeName = "HFHD+HP"
  417. break
  418. case 10:
  419. emrRecord.BdTypeName = "PHF"
  420. break
  421. case 11:
  422. emrRecord.BdTypeName = "HFR"
  423. break
  424. case 12:
  425. emrRecord.BdTypeName = "HDF+HP"
  426. break
  427. default:
  428. emrRecord.BdTypeName = "HD"
  429. break
  430. }
  431. if AssessmentAfterDislysis.Cruor == "透析器-0度" || len(AssessmentAfterDislysis.Cruor) <= 0{
  432. emrRecord.BdCruor = "2"
  433. emrRecord.BdCruorName = "无凝血"
  434. } else {
  435. emrRecord.BdCruor = "1"
  436. emrRecord.BdCruorName = "有凝血"
  437. }
  438. BatchCreateRecord(emrRecord,orgDb)
  439. }
  440. var syncInfo models.MiddleSyncInfo
  441. syncInfo.OrgId = 1041403
  442. syncInfo.SyncTime = i
  443. syncInfo.SyncResultType = 1
  444. syncInfo.SyncRsultRemark = "同步成功"
  445. syncInfo.SyncTotalNum = 0
  446. syncInfo.SyncSuccessNum = 0
  447. syncInfo.SyncInfo = ""
  448. syncInfo.CreateTime = time.Now().Unix()
  449. syncInfo.UpdateTime = time.Now().Unix()
  450. cwderr := CreateSyncInfo(&syncInfo)
  451. if cwderr != nil {
  452. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  453. return
  454. }
  455. }
  456. }
  457. }
  458. // 第四步:关闭数据库连接
  459. CloseDB(orgDb)
  460. }
  461. }
  462. // 福建天福医院透析中心Lis同步
  463. // func SyncFjtfLis() (err error) {
  464. // org_id := int64(10330)
  465. // // 第一步:获取上一次同步的时间点
  466. // syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  467. // var sync_time int64
  468. // if syncLastInfo.ID > 0 {
  469. // sync_time = syncLastInfo.SyncTime
  470. // } else {
  471. // sync_time = 1651334400 // 2022-05-01 00:00:00
  472. // }
  473. // sync_time_temp := time.Unix(sync_time,0)
  474. // syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  475. // // 第二步:获取所有患者的病历号
  476. // patientList, _ := GetPatientGzjhByOrgId(org_id)
  477. // if len(patientList) > 0 {
  478. // for _, patient := range patientList {
  479. // if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  480. // patient_id := patient.ID
  481. // utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  482. // // utils.InfoLog("VipStr:%v", VipStr)
  483. // result, _ := FjtjGetLab(patient.IdCardNo,syncTimeStr)
  484. // var fjtfLab LabResult
  485. // if err := json.Unmarshal([]byte(result), &fjtfLab); err != nil {
  486. // utils.ErrorLog("解析失败:%v", err)
  487. // }
  488. // fmt.Println(fjtfLab)
  489. // if len(fjtfLab.Result) > 0 {
  490. // // 根据获取的头部lab01获取检查结果
  491. // for _, info := range fjtfLab.Result {
  492. // lacResult, _ := FjtjGetLac(info.Lab01)
  493. // var fjtfLac LacResult
  494. // if err := json.Unmarshal([]byte(lacResult), &fjtfLac); err != nil {
  495. // utils.ErrorLog("解析失败:%v", err)
  496. // }
  497. // fmt.Println(fjtfLac)
  498. // for _,cinfo := range fjtfLac.Result{
  499. // project_id := int64(0)
  500. // item_id := int64(0)
  501. // if len(cinfo.Bbx05a) > 0 {
  502. // project_id, _ = GetfjtfProjectID(org_id, cinfo.Bbx05a)
  503. // item_id, _ = GetFjtfItemID(org_id, cinfo.Bbx05a, cinfo.Bbx05, project_id)
  504. // } else {
  505. // project_id, _ = GetfjtfProjectID(org_id, cinfo.Bbx05)
  506. // item_id, _ = GetFjtfItemID(org_id, cinfo.Bbx05, cinfo.Bbx05, project_id)
  507. // }
  508. // tx := writeMiddleDb.Begin()
  509. // var inspection models.MiddleInspection
  510. // var inspection_reference models.MiddleInspectionReference
  511. // loc, _ := time.LoadLocation("Local")
  512. // theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", cinfo.Lac25, loc)
  513. // record_date := theTime.Unix()
  514. // if record_date == 0 {
  515. // record_date = time.Now().Unix()
  516. // }
  517. // inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  518. // var total int
  519. // var RangeOptions string
  520. // var RangeMin string
  521. // var RangeMax string
  522. // // // 判断检查类型
  523. // ItemType := 1
  524. // Range := strings.Split(cinfo.Lac15, "-")
  525. // if len(Range) > 1 {
  526. // RangeMin = cinfo.Lac13
  527. // RangeMax = cinfo.Lac14
  528. // ItemType = 1
  529. // } else {
  530. // ItemType = 2
  531. // RangeOptions = cinfo.Lac15
  532. // }
  533. // 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
  534. // // if inspection_reference.ID > 0 {
  535. // // ItemType = int64(inspection_reference.RangeType)
  536. // // }
  537. // if total <= 0 {
  538. // inspection_reference.OrgId = org_id
  539. // if len(cinfo.Bbx05a) > 0 {
  540. // inspection_reference.ProjectName = cinfo.Bbx05a
  541. // inspection_reference.Project = cinfo.Bbx05a
  542. // } else {
  543. // inspection_reference.ProjectName = cinfo.Bbx05
  544. // inspection_reference.Project = cinfo.Bbx05
  545. // }
  546. // inspection_reference.ProjectId = project_id
  547. // inspection_reference.ItemName = cinfo.Bbx05
  548. // inspection_reference.ItemId = item_id
  549. // inspection_reference.RangeType = ItemType
  550. // inspection_reference.RangeMin = RangeMin
  551. // inspection_reference.RangeMax = RangeMax
  552. // // inspection_reference.RangeValue = RangeValue
  553. // inspection_reference.RangeOptions = RangeOptions
  554. // inspection_reference.Unit = cinfo.Bdg02
  555. // inspection_reference.Status = 1
  556. // inspection_reference.CreatedTime = time.Now().Unix()
  557. // inspection_reference.UpdatedTime = time.Now().Unix()
  558. // inspection_reference.InspectDate = inspect_date
  559. // inspection_reference.UTime = inspect_date
  560. // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  561. // if err != nil {
  562. // tx.Rollback()
  563. // }
  564. // }
  565. // var itotal int
  566. // 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
  567. // if itotal <= 0 {
  568. // inspection.PatientId = patient_id
  569. // inspection.OrgId = org_id
  570. // inspection.ProjectId = project_id
  571. // inspection.ItemName = inspection_reference.ItemName
  572. // inspection.ProjectName = inspection_reference.ProjectName
  573. // inspection.InspectType = ItemType
  574. // inspection.ItemId = item_id
  575. // inspection.InspectValue = cinfo.Lac10
  576. // inspection.InspectDate = inspect_date
  577. // inspection.RecordDate = record_date
  578. // inspection.InspectTips = cinfo.Lac11
  579. // inspection.Status = 1
  580. // inspection.CreatedTime = time.Now().Unix()
  581. // inspection.UpdatedTime = time.Now().Unix()
  582. // inspection.UTime = inspect_date
  583. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  584. // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  585. // if err != nil {
  586. // tx.Rollback()
  587. // }
  588. // }
  589. // tx.Commit()
  590. // }
  591. // }
  592. // }
  593. // } else {
  594. // continue
  595. // }
  596. // }
  597. // }
  598. // var syncInfo models.MiddleSyncInfo
  599. // syncInfo.OrgId = org_id
  600. // syncInfo.SyncTime = time.Now().Unix()
  601. // syncInfo.SyncResultType = 1
  602. // syncInfo.SyncRsultRemark = "同步成功"
  603. // syncInfo.SyncTotalNum = 0
  604. // syncInfo.SyncSuccessNum = 0
  605. // syncInfo.SyncInfo = ""
  606. // syncInfo.CreateTime = time.Now().Unix()
  607. // syncInfo.UpdateTime = time.Now().Unix()
  608. // cwderr := CreateSyncInfo(&syncInfo)
  609. // if cwderr != nil {
  610. // utils.ErrorLog("创建同步信息失败:%v", cwderr)
  611. // return
  612. // }
  613. // SyncToFjtftx()
  614. // return
  615. // }
  616. // type LabResult struct {
  617. // Msg string `json:"msg"`
  618. // Result []struct {
  619. // Lab01 int64 `json:"lab01"`
  620. // Lab58 string `json:"lab58"`
  621. // } `json:"result"`
  622. // Code string `json:"code"`
  623. // }
  624. // func FjtjGetLab(id_card_no string, synctime string) (string, string) {
  625. // inputData := make(map[string]interface{})
  626. // inputData["vaa15"] = id_card_no
  627. // inputData["begintime"] = synctime
  628. // var inputLog string
  629. // bytesData, err := json.Marshal(inputData)
  630. // inputLog = string(bytesData)
  631. // fmt.Println(string(bytesData))
  632. // if err != nil {
  633. // fmt.Println(err.Error())
  634. // return err.Error(), ""
  635. // }
  636. // reader := bytes.NewReader(bytesData)
  637. // url := "http://hip.zptfyy.com/esb/listener/getLab1"
  638. // request, err := http.NewRequest("POST", url, reader)
  639. // if err != nil {
  640. // fmt.Println(err.Error())
  641. // return err.Error(), ""
  642. // }
  643. // request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  644. // request.Header.Set("code", "xt")
  645. // client := http.Client{}
  646. // resp, err := client.Do(request)
  647. // if err != nil {
  648. // fmt.Println(err.Error())
  649. // return err.Error(), ""
  650. // }
  651. // respBytes, err := ioutil.ReadAll(resp.Body)
  652. // if err != nil {
  653. // fmt.Println(err.Error())
  654. // return err.Error(), ""
  655. // }
  656. // str := string(respBytes)
  657. // return str, inputLog
  658. // }
  659. // type LacResult struct {
  660. // Msg string `json:"msg"`
  661. // Result []struct {
  662. // Lab01 int64 `json:"labo1"`
  663. // Bbx05 string `json:"BBX05"`
  664. // Bbx05a string `json:"BBX05A"`
  665. // Bfc03 string `json:"BFC03"`
  666. // Bdg02 string `json:"BDG02"`
  667. // Lac10 string `json:"LAC10"`
  668. // Lac11 string `json:"LAC11"`
  669. // Lac13 string `json:"LAC13"`
  670. // Lac14 string `json:"LAC14"`
  671. // Lac15 string `json:"LAC15"`
  672. // Lac25 string `json:"LAC25"`
  673. // } `json:"result"`
  674. // Code string `json:"code"`
  675. // }
  676. // func FjtjGetLac(lab01 int64) (string, string) {
  677. // inputData := make(map[string]interface{})
  678. // inputData["lab01"] = lab01
  679. // var inputLog string
  680. // bytesData, err := json.Marshal(inputData)
  681. // inputLog = string(bytesData)
  682. // fmt.Println(string(bytesData))
  683. // if err != nil {
  684. // fmt.Println(err.Error())
  685. // return err.Error(), ""
  686. // }
  687. // reader := bytes.NewReader(bytesData)
  688. // url := "http://hip.zptfyy.com/esb/listener/getLac1"
  689. // request, err := http.NewRequest("POST", url, reader)
  690. // if err != nil {
  691. // fmt.Println(err.Error())
  692. // return err.Error(), ""
  693. // }
  694. // request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  695. // request.Header.Set("code", "xt")
  696. // client := http.Client{}
  697. // resp, err := client.Do(request)
  698. // if err != nil {
  699. // fmt.Println(err.Error())
  700. // return err.Error(), ""
  701. // }
  702. // respBytes, err := ioutil.ReadAll(resp.Body)
  703. // if err != nil {
  704. // fmt.Println(err.Error())
  705. // return err.Error(), ""
  706. // }
  707. // str := string(respBytes)
  708. // return str, inputLog
  709. // }
  710. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  711. // func GetfjtfProjectID(org_id int64, project_name string) (project_id int64, err error) {
  712. // var inspection_reference models.MiddleInspectionReference
  713. // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  714. // if inspection_reference.ID > 0 {
  715. // return inspection_reference.ProjectId, err
  716. // } else {
  717. // err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  718. // if inspection_reference.ProjectId > 0 {
  719. // return inspection_reference.ProjectId + 1, err
  720. // } else {
  721. // return 330001, err
  722. // }
  723. // }
  724. // }
  725. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  726. // func GetFjtfItemID(org_id int64,project_name string,item_name string,project_id int64) (item_id int64 ,err error) {
  727. // var inspection_reference models.MiddleInspectionReference
  728. // 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
  729. // if inspection_reference.ID > 0 {
  730. // return inspection_reference.ItemId,err
  731. // } else {
  732. // 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
  733. // utils.InfoLog("inspection_reference: %v",inspection_reference)
  734. // if inspection_reference.ItemId > 0 {
  735. // return inspection_reference.ItemId + 1,err
  736. // } else {
  737. // return project_id * 100 + 1 ,err
  738. // }
  739. // }
  740. // }
  741. // func SyncToFjtftx() {
  742. // utils.TraceLog("检验检查同步任务开始执行")
  743. // org_id := int64(10330)
  744. // // 第一步:跟进org_id 去中间库查出需要同步的数据
  745. // inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  746. // inspections, _ := GetSyncInspectionByOrgId(org_id)
  747. // // 第二步:将数据同步到业务库
  748. // if len(inspection_references) > 0 {
  749. // for _, inspection_reference := range inspection_references {
  750. // SyncInspectionReference(&inspection_reference)
  751. // }
  752. // }
  753. // if len(inspections) > 0 {
  754. // for _, inspection := range inspections {
  755. // SyncInspection(&inspection)
  756. // }
  757. // }
  758. // utils.SuccessLog("检验检查同步任务完成")
  759. // }