package service

import (
	"XT_New/models"
	"github.com/jinzhu/gorm"
	"time"
)

func GetHisSummaryDetailList(keyword string, item_type int64, patient_id int64, org_id int64, start_time int64, end_time int64) (patients []*models.ChargePatient, err error) {

	err = XTReadDB().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 and orders.patient_id = ?", start_time, end_time, org_id, patient_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,psn_cash_pay,acct_pay,fund_pay_sumamt,is_medicine_insurance").
			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("status = 1").
					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,unit,remark").Preload("HisChargeProject", func(db *gorm.DB) *gorm.DB {
						return db.Select("id,project_name,unit,medical_coverage,cost_classify").Where("status = 1 ")
					}).Preload("HisChargeGoodInfo", func(db *gorm.DB) *gorm.DB {
						return db.Select("id,good_name,good_unit,specification_name,packing_unit,medical_insurance_level").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.id = ?", org_id, patient_id).Group("id").Find(&patients).Error

	return

}

func GetModeTemplateName(mode_id int64, user_org_id int64) (template []*models.HisPrescriptionModeTemplate, err error) {

	err = XTReadDB().Where("mode = ? and user_org_id = ? and patient_id = ? and status = 1", mode_id, user_org_id, 0).Find(&template).Error
	return template, err
}

func GetModeTemplateNameDetail(mode_id int64, user_org_id int64) (models.HisPrescriptionModeTemplate, error) {

	template := models.HisPrescriptionModeTemplate{}
	err := XTReadDB().Where("mode = ? and user_org_id = ? and status = 1 and patient_id= 0", mode_id, user_org_id).Find(&template).Error
	return template, err
}

func GetModeTemplateInfo(id int64, user_org_id int64) (models.HisPrescriptionInfoTemplate, error) {

	template := models.HisPrescriptionInfoTemplate{}
	err := XTReadDB().Where("p_template_id = ? and user_org_id = ? and status = 1", id, user_org_id).Find(&template).Error
	return template, err
}

func GetPrescriptionAdviceTemplateDetail(template_id int64, orgid int64) (template []*models.HisPrescriptionAdviceTemplate, err error) {

	err = XTReadDB().Where("prescription_id in(?) and user_org_id = ? and status = 1", template_id, orgid).Find(&template).Error
	return template, err
}

func GetPrescriptionProjectTemplateDetail(template_id int64, orgid int64) (template []*models.HisPrescriptionProjectTemplate, err error) {
	err = XTReadDB().Where("prescription_id in(?) and user_org_id = ? and status = 1", template_id, orgid).Find(&template).Error
	return template, err
}

func GetHisPrescriptionModeTemplate(template_id int64, org_id int64) (prescription []*models.HisPrescriptionInfoModeTemplate, err error) {
	err = readDb.Model(&models.HisPrescriptionInfoModeTemplate{}).
		Preload("HisPrescriptionAdviceTemplate", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProjectTemplate", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisProject").Preload("GoodInfo", "status=1")
		}).
		Where("status = 1 AND p_template_id = ? ", template_id).
		Find(&prescription).Error
	return
}

func GetHisModePrescriptionTemplateByID(template_id int64) (prescription models.HisPrescriptionModeTemplate, err error) {
	err = readDb.Model(&models.HisPrescriptionModeTemplate{}).Where("id = ? AND status = 1 ", template_id).First(&prescription).Error
	return
}

func CreateHisModePrescriptionTemplate(template *models.HisPrescriptionModeTemplate) (err error) {
	err = writeDb.Create(&template).Error
	return

}

func SaveHisModePrescriptionTemplate(template *models.HisPrescriptionModeTemplate) (err error) {
	err = writeDb.Save(&template).Error
	return

}

func CreateHisModePrescriptionInfoTemplate(s *models.HisPrescriptionInfoModeTemplate) (err error) {
	err = writeDb.Save(&s).Error
	return
}

