reward.go 3.97 KB
package models

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

	db "client/icesimba.mail/database"
)

type Reward struct {
	Id        string //奖励id
	Title     string //奖励标题
	Content   string //奖励消息内容
	TrigType  int64  //触发类型 0,开启应用
	EndTime   int64  //结束时间
	ValidTime int64
}

type RewardDB struct {
	Id        sql.NullString //奖励id
	Title     sql.NullString //奖励标题
	Content   sql.NullString //奖励消息内容
	TrigType  sql.NullInt64  //触发类型 0,开启应用
	EndTime   sql.NullInt64  //结束时间
	ValidTime sql.NullInt64
}

type Rewards struct {
	//游戏id
	GameReward []Reward
}

func (in *Rewards) QueryRewardByGameId(gameId, platform string, version int64) (out Rewards, err error) {
	currTime := time.Now().UnixNano() / 1e6
	common.Printf("select * from ice_reward where gameid=%s\n", gameId)
	sqlQue := "select id,title,content,trigtype,endtime,validtime from ice_reward" +
		" where" +
		" gameid=? AND" +
		" (ACTTYPE=1 OR (STARTTIME < ? AND ENDTIME > ?)) AND" +
		" USERFILTER='' AND (PLATFILTER=? or PLATFILTER='all') AND" +
		" VERSIONFILTER<? AND" +
		" ISDEL=0"
	rows, err := db.SqlDB.Query(sqlQue, gameId, currTime, currTime, platform, version)
	if err != nil {
		common.Println(err)
		return
	}
	for rows.Next() {
		reward := Reward{}
		rewarddb := RewardDB{}
		err = rows.Scan(
			&rewarddb.Id,
			&rewarddb.Title,
			&rewarddb.Content,
			&rewarddb.TrigType,
			&rewarddb.EndTime,
			&rewarddb.ValidTime,
		)
		if rewarddb.Id.Valid {
			reward.Id = rewarddb.Id.String
		}
		if rewarddb.Title.Valid {
			reward.Title = rewarddb.Title.String
		}
		if rewarddb.Content.Valid {
			reward.Content = rewarddb.Content.String
		}
		if rewarddb.TrigType.Valid {
			reward.TrigType = rewarddb.TrigType.Int64
		}
		if rewarddb.EndTime.Valid {
			reward.EndTime = rewarddb.EndTime.Int64
		}
		if rewarddb.ValidTime.Valid {
			reward.ValidTime = rewarddb.ValidTime.Int64
		}
		out.GameReward = append(out.GameReward, reward)
	}
	if err != nil {
		common.Println(err)
		return
	}
	return
}

/*通过 reward_id获得 reward 的title和content,返回奖励的title 和 content 以及奖励icon*/
func (in *Reward) QueryReward(gameID string) (out Reward, icon string, err error) {
	fmt.Printf("select * from ice_reward where id=%s\n", in.Id)
	sqlQue := "select title,content from ice_reward where id=? and gameid=? limit 1"
	row := db.SqlDB.QueryRow(sqlQue, in.Id, gameID)
	rewarddb := RewardDB{}
	err = row.Scan(
		&rewarddb.Title,
		&rewarddb.Content,
	)
	if err != nil {
		return
	}
	if rewarddb.Title.Valid {
		out.Title = rewarddb.Title.String
	}
	if rewarddb.Content.Valid {
		out.Content = rewarddb.Content.String
	}
	icon = QueryRPIcon(in.Id)
	return
}

//func (in *Reward) InsertOrUpdateGiftSet() (code int, out Reward, err error) {
//	code, out, err = in.QueryReward()
//	if err != nil {
//		sqlIn := "insert into ice_giftset(gsid,gid,cftype,gtitle,gcontent,pushtype,startdata,enddata,userfilter,versionfilter,platfilter) values (?,?,?,?,?,?,?,?,?,?,?)"
//		_, err = db.SqlDB.Exec(
//			sqlIn,
//			&in.GSId,
//			&in.GameId,
//			&in.TrigType,
//			&in.Title,
//			&in.Content,
//			&in.ActType,
//			&in.StartTime,
//			&in.EndTime,
//			&in.UserFilter,
//			&in.VersionFilter,
//			&in.PlatFilter,
//		)
//		if err != nil {
//			err = errors.New("插入数据失败")
//			code = 400
//			return
//		}
//		code = 200
//	} else {
//		//		sqlIn = "update ice_giftset set gid= cftype,gtitle,gcontent,pushtype,startdata,enddata,userfilter,versionfilter,platfilter) values (?,?,?,?,?,?,?,?,?,?,?)"
//		//		_, err = db.SqlDB.Exec(
//		//			sqlIn,
//		//			&in.GSId,
//		//			&in.GameId,
//		//			&in.TriggerType,
//		//			&in.GiftTitle,
//		//			&in.GiftContent,
//		//			&in.PushType,
//		//			&in.StartTime,
//		//			&in.EndTime,
//		//			&in.UserFilter,
//		//			&in.VersionFilter,
//		//			&in.PlatFilter,
//		//		)
//		//		if err != nil {
//		//			err = errors.New("插入数据失败")
//		//			code = 400
//		//			return
//		//		}
//		//		code = 200
//	}
//	return
//}