checkreward.go 1.81 KB
package models

import (
	"fmt"

	db "client/icesimba.mail/database"
)

/**
* 查询是否有奖励未被阅读
* @userId: 		用户的id
* @funcType:	是否将未阅读修改成阅读
 */
func CheckRewards(rewardIn RewardIn, funcType string) (result string, err error, out RewardOut) {
	rewardIn.InsertRewardUser()
	userId := rewardIn.UserId
	sqlQue := "select REWARDID from ice_reward_user where USERID=? and ISREAD=0 order by STARTTIME desc"
	rows, err := db.SqlDB.Query(sqlQue, userId)
	if err != nil {
		// fmt.Println("sele	ct rewardid err")
		return
	}
	var rewardid string
	//如果有返回结果,根据funcType来决定是否update
	for rows.Next() {
		err = rows.Scan(
			&rewardid,
		)
		if err != nil {
			// fmt.Println("scan err")
			return
		}
		fmt.Println(rewardid)
		out, err = getRewardContent(rewardid, userId)
		if err != nil {
			// fmt.Println("get rewardcontent err")
			return
		}
		result = "true"

		if funcType == "true" {
			sqlQue = "update ice_reward_user set ISREAD=1 where USERID=? and ISREAD=0"
			_, err = db.SqlDB.Exec(sqlQue, userId)
			if err != nil {
				// fmt.Println("update isread err")
				return
			}
		}
		return
	}
	result = "false"
	return

}
func getRewardContent(rewardid string, userId string) (out RewardOut, err error) {
	var (
		title     string
		content   string
		starttime int64
	)
	icon := QueryRPIcon(rewardid)

	sqlQue := "select TITLE,CONTENT,STARTTIME from ice_reward where ID=?"
	err = db.SqlDB.QueryRow(sqlQue, rewardid).Scan(
		&title,
		&content,
		&starttime,
	)
	if err != nil {
		return
	}
	rewardJson := RewardJson{
		RUId:       userId,
		Title:      title,
		Content:    content,
		Createtime: starttime,
		Icon:       icon,
	}
	out.RewardJsons = append(out.RewardJsons, rewardJson)
	return
}