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.ChargePatient, 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  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  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  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  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  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  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  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  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 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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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)
			}).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) (labels []*models.HisLabelPrintInfo, total int64, err error) {
	offset := (page - 1) * limit
	db := readDb.Model(&models.HisLabelPrintInfo{})
	if is_print > 0 {
		db = db.Where("is_print = ?", is_print)
	}
	if len(keywors) > 0 {
		keywors = "%" + keywors + "%"
		db = db.Where("patient_name Like ?", keywors)

	}
	err = db.Where("user_org_id = ? AND record_date = ? AND status = 1 ", user_org_id, record_time).Count(&total).Offset(offset).Limit(limit).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
}
