package eta_trial

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type EtaTrialQuestionnaireRecord struct {
	RecordId        int    `orm:"column(record_id);pk" description:"记录id"`
	UserName        string `description:"用户名"`
	CompanyName     string `description:"公司名"`
	Position        string `description:"职位"`
	Options         string `description:"选项"`
	Mobile          string `description:"手机"`
	Type            int    `description:"1单选 2多选 3简答题"`
	QuestionnaireId int    `description:"题目id"`
	CreateTime      time.Time
}

type EtaTrialQuestionnaireRecordStatistic struct {
	QuestionnaireId int    `description:"题目id"`
	Options         string `description:"选项"`
	Count           int    `description:"数量"`
	Type            int    `description:"1单选 2多选 3简答题"`
	UserName        string `description:"用户名"`
}

func GetETATrialQuestionnaireStatistic(cond string) (items []*EtaTrialQuestionnaireRecordStatistic, err error) {
	sql := `SELECT a.questionnaire_id, a.options,a.type,COUNT(a.options) AS count,a.user_name FROM eta_trial_questionnaire_record AS a  WHERE 1=1 `
	o := orm.NewOrm()
	if cond != "" {
		sql += cond
	}
	sql += "GROUP BY a.options"
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type EtaTrialQuestionnaireRecordStatisticResp struct {
	QuestionnaireId int                                           `description:"题目id"`
	Question        string                                        `description:"题目"`
	Options         []*EtaTrialQuestionnaireRecordStatisticOption `description:"选项和数量"`
	Answers         []EtaTrialQuestionnaireRecordStatisticAnswer  `description:"简答题列表"`
	Type            int                                           `description:"1单选 2多选 3简答题"`
	IsMust          int                                           `description:"是否必填"`
}

type EtaTrialQuestionnaireRecordStatisticAnswer struct {
	Answer   string `description:"答案"`
	UserName string `description:"用户名"`
}

type EtaTrialQuestionnaireRecordStatisticOption struct {
	Index      int     `description:"索引"`
	Option     string  `description:"选项"`
	Count      int     `description:"数量"`
	Percentage float64 `description:"百分比"`
}

type EtaTrialQuestionnaireRecordStatisticCount struct {
	QuestionnaireId int `description:"题目id"`
	Count           int `description:"数量"`
}

func GetETATrialQuestionnaireStatisticCount() (items []*EtaTrialQuestionnaireRecordStatisticCount, err error) {
	sql := `SELECT questionnaire_id,COUNT(questionnaire_id) AS count FROM eta_trial_questionnaire_record WHERE type != 2 GROUP BY questionnaire_id `
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetETATrialQuestionnaireAnswerListById(id, startSize, pageSize int) (items []*EtaTrialQuestionnaireRecord, err error) {
	sql := `SELECT * FROM eta_trial_questionnaire_record WHERE questionnaire_id=? ORDER BY create_time DESC LIMIT ?,?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, id, startSize, pageSize).QueryRows(&items)
	return
}

func GetETATrialQuestionnaireAnswerListByIdCount(id int) (count int, err error) {
	sql := `SELECT count(1) FROM eta_trial_questionnaire_record WHERE questionnaire_id=? ORDER BY create_time DESC`
	o := orm.NewOrm()
	err = o.Raw(sql, id).QueryRow(&count)
	return
}

func GetETATrialQuestionnaireDetailList(startSize, pageSize int) (items []*EtaTrialQuestionnaireRecord, err error) {
	sql := `SELECT * FROM eta_trial_questionnaire_record ORDER BY create_time DESC LIMIT ?,?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
	return
}

func GetETATrialQuestionnaireDetailListByMobile(mobile, startDate, endDate string) (items []*EtaTrialQuestionnaireRecord, err error) {
	sql := `SELECT * FROM eta_trial_questionnaire_record WHERE mobile=? AND create_time >= ? and create_time <= ? ORDER BY create_time DESC`
	o := orm.NewOrm()
	_, err = o.Raw(sql, mobile,startDate, endDate).QueryRows(&items)
	return
}

type EtaTrialQuestionnaireDetailItem struct {
	QuestionnaireId int      ` description:"问卷题目id"`
	Question        string   `description:"题目"`
	Type            int      `description:"1单选 2多选 3简答题"`
	Sort            int      `description:"排序"`
	Options         []string `description:"选项"`
	UserOptions     string   `description:"回答"`
	IsMust int
	CreateTime      string
}

func GetETATrialQuestionnaireDetailListCount() (count int, err error) {
	sql := `SELECT count(1) AS count FROM eta_trial_questionnaire_record  `
	o := orm.NewOrm()
	err = o.Raw(sql).QueryRow(&count)
	return
}

type EtaTrialQuestionnaireRecordRespItem struct {
	RecordId        int    `orm:"column(record_id);pk" description:"记录id"`
	UserName        string `description:"用户名"`
	CompanyName     string `description:"公司名"`
	Position        string `description:"职位"`
	Options         string `description:"选项"`
	Mobile          string `description:"手机"`
	Type            int    `description:"1单选 2多选 3简答题"`
	QuestionnaireId int    `description:"题目id"`
	IsMust          int
	CreateTime      string
}

type QuestionnaireDetailListResp struct {
	List   []*EtaTrialQuestionnaireRecordRespItem
	Paging *paging.PagingItem
}
