package model

import (
	"database/sql"
	"fmt"

	"git.jiaxianghudong.com/go/logs"
	"git.jiaxianghudong.com/webs/pkg/dbx"
)

type LuckyDrawInviteLog struct {
	UserId       uint32 `sql:"user_id" json:"user_id"`               //被邀请人（用户ID）
	InviteUserId int    `sql:"invite_user_id" json:"invite_user_id"` //邀请人
	ActivityTime string `sql:"activity_time" json:"activity_time"`   //活动时间
}

type LuckyDrawLog struct {
	UserId uint32 `sql:"user_id" json:"user_id"`
	DataId int    `sql:"data_id" json:"data_id"`
	Count  int    `sql:"count" json:"count"`
	Tag    string `sql:"tag" json:"tag"`
	Date   string `sql:"date" json:"date"`
}

func (l *LuckyDrawInviteLog) Get() (int, error) {
	var userID int
	var err error
	sqlQuery := fmt.Sprintf("SELECT user_id FROM lucky_draw_invite_log WHERE user_id = ?")
	err = dbx.MySQL.QueryRow(sqlQuery, l.UserId).Scan(&userID)
	if err != nil && err != sql.ErrNoRows {
		logs.Errorf("userID:%d err:%v", l.UserId, err)
		return 0, err
	}
	return userID, nil
}

func (l *LuckyDrawInviteLog) Add() (int64, error) {
	var last int64
	var err error
	sqlQuery := fmt.Sprintf("INSERT INTO lucky_draw_invite_log (`user_id`,`invite_user_id`,`activity_time`) VALUE(?,?,?)")
	last, err = dbx.MySQL.Insert(sqlQuery, l.UserId, l.InviteUserId, l.ActivityTime)
	if err != nil {
		logs.Error(fmt.Sprintf("AddRedPacket param:%#v err:%v", l, err))
		return 0, err
	}

	return last, nil
}

func (l *LuckyDrawLog) Add() (int64, error) {
	var last int64
	var err error
	sqlQuery := fmt.Sprintf("INSERT INTO lucky_draw_log (`user_id`,`data_id`,`count`,`tag`,`date`) VALUE(?,?,?,?,?)")
	last, err = dbx.MySQL.Insert(sqlQuery, l.UserId, l.DataId, l.Count, l.Tag, l.Date)
	if err != nil {
		logs.Error(fmt.Sprintf("LuckyDrawLog param:%#v err:%v", l, err))
		return 0, err
	}
	return last, nil
}

///////////////////////////////////////////////落地页app下载///////////////////////////////////////////////
