wfnfm_service.go 29KB

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