package models

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

type CygxProductInteriorHistory struct {
	Id                int `orm:"column(id);pk"`
	ProductInteriorId int
	UserId            int
	CreateTime        time.Time
	Mobile            string    `description:"手机号"`
	Email             string    `description:"邮箱"`
	CompanyId         int       `description:"公司id"`
	CompanyName       string    `description:"公司名称"`
	ModifyTime        time.Time `description:"修改时间"`
	RealName          string    `description:"用户实际名称"`
	SellerName        string    `description:"所属销售"`
}

// 添加历史信息
func AddCygxProductInteriorHistory(item *CygxProductInteriorHistory) (lastId int64, err error) {
	o := orm.NewOrm()
	item.ModifyTime = time.Now()
	lastId, err = o.Insert(item)
	return
}

type ListProductInteriorPvUvResp struct {
	ProductInteriorId int `description:"文章ID"`
	Pv                int `description:"pv"`
	Uv                int `description:"pv"`
}

// 列表
func GetCygxProductInteriorHistoryList(condition string, pars []interface{}) (items []*ListProductInteriorPvUvResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			COUNT( 1 ) AS pv,
			product_interior_id
		FROM
			cygx_product_interior_history  WHERE 1 = 1  `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY product_interior_id `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type ListProductInteriorPvResp struct {
	ProductInteriorId int `description:"文章ID"`
	Pv                int `description:"pv"`
}

// 列表
func GetCygxProductInteriorHistoryListPv(condition string, pars []interface{}) (items []*ListProductInteriorPvResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT  product_interior_id ,COUNT(*)  as  pv  FROM cygx_product_interior_history WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += " GROUP BY product_interior_id "
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type ListProductInteriorResp struct {
	ProductInteriorId int    `description:"文章ID"`
	Title             string `description:"标题"`
	UserId            int    `description:"用户ID"`
	Mobile            string `description:"手机号"`
	Email             string `description:"邮箱"`
	CompanyId         int    `description:"公司id"`
	CompanyName       string `description:"公司名称"`
	RealName          string `description:"用户实际名称"`
	CreateTime        string `description:"创建时间"`
	RegisterPlatform  int    `description:"来源 1小程序，2：网页"`
	RecordId          int    `description:"日志ID"`
}

// 列表
func GetCygxProductInteriorHistoryListData(condition string, pars []interface{}) (items []*ListProductInteriorResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			a.product_interior_id,
			a.title,
			b.user_id,
			b.id as record_id,
			b.real_name,
			b.mobile,
			b.email,
			b.company_id,
			b.company_name,
			b.create_time 
		FROM
			cygx_product_interior AS a
			INNER JOIN cygx_product_interior_history AS b ON a.product_interior_id = b.product_interior_id   WHERE 1= 1   `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
