package models

import (
	"eta/eta_mini_crm_ht/utils"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// TemplateUsers
// @Description: 临时用户表
type TemplateUsers struct {
	Id           int       `orm:"pk" description:"用户id"`
	UserName     string    `description:"姓名"`
	Mobile       string    `description:"手机号"`
	OpenId       string    `description:"用户openid"`
	UnionId      string    `description:"用户unionid"`
	IsDeleted    int       `description:"是否已删除"`
	GzhOpenId    string    `description:"用户公众号openid"`
	ReadCount    int       `description:"阅读次数"`
	LastReadTime time.Time `description:"最近一次阅读时间"`
	CreatedTime  time.Time `description:"创建时间"`
	UpdatedTime  time.Time `description:"变更时间"`
}

// TemplateUsers
// @Description: 临时用户表
type TemplateUsersItem struct {
	Id           int    `orm:"pk" description:"用户id"`
	UserName     string `description:"姓名"`
	Mobile       string `description:"手机号"`
	OpenId       string `description:"用户openid"`
	UnionId      string `description:"用户unionid"`
	GzhOpenId    string `description:"用户公众号openid"`
	ReadCount    int    `description:"阅读次数"`
	LastReadTime string `description:"最近一次阅读时间"`
	CreatedTime  string `description:"创建时间"`
	UpdatedTime  string `description:"变更时间"`
}

// GetPageTemplateUserList
// @Description: 获取分页用户数据
// @author: Roc
// @datetime 2024-08-09 16:45:15
// @param condition string
// @param pars []interface{}
// @param startSize int
// @param pageSize int
// @return total int
// @return items []*TemplateUsers
// @return err error
func GetPageTemplateUserList(condition string, pars []interface{}, sortStr string, startSize, pageSize int) (total int, items []*TemplateUsers, err error) {
	o := orm.NewOrm()
	totalSql := `SELECT count(1) ct FROM template_users AS a WHERE is_deleted = 0 `
	if condition != "" {
		totalSql += condition
	}
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}

	sql := `SELECT * FROM template_users AS a WHERE  is_deleted = 0 `
	if condition != "" {
		sql += condition
	}
	if sortStr != `` {
		sql += ` ORDER BY ` + sortStr
	}

	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)

	return
}
func GetTemplateUserList() (items []*TemplateUsers, err error) {
	o := orm.NewOrm()

	sql := `SELECT * FROM template_users WHERE  is_deleted = 0 `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// ToItem
// @Description: 转结构体返回
// @author: Roc
// @receiver m
// @datetime 2024-08-09 16:50:41
// @return item TemplateUsersItem
func (m *TemplateUsers) ToItem() (item TemplateUsersItem) {
	item = TemplateUsersItem{
		Id:           m.Id,
		UserName:     m.UserName,
		Mobile:       m.Mobile,
		OpenId:       m.OpenId,
		UnionId:      m.UnionId,
		GzhOpenId:    m.GzhOpenId,
		ReadCount:    m.ReadCount,
		LastReadTime: "",
		CreatedTime:  m.CreatedTime.Format(utils.FormatDateTime),
		UpdatedTime:  m.UpdatedTime.Format(utils.FormatDateTime),
	}
	if !m.LastReadTime.IsZero() {
		item.LastReadTime = m.LastReadTime.Format(utils.FormatDateTime)
	}

	return
}
