gift.go 4.01 KB
package model

import (
	"database/sql"
	"errors"
	"log"
	"time"
)

type sqlExecutor interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

type GiftResults struct {
	Gifts []GiftResult `json:"gifts"`
}

type GiftResult struct {
	Code  string `json:"id"`
	Count int    `json:"count"`
}

type PropResult struct {
	PropID string
	Count  int
}

//1,参数:giftCode,userID,
//2,根据参数取出propid,count集合
//3,count置入结果集,propid用来取code
//4,code置入结果集,调用收尾
//5,收尾:user置入DB,并设置checkTime、status。
const (
	checkTaskRepeatSQL = "SELECT task_id FROM ice_gift_code WHERE code = ? " +
		" AND task_id IN" +
		"(SELECT DISTINCT(task_id) FROM ice_gift_code" +
		" WHERE user = ?" +
		")"
	propidSQL = "SELECT PROPID,COUNT FROM ice_reward_prop" +
		" WHERE REWARDID = (select reward_id from ice_gift_task" +
		" where task_id =(select task_id from ice_gift_code" +
		" where STATUS ='2'" +
		" and code= ?" +
		")" +
		" and ? BETWEEN start_time and end_time" +
		")"
	propCodeSQL = "select CODE from ice_prop where ID = ? and GAMEID = ?"

	setCheckUserSQL = "UPDATE ice_gift_code set check_time=? ,user=? ,status=? WHERE code=?"
	checkTimeSQL    = "select start_time,end_time from ice_gift_task" +
		" where task_id =(select task_id from ice_gift_code" +
		" where code=?)"
	checkStatusSQL = "select STATUS from ice_gift_code where code = ?"
	checkedStatus  = 3
)

//bool 为true则领过,为false则未领过。
func CheckTaskRepeat(db sqlExecutor, code, userId string) (bool, error) {
	var taskId sql.NullString
	err := db.QueryRow(checkTaskRepeatSQL, code, userId).Scan(&taskId)
	if err==sql.ErrNoRows {
		return  false,nil
	}
	if err != nil {
		return false, err
	}
	if taskId.String == "" {
		return false, nil
	}
	return true, nil
}

func CheckNullReason(db sqlExecutor, giftCode string) error {
	//1,判断status是不是2。分类:1,2,3
	var status int
	err := db.QueryRow(checkStatusSQL, giftCode).Scan(&status)
	if err != nil {
		return err
	}
	log.Println("status", status)
	if status != 2 {
		if status == 3 {
			return errors.New("  礼品已被领取")
		}
		if status == 1 {
			return errors.New("  礼品未发放")
		}
		return errors.New("  礼品不存在")
	}
	//2,判断时间是超出了,还是不到
	var starTime, endTime int64
	ResErr := db.QueryRow(checkTimeSQL, giftCode).Scan(&starTime, &endTime)
	if ResErr != nil {
		return ResErr
	}
	nowTime := time.Now().Unix() * 1000
	if nowTime < starTime {
		return errors.New("  未到礼包生效时间")
	}
	if nowTime > endTime {
		return errors.New("  已过礼包有效时间")
	}
	return nil
}

func GetGiftPropid(db sqlExecutor, giftCode string, gameId string) (GiftResults, error) {
	giftResults := new(GiftResults)
	propResult := &PropResult{}
	giftResult := new(GiftResult)
	var code string

	rows, err := db.Query(propidSQL, giftCode, time.Now().Unix()*1000)
	if err == sql.ErrNoRows {
		return *giftResults, nil
	}
	if err != nil {
		return *giftResults, errors.New(" " + err.Error())
	}
	defer rows.Close()
	for rows.Next() {
		rows.Scan(&propResult.PropID, &propResult.Count)
		code, err = GetPropCodeByID(db, propResult.PropID, gameId)
		if err != nil {
			continue
		}
		giftResult.Code = code
		giftResult.Count = propResult.Count
		giftResults.Gifts = append(giftResults.Gifts, *giftResult)
	}
	return *giftResults, nil
}

func GetPropCodeByID(db sqlExecutor, propID string, gameID string) (string, error) {
	var code string
	err := db.QueryRow(propCodeSQL, propID, gameID).Scan(&code)
	if err != nil {
		log.Println("请检查gameID,获取propID时:" + err.Error())
	}
	return code, err
}

func SetCheckStatus(db sqlExecutor, userID string, giftCode string) (int64, error) {
	result, err := db.Exec(setCheckUserSQL, time.Now().Unix()*1000, userID, checkedStatus, giftCode)
	if err != nil {
		return 0, err
	}
	rowCnt, err := result.RowsAffected()
	if err != nil {
		return 0, err
	}

	return rowCnt, nil
}