package exam

import(
	"zhenyihuibao/conf"
	"errors"
	"zhenyihuibao/pkg/logging"
)

var(
	AnswerLogStatusRight = 1
	AnswerLogStatusWrong = 0
)

//试卷答题记录表
type QuesUserAnswerLog struct {
	Id        			int   		`xorm:"not null pk autoincr INT(10)" json:"id"`
	QuesBankId			int			`xorm:"not null comment('试卷id') INT(11)"  json:"quesBankId"`
	UserId 				int			`xorm:"not null comment('userId') INT(11)" json:"userId"`
	QuesTitleId 		int			`xorm:"not null comment('题目id') INT(11)" json:"quesTitleId"`
	QuesUserScoreId   	int 		`xorm:"not null comment('分值id') INT(11)" json:"quesUserScoreId"`
	Answer 				string 		`xorm:"not null comment('答案') index VARCHAR(255)" json:"answer"`
	IsRight				int			`xorm:"not null comment('userId') INT(11)" json:"isRight"`
	Score				int			`xorm:"not null comment('分值') INT(11)" json:"score"`
}

func (this *QuesUserAnswerLog) GetRowByUserScoreId(userScoreId, quesTitId int) (itemRow *QuesUserAnswerLog, err error) {
	rowModel := &QuesUserAnswerLog{
		QuesUserScoreId:	userScoreId,
		QuesTitleId:		quesTitId,
	}
	has, err := conf.SqlServer.Get(rowModel)
	if err != nil || has == false {
		err =  errors.New("找不到该数据!")
		return
	}
	itemRow = rowModel
	return
}

func (this *QuesUserAnswerLog) AddAnswerLog(itemRow *QuesUserAnswerLog)(err error){
	affected, err := conf.SqlServer.Insert(itemRow)
	if err != nil {
		return
	}
	logging.MyLogger.Infof("插入答案 影响行数- %s", affected)
	return
}

func (this *QuesUserAnswerLog) GetTotalScore(userScoreId int)(total int,err error){
	session := conf.SqlServer.Where("1=1")
	ss := new(QuesUserAnswerLog)
	float64_total, err := session.
		Where("ques_user_score_id =?", userScoreId).
		Where("is_right =?", AnswerLogStatusRight).
		Sum(ss, "score")
	if err != nil {
		return
	}
	total = int(float64_total)
	return
}