package cygx

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

type CygxAdvice struct {
	AdviceId      int    `description:"优化建议id"`
	UserId        int    `description:"用户id"`
	UserRealName  string `description:"姓名"`
	CompanyId     int    `description:"公司id"`
	CompanyName   string `description:"公司名称"`
	SalesRealName string `description:"所属销售"`
	Advice        string `description:"优化建议"`
	AdviceImgUrl  string `description:"图片，多张用#分割"`
	CreateTime    string `description:"访谈时间"`
	Mobile        string `description:"手机号"`
	Email         string `description:"邮箱"`
}

type AdviceListResp struct {
	Paging *paging.PagingItem
	List   []*CygxAdvice
}

func GetAdviceListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM cygx_advice AS a
			LEFT JOIN wx_user AS b ON a.user_id=b.user_id
			LEFT JOIN company AS c ON a.company_id=c.company_id`
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetAdviceList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxAdvice, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.real_name AS user_real_name,c.company_name,b.mobile,b.email FROM cygx_advice AS a
			LEFT JOIN wx_user AS b ON a.user_id=b.user_id
			LEFT JOIN company AS c ON a.company_id=c.company_id
			 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}
