material.go 1.06 KB
package models

import (
	"database/sql"
	"strings"

	db "client/icesimba.ad/database"
)

type Material struct {
	Type int64  `json:"type"`
	Src  string `json:"src"`
}

type material struct {
	Type sql.NullInt64
	Src  sql.NullString
}

func (sell *SellPlan) QueryMaterialByAdId() (out []Material, err error) {
	sqlQu := "SELECT material_type,material_src FROM ice_ad_material WHERE ad_id=?"
	rows, err := db.SqlDB.Query(
		sqlQu,
		sell.AdId,
	)
	materialNull := material{}
	for rows.Next() {
		err = rows.Scan(
			&materialNull.Type,
			&materialNull.Src,
		)
		if err != nil {
			continue
		}
		mater := Material{}
		if materialNull.Type.Valid {
			mater.Type = materialNull.Type.Int64
		}
		if materialNull.Src.Valid {
			srcUrl := materialNull.Src.String
			if strings.Index(srcUrl, "http") == 0 {
				mater.Src = srcUrl
			} else {
				urlHost := "http://iceplay.oss-cn-beijing.aliyuncs.com"
				if !strings.Contains(srcUrl, "/") || strings.Index(srcUrl, "/") != 0 {
					urlHost = urlHost + "/"
				}
				mater.Src = urlHost + srcUrl
			}
		}
		out = append(out, mater)
	}
	return
}