package cygx

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

type CygxInterviewApply struct {
	InterviewApplyId int    `description:"访谈申请id"`
	UserId           int    `description:"用户id"`
	UserRealName     string `description:"姓名"`
	Mobile           string `description:"手机号"`
	CompanyId        int    `description:"公司id"`
	CompanyName      string `description:"公司名称"`
	SalesRealName    string `description:"所属销售"`
	Status           string `description:"申请状态:默认:待邀请;'待访谈','已完成','已取消'"`
	InterviewTime    string `description:"访谈时间"`
	CreateTime       string `description:"申请时间"`
	ModifyTime       string `description:"修改时间"`
	ArticleId        int    `description:"纪要id"`
	ArticleTitle     string `description:"纪要标题"`
	ArticleIdMd5     string `description:"纪要id"`
	CategoryName     string `description:"所属行业"`
}

type InterviewApplyListResp struct {
	Paging *paging.PagingItem
	List   []*CygxInterviewApply
}

func GetInterviewApplyList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxInterviewApply, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT a.*,d.category_name
			FROM cygx_interview_apply AS a
            LEFT JOIN cygx_article AS d on a.article_id=d.article_id
			WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetInterviewApplyListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_interview_apply AS a
            WHERE 1=1    `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetCygxInterviewApplyDetail(interviewApplyId int) (item *CygxInterviewApply, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT * FROM cygx_interview_apply WHERE interview_apply_id=? `
	err = o.Raw(sql, interviewApplyId).QueryRow(&item)
	return
}

// 申请领取参数
type InterviewApplyStatusReq struct {
	InterviewApplyId int    `description:"访谈申请id"`
	InterviewTime    string `description:"访谈时间"`
	HandleType       int    `description:"操作类型：1:更新，2：取消"`
}

func InterviewApplyStatusModify(interviewApplyId int, status, interviewTime string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	if interviewTime != "" {
		sql = `UPDATE cygx_interview_apply SET status=?,modify_time=NOW() ,interview_time=? WHERE interview_apply_id=? `
		_, err = o.Raw(sql, status, interviewTime, interviewApplyId).Exec()
	} else {
		sql = `UPDATE cygx_interview_apply SET status=?,modify_time=NOW() WHERE interview_apply_id=? `
		_, err = o.Raw(sql, status, interviewApplyId).Exec()
	}
	return
}