func CreateHisModePrescriptionAdviceTemplate(s *models.HisPrescriptionAdviceModeTemplate) (err error) {
	err = writeDb.Save(&s).Error
	return
}

func GetHisModePrescriptionTemplateByModeId(mode_id int64, patient_id int64, user_org_id int64) (prescription models.HisPrescriptionModeTemplate, err error) {
	err = readDb.Model(&models.HisPrescriptionModeTemplate{}).Where("mode = ? AND status = 1 AND patient_id = ? and user_org_id = ?", mode_id, patient_id, user_org_id).First(&prescription).Error
	return
}

func GetHisModePrescriptionTemplateByModeIdTwo(mode_id int64, patient_id int64, user_org_id int64, id int64) (prescription models.HisPrescriptionModeTemplate, err error) {
	err = readDb.Model(&models.HisPrescriptionModeTemplate{}).Where("mode = ? AND status = 1 AND patient_id = ? and user_org_id = ? and id <> ?", mode_id, patient_id, user_org_id, id).First(&prescription).Error
	return
}

func CreateHisModePrescriptionProjectTemplate(project *models.HisPrescriptionProjectModeTemplate) (err error) {
	err = writeDb.Save(&project).Error
	return
}

func DelelteHisModePrescriptionTemplate(id int64, user_org_id int64) (err error) {
	err = writeDb.Model(&models.HisPrescriptionModeTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0}).Error
	err = writeDb.Model(&models.HisPrescriptionInfoModeTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
	return
}

