package service import ( "Xcx_New/models" "crypto/aes" "crypto/cipher" "encoding/base64" "encoding/json" "errors" "fmt" "github.com/jinzhu/gorm" "time" ) func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) { role := models.XcxAdminUserRole{} err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &role, nil } func CreateXcxAdminUser(role models.XcxAdminUserRole) error { err := UserReadDB().Create(&role).Error return err } func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) { role := models.XcxAdminUserRole{} err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error return role, err } func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) { patients := models.XcxPatients{} err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error return patients, err } func GetPatientListByPatientId(id int64) (models.XcxPatients, error) { patients := models.XcxPatients{} err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error return patients, err } func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) { aesKey, err := base64.StdEncoding.DecodeString(session_key) if err != nil { return nil, err } ivKey, err := base64.StdEncoding.DecodeString(iv) if err != nil { return nil, err } decodeData, err := base64.StdEncoding.DecodeString(encrypted_data) if err != nil { return nil, err } dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey) if err != nil { return nil, err } var result map[string]interface{} err = json.Unmarshal(dataBytes, &result) watermark := result["watermark"].(map[string]interface{}) if watermark["appid"] != app_id { return nil, errors.New("Invalid appid data!") } return result, err } func AesDecrypt(crypted, key, iv []byte) ([]byte, error) { block, err := aes.NewCipher(key) fmt.Println("w2222", err) if err != nil { return nil, err } blockMode := cipher.NewCBCDecrypter(block, iv) origData := make([]byte, len(crypted)) blockMode.CryptBlocks(origData, crypted) // 去除填充 length := len(origData) unp := int(origData[length-1]) return origData[:(length - unp)], nil } func GetFirstDateOfWeek() (weekMonday string) { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) weekMonday = weekStartDate.Format("2006-01-02") return } func GetWeekDayOfWeek() (weekMonday string) { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) weekMonday = weekStartDate.Format("2006-01-02") return } func GetMobilePatient(mobile string, idcard string) (*models.XcxPatients, error) { patient := models.XcxPatients{} err := XTReadDB().Model(&patient).Where("(phone = ? or relative_phone =?) and id_card_no =?", mobile, mobile, idcard).Find(&patient).Error //err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &patient, nil } func GetTemplateMode(orgid int64) (models.XcxGobalTemplate, error) { template := models.XcxGobalTemplate{} err := XTReadDB().Model(&template).Where("org_id = ? and status = 1", orgid).Find(&template).Error return template, err } func GetMobilePatientOne(mobile string) (*models.XcxAdminUserRole, error) { patient := models.XcxAdminUserRole{} err := XTReadDB().Model(&patient).Where("mobile = ? and status = 1", mobile).Find(&patient).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &patient, nil } func Dncrypt(rawData, key, iv string) (string, error) { data, err := base64.StdEncoding.DecodeString(rawData) key_b, err_1 := base64.StdEncoding.DecodeString(key) iv_b, _ := base64.StdEncoding.DecodeString(iv) if err != nil { return "", err } if err_1 != nil { return "", err_1 } dnData, err := AesCBCDncrypt(data, key_b, iv_b) if err != nil { return "", err } return string(dnData), nil } // 解密 func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { panic(err) } blockSize := block.BlockSize() if len(encryptData) < blockSize { panic("ciphertext too short") } if len(encryptData)%blockSize != 0 { panic("ciphertext is not a multiple of the block size") } mode := cipher.NewCBCDecrypter(block, iv) mode.CryptBlocks(encryptData, encryptData) // 解填充 encryptData = PKCS7UnPadding(encryptData) return encryptData, nil } //去除填充 func PKCS7UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) if length-unpadding < 0 { return origData[:0] } fmt.Println("changdu", length) fmt.Println("data", unpadding) return origData[:(length - unpadding)] } func GetScheduleInfo(startime int64, endtime int64, patient_id int64) (schedule []*models.VmBloodSchedule, err error) { db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1") table := XTReadDB().Table("xt_patients as t").Where("t.status = 1") fmt.Println(table) if startime > 0 { db = db.Where("x.schedule_date >=?", startime) } if endtime > 0 { db = db.Where("x.schedule_date <=?", endtime) } if patient_id > 0 { db = db.Where("x.patient_id = ?", patient_id) } err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error return schedule, err } func GetTodayDialysis(recordDate int64, patient_id int64) (models.XcXDialysisOrder, error) { order := models.XcXDialysisOrder{} err := XTReadDB().Model(&order).Where("dialysis_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&order).Error return order, err } func GetTodayPrescription(recordDate int64, patient_id int64) (models.XcxXtDialysisPrescription, error) { prescription := models.XcxXtDialysisPrescription{} err := XTReadDB().Model(&prescription).Where("record_date = ? and patient_id = ? and status = 1", recordDate, patient_id).Find(&prescription).Error return prescription, err } func GetNextPatientSchedule(patient_id int64, dialysis_date int64) (models.BloodSchedule, error) { schedule := models.BloodSchedule{} //err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date>?", patient_id, dialysis_date).Order("schedule_date asc").First(&schedule).Error //return schedule, err db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1") table := XTReadDB().Table("xt_device_zone as z").Where("z.status =1") tables := XTReadDB().Table("xt_device_number as n").Where("n.status =1") fmt.Println(table, tables) if patient_id > 0 { db = db.Where("x.patient_id = ?", patient_id) } if dialysis_date > 0 { db = db.Where("x.schedule_date >?", dialysis_date) } err := db.Select("x.patient_id,x.schedule_type,x.schedule_date,x.mode_id,z.name,n.number").Joins("left join xt_device_zone as z on z.id = x.partition_id").Joins("left join xt_device_number as n on n.id = x.bed_id").Order("x.schedule_date asc").Limit(1).Scan(&schedule).Error return schedule, err } func GetXcxPatientInfo(patient_id int64) (models.XcxPatients, error) { patients := models.XcxPatients{} err := XTReadDB().Model(&patients).Where("id = ? and status = 1 ", patient_id).Find(&patients).Error return patients, err } func GetOrgInfo(id int64) (models.XcxSgjUserOrg, error) { org := models.XcxSgjUserOrg{} err := UserReadDB().Model(&org).Where("id = ? and status= 1", id).Find(&org).Error return org, err } func GetEducationList(startime int64, endtime int64, patient_id int64) (treatment []*models.XcxTreatmentSummary, err error) { err = XTReadDB().Model(&treatment).Where("assessment_date>=? and assessment_date<=? and patient_id = ? and status =1", startime, endtime, patient_id).Order("assessment_date desc").Find(&treatment).Error return treatment, err } func GetDoctorAdviceConfig(orgid int64) (models.XcxDoctorAdviceConfig, error) { config := models.XcxDoctorAdviceConfig{} err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error return config, err } func GetDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) { err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Group("advice_date").Order("advice_date desc").Find(&advice).Error return advice, err } func GetHisDoctorAdviceGroup(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) { err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Group("advice_date").Order("advice_date desc").Find(&advice).Error return advice, err } func GetDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxDoctorAdvice, err error) { err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error return advice, err } func GetHisDoctorAdvice(patient_id int64, startime int64, endtime int64) (advice []*models.XcxHisDoctorAdviceInfo, err error) { err = XTReadDB().Where("patient_id = ? and advice_date >=? and advice_date<=? and status = 1", patient_id, startime, endtime).Find(&advice).Error return advice, err } // 用户排班信息 func MobileGetSchedualDetailOne(orgID int64, patientID int64) (*MDialysisScheduleVM, error) { var vm MDialysisScheduleVM err := readDb. Table("xt_schedule"). Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID). Where("status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientID). First(&vm).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &vm, err } // 接诊评估 func MobileGetReceiverTreatmentAccessRecordOne(orgID int64, patientID int64, record_date int64) (*models.ReceiveTreatmentAsses, error) { var record models.ReceiveTreatmentAsses err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, record_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 透前评估 func MobileGetPredialysisEvaluationOne(orgID int64, patientID int64, assessment_date int64) (*models.PredialysisEvaluation, error) { var record models.PredialysisEvaluation err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, assessment_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 获取 maxDate 之前一次的透前评估记录 func MobileGetLastTimePredialysisEvaluationOne(orgID int64, patientID int64) (*models.PredialysisEvaluation, error) { var record models.PredialysisEvaluation err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("assessment_date desc").Last(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } func MobileGetDoctorAdvicesByGroupsOne(orgID int64, patientID int64, advicedate int64) ([]*models.DoctorAdvice, error) { var records []*models.DoctorAdvice err := readDb. Model(&models.DoctorAdvice{}). Where("patient_id = ? and user_org_id = ? and status = 1 and (advice_type = 2 || advice_type = 3) and advice_date = ?", patientID, orgID, advicedate). Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno,way,drug_id,drug_name_id, IF(parent_id > 0, parent_id, id) as advice_order"). Order("start_time asc, groupno desc, advice_order desc, id asc"). Scan(&records).Error if err != nil { return nil, err } return records, nil } // 透析记录 func MobileGetSchedualDialysisRecordOne(orgID int64, patientID int64, dialysis_date int64) (*models.DialysisOrder, error) { var record models.DialysisOrder err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1 ", orgID).Where("user_org_id = ? AND patient_id = ? and dialysis_date = ? ", orgID, patientID, dialysis_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 双人核对 func MobileGetDoubleCheckOne(orgID int64, patientID int64, check_date int64) (*models.DoubleCheck, error) { var record models.DoubleCheck err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, check_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 透析监测记录 func MobileGetMonitorRecordsOne(orgID int64, patientID int64, monitoring_date int64) ([]*models.MonitoringRecord, error) { var records []*models.MonitoringRecord err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, monitoring_date).Order("operate_time asc").Find(&records).Error if err != nil { return nil, err } return records, nil } func MobileGetLastMonitorRecordOne(orgID int64, patientID int64) (*models.MonitoringRecord, error) { var record models.MonitoringRecord err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Order("operate_time desc").Last(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 透后评估 func MobileGetAssessmentAfterDislysisOne(orgID int64, patientID int64, assessment_date int64) (*models.AssessmentAfterDislysis, error) { var record models.AssessmentAfterDislysis err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date =? ", patientID, orgID, assessment_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 治疗小结 func MobileGetTreatmentSummaryOne(orgID int64, patientID int64, assessment_date int64) (*models.TreatmentSummary, error) { var record models.TreatmentSummary err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date= ?", patientID, orgID, assessment_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } // 透析处方 func MobileGetDialysisPrescribeByModeIdSix(orgID int64, patientID int64, mode_id int64, record_date int64) (*models.DialysisPrescription, error) { var record models.DialysisPrescription err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ? and record_date = ?", patientID, orgID, mode_id, record_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } func GetHisPrescriptionProjectsOne(user_org_id int64, patient_id int64) (projects []*models.HisPrescriptionProject, err error) { err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1").Preload("GoodInfo", "status = 1").Where("user_org_id = ? AND patient_id = ? AND status = 1", user_org_id, patient_id).Find(&projects).Error return } func GetDialyStockOutOne(orgid int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) { db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1") if orgid > 0 { db = db.Where("x.user_org_id = ?", orgid) } if patientId > 0 { db = db.Where("x.patient_id = ?", patientId) } err = db.Select("x.id,x.user_org_id,x.patient_id,x.record_date,x.good_id,x.good_type_id,x.count,x.commdity_code,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id and t.org_id = ? and t.status = 1", orgid).Scan(&prepare).Error return prepare, err } func MobileGetDialysisPrescribeByModeIdFive(orgID int64, patientID int64, record_date int64) (*models.DialysisPrescription, error) { var record models.DialysisPrescription err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, record_date).Find(&record).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &record, nil } func GetAllHisDoctorAdviceOne(orgid int64, patientid int64, advice_date int64) (his []*models.HisDoctorAdviceInfo, err error) { err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 and advice_date = ?", patientid, orgid, advice_date).Find(&his).Error return } func GetDoctorName(orgid int64) (role []*models.XcxSgjUserAdminRole, err error) { err = UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&role).Error return role, err } func GetPrescripionFieldConfig(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 1 ", orgid).Find(&list).Error return list, err } func GetReceiveTreatmentAssess(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 2 ", orgid).Find(&list).Error return list, err } func GetDialysisBefor(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 3", orgid).Find(&list).Error return list, err } func GetDialysisOrderConfig(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 7"+ "", orgid).Find(&list).Error return list, err } func GetMonitorRecordConfig(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 4", orgid).Find(&list).Error return list, err } func GetDialysisAfeterConfig(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 5", orgid).Find(&list).Error return list, err } func GetTreatMentConfig(orgid int64) (list []*models.XcxFiledConfig, err error) { err = XTReadDB().Model(&list).Where("org_id = ? and module = 6", orgid).Find(&list).Error return list, err } func GetDataConfig(orgid int64) (list []*models.XcxDataConfig, err error) { err = XTReadDB().Model(&list).Where("(org_id = ? or org_id = 0) and status = 1", orgid).Find(&list).Error return list, err } func GetLastScheduleListOne(patient_id int64, schedule_date int64) (models.XcxSchedule, error) { schedule := models.XcxSchedule{} err := XTReadDB().Model(&schedule).Where("patient_id = ? and status = 1 and schedule_date <=?", patient_id, schedule_date).Order("schedule_date desc").Last(&schedule).Error return schedule, err } func GetInspectionGroupList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) { db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1") if patientid > 0 { db = db.Where("x.patient_id = ?", patientid) } if startime > 0 { db = db.Where("x.inspect_date >= ?", startime) } if endtime > 0 { db = db.Where("x.inspect_date <= ?", endtime) } err = db.Group("x.inspect_date,x.project_id").Find(&inspection).Error return inspection, err } func GetInsepctionList(patientid int64, startime int64, endtime int64) (inspection []*models.XcxInspection, err error) { db := XTReadDB().Table("xt_inspection as x").Where("x.status =1") table := XTReadDB().Table("xt_inspection_reference as r") fmt.Print("table", table) if patientid > 0 { db = db.Where("x.patient_id = ?", patientid) } if startime > 0 { db = db.Where("x.inspect_date >= ?", startime) } if endtime > 0 { db = db.Where("x.inspect_date <= ?", endtime) } err = db.Group("x.id").Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.status,x.created_time,x.updated_time,r.range_type,r.range_min,r.range_max,r.range_value,r.range_options,r.unit").Joins("left join xt_inspection_reference as r on (r.item_id = x.item_id AND r.org_id > 0) OR ( x.item_id = r.id AND r.org_id = 0) ").Scan(&inspection).Error return inspection, err } func GetArticList(activityID int64, page int64, limit int64) (story []*models.SybStory, total int64, err error) { offset := (page - 1) * limit err = UserReadDB().Model(&story).Where("activity_id = ? and audit_status = 2", activityID).Count(&total).Order("ctime desc").Offset(offset).Limit(limit).Find(&story).Error return story, total, err } func GetArticListDetail(id int64) (models.SybStory, error) { story := models.SybStory{} err := UserReadDB().Where("id = ?", id).Find(&story).Error return story, err } func GetStoryScoreList(activityID int64, page int64, limit int64) (score []*models.SybStoryScore, total int64, err error) { offset := (page - 1) * limit err = UserReadDB().Model(&score).Where("activity_id = ?", activityID).Count(&total).Offset(offset).Limit(limit).Preload("SybStory").Find(&score).Error return score, total, err } func SendWxAuthAPI(appID string, appSecret string, grant_type string, code string) (map[string]string, error) { var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code) fmt.Println("url233232323322323", url) resp, err := http.DefaultClient.Get(code2sessionURL) fmt.Println("resp32332232332322", resp) var wxMap map[string]string err = json.NewDecoder(resp.Body).Decode(&wxMap) if err != nil { return wxMap, err } defer resp.Body.Close() return wxMap, err } func SendWxAuthAPIOne(appID string, appSecret string, grant_type string, code string) (string, error) { var code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grant_type var url = fmt.Sprintf(code2sessionURL, appID, appSecret, code) fmt.Println(url) resp, err := http.DefaultClient.Get(code2sessionURL) if err != nil { return "", err } var wxMap map[string]string err = json.NewDecoder(resp.Body).Decode(&wxMap) if err != nil { return "", err } defer resp.Body.Close() fmt.Println("2333232232", wxMap) return wxMap["session_key"], nil }