package rai_serve

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

// 权益服务明细表
type CygxRaiServeBill struct {
	BillId              int       `orm:"column(bill_id);pk" description:"服务明细主键ID"`
	Content             string    `comment:"服务内容说明"`
	ServeTypeId         int       `comment:"服务类型ID"`
	ServeTypeName       string    `comment:"服务类型"`
	UserId              int       `comment:"用户ID"`
	Mobile              string    `comment:"手机号"`
	Email               string    `comment:"邮箱"`
	CompanyId           int       `comment:"公司ID"`
	CompanyName         string    `comment:"公司名称"`
	RealName            string    `comment:"用户实际名称"`
	RegisterPlatform    int       `comment:"来源 1小程序，2：网页"`
	ServeCount          float64   `comment:"服务量小计"`
	IsKp                int       `comment:"是否是KP,1:是、0：否"`
	SourceId            int       `comment:"来源ID"`
	Source              string    `comment:"来源 "`
	WeekStartDate       string    `comment:"周一开始日期"`
	WeekEndDate         string    `comment:"周日结束日期"`
	MonthStartDate      string    `comment:"月份开始日期"`
	MonthEndDate        string    `comment:"月份结束日期"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	CreateTime          time.Time `comment:"创建时间"`
	ViewTime            string    `comment:"浏览时间"`
	ActivityId          int       `description:"活动ID"`
	RecordId            int       `description:"日志ID"`
}

// Redis对列消息中的结构体
type RaiServeBillRedis struct {
	Content          string    `comment:"服务内容说明"`
	UserId           int       `comment:"用户ID"`
	ComapnyId        int       `comment:"公司ID"`
	SourceId         int       `comment:"来源ID"`
	Source           string    `comment:"来源"`
	RegisterPlatform int       `comment:"来源 1小程序，2：网页"`
	ViewTime         time.Time `comment:"浏览时间"`
}

// 添加
func AddCygxRaiServeBill(item *CygxRaiServeBill) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

func GetCygxRaiServeBillCountByUserAndSource(userId, sourceId int, source string) (count int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT COUNT(1) AS count FROM  cygx_rai_serve_bill WHERE  user_id = ?  AND source_id = 2 AND  source = ?  `
	err = o.Raw(sql, userId, sourceId, source).QueryRow(&count)
	return
}

// 列表
func GetCygxRaiServeBillListAll(condition string, pars []interface{}) (items []*CygxRaiServeBill, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_rai_serve_bill   WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// AddCygxRaiServeBillMulti 批量添加
func AddCygxRaiServeBillMulti(items []*CygxRaiServeBill) (err error) {
	if len(items) == 0 {
		return
	}
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	if len(items) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// 批量删除
func DelCygxRaiServeCompanyMulti(delBillIds []int) (err error) {
	lenArr := len(delBillIds)
	if lenArr == 0 {
		return
	}
	o := orm.NewOrm()
	sql := ` DELETE FROM cygx_rai_serve_bill  WHERE bill_id  IN  (` + utils.GetOrmInReplace(lenArr) + `) `
	_, err = o.Raw(sql, delBillIds).Exec()

	return
}

// UpdateActivitySignupDetailMulti 批量修改
func UpdateCygxRaiServeBillMulti(items []*CygxRaiServeBill) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw(` UPDATE cygx_rai_serve_bill SET 
							month_start_date = ?,
							month_end_date = ?
							WHERE bill_id = ? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.MonthStartDate,
			v.MonthEndDate,
			v.BillId)
		if err != nil {
			return
		}
	}
	return
}
