sync.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. package controllers
  2. import (
  3. "Data_Upload_Api/enums"
  4. "Data_Upload_Api/models/sz"
  5. "Data_Upload_Api/service"
  6. "Data_Upload_Api/utils"
  7. "github.com/astaxie/beego"
  8. "github.com/jinzhu/gorm"
  9. "math"
  10. "strconv"
  11. "strings"
  12. _ "strings"
  13. "time"
  14. )
  15. type SyncController struct {
  16. BaseAPIController
  17. }
  18. func (c *SyncController) SyncToSZIC() {
  19. // 第一步:到上报配置表中找到深圳需要上报的机构
  20. sz_province, _ := beego.AppConfig.Int64("sz_province")
  21. sz_city, _ := beego.AppConfig.Int64("sz_city")
  22. configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3)
  23. for _, org := range configs {
  24. // 第二步:跟进配置,创建数据库连接
  25. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  26. orgDb, err := service.CreateDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  27. if err != nil {
  28. utils.ErrorLog("创建数据库连接失败:%v", err)
  29. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  30. return
  31. }
  32. // 第三步:开始同步数据
  33. c.SyncHospital(orgDb, org.OrgId, org.HospitalId, org.InstType, org.DepartmentName)
  34. // 第四步:关闭数据库连接
  35. service.CloseDB(orgDb)
  36. }
  37. }
  38. }
  39. // 同步医院信息
  40. func (c *SyncController) SyncHospital(rdb *gorm.DB, org_id int64, hospital_id int64, inst_type int64, department_name string) {
  41. // 第一步:根据机构id获取上次同步时间
  42. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 1)
  43. var sync_time int64
  44. if syncLastInfo.ID > 0 {
  45. sync_time = syncLastInfo.SyncTime
  46. } else {
  47. sync_time = 0
  48. }
  49. // 第二步:跟进上次同步时间找出这个时间段内增加的数据
  50. org, _ := service.FindOrgData(org_id, sync_time)
  51. if org.ID > 0 {
  52. hospital := &sz.TempHdHospital{
  53. HospitalId: strconv.FormatInt(hospital_id, 10),
  54. InstType: strconv.FormatInt(inst_type, 10),
  55. DepartMentName: department_name,
  56. AuthorizedBeds: int64(len(org.DeviceNumber)),
  57. CreateTime: time.Now(),
  58. UpdateTime: time.Now(),
  59. Sjscsj: time.Now(),
  60. Xgbz: 0,
  61. }
  62. err := service.CreateOrgRecord(hospital)
  63. if err == nil {
  64. // 第三步:同步成功后,添加同步记录
  65. upload := &sz.DataUpload{
  66. SyncType: 1,
  67. OrgId: org_id,
  68. SyncTime: time.Now().Unix(),
  69. SyncResultType: 1,
  70. CreateTime: time.Now().Unix(),
  71. UpdateTime: time.Now().Unix(),
  72. }
  73. err := service.CreateUploadRecord(upload)
  74. if err != nil {
  75. utils.ErrorLog("%v", err)
  76. }
  77. }
  78. }
  79. }
  80. // 同步水处理器
  81. func (c *SyncController) SyncWMS(rdb *gorm.DB, org_id int64, hospital_id int64) {
  82. // 第一步:根据机构id获取上次同步时间
  83. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 22)
  84. var sync_time int64
  85. if syncLastInfo.ID > 0 {
  86. sync_time = syncLastInfo.SyncTime
  87. } else {
  88. sync_time = 0
  89. }
  90. //同步水处理机
  91. waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  92. var wms []*sz.TempHdWm
  93. for _, item := range waterMachine {
  94. wm := &sz.TempHdWm{
  95. HospitalId: strconv.FormatInt(hospital_id, 10),
  96. EquipmentId: strconv.FormatInt(item.ID, 10),
  97. EquipmentBrand: item.ManufactureFactory,
  98. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  99. EnableTime: time.Unix(item.StartDate, 0),
  100. DiscardedTime: time.Unix(item.RubbishDate, 0),
  101. EquipmentType: strconv.FormatInt(item.DeviceType, 10),
  102. CreateTime: time.Now(),
  103. UpdateTime: time.Now(),
  104. Sjscsj: time.Now(),
  105. Xgbz: strconv.FormatInt(0, 10),
  106. }
  107. wms = append(wms, wm)
  108. }
  109. err := service.BatchCreateWMsRecord(wms, rdb)
  110. if err == nil {
  111. // 第三步:同步成功后,添加同步记录
  112. upload := &sz.DataUpload{
  113. SyncType: 22,
  114. OrgId: org_id,
  115. SyncTime: time.Now().Unix(),
  116. SyncResultType: 1,
  117. CreateTime: time.Now().Unix(),
  118. UpdateTime: time.Now().Unix(),
  119. }
  120. err := service.CreateUploadRecord(upload)
  121. if err != nil {
  122. utils.ErrorLog("%v", err)
  123. }
  124. } else {
  125. //错误处理
  126. }
  127. }
  128. // 同步员工
  129. func (c *SyncController) SyncStaff(rdb *gorm.DB, org_id int64, hospital_id int64) {
  130. // 第一步:根据机构id获取上次同步时间
  131. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 21)
  132. var sync_time int64
  133. if syncLastInfo.ID > 0 {
  134. sync_time = syncLastInfo.SyncTime
  135. } else {
  136. sync_time = 0
  137. }
  138. //同步员工信息
  139. roles, _ := service.FindOrgRolesData(org_id, sync_time, time.Now().Unix())
  140. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  141. var staffs []*sz.TempHdStaff
  142. for _, role := range roles {
  143. var title string
  144. switch role.UserType {
  145. case 2:
  146. title = strconv.FormatInt(1, 10)
  147. break
  148. case 3:
  149. title = strconv.FormatInt(2, 10)
  150. break
  151. }
  152. staff := &sz.TempHdStaff{
  153. HospitalId: strconv.FormatInt(hospital_id, 10),
  154. StaffId: strconv.FormatInt(role.ID, 10),
  155. StaffName: role.UserName,
  156. Position: title,
  157. PermanentType: strconv.FormatInt(1, 10),
  158. CreateTime: time.Now(),
  159. UpdateTime: time.Now(),
  160. Sjscsj: time.Now(),
  161. Xgbz: strconv.FormatInt(0, 10),
  162. }
  163. staffs = append(staffs, staff)
  164. }
  165. err := service.BatchCreateStaffRecord(staffs, rdb)
  166. if err == nil {
  167. // 第三步:同步成功后,添加同步记录
  168. upload := &sz.DataUpload{
  169. SyncType: 21,
  170. OrgId: org_id,
  171. SyncTime: time.Now().Unix(),
  172. SyncResultType: 1,
  173. CreateTime: time.Now().Unix(),
  174. UpdateTime: time.Now().Unix(),
  175. }
  176. err := service.CreateUploadRecord(upload)
  177. if err != nil {
  178. utils.ErrorLog("%v", err)
  179. }
  180. } else {
  181. //错误处理
  182. }
  183. }
  184. // 同步病人
  185. func (c *SyncController) SyncPatient(rdb *gorm.DB, org_id int64, hospital_id int64) {
  186. // 第一步:根据机构id获取上次同步时间
  187. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 15)
  188. var sync_time int64
  189. if syncLastInfo.ID > 0 {
  190. sync_time = syncLastInfo.SyncTime
  191. } else {
  192. sync_time = 0
  193. }
  194. //同步员工信息
  195. patients, _ := service.FindOrgPatientData(org_id, sync_time, time.Now().Unix())
  196. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  197. var hdPatients []*sz.TempHdPatient
  198. for _, patient := range patients {
  199. idcard_year, _ := strconv.Atoi(Substr(patient.IdCardNo, 6, 4)) // 年
  200. idcard_mo, _ := strconv.Atoi(Substr(patient.IdCardNo, 10, 2)) // 月
  201. idcard_day, _ := strconv.Atoi(Substr(patient.IdCardNo, 12, 2)) // 日
  202. date := strconv.Itoa(idcard_year) + "-" + strconv.Itoa(idcard_mo) + "-" + strconv.Itoa(idcard_day)
  203. local, _ := time.LoadLocation("Local")
  204. birthday, _ := time.ParseInLocation("2006-01-02", date, local)
  205. var isCKD int
  206. value := strings.Index(patient.Diagnose, "慢性肾脏病")
  207. value2 := strings.Index(patient.Diagnose, "CKD")
  208. value3 := strings.Index(patient.Diagnose, "慢性肾衰竭")
  209. if value != -1 || value2 != -1 || value3 != -1 {
  210. isCKD = 1
  211. } else {
  212. isCKD = 0
  213. }
  214. p := &sz.TempHdPatient{
  215. HospitalId: strconv.FormatInt(hospital_id, 10),
  216. PatientNk: strconv.FormatInt(patient.ID, 10),
  217. CardNo: "000000000000000000",
  218. CardType: "01",
  219. IdNo: patient.IdCardNo,
  220. IdType: "01",
  221. PatientName: patient.Name,
  222. Gender: strconv.FormatInt(patient.Gender, 10),
  223. BornDate: birthday,
  224. DiagnosisSummary: patient.Diagnose,
  225. IsCrf: strconv.Itoa(isCKD),
  226. CreatedTime: time.Now(),
  227. UpdateTime: time.Now(),
  228. Sjscsj: time.Now(),
  229. Xgbz: strconv.FormatInt(0, 10),
  230. }
  231. if patient.FirstDialysisDate != 0 {
  232. p.DialysisStartTime = time.Unix(patient.FirstDialysisDate, 0)
  233. }
  234. if patient.HospitalFirstDialysisDate != 0 {
  235. p.LocalStartTime = time.Unix(patient.HospitalFirstDialysisDate, 0)
  236. }
  237. hdPatients = append(hdPatients, p)
  238. }
  239. err := service.BatchCreatePatinet(hdPatients, rdb)
  240. if err == nil {
  241. // 第三步:同步成功后,添加同步记录
  242. upload := &sz.DataUpload{
  243. SyncType: 15,
  244. OrgId: org_id,
  245. SyncTime: time.Now().Unix(),
  246. SyncResultType: 1,
  247. CreateTime: time.Now().Unix(),
  248. UpdateTime: time.Now().Unix(),
  249. }
  250. err := service.CreateUploadRecord(upload)
  251. if err != nil {
  252. utils.ErrorLog("%v", err)
  253. }
  254. } else {
  255. //错误处理
  256. }
  257. }
  258. func Substr(str string, start, length int) string {
  259. rs := []rune(str)
  260. rl := len(rs)
  261. end := 0
  262. if start < 0 {
  263. start = rl - 1 + start
  264. }
  265. end = start + length
  266. if start > end {
  267. start, end = end, start
  268. }
  269. if start < 0 {
  270. start = 0
  271. }
  272. if start > rl {
  273. start = rl
  274. }
  275. if end < 0 {
  276. end = 0
  277. }
  278. if end > rl {
  279. end = rl
  280. }
  281. return string(rs[start:end])
  282. }
  283. // 同步排班
  284. func (c *SyncController) SyncShift(rdb *gorm.DB, org_id int64, hospital_id int64) {
  285. // 第一步:根据机构id获取上次同步时间
  286. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 19)
  287. var sync_time int64
  288. if syncLastInfo.ID > 0 {
  289. sync_time = syncLastInfo.SyncTime
  290. } else {
  291. sync_time = 0
  292. }
  293. var hdShift []*sz.TempHdShift
  294. schs, _ := service.FindOrgScheduleData(org_id, sync_time, time.Now().Unix())
  295. for _, sch := range schs {
  296. shift := &sz.TempHdShift{
  297. PsId: strconv.FormatInt(sch.ID, 10),
  298. HospitalId: strconv.FormatInt(sch.UserOrgId, 10),
  299. PatientNk: strconv.FormatInt(sch.PatientId, 10),
  300. ScheduleDate: time.Unix(sch.ScheduleDate, 0),
  301. ShiftType: strconv.FormatInt(sch.ScheduleType, 10),
  302. SickbedNo: sch.DeviceNumber.Number,
  303. ScheduleStatus: "1",
  304. CreateTime: time.Now(),
  305. Sjscsj: time.Now(),
  306. Xgbz: 0,
  307. }
  308. hdShift = append(hdShift, shift)
  309. }
  310. err := service.BatchCreateSchedual(hdShift, rdb)
  311. if err == nil {
  312. // 第三步:同步成功后,添加同步记录
  313. upload := &sz.DataUpload{
  314. SyncType: 19,
  315. OrgId: org_id,
  316. SyncTime: time.Now().Unix(),
  317. SyncResultType: 1,
  318. CreateTime: time.Now().Unix(),
  319. UpdateTime: time.Now().Unix(),
  320. }
  321. err := service.CreateUploadRecord(upload)
  322. if err != nil {
  323. utils.ErrorLog("%v", err)
  324. }
  325. } else {
  326. //错误处理
  327. }
  328. }
  329. // 同步处方
  330. func (c *SyncController) SyncPs(rdb *gorm.DB, org_id int64, hospital_id int64) {
  331. // 第一步:根据机构id获取上次同步时间
  332. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 17)
  333. var sync_time int64
  334. if syncLastInfo.ID > 0 {
  335. sync_time = syncLastInfo.SyncTime
  336. } else {
  337. sync_time = 0
  338. }
  339. var hdPrescription []*sz.TempHdPs
  340. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  341. for _, item := range prescriptions {
  342. ps := &sz.TempHdPs{
  343. PrescribeId: strconv.FormatInt(item.ID, 10),
  344. HospitalId: strconv.FormatInt(hospital_id, 10),
  345. PatientNk: strconv.FormatInt(item.PatientId, 10),
  346. K: item.Kalium,
  347. Ca: item.Calcium,
  348. Na: item.Sodium,
  349. CreateTime: time.Now(),
  350. Sjscsj: time.Now(),
  351. Xgbz: 0,
  352. PrescribeTime: time.Unix(item.RecordDate, 0),
  353. }
  354. switch item.ModeId {
  355. case 1:
  356. ps.DialysisFrequency = item.AssessmentBeforeDislysis.DialysisCount
  357. ps.FrequencyUnit = 1
  358. ps.DialysisDuration = item.DialysisDuration
  359. ps.Hdf = "0"
  360. ps.Hp = "0"
  361. break
  362. case 2:
  363. ps.Hdf = "1"
  364. ps.Hp = "0"
  365. ps.HdfFrequency = item.AssessmentBeforeDislysis.DialysisCount
  366. ps.HdfFrequencyUnit = 1
  367. ps.HpDuration = item.DialysisDuration
  368. break
  369. case 4:
  370. ps.Hdf = "0"
  371. ps.Hp = "1"
  372. ps.HpFrequency = item.AssessmentBeforeDislysis.DialysisCount
  373. ps.HpFrequencyUnit = 1
  374. ps.HpDuration = item.DialysisDuration
  375. break
  376. }
  377. hdPrescription = append(hdPrescription, ps)
  378. }
  379. err := service.BatchCreatePs(hdPrescription, rdb)
  380. if err == nil {
  381. // 第三步:同步成功后,添加同步记录
  382. upload := &sz.DataUpload{
  383. SyncType: 17,
  384. OrgId: org_id,
  385. SyncTime: time.Now().Unix(),
  386. SyncResultType: 1,
  387. CreateTime: time.Now().Unix(),
  388. UpdateTime: time.Now().Unix(),
  389. }
  390. err := service.CreateUploadRecord(upload)
  391. if err != nil {
  392. utils.ErrorLog("%v", err)
  393. }
  394. } else {
  395. //错误处理
  396. }
  397. }
  398. // 同步处方药物
  399. func (c *SyncController) SyncPsMedicine(rdb *gorm.DB, org_id int64, hospital_id int64) {
  400. // 第一步:根据机构id获取上次同步时间
  401. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 18)
  402. var sync_time int64
  403. if syncLastInfo.ID > 0 {
  404. sync_time = syncLastInfo.SyncTime
  405. } else {
  406. sync_time = 0
  407. }
  408. var tempPsmedicine []*sz.TempHdPsMedicine
  409. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  410. for _, item := range prescriptions {
  411. psm := &sz.TempHdPsMedicine{
  412. PrescribeId: strconv.FormatInt(item.ID, 10),
  413. HospitalId: strconv.FormatInt(item.UserOrgId, 10),
  414. MedicineTypeId: "2",
  415. CreatedTime: time.Now(),
  416. Sjscsj: time.Now(),
  417. Xgbz: 0,
  418. }
  419. switch item.Anticoagulant {
  420. case 1:
  421. psm.MedicineId = "3"
  422. break
  423. case 2:
  424. psm.MedicineId = "1"
  425. break
  426. case 3:
  427. psm.MedicineId = "2"
  428. break
  429. case 4:
  430. psm.MedicineId = "5"
  431. break
  432. case 5:
  433. psm.MedicineId = "4"
  434. break
  435. }
  436. tempPsmedicine = append(tempPsmedicine, psm)
  437. }
  438. err := service.BatchCreatePsm(tempPsmedicine, rdb)
  439. if err == nil {
  440. // 第三步:同步成功后,添加同步记录
  441. upload := &sz.DataUpload{
  442. SyncType: 18,
  443. OrgId: org_id,
  444. SyncTime: time.Now().Unix(),
  445. SyncResultType: 1,
  446. CreateTime: time.Now().Unix(),
  447. UpdateTime: time.Now().Unix(),
  448. }
  449. err := service.CreateUploadRecord(upload)
  450. if err != nil {
  451. utils.ErrorLog("%v", err)
  452. }
  453. } else {
  454. //错误处理
  455. }
  456. }
  457. // 同步转院信息
  458. func (c *SyncController) SyncPatientOut(rdb *gorm.DB, org_id int64, hospital_id int64) {
  459. // 第一步:根据机构id获取上次同步时间
  460. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 16)
  461. var sync_time int64
  462. if syncLastInfo.ID > 0 {
  463. sync_time = syncLastInfo.SyncTime
  464. } else {
  465. sync_time = 0
  466. }
  467. var hdPatientOuts []*sz.TempHdPatientOut
  468. patients, _ := service.FindOrgPatientOutData(org_id, sync_time, time.Now().Unix())
  469. //
  470. for _, patient := range patients {
  471. p := &sz.TempHdPatientOut{
  472. HospitalId: strconv.FormatInt(patient.UserOrgId, 10),
  473. PatientNk: strconv.FormatInt(patient.ID, 10),
  474. SequelaeType: "99",
  475. CreateTime: time.Now(),
  476. Sjscsj: time.Now(),
  477. Xgbz: 0,
  478. }
  479. if len(patient.PatientLapseto) > 0 {
  480. p.SequelaeDate = time.Unix(patient.PatientLapseto[len(patient.PatientLapseto)].LapsetoTime, 0)
  481. }
  482. hdPatientOuts = append(hdPatientOuts, p)
  483. }
  484. err := service.BatchCreatePatientOut(hdPatientOuts, rdb)
  485. if err == nil {
  486. // 第三步:同步成功后,添加同步记录
  487. upload := &sz.DataUpload{
  488. SyncType: 16,
  489. OrgId: org_id,
  490. SyncTime: time.Now().Unix(),
  491. SyncResultType: 1,
  492. CreateTime: time.Now().Unix(),
  493. UpdateTime: time.Now().Unix(),
  494. }
  495. err := service.CreateUploadRecord(upload)
  496. if err != nil {
  497. utils.ErrorLog("%v", err)
  498. }
  499. } else {
  500. //错误处理
  501. }
  502. }
  503. // 同步处方信息
  504. func (c *SyncController) SyncDoctorAdvice(rdb *gorm.DB, org_id int64, hospital_id int64) {
  505. // 第一步:根据机构id获取上次同步时间
  506. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 6)
  507. var sync_time int64
  508. if syncLastInfo.ID > 0 {
  509. sync_time = syncLastInfo.SyncTime
  510. } else {
  511. sync_time = 0
  512. }
  513. var hdDoctorAdvices []*sz.TempHdDoctorsAdvice
  514. dialysisPrescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  515. for _, dp := range dialysisPrescriptions {
  516. advice := &sz.TempHdDoctorsAdvice{
  517. MedicalOrDerId: strconv.FormatInt(dp.ID, 10),
  518. HospitalId: strconv.FormatInt(dp.UserOrgId, 10),
  519. DialysisId: strconv.FormatInt(dp.DialysisOrder.ID, 10),
  520. OrderType: "1",
  521. PatientNk: strconv.FormatInt(dp.PatientId, 10),
  522. DialysisDuration: dp.DialysisDurationHour*60 + dp.DialysisDurationMinute,
  523. BloodVol: int64(math.Floor(dp.BloodFlowVolume + 0/5)),
  524. CreateTime: time.Now(),
  525. Sjscsj: time.Now(),
  526. Xgbz: 0,
  527. }
  528. hdDoctorAdvices = append(hdDoctorAdvices, advice)
  529. }
  530. err := service.BatchCreateDoctorsAdvice(hdDoctorAdvices, rdb)
  531. if err == nil {
  532. // 第三步:同步成功后,添加同步记录
  533. upload := &sz.DataUpload{
  534. SyncType: 6,
  535. OrgId: org_id,
  536. SyncTime: time.Now().Unix(),
  537. SyncResultType: 1,
  538. CreateTime: time.Now().Unix(),
  539. UpdateTime: time.Now().Unix(),
  540. }
  541. err := service.CreateUploadRecord(upload)
  542. if err != nil {
  543. utils.ErrorLog("%v", err)
  544. }
  545. } else {
  546. //错误处理
  547. }
  548. }
  549. //
  550. func (c *SyncController) SyncMiddle(rdb *gorm.DB, org_id int64, hospital_id int64) {
  551. // 第一步:根据机构id获取上次同步时间
  552. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 6)
  553. var sync_time int64
  554. if syncLastInfo.ID > 0 {
  555. sync_time = syncLastInfo.SyncTime
  556. } else {
  557. sync_time = 0
  558. }
  559. var hdMiddle []*sz.TempHdMiddle
  560. monitors, _ := service.FindOrgMonitorRecordData(org_id, sync_time, time.Now().Unix())
  561. for _, item := range monitors {
  562. mid := &sz.TempHdMiddle{
  563. HospitalId: strconv.FormatInt(hospital_id, 10),
  564. DialysisId: strconv.FormatInt(item.DialysisOrder.ID, 10),
  565. MonitorTime: time.Unix(item.OperateTime, 0),
  566. Sbp: int64(math.Floor(item.SystolicBloodPressure + 0/5)),
  567. Dbp: int64(math.Floor(item.DiastolicBloodPressure + 0/5)),
  568. PatientNk: strconv.FormatInt(item.PatientId, 10),
  569. CreateTime: time.Now(),
  570. Sjscsj: time.Now(),
  571. Xgbz: 0,
  572. }
  573. hdMiddle = append(hdMiddle, mid)
  574. }
  575. err := service.BatchCreateMonitor(hdMiddle, rdb)
  576. if err == nil {
  577. // 第三步:同步成功后,添加同步记录
  578. upload := &sz.DataUpload{
  579. SyncType: 6,
  580. OrgId: org_id,
  581. SyncTime: time.Now().Unix(),
  582. SyncResultType: 1,
  583. CreateTime: time.Now().Unix(),
  584. UpdateTime: time.Now().Unix(),
  585. }
  586. err := service.CreateUploadRecord(upload)
  587. if err != nil {
  588. utils.ErrorLog("%v", err)
  589. }
  590. } else {
  591. //错误处理
  592. }
  593. }