package controllers import ( "Data_Upload_Api/enums" "Data_Upload_Api/models/sz" "Data_Upload_Api/service" "Data_Upload_Api/utils" "github.com/astaxie/beego" "github.com/jinzhu/gorm" "math" "strconv" "strings" _ "strings" "time" "fmt" ) type SyncController struct { BaseAPIController } func SyncAPIRegisterRouters() { beego.Router("/sync/upload", &SyncController{}, "get:SyncToSZIC") } func (c *SyncController) SyncToSZIC() { // 第一步:到上报配置表中找到深圳需要上报的机构 sz_province, _ := beego.AppConfig.Int64("sz_province") sz_city, _ := beego.AppConfig.Int64("sz_city") configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3) for _, org := range configs { // 第二步:跟进配置,创建数据库连接 if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 { orgDb, err := service.CreateDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName) if err != nil { utils.ErrorLog("创建数据库连接失败:%v", err) c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } // 第三步:开始同步数据 // 同步医院信息 t_hd_hospital c.SyncHospital(orgDb, org.OrgId, org.HospitalId, org.InstType, org.DepartmentName) // 同步水处理器 t_hd_wm c.SyncWMS(orgDb,org.OrgId, org.HospitalId) // 同步员工 t_hd_staff c.SyncStaff(orgDb,org.OrgId, org.HospitalId) // 同步病人 t_hd_patient c.SyncPatient(orgDb,org.OrgId, org.HospitalId) // 同步排班 t_hd_shift c.SyncShift(orgDb,org.OrgId, org.HospitalId) // 同步处方 t_hd_ps c.SyncPs(orgDb,org.OrgId, org.HospitalId) // 同步处方药品 t_hd_ps c.SyncPsMedicine(orgDb,org.OrgId, org.HospitalId) // 同步转院信息 t_hd_patient_out c.SyncPatientOut(orgDb,org.OrgId, org.HospitalId) // 同步处方信息 t_hd_doctors_advice c.SyncDoctorAdvice(orgDb,org.OrgId, org.HospitalId) // 同步透中信息 t_hd_middle c.SyncMiddle(orgDb,org.OrgId, org.HospitalId) // // 同步设备 t_hd_other_machine c.SyncOtherMachine(orgDb,org.OrgId, org.HospitalId) // // 同步维修 t_hd_maintain c.SyncMachineRepair(orgDb,org.OrgId, org.HospitalId) // // 同步设备 t_hd_dm c.SyncDM(orgDb,org.OrgId, org.HospitalId) // // 同步患者透析记录 t_hd_dialysis c.SyncDialysis(orgDb,org.OrgId, org.HospitalId) // 第四步:关闭数据库连接 service.CloseDB(orgDb) } } c.ServeSuccessJSON(map[string]interface{}{ "resultList": "12345", }) return } // 同步医院信息 func (c *SyncController) SyncHospital(rdb *gorm.DB, org_id int64, hospital_id string, inst_type int64, department_name string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 1) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } // 第二步:跟进上次同步时间找出这个时间段内增加的数据 org, _ := service.FindOrgData(org_id, sync_time) if org.ID > 0 { hospital := &sz.TempHdHospital{ HospitalId: hospital_id, InstType: strconv.FormatInt(inst_type, 10), DepartMentName: department_name, AuthorizedBeds: int64(len(org.DeviceNumber)), CreateTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } err := service.CreateOrgRecord(rdb,hospital) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 1, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } } } // 同步水处理器 func (c *SyncController) SyncWMS(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 22) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步水处理机 waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix()) var wms []*sz.TempHdWm for _, item := range waterMachine { wm := &sz.TempHdWm{ HospitalId: hospital_id, EquipmentId: strconv.FormatInt(item.ID, 10), EquipmentBrand: item.ManufactureFactory, EquipmentModel: strconv.FormatInt(item.UnitType, 10), EnableTime: time.Unix(item.StartDate, 0), DiscardedTime: time.Unix(item.RubbishDate, 0), EquipmentType: strconv.FormatInt(item.DeviceType, 10), CreateTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } wms = append(wms, wm) } err := service.BatchCreateWMsRecord(wms, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 22, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步员工 func (c *SyncController) SyncStaff(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 21) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步员工信息 roles, _ := service.FindOrgRolesData(org_id, sync_time, time.Now().Unix()) //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix()) var staffs []*sz.TempHdStaff for _, role := range roles { var title string switch role.UserType { case 2: title = strconv.FormatInt(1, 10) break case 3: title = strconv.FormatInt(2, 10) break } staff := &sz.TempHdStaff{ HospitalId: hospital_id, StaffId: strconv.FormatInt(role.ID, 10), StaffName: role.UserName, Position: title, PermanentType: strconv.FormatInt(1, 10), CreateTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } staffs = append(staffs, staff) } err := service.BatchCreateStaffRecord(staffs, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 21, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步病人 func (c *SyncController) SyncPatient(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 15) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步员工信息 patients, _ := service.FindOrgPatientData(org_id, sync_time, time.Now().Unix()) //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix()) var hdPatients []*sz.TempHdPatient for _, patient := range patients { idcard_year, _ := strconv.Atoi(Substr(patient.IdCardNo, 6, 4)) // 年 idcard_mo, _ := strconv.Atoi(Substr(patient.IdCardNo, 10, 2)) // 月 idcard_day, _ := strconv.Atoi(Substr(patient.IdCardNo, 12, 2)) // 日 date := strconv.Itoa(idcard_year) if idcard_mo < 10 { date = date + "-0" + strconv.Itoa(idcard_mo) } else { date = date + "-" + strconv.Itoa(idcard_mo) } if idcard_day < 10 { date = date + "-0" + strconv.Itoa(idcard_day) } else { date = date + "-" + strconv.Itoa(idcard_day) } local, _ := time.LoadLocation("Local") birthday, _ := time.ParseInLocation("2006-01-02", date, local) fmt.Println(birthday) var isCKD int value := strings.Index(patient.Diagnose, "慢性肾脏病") value2 := strings.Index(patient.Diagnose, "CKD") value3 := strings.Index(patient.Diagnose, "慢性肾衰竭") if value != -1 || value2 != -1 || value3 != -1 { isCKD = 1 } else { isCKD = 0 } p := &sz.TempHdPatient{ HospitalId: hospital_id, PatientNk: strconv.FormatInt(patient.ID, 10), CardNo: "000000000000000000", CardType: "01", IdNo: patient.IdCardNo, IdType: "01", PatientName: patient.Name, Gender: strconv.FormatInt(patient.Gender, 10), BornDate: birthday, DiagnosisSummary: patient.Diagnose, IsCrf: strconv.Itoa(isCKD), CreatedTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } if patient.FirstDialysisDate != 0 { p.DialysisStartTime = time.Unix(patient.FirstDialysisDate, 0) } if patient.HospitalFirstDialysisDate != 0 { p.LocalStartTime = time.Unix(patient.HospitalFirstDialysisDate, 0) } hdPatients = append(hdPatients, p) } err := service.BatchCreatePatinet(hdPatients, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 15, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } func Substr(str string, start, length int) string { rs := []rune(str) rl := len(rs) end := 0 if start < 0 { start = rl - 1 + start } end = start + length if start > end { start, end = end, start } if start < 0 { start = 0 } if start > rl { start = rl } if end < 0 { end = 0 } if end > rl { end = rl } return string(rs[start:end]) } // 同步排班 func (c *SyncController) SyncShift(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 19) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var hdShift []*sz.TempHdShift schs, _ := service.FindOrgScheduleData(org_id, sync_time, time.Now().Unix()) for _, sch := range schs { shift := &sz.TempHdShift{ PsId: strconv.FormatInt(sch.ID, 10), HospitalId: strconv.FormatInt(sch.UserOrgId, 10), PatientNk: strconv.FormatInt(sch.PatientId, 10), ScheduleDate: time.Unix(sch.ScheduleDate, 0), ShiftType: strconv.FormatInt(sch.ScheduleType, 10), SickbedNo: sch.DeviceNumber.Number, ScheduleStatus: "1", CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } hdShift = append(hdShift, shift) } err := service.BatchCreateSchedual(hdShift, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 19, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步处方 func (c *SyncController) SyncPs(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 17) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var hdPrescription []*sz.TempHdPs prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix()) for _, item := range prescriptions { ps := &sz.TempHdPs{ PrescribeId: strconv.FormatInt(item.ID, 10), HospitalId: hospital_id, PatientNk: strconv.FormatInt(item.PatientId, 10), K: item.Kalium, Ca: item.Calcium, Na: item.Sodium, CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, PrescribeTime: time.Unix(item.RecordDate, 0), } switch item.ModeId { case 1: ps.DialysisFrequency = item.AssessmentBeforeDislysis.DialysisCount ps.FrequencyUnit = 1 ps.DialysisDuration = item.DialysisDuration ps.Hdf = "0" ps.Hp = "0" break case 2: ps.Hdf = "1" ps.Hp = "0" ps.HdfFrequency = item.AssessmentBeforeDislysis.DialysisCount ps.HdfFrequencyUnit = 1 ps.HpDuration = item.DialysisDuration break case 4: ps.Hdf = "0" ps.Hp = "1" ps.HpFrequency = item.AssessmentBeforeDislysis.DialysisCount ps.HpFrequencyUnit = 1 ps.HpDuration = item.DialysisDuration break } hdPrescription = append(hdPrescription, ps) } err := service.BatchCreatePs(hdPrescription, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 17, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步处方药物 func (c *SyncController) SyncPsMedicine(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 18) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var tempPsmedicine []*sz.TempHdPsMedicine prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix()) for _, item := range prescriptions { psm := &sz.TempHdPsMedicine{ PrescribeId: strconv.FormatInt(item.ID, 10), HospitalId: strconv.FormatInt(item.UserOrgId, 10), MedicineTypeId: "2", CreatedTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } switch item.Anticoagulant { case 1: psm.MedicineId = "3" break case 2: psm.MedicineId = "1" break case 3: psm.MedicineId = "2" break case 4: psm.MedicineId = "5" break case 5: psm.MedicineId = "4" break } tempPsmedicine = append(tempPsmedicine, psm) } err := service.BatchCreatePsm(tempPsmedicine, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 18, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步转院信息 func (c *SyncController) SyncPatientOut(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 16) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var hdPatientOuts []*sz.TempHdPatientOut patients, _ := service.FindOrgPatientOutData(org_id, sync_time, time.Now().Unix()) // for _, patient := range patients { p := &sz.TempHdPatientOut{ HospitalId: strconv.FormatInt(patient.UserOrgId, 10), PatientNk: strconv.FormatInt(patient.ID, 10), SequelaeType: "99", CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } if len(patient.PatientLapseto) > 0 { p.SequelaeDate = time.Unix(patient.PatientLapseto[len(patient.PatientLapseto) - 1].LapsetoTime, 0) } hdPatientOuts = append(hdPatientOuts, p) } err := service.BatchCreatePatientOut(hdPatientOuts, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 16, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步处方信息 func (c *SyncController) SyncDoctorAdvice(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 6) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var hdDoctorAdvices []*sz.TempHdDoctorsAdvice dialysisPrescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix()) for _, dp := range dialysisPrescriptions { advice := &sz.TempHdDoctorsAdvice{ MedicalOrDerId: strconv.FormatInt(dp.ID, 10), HospitalId: strconv.FormatInt(dp.UserOrgId, 10), DialysisId: strconv.FormatInt(dp.DialysisOrder.ID, 10), OrderType: "1", PatientNk: strconv.FormatInt(dp.PatientId, 10), DialysisDuration: dp.DialysisDurationHour*60 + dp.DialysisDurationMinute, BloodVol: int64(math.Floor(dp.BloodFlowVolume + 0/5)), CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } hdDoctorAdvices = append(hdDoctorAdvices, advice) } err := service.BatchCreateDoctorsAdvice(hdDoctorAdvices, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 6, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // func (c *SyncController) SyncMiddle(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 36) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var hdMiddle []*sz.TempHdMiddle monitors, _ := service.FindOrgMonitorRecordData(org_id, sync_time, time.Now().Unix()) for _, item := range monitors { mid := &sz.TempHdMiddle{ HospitalId: hospital_id, DialysisId: strconv.FormatInt(item.DialysisOrder.ID, 10), MonitorTime: time.Unix(item.OperateTime, 0), Sbp: int64(math.Floor(item.SystolicBloodPressure + 0/5)), Dbp: int64(math.Floor(item.DiastolicBloodPressure + 0/5)), PatientNk: strconv.FormatInt(item.PatientId, 10), CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: 0, } hdMiddle = append(hdMiddle, mid) } err := service.BatchCreateMonitor(hdMiddle, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 36, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步设备 func (c *SyncController) SyncOtherMachine(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 13) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步设备 waterMachine, _ := service.FindOrgOtherMachineData(org_id, sync_time, time.Now().Unix()) var wms []*sz.TempHdOtherMachine for _, item := range waterMachine { wm := &sz.TempHdOtherMachine{ HospitalId: hospital_id, EquipmentId: strconv.FormatInt(item.ID, 10), EquipmentBrand: item.ManufactureFactory, EquipmentModel: strconv.FormatInt(item.UnitType, 10), EnableTime: time.Unix(item.StartDate, 0), DiscardedTime: time.Unix(item.RubbishDate, 0), CreateTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } switch item.MachineStatus { case 1: wm.Status = "1" break case 4: wm.Status = "3" break } wms = append(wms, wm) } err := service.BatchCreateOtherMachineRecord(wms, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 13, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步维修 func (c *SyncController) SyncMachineRepair(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 11) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步设备 hdMaintain, _ := service.FindOrgMachineRepairData(org_id, sync_time, time.Now().Unix()) var maintain []*sz.TempHdMaintain for _, item := range hdMaintain { mt := &sz.TempHdMaintain{ HospitalId: hospital_id, EquipmentId: strconv.FormatInt(item.VMDeviceAddmacher.ID, 10), RepairTime: time.Unix(item.GuaranteeDate, 0), CreateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } switch item.VMDeviceAddmacher.DeviceType { case 1: mt.EquipmentType = "11" break case 2: mt.EquipmentType = "21" break case 3: mt.EquipmentType = "99" break } maintain = append(maintain, mt) } err := service.BatchCreateMainTain(maintain, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 11, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步设备 func (c *SyncController) SyncDM(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 5) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } //同步设备 dm, _ := service.FindOrgMachineData(org_id, sync_time, time.Now().Unix()) var hdms []*sz.TempHdDm for _, item := range dm { hdm := &sz.TempHdDm{ HospitalId: hospital_id, EquipmentId: strconv.FormatInt(item.ID, 10), SickbedNo: item.DeviceNumber.Number, EquipmentBrand: item.ManufactureFactory, EquipmentModel: strconv.FormatInt(item.UnitType, 10), EnableTime: time.Unix(item.StartDate, 0), DiscardedTime: time.Unix(item.RubbishDate, 0), CreateTime: time.Now(), UpdateTime: time.Now(), Sjscsj: time.Now(), Xgbz: strconv.FormatInt(0, 10), } switch item.MachineStatus { case 1: hdm.Status = "1" break case 4: hdm.Status = "3" break } switch item.DeviceType { case 1: hdm.EquipmentType = "11" break case 2: hdm.EquipmentType = "98" break case 3: hdm.EquipmentType = "99" break } hdms = append(hdms, hdm) } err := service.BatchCreateHDMRecord(hdms, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 5, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } } // 同步患者透析记录 func (c *SyncController) SyncDialysis(rdb *gorm.DB, org_id int64, hospital_id string) { // 第一步:根据机构id获取上次同步时间 syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 2) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } var tempDialysis []*sz.TempHdDialysis dialysisData, _ := service.FindOrgDialysisData(org_id, sync_time, time.Now().Unix()) for _, item := range dialysisData { psm := &sz.TempHdDialysis{ DialysisId: strconv.FormatInt(item.ID, 10), HospitalId: strconv.FormatInt(item.UserOrgId, 10), PatientNk: strconv.FormatInt(item.PatientId, 10), PsId: strconv.FormatInt(item.Schedule.ID, 10), DialysisDate: time.Unix(item.DialysisDate, 0), SickbedNo: item.DeviceNumber.Number, DivisionId: strconv.FormatInt(item.Schedule.PartitionId,10), EquipmentId: strconv.FormatInt(item.DeviceNumber.ID, 10), TotalTreatDuration: item.AssessmentAfterDislysis.ActualTreatmentHour * 60 + item.AssessmentAfterDislysis.ActualTreatmentMinute, BeforeSbp: int64(item.AssessmentBeforeDislysis.SystolicBloodPressure), BeforeDbp: int64(item.AssessmentBeforeDislysis.DiastolicBloodPressure), AfterSbp: int64(item.AssessmentAfterDislysis.SystolicBloodPressure), AfterDbp: int64(item.AssessmentAfterDislysis.DiastolicBloodPressure), StartTime: time.Unix(item.StartTime, 0), EndTime: time.Unix(item.EndTime, 0), CreateTime: time.Unix(item.CreatedTime, 0), Sjscsj: time.Now(), Xgbz: 0, } if item.AssessmentAfterDislysis.ActualUltrafiltration < 10 { psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration * 1000) } else { psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration) } if item.VMDialysisPrescription.TargetUltrafiltration < 10 { psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration * 1000) } else { psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration) } if item.AssessmentAfterDislysis.ActualDisplacement < 10 { psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement * 1000) } else { psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement) } tempDialysis = append(tempDialysis, psm) } err := service.BatchCreateDialysis(tempDialysis, rdb) if err == nil { // 第三步:同步成功后,添加同步记录 upload := &sz.DataUpload{ SyncType: 2, OrgId: org_id, SyncTime: time.Now().Unix(), SyncResultType: 1, CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(), } err := service.CreateUploadRecord(upload) if err != nil { utils.ErrorLog("%v", err) } } else { //错误处理 } }