func DelelteHisModePrescriptionProjectTemplate(id int64, user_org_id int64) (err error) {
	err = writeDb.Model(&models.HisPrescriptionProjectModeTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error

	return
}

func DelelteHisModePrescriptionAdviceTemplate(id int64, user_org_id int64) (err error) {
	err = writeDb.Model(&models.HisPrescriptionAdviceModeTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
	return
}

func DelelteHisPrescriptionInfoModeTemplate(id int64, user_org_id int64) (err error) {
	err = writeDb.Model(&models.HisPrescriptionInfoModeTemplate{}).Where("user_org_id = ? AND id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
	err = writeDb.Model(&models.HisPrescriptionAdviceModeTemplate{}).Where("user_org_id = ? AND prescription_id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
	err = writeDb.Model(&models.HisPrescriptionProjectModeTemplate{}).Where("user_org_id = ? AND prescription_id = ?", user_org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
	return
}

func GetAdviceGatherDetaiList(keyword string, item_type int64, patient_id int64, org_id int64, start_time int64, end_time int64) (orderInfo []*models.VmHisOrderInfo, err error) {

	db := XTReadDB().Model(&orderInfo).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ? and advice_id > 0", org_id)
	}
	if start_time > 0 {
		db = db.Where("ctime >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("ctime <=?", end_time)
	}
	err = db.Preload("VmHisDoctorAdviceInfo", "patient_id = ? and advice_date>=? and advice_date<=?", patient_id, start_time, end_time).Group("advice_id").Scan(&orderInfo).Error
	return orderInfo, err
}

func GetProjectGatherDetailList(keyword string, item_type int64, patient_id int64, org_id int64, start_time int64, end_time int64) (orderInfo []*models.VmHisOrderInfo, err error) {
	db := XTReadDB().Model(&orderInfo).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ? and project_id > 0", org_id)
	}
	if start_time > 0 {
		db = db.Where("ctime >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("ctime <=?", end_time)
	}
	err = db.Group("project_id").Preload("HisPrescriptionProject", "patient_id = ? and record_date>=? and record_date<=?", patient_id, start_time, end_time).Find(&orderInfo).Error
	return orderInfo, err
}

func GetGatherHisOrder(keyword string, item_type int64, patient_id int64, org_id int64, start_time int64, end_time int64) (order []*models.GatherHisOrder, err error) {

	db := XTReadDB().Model(&order).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ?", org_id)
	}
	if start_time > 0 {
		db = db.Where("settle_accounts_date >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("settle_accounts_date<=?", end_time)
	}

	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}

	if len(keyword) > 0 {

	}

	err = db.Find(&order).Error

	return order, err
}

func GetGatherHisOrderTwo(keyword string, item_type int64, patient_id int64, org_id int64, start_time int64, end_time int64) (models.GatherHisOrder, error) {

	hisOrder := models.GatherHisOrder{}
	db := XTReadDB().Model(&hisOrder).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ?", org_id)
	}
	if start_time > 0 {
		db = db.Where("settle_accounts_date >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("settle_accounts_date<=?", end_time)
	}

	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}

	if len(keyword) > 0 {

	}

	err = db.Last(&hisOrder).Error

	return hisOrder, err
}

func GetHisSummaryPatientInfo(org_id int64, patient_id int64, start_time int64, end_time int64) (info models.HisPatient, err error) {
	err = readDb.Model(&models.HisPatient{}).Where("user_org_id = ? AND status = 1 AND record_date >= ? AND  record_date <= ? AND patient_id = ?", org_id, start_time, end_time, patient_id).Last(&info).Error
	return
}

func GetHisSummaryPatientInfoSix(org_id int64, patient_id int64, start_time int64, end_time int64) (info models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("user_org_id = ? AND status = 1 AND record_date >= ? AND  record_date <= ? AND patient_id = ?", org_id, start_time, end_time, patient_id).Last(&info).Error
	return
}

func GetDetailPatients(keywords string, org_id int64) (patients []*models.ChargePatientTwo, err error) {
	if len(keywords) == 0 {
		err = readDb.Model(&models.ChargePatientTwo{}).Where("user_org_id = ? AND status = 1", org_id).Find(&patients).Error
	}

	if len(keywords) > 0 {
		keyword := "%" + keywords + "%"
		err = readDb.Model(&models.ChargePatientTwo{}).Where("user_org_id = ? AND status = 1 and name like ?", org_id, keyword).Find(&patients).Error
	}

	return
}

func GetHisDepatment(user_org_id int64) (hisdepartment []*models.XtHisDepartment, err error) {

	err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&hisdepartment).Error
	return hisdepartment, err
}

func GetHisPatientInfoFive(org_id int64, patient_id int64, startime int64, endtime int64) (info models.HisPatient, err error) {
	err = readDb.Model(&models.HisPatient{}).Where("user_org_id = ? AND status = 1 and patient_id = ? AND record_date >= ? and record_date <=?", org_id, patient_id, startime, endtime).Last(&info).Error
	return
}

func GetHisPrescriptionTenty(org_id int64, patient_id int64, start_time int64, end_time int64, keyword string, item_type int64) (prescription []*models.HisPrescription, err error) {

	db := readDb.Model(&models.HisPrescription{}).Where("status = 1")
	if org_id > 0 {
		db = db.Where("user_org_id = ?", org_id)
	}
	if start_time > 0 {
		db = db.Where("record_date >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("record_date <=?", end_time)
	}
	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}
	if len(keyword) > 0 {
		err = db.Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
		}).Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1")
		}).Preload("HisAdditionalCharge", "status = 1 AND user_org_id = ?", org_id).Find(&prescription).Error
	} else {
		err = db.Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
		}).Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1")
		}).
			Preload("HisAdditionalCharge", "status = 1 AND user_org_id = ?", org_id).Find(&prescription).Error
	}

	return
}

func GetHisLastOrder(org_id int64, patient_id int64, start_time int64, end_time int64) (models.HisOrder, error) {

	order := models.HisOrder{}

	err := XTReadDB().Where("user_org_id = ? and patient_id = ? and ctime>=? and ctime<=? and fa_piao_number<> ''", org_id, patient_id, start_time, end_time).Last(&order).Error
	return order, err
}

