sellplan.go 1.08 KB
package models

import (
	"database/sql"
	"errors"

	db "client/icesimba.ad/database"
)

type SellPlan struct {
	AdId       string
	GameId     string
	PositionId string
	Percent    float64
}

type sellPlan struct {
	AdId       sql.NullString
	GameId     sql.NullString
	PositionId sql.NullString
	Percent    sql.NullFloat64
}

func (sell *SellPlan) QueryAdIdByPositionId() (out []SellPlan, err error) {
	sqlQu := "SELECT ad_id,persent FROM ice_ad_sellplan WHERE position_id=? AND position_app_id =?"
	rows, err := db.SqlDB.Query(
		sqlQu,
		sell.PositionId,
		sell.GameId,
	)
	if err != nil {
		return
	}
	sellNull := sellPlan{}
	for rows.Next() {
		err = rows.Scan(
			&sellNull.AdId,
			&sellNull.Percent,
		)
		if err != nil {
			continue
		}
		if sellNull.AdId.Valid {
			sell.AdId = sellNull.AdId.String
		}
		if sellNull.Percent.Valid {
			sell.Percent = sellNull.Percent.Float64
		}
		out = append(out, *sell)
	}
	return
}

func SellsFilter(sells []SellPlan) (sell SellPlan, err error) {
	if len(sells) <= 0 {
		err = errors.New("this position is no ads")
	} else {
		sell = sells[0]
	}
	return
}