ad_event_record.go 3.65 KB
package models

import (
	"database/sql"

	db "client/icesimba.ad/database"
)

type AdAppPosition struct {
	AdId            string
	PositionId      string
	PositionApp     string
	PositionVersion string
}
type adAppPosition struct {
	AdId            sql.NullString
	PositionId      sql.NullString
	PositionApp     sql.NullString
	PositionVersion sql.NullString
}

func PostAdEventRecord(
	adId string,
	positionId string,
	gameId string,
	positionVersion string,
	iceVersion string,
	channel string,
	platform string,
	model string,
	deviceId string,
	ip string,
	eventCode string) (err error) {

	eventObj, err := QueryEventByCode(eventCode)
	adObj, err := QueryAdById(adId)
	chanObj, err := QueryChannelByName(channel)
	platObj, err := QueryPlatformByName(platform)

	sqlIn := "INSERT INTO ice_ad_eventrecord(event_id,event_name," +
		"ad_id,ad_app," +
		"position_id,position_app,position_app_version," +
		"device_id,channel_id,platform_id,model,ip,sdk_version)" +
		"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"

	_, err = db.SqlDB.Exec(sqlIn,
		eventObj.Id,
		eventObj.Name,
		adId,
		adObj.AppId,
		positionId,
		gameId,
		positionVersion,
		deviceId,
		chanObj.Id,
		platObj.Id,
		model,
		ip,
		iceVersion,
	)
	return
}

func PostAdEventActiveRecord(
	adId string,
	positionId string,
	gameId string,
	positionVersion string,
	iceVersion string,
	channel string,
	platform string,
	model string,
	deviceId string,
	ip string,
	eventCode string) (err error) {

	if IsAdEventRecordExist(deviceId, eventCode, gameId) {
		return
	}

	adEventClickCode := "icead_click"
	adEventRecordObj, err := QueryAdEventRecord(deviceId, adEventClickCode, gameId)
	if err != nil {
		return
	}
	eventObj, err := QueryEventByCode(eventCode)
	chanObj, err := QueryChannelByName(channel)
	platObj, err := QueryPlatformByName(platform)

	sqlIn := "INSERT INTO ice_ad_eventrecord(event_id,event_name," +
		"ad_id,ad_app," +
		"position_id,position_app,position_app_version," +
		"device_id,channel_id,platform_id,model,ip,sdk_version)" +
		"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"

	_, err = db.SqlDB.Exec(sqlIn,
		eventObj.Id,
		eventObj.Name,
		adEventRecordObj.AdId,
		gameId,
		adEventRecordObj.PositionId,
		adEventRecordObj.PositionApp,
		adEventRecordObj.PositionVersion,
		deviceId,
		chanObj.Id,
		platObj.Id,
		model,
		ip,
		iceVersion,
	)
	return
}

func IsAdEventRecordExist(deviceId string, eventCode string, gameId string) bool {
	eventObj, err := QueryEventByCode(eventCode)
	sqlQu := "select id from ice_ad_eventrecord where device_id=? and event_id=? and ad_app=? limit 1"
	var idNull sql.NullString
	err = db.SqlDB.QueryRow(
		sqlQu,
		deviceId,
		eventObj.Id,
		gameId,
	).Scan(
		&idNull,
	)
	if err != nil {
		return false
	}
	return true
}

func QueryAdEventRecord(deviceId string, eventCode string, gameId string) (out AdAppPosition, err error) {
	eventObj, err := QueryEventByCode(eventCode)
	sqlQu := "SELECT ad_id,position_id,position_app,position_app_version" +
		" FROM ice_ad_eventrecord" +
		" WHERE create_time<=NOW() " +
		" AND ad_app=? AND event_id=? AND device_id=?" +
		" ORDER BY create_time DESC LIMIT 1"
	adAppPosNull := adAppPosition{}
	err = db.SqlDB.QueryRow(
		sqlQu,
		gameId,
		eventObj.Id,
		deviceId,
	).Scan(
		&adAppPosNull.AdId,
		&adAppPosNull.PositionId,
		&adAppPosNull.PositionApp,
		&adAppPosNull.PositionVersion,
	)
	if err != nil {
		return
	}
	if adAppPosNull.AdId.Valid {
		out.AdId = adAppPosNull.AdId.String
	}
	if adAppPosNull.PositionId.Valid {
		out.PositionId = adAppPosNull.PositionId.String
	}
	if adAppPosNull.PositionApp.Valid {
		out.PositionApp = adAppPosNull.PositionApp.String
	}
	if adAppPosNull.PositionVersion.Valid {
		out.PositionVersion = adAppPosNull.PositionVersion.String
	}
	return
}