package service import ( "XT_New/models" "github.com/jinzhu/gorm" "time" ) func GetAllPatientChargeDetails(org_id int64, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.NewChargePatient, err error) { if len(keyword) == 0 { switch item_type { case 0: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 1: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id > 0 AND project_id = 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 2: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 3: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break } } else { keyword := "%" + keyword + "%" switch item_type { case 0: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 1: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id > 0 AND project_id = 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 2: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 3: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type").Where("advice_id = 0 AND project_id > 0").Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break } } return } func GetNewAllPatientDrugChargeDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, keyword string, item_type int64, time_type string) (patients []*models.NewChargeDetail, err error) { if len(keyword) == 0 { switch item_type { case 0: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify, oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 1: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break } } else { keyword := "%" + keyword + "%" switch item_type { case 0: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 1: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("-1000 as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,oi.advice_id as advice_id, 0 as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,drug.drug_name as item_name,drug.dose as dose,drug.dose_unit as dose_unit, drug.min_number as min_number,drug.min_unit as min_unit, drug.max_unit as max_unit,drug.id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_doctor_advice_info a ON oi.advice_id = a.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Joins(" JOIN xt_base_drug drug on a.drug_id = drug.id"). Where("oi.advice_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break } } return } func GetNewAllPatientProjectAndGoodChargeDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, keyword string, item_type int64, time_type string) (patients []*models.NewChargeDetail, err error) { if len(keyword) == 0 { switch item_type { case 0: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id "). Joins(" left join xt_his_project project on p.project_id = project.id"). Joins(" left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id "). Joins(" left join xt_his_project project on p.project_id = project.id"). Joins(" left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } //err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeOrder", func(db *gorm.DB) *gorm.DB { // return db.Select("id,user_org_id,his_patient_id,settle_accounts_date,status,number,order_status,mdtrt_id,patient_id"). // Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { // return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). // Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { // return db.Preload("Drug", "status = 1").Where("status = 1") // }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { // return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { // return db.Select("id,project_name,unit").Where("status = 1 ") // }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { // return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") // }).Where("status = 1 ") // }).Where("status = 1") // }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id) //}).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 2: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 3: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 4: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 3", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error } break case 5: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error } break case 6: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 6", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 6", org_id, start_time, end_time).Scan(&patients).Error } break case 7: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1 ", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id"). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error } break } } else { keyword := "%" + keyword + "%" switch item_type { case 0: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id "). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id "). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 2: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } break case 3: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 3"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 ", org_id, start_time, end_time).Scan(&patients).Error } case 4: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 3 ", org_id, start_time, end_time).Scan(&patients).Error } case 5: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 2", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 2 ", org_id, start_time, end_time).Scan(&patients).Error } case 6: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 10", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 10", org_id, start_time, end_time).Scan(&patients).Error } case 7: if time_type == "0" { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and o.settle_accounts_date >= ? and o.settle_accounts_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error } else { err = readDb.Table("his_order_info as oi").Select("(case p.type when 2 then project.cost_classify when 3 then -100 END) as cost_classify,FROM_UNIXTIME(o.settle_accounts_date, '%Y-%m-%d %H:%i:%S') as pdate,0 as advice_id, oi.project_id as project_id, oi.patient_id as p_id,oi.cnt as cnt,oi.pric as pric,pp.name as p_name,p.type as p_type, (case p.type when 2 then project.project_name when 3 then good.good_name END) as item_name,(case p.type when 2 then '' when 3 then good.specification_name END) as specification_name, p.project_id as item_id"). Joins("JOIN his_order o ON oi.order_number = o.number and o.setl_time BETWEEN ? AND ? and o.order_status = 2 and o.status = 1", start_time_str, end_time_str). Joins(" JOIN his_prescription_project p ON oi.project_id = p.id and p.type = 2"). Joins("left join xt_his_project project on p.project_id = project.id"). Joins("left join xt_good_information good on p.project_id = good.id"). Joins(" JOIN xt_patients pp on oi.patient_id = pp.id and pp.name like ?", keyword). Where("oi.project_id > 0 and oi.user_org_id = ? and oi.upload_date >= ? and oi.upload_date <= ? and oi.status = 1 and project.cost_classify = 11", org_id, start_time, end_time).Scan(&patients).Error } break } } return } func GetAllPatientChargeSettle(org_id int64, start_time int64, end_time int64, keyword string, item_type int64) (patients []*models.SettlePatient, err error) { if len(keyword) == 0 { switch item_type { case 0: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 1: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 1 ", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 2: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 0", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 3: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 14", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 4: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 11", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 5: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2 AND orders.insutype = 390", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 ", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break case 6: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2 AND orders.insutype = 310 ", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ?", org_id).Group("id").Find(&patients).Error break } } else { keyword = "%" + keyword + "%" switch item_type { case 0: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 1: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 1 ", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 2: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND is_medicine_insurance = 0", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 3: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 14", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break case 4: err = readDb2.Table("xt_patients as p").Select("p.id,p.user_org_id,p.name,p.lapseto,p.status,p.id_card_no").Joins("JOIN his_order AS orders ON orders.patient_id = p.id AND orders.status = 1 AND orders.ctime >= ? AND orders.ctime <= ? AND orders.user_org_id = ? AND orders.order_status = 2", start_time, end_time, org_id).Preload("HisChargeSettleOrder", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND med_type = 11", start_time, end_time, org_id).Order("ctime") }).Where("p.status = 1 AND p.user_org_id = ? AND p.name LIKE ?", org_id, keyword).Group("id").Find(&patients).Error break } } return } func GetLabelPrintList(page int64, limit int64, user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) { offset := (page - 1) * limit db := readDb.Model(&models.HisLabelPrintInfo{}) db = db.Preload("HisProjectTeam", "status = 1") if is_print > 0 { db = db.Where("is_print = ?", is_print) } if len(keywors) > 0 { keywors = "%" + keywors + "%" db = db.Where("patient_name Like ?", keywors) } if tube_color > 0 { db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?", tube_color) //db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?",tube_color) } err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Offset(offset).Limit(limit).Find(&labels).Error return } func GetLabelPrintListTwo(page int64, limit int64, user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) { offset := (page - 1) * limit db := readDb.Model(&models.HisLabelPrintInfo{}) db = db.Preload("HisProjectTeam", "status = 1") if is_print > 0 { db = db.Where("is_print = ?", is_print) } if len(keywors) > 0 { keywors = "%" + keywors + "%" db = db.Where("patient_name Like ?", keywors) } if tube_color > 0 { //db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?",tube_color) db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?", tube_color) } err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Offset(offset).Limit(limit).Find(&labels).Error return } func GetLabelPrintListThree(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) { db := readDb.Model(&models.HisLabelPrintInfo{}) db = db.Preload("HisProjectTeam", "status = 1") if is_print > 0 { db = db.Where("is_print = ?", is_print) } if len(keywors) > 0 { keywors = "%" + keywors + "%" db = db.Where("patient_name Like ?", keywors) } //if tube_color > 0 { // db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?", tube_color) //} err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Find(&labels).Error return } //func GetLabelPrintListFour(user_org_id int64, record_time int64, is_print int64, keywors string, tube_color int64) (labels []*models.HisLabelPrintInfo, total int64, err error) { // db := readDb.Model(&models.HisLabelPrintInfo{}) // db = db.Preload("HisProjectTeam", "status = 1") // // if is_print > 0 { // db = db.Where("is_print = ?", is_print) // } // if len(keywors) > 0 { // keywors = "%" + keywors + "%" // db = db.Where("patient_name Like ?", keywors) // // } // //if tube_color > 0 { // // //db = db.Joins("Right join xt_his_project_team as team on team.id = his_label_print_info.item_id AND his_label_print_info.item_id <> 0 and team.status = 1 and team.tube_color = ?",tube_color) // // db = db.Joins("Right join xt_his_project as pro on pro.id = his_label_print_info.project_id AND his_label_print_info.item_id = 0 and his_label_print_info.project_id > 0 and pro.status = 1 and pro.tube_color = ?", tube_color) // //} // // err = db.Where("his_label_print_info.user_org_id = ? AND his_label_print_info.record_date = ? AND his_label_print_info.status = 1 ", user_org_id, record_time).Count(&total).Find(&labels).Error // return //} func GetLabelPrintInfo(id int64) (labels models.HisLabelPrintInfo, err error) { db := readDb.Model(&models.HisLabelPrintInfo{}) err = db.Where("id = ?", id).First(&labels).Error return } func AddSigleFapiaoRecord(dealer *models.HisFapiaoRecord) (err error, record *models.HisFapiaoRecord) { err = writeDb.Create(&dealer).Error return err, dealer } func ModifyFapiao(mesick *models.HisFapiaoRecord) error { err := writeDb.Save(&mesick).Error return err } func UpdateFapiaoIsUse(org_id int64) { writeDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ?", org_id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "is_use": 0}) } func FindAllFapiaoList(orgId int64, page int64, limit int64) (list []*models.HisFapiaoRecord, total int64, err error) { offset := (page - 1) * limit db := readDb.Model(&models.HisFapiaoRecord{}) db = db.Where("user_org_id = ? AND status = 1", orgId) err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error return } func DeleteFapiaoById(id int64) error { err := writeDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error return err } func FindFapiaoById(id int64) (*models.HisFapiaoRecord, error) { dealer := &models.HisFapiaoRecord{} err := readDb.Model(&models.HisFapiaoRecord{}).Where("id = ? AND status = 1", id).First(&dealer).Error return dealer, err } func FindFapiaoByIsUse(org_id int64) (models.HisFapiaoRecord, error) { record := models.HisFapiaoRecord{} err := readDb.Model(&models.HisFapiaoRecord{}).Where("user_org_id = ? AND status = 1 AND is_use = 1", org_id).First(&record).Error return record, err } func FindHisYidiClearRecord(org_id int64) (records []*models.HisYidiClearRecord, err error) { err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1", org_id).Find(&records).Error return } func GetHisYidiClearRecordById(org_id int64, id int64) (record models.HisYidiClearRecord, err error) { err = readDb.Model(&models.HisYidiClearRecord{}).Where("user_org_id = ? AND status = 1 AND id = ?", org_id, id).First(&record).Error return } func GetAllPatientTwo(org_id int64) (patients []*models.ChargePatientTwo, err error) { err = readDb.Model(&models.ChargePatientTwo{}).Where("user_org_id = ? AND status = 1", org_id).Find(&patients).Error return } func GetPatientChargeDetails(patient_id int64, org_id int64, start_time int64, end_time int64, keyword string, item_type int64, settle_type int64) (patients []*models.HisChargeOrderTwo, err error) { if len(keyword) == 0 { switch item_type { case 0: readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }) }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 1: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Where("status = 1 AND advice_id > 0 AND project_id = 0") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 2: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break case 3: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break } } else { //keyword := "%" + keyword + "%" switch item_type { case 0: readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }) }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 1: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Where("status = 1 AND advice_id > 0 AND project_id = 0") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 2: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break case 3: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break } } return } func GetPatientGather(patient_id int64, org_id int64, start_time int64, end_time int64, keyword string, item_type int64, settle_type int64) (patients []*models.HisChargeOrderTwo, err error) { if len(keyword) == 0 { switch item_type { case 0: readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }) }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 1: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Where("status = 1 AND advice_id > 0 AND project_id = 0") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 2: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break case 3: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break } } else { //keyword := "%" + keyword + "%" switch item_type { case 0: readDb2.Model(&models.HisChargeOrderTwo{}).Joins("join his_patient his on his.number = his_order.mdtrt_id AND his.patient_id = ").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }) }).Where("status = 1 AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 1: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }).Where("status = 1 AND advice_id > 0 AND project_id = 0") }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type=?", start_time, end_time, org_id, patient_id, settle_type) break case 2: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break case 3: readDb2.Model(&models.HisChargeOrderTwo{}).Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 AND advice_id = 0 AND project_id > 0 ") }) }).Where("status = 1 AND ctime >= ? AND ctime <= ? AND user_org_id = ? AND order_status = 2 AND patient_id = ? AND p_type = ?", start_time, end_time, org_id, patient_id, settle_type) break } } return } func GetAllChargeDetailsTwo(org_id int64, start_time int64, end_time int64) (patients []*models.HisChargeOrder, err error) { err = readDb2.Model(&models.HisChargeOrder{}).Preload("Patients", "status = 1").Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,order_number,advice_id,det_item_fee_sumamt,cnt,pric,med_chrgitm_type,status,chld_medc_flag,chrgitm_lv,user_org_id,project_id,type"). Preload("HisChargeDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB { return db.Preload("Drug", "status = 1").Where("status = 1") }). Preload("HisChargePrescriptionProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_id,user_org_id,status,patient_id,record_date,count,type"). Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB { return db.Select("id,project_name,unit").Where("status = 1 ") }). Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB { return db.Select("id,good_name,good_unit,specification_name").Where("status = 1 ") }).Where("status = 1 ") }).Where("status = 1") }).Where("status = 1 AND user_org_id = ? AND settle_accounts_date >= ? AND settle_accounts_date <= ? AND order_status = 2", org_id, start_time, end_time).Group("id").Find(&patients).Error return } func GetAllLisDataFor10191(org_id int64) (list []models.HisLabelPrintStatusInfo, err error) { err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Preload("Patient", "status = 1").Where("user_org_id = ? AND status = 1 and apply_code = ''", org_id).Find(&list).Error return } func GetHisPrescriptionDrugDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, patient_id int64, item_type int64) (patients []*models.NewPDetail, err error) { db := readDb.Table("his_doctor_advice_info as oi").Select("oo.setl_time as setl_time, drug.id as item_id,1 as p_type,oi.id as advice_id,0 as project_id,pp.id as p_id,pp.name as p_name,FROM_UNIXTIME(o.`record_date`, '%Y-%m-%d') as record_date, drug.drug_type as item_cost_type,o.med_type as med_type,oi.`prescribing_number` as cnt,oi.`price` as pric,drug.drug_name as item_name,CONCAT(drug.dose, \"\", drug.dose_unit, \"*\", drug.min_number, \"\", drug.min_unit, \"/\", drug.max_unit) as spec,o.order_status as order_status,oi.`prescribing_number_unit` as unit"). Joins("JOIN xt_base_drug drug ON oi.drug_id = drug.id ") if patient_id == 0 { db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id") } else { db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id and pp.id = ?", patient_id) } if item_type == 0 { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1", start_time_str, end_time_str) } else { if item_type == 2 { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status = 2", start_time_str, end_time_str) } else { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status <> 2", start_time_str, end_time_str) } } db = db.Joins("left JOIN `his_order` oo on oo.`number` = o.`batch_number` and oo.user_org_id = ?", org_id) err = db.Where(" oi.user_org_id = ? AND o.record_date >= ? AND o.record_date <= ? AND oi.status = 1", org_id, start_time, end_time).Group("oi.id").Scan(&patients).Error return } func GetHisPrescriptionProjectDetails(org_id int64, start_time_str string, end_time_str string, start_time int64, end_time int64, patient_id int64, item_type int64) (patients []*models.NewPDetail, err error) { db := readDb.Table("`his_prescription_project` AS oi").Select("oo.setl_time as setl_time,(case oi.type\n\t\t\tWHEN 2 THEN\n\t\t\tproject.id\n\t\t\tWHEN 3 THEN\n\t\t\tgood.id END) AS item_id,oi.type as p_type,oi.id as project_id,0 as advice_id,pp.id as p_id,(case oi.type\n\t\t\tWHEN 2 THEN\n\t\t\tproject.`cost_classify`\n\t\t\tWHEN 3 THEN\n\t\t\t-100 END) AS item_cost_type,\n\t o.med_type as med_type,\n\t oi.count AS cnt,\n \t oi.price AS pric,\n \t pp.name AS p_name, \n \tFROM_UNIXTIME(o.`record_date`,'%Y-%m-%d') as record_date,\n\t\t (case oi.type\n\tWHEN 2 THEN\n\tproject.project_name\n\tWHEN 3 THEN\n\tgood.good_name END) AS item_name,\n\t\t (case oi.type\n\tWHEN 2 THEN\n\t\"\"\n\tWHEN 3 THEN\n\tgood.`specification_name` END) AS spec,\n\toi.`unit` as unit,o.order_status as order_status") db = db.Joins("LEFT JOIN xt_his_project project\n\tON oi.project_id = project.id"). Joins("LEFT JOIN xt_good_information good\n\tON oi.project_id = good.id\n") //Joins("JOIN his_order ord ON o.batch_number = ord.number ") if patient_id > 0 { db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id and pp.id = ?", patient_id) } else { db = db.Joins("JOIN xt_patients pp on oi.patient_id = pp.id") } if item_type > 0 { if item_type == 2 { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status = 2", start_time_str, end_time_str) } else { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1 and o.order_status <> 2", start_time_str, end_time_str) } } else { db = db.Joins("JOIN `his_prescription` o ON oi.`prescription_id` = o.id and FROM_UNIXTIME(o.record_date, '%Y-%m-%d %H:%i:%S') BETWEEN ? AND ? and o.status = 1", start_time_str, end_time_str) } db = db.Joins("left JOIN `his_order` oo on oo.`number` = o.`batch_number` and oo.user_org_id = ?", org_id) err = db.Where(" oi.user_org_id = ? AND o.record_date >= ? AND o.record_date <= ? AND oi.status = 1", org_id, start_time, end_time).Group("oi.id").Scan(&patients).Error return }