sync.go 27KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. "fmt"
  15. )
  16. type SyncController struct {
  17. BaseAPIController
  18. }
  19. func SyncAPIRegisterRouters() {
  20. beego.Router("/sync/upload", &SyncController{}, "get:SyncToSZIC")
  21. }
  22. func (c *SyncController) SyncToSZIC() {
  23. // 第一步:到上报配置表中找到深圳需要上报的机构
  24. sz_province, _ := beego.AppConfig.Int64("sz_province")
  25. sz_city, _ := beego.AppConfig.Int64("sz_city")
  26. configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3)
  27. for _, org := range configs {
  28. // 第二步:跟进配置,创建数据库连接
  29. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  30. orgDb, err := service.CreateDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  31. if err != nil {
  32. utils.ErrorLog("创建数据库连接失败:%v", err)
  33. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  34. return
  35. }
  36. // 第三步:开始同步数据
  37. // 同步医院信息 t_hd_hospital
  38. c.SyncHospital(orgDb, org.OrgId, org.HospitalId, org.InstType, org.DepartmentName)
  39. // 同步水处理器 t_hd_wm
  40. c.SyncWMS(orgDb,org.OrgId, org.HospitalId)
  41. // 同步员工 t_hd_staff
  42. c.SyncStaff(orgDb,org.OrgId, org.HospitalId)
  43. // 同步病人 t_hd_patient
  44. c.SyncPatient(orgDb,org.OrgId, org.HospitalId)
  45. // 同步排班 t_hd_shift
  46. c.SyncShift(orgDb,org.OrgId, org.HospitalId)
  47. // 同步处方 t_hd_ps
  48. c.SyncPs(orgDb,org.OrgId, org.HospitalId)
  49. // 同步处方药品 t_hd_ps
  50. c.SyncPsMedicine(orgDb,org.OrgId, org.HospitalId)
  51. // 同步转院信息 t_hd_patient_out
  52. c.SyncPatientOut(orgDb,org.OrgId, org.HospitalId)
  53. // 同步处方信息 t_hd_doctors_advice
  54. c.SyncDoctorAdvice(orgDb,org.OrgId, org.HospitalId)
  55. // 同步透中信息 t_hd_middle
  56. c.SyncMiddle(orgDb,org.OrgId, org.HospitalId)
  57. // // 同步设备 t_hd_other_machine
  58. c.SyncOtherMachine(orgDb,org.OrgId, org.HospitalId)
  59. // // 同步维修 t_hd_maintain
  60. c.SyncMachineRepair(orgDb,org.OrgId, org.HospitalId)
  61. // // 同步设备 t_hd_dm
  62. c.SyncDM(orgDb,org.OrgId, org.HospitalId)
  63. // // 同步患者透析记录 t_hd_dialysis
  64. c.SyncDialysis(orgDb,org.OrgId, org.HospitalId)
  65. // 第四步:关闭数据库连接
  66. service.CloseDB(orgDb)
  67. }
  68. }
  69. c.ServeSuccessJSON(map[string]interface{}{
  70. "resultList": "12345",
  71. })
  72. return
  73. }
  74. // 同步医院信息
  75. func (c *SyncController) SyncHospital(rdb *gorm.DB, org_id int64, hospital_id string, inst_type int64, department_name string) {
  76. // 第一步:根据机构id获取上次同步时间
  77. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 1)
  78. var sync_time int64
  79. if syncLastInfo.ID > 0 {
  80. sync_time = syncLastInfo.SyncTime
  81. } else {
  82. sync_time = 0
  83. }
  84. // 第二步:跟进上次同步时间找出这个时间段内增加的数据
  85. org, _ := service.FindOrgData(org_id, sync_time)
  86. if org.ID > 0 {
  87. hospital := &sz.TempHdHospital{
  88. HospitalId: hospital_id,
  89. InstType: strconv.FormatInt(inst_type, 10),
  90. DepartMentName: department_name,
  91. AuthorizedBeds: int64(len(org.DeviceNumber)),
  92. CreateTime: time.Now(),
  93. UpdateTime: time.Now(),
  94. Sjscsj: time.Now(),
  95. Xgbz: 0,
  96. }
  97. err := service.CreateOrgRecord(rdb,hospital)
  98. if err == nil {
  99. // 第三步:同步成功后,添加同步记录
  100. upload := &sz.DataUpload{
  101. SyncType: 1,
  102. OrgId: org_id,
  103. SyncTime: time.Now().Unix(),
  104. SyncResultType: 1,
  105. CreateTime: time.Now().Unix(),
  106. UpdateTime: time.Now().Unix(),
  107. }
  108. err := service.CreateUploadRecord(upload)
  109. if err != nil {
  110. utils.ErrorLog("%v", err)
  111. }
  112. }
  113. }
  114. }
  115. // 同步水处理器
  116. func (c *SyncController) SyncWMS(rdb *gorm.DB, org_id int64, hospital_id string) {
  117. // 第一步:根据机构id获取上次同步时间
  118. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 22)
  119. var sync_time int64
  120. if syncLastInfo.ID > 0 {
  121. sync_time = syncLastInfo.SyncTime
  122. } else {
  123. sync_time = 0
  124. }
  125. //同步水处理机
  126. waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  127. var wms []*sz.TempHdWm
  128. for _, item := range waterMachine {
  129. wm := &sz.TempHdWm{
  130. HospitalId: hospital_id,
  131. EquipmentId: strconv.FormatInt(item.ID, 10),
  132. EquipmentBrand: item.ManufactureFactory,
  133. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  134. EnableTime: time.Unix(item.StartDate, 0),
  135. DiscardedTime: time.Unix(item.RubbishDate, 0),
  136. EquipmentType: strconv.FormatInt(item.DeviceType, 10),
  137. CreateTime: time.Now(),
  138. UpdateTime: time.Now(),
  139. Sjscsj: time.Now(),
  140. Xgbz: strconv.FormatInt(0, 10),
  141. }
  142. wms = append(wms, wm)
  143. }
  144. err := service.BatchCreateWMsRecord(wms, rdb)
  145. if err == nil {
  146. // 第三步:同步成功后,添加同步记录
  147. upload := &sz.DataUpload{
  148. SyncType: 22,
  149. OrgId: org_id,
  150. SyncTime: time.Now().Unix(),
  151. SyncResultType: 1,
  152. CreateTime: time.Now().Unix(),
  153. UpdateTime: time.Now().Unix(),
  154. }
  155. err := service.CreateUploadRecord(upload)
  156. if err != nil {
  157. utils.ErrorLog("%v", err)
  158. }
  159. } else {
  160. //错误处理
  161. }
  162. }
  163. // 同步员工
  164. func (c *SyncController) SyncStaff(rdb *gorm.DB, org_id int64, hospital_id string) {
  165. // 第一步:根据机构id获取上次同步时间
  166. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 21)
  167. var sync_time int64
  168. if syncLastInfo.ID > 0 {
  169. sync_time = syncLastInfo.SyncTime
  170. } else {
  171. sync_time = 0
  172. }
  173. //同步员工信息
  174. roles, _ := service.FindOrgRolesData(org_id, sync_time, time.Now().Unix())
  175. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  176. var staffs []*sz.TempHdStaff
  177. for _, role := range roles {
  178. var title string
  179. switch role.UserType {
  180. case 2:
  181. title = strconv.FormatInt(1, 10)
  182. break
  183. case 3:
  184. title = strconv.FormatInt(2, 10)
  185. break
  186. }
  187. staff := &sz.TempHdStaff{
  188. HospitalId: hospital_id,
  189. StaffId: strconv.FormatInt(role.ID, 10),
  190. StaffName: role.UserName,
  191. Position: title,
  192. PermanentType: strconv.FormatInt(1, 10),
  193. CreateTime: time.Now(),
  194. UpdateTime: time.Now(),
  195. Sjscsj: time.Now(),
  196. Xgbz: strconv.FormatInt(0, 10),
  197. }
  198. staffs = append(staffs, staff)
  199. }
  200. err := service.BatchCreateStaffRecord(staffs, rdb)
  201. if err == nil {
  202. // 第三步:同步成功后,添加同步记录
  203. upload := &sz.DataUpload{
  204. SyncType: 21,
  205. OrgId: org_id,
  206. SyncTime: time.Now().Unix(),
  207. SyncResultType: 1,
  208. CreateTime: time.Now().Unix(),
  209. UpdateTime: time.Now().Unix(),
  210. }
  211. err := service.CreateUploadRecord(upload)
  212. if err != nil {
  213. utils.ErrorLog("%v", err)
  214. }
  215. } else {
  216. //错误处理
  217. }
  218. }
  219. // 同步病人
  220. func (c *SyncController) SyncPatient(rdb *gorm.DB, org_id int64, hospital_id string) {
  221. // 第一步:根据机构id获取上次同步时间
  222. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 15)
  223. var sync_time int64
  224. if syncLastInfo.ID > 0 {
  225. sync_time = syncLastInfo.SyncTime
  226. } else {
  227. sync_time = 0
  228. }
  229. //同步员工信息
  230. patients, _ := service.FindOrgPatientData(org_id, sync_time, time.Now().Unix())
  231. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  232. var hdPatients []*sz.TempHdPatient
  233. for _, patient := range patients {
  234. idcard_year, _ := strconv.Atoi(Substr(patient.IdCardNo, 6, 4)) // 年
  235. idcard_mo, _ := strconv.Atoi(Substr(patient.IdCardNo, 10, 2)) // 月
  236. idcard_day, _ := strconv.Atoi(Substr(patient.IdCardNo, 12, 2)) // 日
  237. date := strconv.Itoa(idcard_year)
  238. if idcard_mo < 10 {
  239. date = date + "-0" + strconv.Itoa(idcard_mo)
  240. } else {
  241. date = date + "-" + strconv.Itoa(idcard_mo)
  242. }
  243. if idcard_day < 10 {
  244. date = date + "-0" + strconv.Itoa(idcard_day)
  245. } else {
  246. date = date + "-" + strconv.Itoa(idcard_day)
  247. }
  248. local, _ := time.LoadLocation("Local")
  249. birthday, _ := time.ParseInLocation("2006-01-02", date, local)
  250. fmt.Println(birthday)
  251. var isCKD int
  252. value := strings.Index(patient.Diagnose, "慢性肾脏病")
  253. value2 := strings.Index(patient.Diagnose, "CKD")
  254. value3 := strings.Index(patient.Diagnose, "慢性肾衰竭")
  255. if value != -1 || value2 != -1 || value3 != -1 {
  256. isCKD = 1
  257. } else {
  258. isCKD = 0
  259. }
  260. p := &sz.TempHdPatient{
  261. HospitalId: hospital_id,
  262. PatientNk: strconv.FormatInt(patient.ID, 10),
  263. CardNo: "000000000000000000",
  264. CardType: "01",
  265. IdNo: patient.IdCardNo,
  266. IdType: "01",
  267. PatientName: patient.Name,
  268. Gender: strconv.FormatInt(patient.Gender, 10),
  269. BornDate: birthday,
  270. DiagnosisSummary: patient.Diagnose,
  271. IsCrf: strconv.Itoa(isCKD),
  272. CreatedTime: time.Now(),
  273. UpdateTime: time.Now(),
  274. Sjscsj: time.Now(),
  275. Xgbz: strconv.FormatInt(0, 10),
  276. }
  277. if patient.FirstDialysisDate != 0 {
  278. p.DialysisStartTime = time.Unix(patient.FirstDialysisDate, 0)
  279. }
  280. if patient.HospitalFirstDialysisDate != 0 {
  281. p.LocalStartTime = time.Unix(patient.HospitalFirstDialysisDate, 0)
  282. }
  283. hdPatients = append(hdPatients, p)
  284. }
  285. err := service.BatchCreatePatinet(hdPatients, rdb)
  286. if err == nil {
  287. // 第三步:同步成功后,添加同步记录
  288. upload := &sz.DataUpload{
  289. SyncType: 15,
  290. OrgId: org_id,
  291. SyncTime: time.Now().Unix(),
  292. SyncResultType: 1,
  293. CreateTime: time.Now().Unix(),
  294. UpdateTime: time.Now().Unix(),
  295. }
  296. err := service.CreateUploadRecord(upload)
  297. if err != nil {
  298. utils.ErrorLog("%v", err)
  299. }
  300. } else {
  301. //错误处理
  302. }
  303. }
  304. func Substr(str string, start, length int) string {
  305. rs := []rune(str)
  306. rl := len(rs)
  307. end := 0
  308. if start < 0 {
  309. start = rl - 1 + start
  310. }
  311. end = start + length
  312. if start > end {
  313. start, end = end, start
  314. }
  315. if start < 0 {
  316. start = 0
  317. }
  318. if start > rl {
  319. start = rl
  320. }
  321. if end < 0 {
  322. end = 0
  323. }
  324. if end > rl {
  325. end = rl
  326. }
  327. return string(rs[start:end])
  328. }
  329. // 同步排班
  330. func (c *SyncController) SyncShift(rdb *gorm.DB, org_id int64, hospital_id string) {
  331. // 第一步:根据机构id获取上次同步时间
  332. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 19)
  333. var sync_time int64
  334. if syncLastInfo.ID > 0 {
  335. sync_time = syncLastInfo.SyncTime
  336. } else {
  337. sync_time = 0
  338. }
  339. var hdShift []*sz.TempHdShift
  340. schs, _ := service.FindOrgScheduleData(org_id, sync_time, time.Now().Unix())
  341. for _, sch := range schs {
  342. shift := &sz.TempHdShift{
  343. PsId: strconv.FormatInt(sch.ID, 10),
  344. HospitalId: strconv.FormatInt(sch.UserOrgId, 10),
  345. PatientNk: strconv.FormatInt(sch.PatientId, 10),
  346. ScheduleDate: time.Unix(sch.ScheduleDate, 0),
  347. ShiftType: strconv.FormatInt(sch.ScheduleType, 10),
  348. SickbedNo: sch.DeviceNumber.Number,
  349. ScheduleStatus: "1",
  350. CreateTime: time.Now(),
  351. Sjscsj: time.Now(),
  352. Xgbz: 0,
  353. }
  354. hdShift = append(hdShift, shift)
  355. }
  356. err := service.BatchCreateSchedual(hdShift, rdb)
  357. if err == nil {
  358. // 第三步:同步成功后,添加同步记录
  359. upload := &sz.DataUpload{
  360. SyncType: 19,
  361. OrgId: org_id,
  362. SyncTime: time.Now().Unix(),
  363. SyncResultType: 1,
  364. CreateTime: time.Now().Unix(),
  365. UpdateTime: time.Now().Unix(),
  366. }
  367. err := service.CreateUploadRecord(upload)
  368. if err != nil {
  369. utils.ErrorLog("%v", err)
  370. }
  371. } else {
  372. //错误处理
  373. }
  374. }
  375. // 同步处方
  376. func (c *SyncController) SyncPs(rdb *gorm.DB, org_id int64, hospital_id string) {
  377. // 第一步:根据机构id获取上次同步时间
  378. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 17)
  379. var sync_time int64
  380. if syncLastInfo.ID > 0 {
  381. sync_time = syncLastInfo.SyncTime
  382. } else {
  383. sync_time = 0
  384. }
  385. var hdPrescription []*sz.TempHdPs
  386. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  387. for _, item := range prescriptions {
  388. ps := &sz.TempHdPs{
  389. PrescribeId: strconv.FormatInt(item.ID, 10),
  390. HospitalId: hospital_id,
  391. PatientNk: strconv.FormatInt(item.PatientId, 10),
  392. K: item.Kalium,
  393. Ca: item.Calcium,
  394. Na: item.Sodium,
  395. CreateTime: time.Now(),
  396. Sjscsj: time.Now(),
  397. Xgbz: 0,
  398. PrescribeTime: time.Unix(item.RecordDate, 0),
  399. }
  400. switch item.ModeId {
  401. case 1:
  402. ps.DialysisFrequency = item.AssessmentBeforeDislysis.DialysisCount
  403. ps.FrequencyUnit = 1
  404. ps.DialysisDuration = item.DialysisDuration
  405. ps.Hdf = "0"
  406. ps.Hp = "0"
  407. break
  408. case 2:
  409. ps.Hdf = "1"
  410. ps.Hp = "0"
  411. ps.HdfFrequency = item.AssessmentBeforeDislysis.DialysisCount
  412. ps.HdfFrequencyUnit = 1
  413. ps.HpDuration = item.DialysisDuration
  414. break
  415. case 4:
  416. ps.Hdf = "0"
  417. ps.Hp = "1"
  418. ps.HpFrequency = item.AssessmentBeforeDislysis.DialysisCount
  419. ps.HpFrequencyUnit = 1
  420. ps.HpDuration = item.DialysisDuration
  421. break
  422. }
  423. hdPrescription = append(hdPrescription, ps)
  424. }
  425. err := service.BatchCreatePs(hdPrescription, rdb)
  426. if err == nil {
  427. // 第三步:同步成功后,添加同步记录
  428. upload := &sz.DataUpload{
  429. SyncType: 17,
  430. OrgId: org_id,
  431. SyncTime: time.Now().Unix(),
  432. SyncResultType: 1,
  433. CreateTime: time.Now().Unix(),
  434. UpdateTime: time.Now().Unix(),
  435. }
  436. err := service.CreateUploadRecord(upload)
  437. if err != nil {
  438. utils.ErrorLog("%v", err)
  439. }
  440. } else {
  441. //错误处理
  442. }
  443. }
  444. // 同步处方药物
  445. func (c *SyncController) SyncPsMedicine(rdb *gorm.DB, org_id int64, hospital_id string) {
  446. // 第一步:根据机构id获取上次同步时间
  447. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 18)
  448. var sync_time int64
  449. if syncLastInfo.ID > 0 {
  450. sync_time = syncLastInfo.SyncTime
  451. } else {
  452. sync_time = 0
  453. }
  454. var tempPsmedicine []*sz.TempHdPsMedicine
  455. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  456. for _, item := range prescriptions {
  457. psm := &sz.TempHdPsMedicine{
  458. PrescribeId: strconv.FormatInt(item.ID, 10),
  459. HospitalId: strconv.FormatInt(item.UserOrgId, 10),
  460. MedicineTypeId: "2",
  461. CreatedTime: time.Now(),
  462. Sjscsj: time.Now(),
  463. Xgbz: 0,
  464. }
  465. switch item.Anticoagulant {
  466. case 1:
  467. psm.MedicineId = "3"
  468. break
  469. case 2:
  470. psm.MedicineId = "1"
  471. break
  472. case 3:
  473. psm.MedicineId = "2"
  474. break
  475. case 4:
  476. psm.MedicineId = "5"
  477. break
  478. case 5:
  479. psm.MedicineId = "4"
  480. break
  481. }
  482. tempPsmedicine = append(tempPsmedicine, psm)
  483. }
  484. err := service.BatchCreatePsm(tempPsmedicine, rdb)
  485. if err == nil {
  486. // 第三步:同步成功后,添加同步记录
  487. upload := &sz.DataUpload{
  488. SyncType: 18,
  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) SyncPatientOut(rdb *gorm.DB, org_id int64, hospital_id string) {
  505. // 第一步:根据机构id获取上次同步时间
  506. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 16)
  507. var sync_time int64
  508. if syncLastInfo.ID > 0 {
  509. sync_time = syncLastInfo.SyncTime
  510. } else {
  511. sync_time = 0
  512. }
  513. var hdPatientOuts []*sz.TempHdPatientOut
  514. patients, _ := service.FindOrgPatientOutData(org_id, sync_time, time.Now().Unix())
  515. //
  516. for _, patient := range patients {
  517. p := &sz.TempHdPatientOut{
  518. HospitalId: strconv.FormatInt(patient.UserOrgId, 10),
  519. PatientNk: strconv.FormatInt(patient.ID, 10),
  520. SequelaeType: "99",
  521. CreateTime: time.Now(),
  522. Sjscsj: time.Now(),
  523. Xgbz: 0,
  524. }
  525. if len(patient.PatientLapseto) > 0 {
  526. p.SequelaeDate = time.Unix(patient.PatientLapseto[len(patient.PatientLapseto) - 1].LapsetoTime, 0)
  527. }
  528. hdPatientOuts = append(hdPatientOuts, p)
  529. }
  530. err := service.BatchCreatePatientOut(hdPatientOuts, rdb)
  531. if err == nil {
  532. // 第三步:同步成功后,添加同步记录
  533. upload := &sz.DataUpload{
  534. SyncType: 16,
  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) SyncDoctorAdvice(rdb *gorm.DB, org_id int64, hospital_id string) {
  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 hdDoctorAdvices []*sz.TempHdDoctorsAdvice
  560. dialysisPrescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  561. for _, dp := range dialysisPrescriptions {
  562. advice := &sz.TempHdDoctorsAdvice{
  563. MedicalOrDerId: strconv.FormatInt(dp.ID, 10),
  564. HospitalId: strconv.FormatInt(dp.UserOrgId, 10),
  565. DialysisId: strconv.FormatInt(dp.DialysisOrder.ID, 10),
  566. OrderType: "1",
  567. PatientNk: strconv.FormatInt(dp.PatientId, 10),
  568. DialysisDuration: dp.DialysisDurationHour*60 + dp.DialysisDurationMinute,
  569. BloodVol: int64(math.Floor(dp.BloodFlowVolume + 0/5)),
  570. CreateTime: time.Now(),
  571. Sjscsj: time.Now(),
  572. Xgbz: 0,
  573. }
  574. hdDoctorAdvices = append(hdDoctorAdvices, advice)
  575. }
  576. err := service.BatchCreateDoctorsAdvice(hdDoctorAdvices, rdb)
  577. if err == nil {
  578. // 第三步:同步成功后,添加同步记录
  579. upload := &sz.DataUpload{
  580. SyncType: 6,
  581. OrgId: org_id,
  582. SyncTime: time.Now().Unix(),
  583. SyncResultType: 1,
  584. CreateTime: time.Now().Unix(),
  585. UpdateTime: time.Now().Unix(),
  586. }
  587. err := service.CreateUploadRecord(upload)
  588. if err != nil {
  589. utils.ErrorLog("%v", err)
  590. }
  591. } else {
  592. //错误处理
  593. }
  594. }
  595. //
  596. func (c *SyncController) SyncMiddle(rdb *gorm.DB, org_id int64, hospital_id string) {
  597. // 第一步:根据机构id获取上次同步时间
  598. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 36)
  599. var sync_time int64
  600. if syncLastInfo.ID > 0 {
  601. sync_time = syncLastInfo.SyncTime
  602. } else {
  603. sync_time = 0
  604. }
  605. var hdMiddle []*sz.TempHdMiddle
  606. monitors, _ := service.FindOrgMonitorRecordData(org_id, sync_time, time.Now().Unix())
  607. for _, item := range monitors {
  608. mid := &sz.TempHdMiddle{
  609. HospitalId: hospital_id,
  610. DialysisId: strconv.FormatInt(item.DialysisOrder.ID, 10),
  611. MonitorTime: time.Unix(item.OperateTime, 0),
  612. Sbp: int64(math.Floor(item.SystolicBloodPressure + 0/5)),
  613. Dbp: int64(math.Floor(item.DiastolicBloodPressure + 0/5)),
  614. PatientNk: strconv.FormatInt(item.PatientId, 10),
  615. CreateTime: time.Now(),
  616. Sjscsj: time.Now(),
  617. Xgbz: 0,
  618. }
  619. hdMiddle = append(hdMiddle, mid)
  620. }
  621. err := service.BatchCreateMonitor(hdMiddle, rdb)
  622. if err == nil {
  623. // 第三步:同步成功后,添加同步记录
  624. upload := &sz.DataUpload{
  625. SyncType: 36,
  626. OrgId: org_id,
  627. SyncTime: time.Now().Unix(),
  628. SyncResultType: 1,
  629. CreateTime: time.Now().Unix(),
  630. UpdateTime: time.Now().Unix(),
  631. }
  632. err := service.CreateUploadRecord(upload)
  633. if err != nil {
  634. utils.ErrorLog("%v", err)
  635. }
  636. } else {
  637. //错误处理
  638. }
  639. }
  640. // 同步设备
  641. func (c *SyncController) SyncOtherMachine(rdb *gorm.DB, org_id int64, hospital_id string) {
  642. // 第一步:根据机构id获取上次同步时间
  643. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 13)
  644. var sync_time int64
  645. if syncLastInfo.ID > 0 {
  646. sync_time = syncLastInfo.SyncTime
  647. } else {
  648. sync_time = 0
  649. }
  650. //同步设备
  651. waterMachine, _ := service.FindOrgOtherMachineData(org_id, sync_time, time.Now().Unix())
  652. var wms []*sz.TempHdOtherMachine
  653. for _, item := range waterMachine {
  654. wm := &sz.TempHdOtherMachine{
  655. HospitalId: hospital_id,
  656. EquipmentId: strconv.FormatInt(item.ID, 10),
  657. EquipmentBrand: item.ManufactureFactory,
  658. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  659. EnableTime: time.Unix(item.StartDate, 0),
  660. DiscardedTime: time.Unix(item.RubbishDate, 0),
  661. CreateTime: time.Now(),
  662. UpdateTime: time.Now(),
  663. Sjscsj: time.Now(),
  664. Xgbz: strconv.FormatInt(0, 10),
  665. }
  666. switch item.MachineStatus {
  667. case 1:
  668. wm.Status = "1"
  669. break
  670. case 4:
  671. wm.Status = "3"
  672. break
  673. }
  674. wms = append(wms, wm)
  675. }
  676. err := service.BatchCreateOtherMachineRecord(wms, rdb)
  677. if err == nil {
  678. // 第三步:同步成功后,添加同步记录
  679. upload := &sz.DataUpload{
  680. SyncType: 13,
  681. OrgId: org_id,
  682. SyncTime: time.Now().Unix(),
  683. SyncResultType: 1,
  684. CreateTime: time.Now().Unix(),
  685. UpdateTime: time.Now().Unix(),
  686. }
  687. err := service.CreateUploadRecord(upload)
  688. if err != nil {
  689. utils.ErrorLog("%v", err)
  690. }
  691. } else {
  692. //错误处理
  693. }
  694. }
  695. // 同步维修
  696. func (c *SyncController) SyncMachineRepair(rdb *gorm.DB, org_id int64, hospital_id string) {
  697. // 第一步:根据机构id获取上次同步时间
  698. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 11)
  699. var sync_time int64
  700. if syncLastInfo.ID > 0 {
  701. sync_time = syncLastInfo.SyncTime
  702. } else {
  703. sync_time = 0
  704. }
  705. //同步设备
  706. hdMaintain, _ := service.FindOrgMachineRepairData(org_id, sync_time, time.Now().Unix())
  707. var maintain []*sz.TempHdMaintain
  708. for _, item := range hdMaintain {
  709. mt := &sz.TempHdMaintain{
  710. HospitalId: hospital_id,
  711. EquipmentId: strconv.FormatInt(item.VMDeviceAddmacher.ID, 10),
  712. RepairTime: time.Unix(item.GuaranteeDate, 0),
  713. CreateTime: time.Now(),
  714. Sjscsj: time.Now(),
  715. Xgbz: strconv.FormatInt(0, 10),
  716. }
  717. switch item.VMDeviceAddmacher.DeviceType {
  718. case 1:
  719. mt.EquipmentType = "11"
  720. break
  721. case 2:
  722. mt.EquipmentType = "21"
  723. break
  724. case 3:
  725. mt.EquipmentType = "99"
  726. break
  727. }
  728. maintain = append(maintain, mt)
  729. }
  730. err := service.BatchCreateMainTain(maintain, rdb)
  731. if err == nil {
  732. // 第三步:同步成功后,添加同步记录
  733. upload := &sz.DataUpload{
  734. SyncType: 11,
  735. OrgId: org_id,
  736. SyncTime: time.Now().Unix(),
  737. SyncResultType: 1,
  738. CreateTime: time.Now().Unix(),
  739. UpdateTime: time.Now().Unix(),
  740. }
  741. err := service.CreateUploadRecord(upload)
  742. if err != nil {
  743. utils.ErrorLog("%v", err)
  744. }
  745. } else {
  746. //错误处理
  747. }
  748. }
  749. // 同步设备
  750. func (c *SyncController) SyncDM(rdb *gorm.DB, org_id int64, hospital_id string) {
  751. // 第一步:根据机构id获取上次同步时间
  752. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 5)
  753. var sync_time int64
  754. if syncLastInfo.ID > 0 {
  755. sync_time = syncLastInfo.SyncTime
  756. } else {
  757. sync_time = 0
  758. }
  759. //同步设备
  760. dm, _ := service.FindOrgMachineData(org_id, sync_time, time.Now().Unix())
  761. var hdms []*sz.TempHdDm
  762. for _, item := range dm {
  763. hdm := &sz.TempHdDm{
  764. HospitalId: hospital_id,
  765. EquipmentId: strconv.FormatInt(item.ID, 10),
  766. SickbedNo: item.DeviceNumber.Number,
  767. EquipmentBrand: item.ManufactureFactory,
  768. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  769. EnableTime: time.Unix(item.StartDate, 0),
  770. DiscardedTime: time.Unix(item.RubbishDate, 0),
  771. CreateTime: time.Now(),
  772. UpdateTime: time.Now(),
  773. Sjscsj: time.Now(),
  774. Xgbz: strconv.FormatInt(0, 10),
  775. }
  776. switch item.MachineStatus {
  777. case 1:
  778. hdm.Status = "1"
  779. break
  780. case 4:
  781. hdm.Status = "3"
  782. break
  783. }
  784. switch item.DeviceType {
  785. case 1:
  786. hdm.EquipmentType = "11"
  787. break
  788. case 2:
  789. hdm.EquipmentType = "98"
  790. break
  791. case 3:
  792. hdm.EquipmentType = "99"
  793. break
  794. }
  795. hdms = append(hdms, hdm)
  796. }
  797. err := service.BatchCreateHDMRecord(hdms, rdb)
  798. if err == nil {
  799. // 第三步:同步成功后,添加同步记录
  800. upload := &sz.DataUpload{
  801. SyncType: 5,
  802. OrgId: org_id,
  803. SyncTime: time.Now().Unix(),
  804. SyncResultType: 1,
  805. CreateTime: time.Now().Unix(),
  806. UpdateTime: time.Now().Unix(),
  807. }
  808. err := service.CreateUploadRecord(upload)
  809. if err != nil {
  810. utils.ErrorLog("%v", err)
  811. }
  812. } else {
  813. //错误处理
  814. }
  815. }
  816. // 同步患者透析记录
  817. func (c *SyncController) SyncDialysis(rdb *gorm.DB, org_id int64, hospital_id string) {
  818. // 第一步:根据机构id获取上次同步时间
  819. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 2)
  820. var sync_time int64
  821. if syncLastInfo.ID > 0 {
  822. sync_time = syncLastInfo.SyncTime
  823. } else {
  824. sync_time = 0
  825. }
  826. var tempDialysis []*sz.TempHdDialysis
  827. dialysisData, _ := service.FindOrgDialysisData(org_id, sync_time, time.Now().Unix())
  828. for _, item := range dialysisData {
  829. psm := &sz.TempHdDialysis{
  830. DialysisId: strconv.FormatInt(item.ID, 10),
  831. HospitalId: strconv.FormatInt(item.UserOrgId, 10),
  832. PatientNk: strconv.FormatInt(item.PatientId, 10),
  833. PsId: strconv.FormatInt(item.Schedule.ID, 10),
  834. DialysisDate: time.Unix(item.DialysisDate, 0),
  835. SickbedNo: item.DeviceNumber.Number,
  836. DivisionId: strconv.FormatInt(item.Schedule.PartitionId,10),
  837. EquipmentId: strconv.FormatInt(item.DeviceNumber.ID, 10),
  838. TotalTreatDuration: item.AssessmentAfterDislysis.ActualTreatmentHour * 60 + item.AssessmentAfterDislysis.ActualTreatmentMinute,
  839. BeforeSbp: int64(item.AssessmentBeforeDislysis.SystolicBloodPressure),
  840. BeforeDbp: int64(item.AssessmentBeforeDislysis.DiastolicBloodPressure),
  841. AfterSbp: int64(item.AssessmentAfterDislysis.SystolicBloodPressure),
  842. AfterDbp: int64(item.AssessmentAfterDislysis.DiastolicBloodPressure),
  843. StartTime: time.Unix(item.StartTime, 0),
  844. EndTime: time.Unix(item.EndTime, 0),
  845. CreateTime: time.Unix(item.CreatedTime, 0),
  846. Sjscsj: time.Now(),
  847. Xgbz: 0,
  848. }
  849. if item.AssessmentAfterDislysis.ActualUltrafiltration < 10 {
  850. psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration * 1000)
  851. } else {
  852. psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration)
  853. }
  854. if item.VMDialysisPrescription.TargetUltrafiltration < 10 {
  855. psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration * 1000)
  856. } else {
  857. psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration)
  858. }
  859. if item.AssessmentAfterDislysis.ActualDisplacement < 10 {
  860. psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement * 1000)
  861. } else {
  862. psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement)
  863. }
  864. tempDialysis = append(tempDialysis, psm)
  865. }
  866. err := service.BatchCreateDialysis(tempDialysis, rdb)
  867. if err == nil {
  868. // 第三步:同步成功后,添加同步记录
  869. upload := &sz.DataUpload{
  870. SyncType: 2,
  871. OrgId: org_id,
  872. SyncTime: time.Now().Unix(),
  873. SyncResultType: 1,
  874. CreateTime: time.Now().Unix(),
  875. UpdateTime: time.Now().Unix(),
  876. }
  877. err := service.CreateUploadRecord(upload)
  878. if err != nil {
  879. utils.ErrorLog("%v", err)
  880. }
  881. } else {
  882. //错误处理
  883. }
  884. }