package Patient

import (
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"github.com/beego/beego/v2/adapter/orm"
	"time"
)

var (
	Question1 = "初入病房时医护人员及时询问病情和检查"
	Question2 = "是否有告知管床医生和护士是谁"
	Question3 = "对住院期间入院注意事项的交代"
	Question4 = "在住院期间，对病情的解释"
	Question5 = "对医护人员的服务态度"
)

// 患者满意度
type PatientSatisfaction struct {
	Id          int    `orm:"column(ID);size(11);auto;pk"`
	T_id        string `orm:"size(256);null"`       // 发送id
	T_uid       int    `orm:"index;size(100);null"` // 用户id
	T_pid       int    `orm:"index;size(100);null"` // 患者id
	T_question1 string `orm:"size(256);null"`       // 问题1
	T_question2 string `orm:"size(256);null"`       // 问题2
	T_question3 string `orm:"size(256);null"`       // 问题3
	T_question4 string `orm:"size(256);null"`       // 问题4
	T_question5 string `orm:"size(256);null"`       // 问题5

	CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
}

func (t *PatientSatisfaction) TableName() string {
	return "patient_satisfaction" // 数据库名称   // ************** 替换 FormulaList **************
}

type PatientSatisfaction_R struct {
	Id          int
	T_id        string // 用户id
	T_uid       int    // 用户id
	T_pid       int    // 患者id
	T_p_name    string // 患者名称
	T_question1 string // 问题1
	T_question2 string // 问题2
	T_question3 string // 问题3
	T_question4 string // 问题4
	T_question5 string // 问题5
	CreateTime  string //auto_now_add 第一次保存时才设置时间
}

func PatientSatisfactionToPatientSatisfaction_R(r PatientSatisfaction) (m PatientSatisfaction_R) {
	m.Id = r.Id
	m.T_id = r.T_id
	m.T_uid = r.T_uid
	m.T_pid = r.T_pid
	m.T_p_name = Read_Patient_T_name_Get(r.T_pid)
	m.T_question1 = r.T_question1
	m.T_question2 = r.T_question2
	m.T_question3 = r.T_question3
	m.T_question4 = r.T_question4
	m.T_question5 = r.T_question5
	m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
	return
}

func init() {
	//注册模型
	orm.RegisterModel(new(PatientSatisfaction))
}

// 获取 ById
func Read_PatientSatisfaction_ByT_uid(T_uid int) (r []PatientSatisfaction, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(PatientSatisfaction))
	_, err = qs.Filter("T_uid", T_uid).All(&r)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return
}

// 获取 ById
func Read_PatientSatisfaction_List(T_uid int, page, page_z int) (r_ []PatientSatisfaction_R, cnt int64) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(PatientSatisfaction))
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = int64((page - 1) * page_z)
	}
	var r []PatientSatisfaction
	_, err := qs.Limit(page_z, offset).Filter("T_uid", T_uid).OrderBy("-Id").All(&r)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	cnt, err = qs.Filter("T_uid", T_uid).Count()
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}

	for _, v := range r {
		r_ = append(r_, PatientSatisfactionToPatientSatisfaction_R(v))
	}

	return
}

// 添加
func Add_PatientSatisfaction(r PatientSatisfaction) (id int64, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(PatientSatisfaction))
	var obj PatientSatisfaction
	err = qs.Filter("T_id", r.T_id).One(&obj)
	if err != nil && err.Error() == orm.ErrNoRows.Error() {
		id, err = o.Insert(&r)
		if err != nil {
			logs.Error(lib.FuncName(), err)
		}
		return
	}
	if obj.Id > 0 {
		r.Id = obj.Id
		_, err := o.Update(&r, "T_question1", "T_question2", "T_question3", "T_question4", "T_question5")
		if err != nil {
			logs.Error(lib.FuncName(), err)
		}
	}
	return int64(r.Id), err
}

// 获取 ById
func Read_PatientSatisfaction_ByT_id(T_id string) (r PatientSatisfaction, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(PatientSatisfaction))
	err = qs.Filter("T_id", T_id).One(&r)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return
}
