reward-user.go 4.25 KB
package models

import (
	"common"
	"database/sql"
	"errors"
	"time"

	db "client/icesimba.mail/database"
)

/*礼物配置表和用户表关系表*/
type RewardUser struct {
	RUId      string
	UserId    string //用户Id
	RewardId  string //礼品发放配置Id
	IsPost    int64  //是否发放
	IsGet     int64  //是否领取
	StartTime int64
	EndTime   int64
	Title     string
	Content   string
}
type RUDB struct {
	RUId      sql.NullString
	UserId    sql.NullString //用户Id
	RewardId  sql.NullString //礼品发放配置Id
	IsPost    sql.NullInt64  //是否发放
	IsGet     sql.NullInt64  //是否领取
	StartTime sql.NullInt64
	EndTime   sql.NullInt64
	Title     sql.NullString
	Content   sql.NullString
}

func (in *RewardUser) IsUserHasReward() bool {
	currTime := time.Now().UnixNano() / 1e6
	sqlQue := "select ruid from ice_reward_user" +
		" where" +
		" userid=? AND" +
		" STARTTIME < ? AND" +
		" ENDTIME > ? AND ISGET=0 limit 1"
	var ruid string
	err := db.SqlDB.QueryRow(sqlQue, in.UserId, currTime, currTime).Scan(
		&ruid,
	)
	if err == nil && ruid != "" {
		return true
	}
	return false
}

/*通过userId查询rewar_user表,获取rewardId的数组*/
func (in *RewardUser) QueryRUByUserId() (out []RewardUser, err error) {
	currTime := time.Now().UnixNano() / 1e6
	sqlQue := "select ruid,rewardid,starttime,title,content from ice_reward_user" +
		" where" +
		" userid=? AND" +
		" STARTTIME < ? AND" +
		" ENDTIME > ? AND ISGET=0"
	rows, err := db.SqlDB.Query(sqlQue, in.UserId, currTime, currTime)
	if err != nil {
		return
	}
	for rows.Next() {
		userReward := RewardUser{}
		rudb := RUDB{}
		err = rows.Scan(
			&rudb.RUId,
			&rudb.RewardId,
			&rudb.StartTime,
			&rudb.Title,
			&rudb.Content,
		)
		userReward.UserId = in.UserId
		if rudb.RUId.Valid {
			userReward.RUId = rudb.RUId.String
		}
		if rudb.RewardId.Valid {
			userReward.RewardId = rudb.RewardId.String
		}
		if rudb.StartTime.Valid {
			userReward.StartTime = rudb.StartTime.Int64
		}
		if rudb.Title.Valid {
			userReward.Title = rudb.Title.String
		}
		if rudb.Content.Valid {
			userReward.Content = rudb.Content.String
		}
		out = append(out, userReward)

		if err != nil {
			common.Println(err)
			continue
		}
	}
	if err != nil {
		common.Println(err)
		return
	}
	sqlQue = "update ice_reward_user set ISREAD=1 where USERID=? and ISREAD=0"
	_, err = db.SqlDB.Exec(sqlQue, in.UserId)
	if err != nil {
		return
	}
	return
}

func (in *RewardUser) InsertRewardUser() (err error) {
	ruId := common.GetUuid()
	sqlIn := "insert into ice_reward_user(ruid,userid,rewardid,isget,starttime,endtime) VALUES (?, ?, ?, ?, ?, ?)"
	_, err = db.SqlDB.Exec(
		sqlIn,
		ruId,
		in.UserId,
		in.RewardId,
		in.IsGet,
		in.StartTime,
		in.EndTime,
	)
	if err != nil {
		return
	}
	return
}

func (in *RewardUser) GainRewardUser() (out Props, err error) {
	if in.RewardId == "" {
		err = errors.New("奖励不存在")
		return
	}
	reprop := RewardProp{
		RewardId: in.RewardId,
	}
	out, err = reprop.QueryRewardProp()
	sqlUp := "update ice_reward_user set isget=? where userid=? and ruid=?"
	stmt, err := db.SqlDB.Prepare(sqlUp)
	defer stmt.Close()
	_, err = stmt.Exec(
		in.IsGet,
		in.UserId,
		in.RUId,
	)
	if err != nil {
		return
	}
	return
}

func (in *RewardUser) IsRUExist() bool {
	sqlQue := "select ruid,starttime,endtime from ice_reward_user where userid=? and rewardid=?"
	var (
		ruid  string
		start int64
		end   int64
	)
	err := db.SqlDB.QueryRow(
		sqlQue,
		in.UserId,
		in.RewardId,
	).Scan(
		&ruid,
		&start,
		&end,
	)
	curr := time.Now().UnixNano() / 1e6
	if err != nil || start > curr || curr > end {
		return false
	}
	return true
}

func (in *RewardUser) QueryRUByRUId() (out RewardUser, err error) {
	sqlQue := "select rewardid,isget,starttime,endtime from ice_reward_user where userid=? and ruid=?"
	row := db.SqlDB.QueryRow(
		sqlQue,
		in.UserId,
		in.RUId,
	)
	rudb := RUDB{}
	err = row.Scan(
		&rudb.RewardId,
		&rudb.IsGet,
		&rudb.StartTime,
		&rudb.EndTime,
	)
	if err != nil {
		err = errors.New("没有奖励")
		return
	}
	out.UserId = in.UserId
	out.RUId = in.RUId
	if rudb.RewardId.Valid {
		out.RewardId = rudb.RewardId.String
	}
	if rudb.IsGet.Valid {
		out.IsGet = rudb.IsGet.Int64
	}
	if rudb.StartTime.Valid {
		out.StartTime = rudb.StartTime.Int64
	}
	if rudb.EndTime.Valid {
		out.EndTime = rudb.EndTime.Int64
	}
	return
}