12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- package controllers
-
- import (
- "Data_Upload_Api/enums"
- "Data_Upload_Api/models/sz"
- "Data_Upload_Api/service"
- "Data_Upload_Api/utils"
- "fmt"
- "github.com/astaxie/beego"
- "github.com/jinzhu/gorm"
- "math"
- "strconv"
- "strings"
- _ "strings"
- "time"
- )
-
- type SyncController struct {
- BaseAPIController
- }
-
- func SyncAPIRegisterRouters() {
- beego.Router("/sync/upload", &SyncController{}, "get:SyncToSZIC")
- beego.Router("/sync/upload/bx", &SyncController{}, "get:SyncToBX")
-
- }
-
- func (c *SyncController) SyncToBX() {
- // 第一步:到上报配置表中找到深圳需要上报的机构
- //sz_province, _ := beego.AppConfig.Int64("sz_province")
- //sz_city, _ := beego.AppConfig.Int64("sz_city")
- //configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3)
-
- org := &sz.DataUploadConfig{
- OrgId: 3907,
- ProvinceId: 19,
- CityId: 291,
- DepartmentName: "深圳宝兴医院",
- HospitalId: "455835506",
- InstType: 1,
- DbHost: "172.16.173.151",
- DbPort: "1433",
- DbUser: "sa",
- DbName: "123.com",
- }
-
- //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.CreateSqlServiceDB(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) 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: item.DeviceType,
- CreateTime: time.Now(),
- UpdateTime: time.Now(),
- Sjscsj: time.Now(),
- Xgbz: 0,
- }
-
- 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 int
- switch role.UserType {
- case 2:
- title = 1
- break
- case 3:
- title = 2
- break
- }
-
- staff := &sz.TempHdStaff{
- HospitalId: hospital_id,
- StaffId: strconv.FormatInt(role.ID, 10),
- StaffName: role.UserName,
- Position: int64(title),
- PermanentType: 1,
- CreateTime: time.Now(),
- UpdateTime: time.Now(),
- Sjscsj: time.Now(),
- Xgbz: 0,
- }
- 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: patient.ID,
- CardNo: "000000000000000000",
- CardType: 1,
- IdNo: patient.IdCardNo,
- IdType: "01",
- PatientName: patient.Name,
- Gender: strconv.FormatInt(patient.Gender, 10),
- BornDate: birthday,
- DiagnosisSummary: patient.Diagnose,
- IsCrf: strconv.Itoa(isCKD),
- CreateTime: time.Now(),
- UpdateTime: time.Now(),
- Sjscsj: time.Now(),
- Xgbz: 0,
- }
-
- 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",
- CreateTime: 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: 0,
- }
- 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 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 {
- //错误处理
-
- }
-
- }
|