func GetHisSchedule(patient_id int64, org_id int64, start_time int64, end_time int64) (schedule []*models.XtSchedule, err error) {

	err = XTReadDB().Where("patient_id = ? and user_org_id = ? and schedule_date >=? and schedule_date<=? and status = 1", patient_id, org_id, start_time, end_time).Find(&schedule).Error
	return schedule, err
}

func GetHisChargeOrder(org_id int64, patient_id int64, start_time int64, end_time int64, keyword string, item_type int64) (order []*models.HisChargeOrder, err error) {

	db := XTReadDB().Model(&order).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ?", org_id)
	}
	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}

	if start_time > 0 {
		db = db.Where("settle_accounts_date>=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("settle_accounts_date<=?", end_time)
	}
	err = db.Preload("HisChargeOrderInfo", func(db *gorm.DB) *gorm.DB {
		return db.Where("status = 1").Preload("HisChargePrescriptionProject", "status=1").Preload("HisChargeDoctorAdviceInfo", "status=1")
	}).Error
	return order, err
}

func GetAllHisOrderList(org_id int64, patient_id int64, start_time int64, end_time int64) (order []*models.VmHisOrder, err error) {

	db := XTReadDB().Model(&order).Where("status = 1")

	if org_id > 0 {
		db = db.Where("user_org_id = ?", org_id)
	}
	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}

	if start_time > 0 {
		db = db.Where("settle_accounts_date >=?", start_time)
	}
	if end_time > 0 {
		db = db.Where("settle_accounts_date<=?", end_time)
	}

	err = db.Find(&order).Error

	return order, err
}

func GetHisOrderListByMedChrgitemType(patient_id int64, startime int64, endtime int64, orgid int64) (info []*models.HisOrderInfoTwenty, err error) {

	db := XTReadDB().Model(&models.HisOrderInfoTwenty{}).Where("status = 1")

	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}
	if startime > 0 {
		db = db.Where("settle_accounts_date >= ?", startime)
	}
	if endtime > 0 {
		db = db.Where("settle_accounts_date<=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("user_org_id = ?", orgid)
	}
	err = db.Group("med_chrgitm_type").Preload("Patients", "status = 1").Preload("HisHospitalCheckRecordTwenty", "status = 1").Find(&info).Error
	return info, err
}

func GetOrderInfoList(patient_id int64, startime int64, endtime int64, orgid int64) (info []*models.HisOrderInfoTwenty, err error) {
	db := XTReadDB().Model(&models.HisOrderInfoTwenty{}).Where("status = 1")

	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}
	if startime > 0 {
		db = db.Where("upload_date >= ?", startime)
	}
	if endtime > 0 {
		db = db.Where("upload_date<=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("user_org_id = ?", orgid)
	}
	err = db.Preload("HisOrderTenty", "status = 1").Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
		return db.Where("status = 1 AND user_org_id = ?", orgid).Preload("HisProject").Preload("GoodInfo", "status=1")
	}).Preload("HisDoctorAdvice", "status = 1").Preload("Patients", "status = 1").Preload("HisHospitalCheckRecordTwenty", "status = 1").Find(&info).Error
	return info, err
}

func GetHisOrderListTwenty(patient_id int64, startime int64, endtime int64, orgid int64) (info []*models.HisOrderTenty, err error) {

	db := XTReadDB().Model(&models.HisOrderTenty{}).Where("status = 1")

	if patient_id > 0 {
		db = db.Where("patient_id = ?", patient_id)
	}

	if startime > 0 {
		db = db.Where("settle_accounts_date>=?", startime)
	}

	if endtime > 0 {
		db = db.Where("settle_accounts_date<=?", endtime)
	}

	if orgid > 0 {
		db = db.Where("user_org_id = ?", orgid)
	}

	err = db.Preload("HisOrderInfoTwenty", "status=1").Preload("Patients", "status = 1").Preload("HisHospitalCheckRecordTwenty", "status=1").Order("settle_accounts_date asc").Find(&info).Error
	return info, err
}

func GetHisAdviceOrderList() {

}
