package models

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

type CygxArticleHistoryRecordAll struct {
	Id               int `orm:"column(id);pk"`
	ArticleId        int
	UserId           int
	CreateTime       string
	ModifyTime       time.Time
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	CompanyId        int       `description:"公司id"`
	CompanyName      string    `description:"公司名称"`
	StopTime         int       `description:"停留时间"`
	OutType          int       `description:"退出方式，1正常退出，2强制关闭"`
	Source           string    `description:"来源，MOBILE：手机端，PC：电脑端"`
	RealName         string    `description:"用户实际名称"`
	CreateDateApi    time.Time `description:"同步创建时间"`
	CelueHistoryId   int       `description:"策略平台记录的ID"`
	Platfor          int       `description:"PV阅读记录来源，1：查研观向，2：策略平台"`
	IsDel            int       `description:"是否删除"`
	RegisterPlatform int       `description:"来源"`
	CompanyStatus    string    `description:"公司状态"`
	SellerName       string    `description:"所属销售"`
}

// 获取数量
func GetCygxArticleHistoryRecordAllCountBycondition(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_article_history_record_all as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxArticleHistoryRecordAllList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxArticleHistoryRecordAll, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_article_history_record_all as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type CygxArticleHistoryRecordAllResp struct {
	Id               int `orm:"column(id);pk"`
	ArticleId        int
	UserId           int
	CreateTime       string
	ModifyTime       time.Time
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	CompanyId        int       `description:"公司id"`
	CompanyName      string    `description:"公司名称"`
	StopTime         int       `description:"停留时间"`
	OutType          int       `description:"退出方式，1正常退出，2强制关闭"`
	Source           string    `description:"来源，MOBILE：手机端，PC：电脑端"`
	RealName         string    `description:"用户实际名称"`
	CreateDateApi    time.Time `description:"同步创建时间"`
	CelueHistoryId   int       `description:"策略平台记录的ID"`
	Platfor          int       `description:"PV阅读记录来源，1：查研观向，2：策略平台"`
	IsDel            int       `description:"是否删除"`
	RegisterPlatform int       `description:"来源"`
	Title            string    `description:"标题"`
	CategoryName     string    `description:"一级分类"`
}

// 列表
func GetCygxArticleHistoryRecordAllListNoLimit(condition string, pars []interface{}) (items []*CygxArticleHistoryRecordAllResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT 	art.* ,a.title,a.category_name FROM cygx_article_history_record_all as art INNER JOIN cygx_article AS a  ON a.article_id = art.article_id    WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// AddCygxArticleHistoryRecordAllMulti 批量添加
func AddCygxArticleHistoryRecordAllMulti(itemsCelue []*CygxCelueArticleHistoryRecord, items []*CygxArticleHistoryRecordAll) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	//批量插入新的关联数据
	if len(itemsCelue) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(itemsCelue), itemsCelue)
	}

	//批量插入新的关联数据
	if len(items) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// 列表
func GetCygxArticleHistoryRecordAllListArticleIdCrm16_5() (items []*CygxArticleHistoryRecordAll, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			article_id
		FROM
			cygx_article_history_record_all 
		WHERE
			create_time > '2024-09-01' 
			AND user_id > 0 
			AND is_del = 0 
		GROUP BY
			article_id `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 列表
func GetCygxArticleHistoryRecordAllListUserIdCrm16_5() (items []*CygxArticleHistoryRecordAll, err error) {
	o := orm.NewOrm()
	sql := `SELECT
    		create_time,
    		article_id,
    		mobile,
			user_id
		FROM
			cygx_article_history_record_all 
		WHERE
			create_time > '2024-09-01' 
			AND user_id > 0 
			AND is_del = 0 `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
