wfnfm_service.go 29KB

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