package serviceGame
import (
"context"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"strconv"
"tyj_admin/api/v1/game"
"tyj_admin/internal/consts"
"tyj_admin/internal/dao"
"tyj_admin/internal/model/do"
"tyj_admin/internal/model/entity"
"tyj_admin/internal/serviceGame/internal"
"tyj_admin/library/libUtils"
"tyj_admin/library/liberr"
)
type IGameCCD interface {
GetCCD ( ctx context . Context , req * game . GetCCDReq ) ( res * game . GetCCDRes , err error )
AddCCD ( ctx context . Context , req * game . AddCCDReq ) ( res * game . AddCCDRes , err error )
DelCCD ( ctx context . Context , req * game . DelCCDReq ) ( res * game . DelCCDRes , err error )
GetGameCCD ( ctx context . Context , req * game . GetGameCCDReq ) ( res * game . GetGameCCDRes , err error )
GetGameCCD1 ( ctx context . Context , req * game . GetGameCCD1Req ) ( res * game . GetGameCCD1Res , err error )
GetGameCCD2 ( ctx context . Context , req * game . GetGameCCDReq ) ( res * game . GetGameCCDRes , err error )
}
type gameCCDImpl struct {
}
var gameCCDService = gameCCDImpl { }
func GameCCD ( ) IGameCCD {
return & gameCCDService
}
func ( c * gameCCDImpl ) GetCCD ( ctx context . Context , req * game . GetCCDReq ) ( res * game . GetCCDRes , err error ) {
//ip := libUtils.GetClientIp(ctx)
//fmt.Println("ip: ", ip)
err = g . Try ( ctx , func ( ctx context . Context ) {
res = new ( game . GetCCDRes )
model := dao . GameCcdUrl . Ctx ( ctx )
if req . Platform != "" {
model = model . Where ( "platform=?" , req . Platform )
}
if req . VersionName != "" {
model = model . Where ( "versionName=?" , req . VersionName )
}
if req . ChannelType != "" {
model = model . Where ( "channelType=?" , req . ChannelType )
}
if req . Ip != "" {
model = model . Where ( "ip=?" , req . Ip )
}
model = model . OrderDesc ( "versionName" )
res . Total , err = model . Count ( )
liberr . ErrIsNil ( ctx , err , "mysql err" )
model = model . Page ( req . PageNum , req . PageSize )
err = model . Scan ( & res . CCDList )
} )
return
}
func ( c * gameCCDImpl ) AddCCD ( ctx context . Context , req * game . AddCCDReq ) ( res * game . AddCCDRes , err error ) {
res = new ( game . AddCCDRes )
g . Try ( ctx , func ( ctx context . Context ) {
model := dao . GameCcdUrl . Ctx ( ctx )
model = model . Where ( "channelType = ?" , req . ChannelType )
model = model . Where ( "platform=? " , req . Platform )
model = model . Where ( "versionName=? " , req . VersionName )
model = model . Where ( "ip=? " , req . Ip )
var ccdList [ ] * entity . GameCcdUrl
err = model . Scan ( & ccdList )
if err != nil {
return
}
if len ( ccdList ) > 0 {
model = dao . GameCcdUrl . Ctx ( ctx )
_ , err = model . Where ( "id=" , ccdList [ 0 ] . Id ) . Data ( & do . GameCcdUrl { State : req . State , HotfixVersion : req . HotfixVersion , Url : req . Url } ) . Update ( )
} else {
model = dao . GameCcdUrl . Ctx ( ctx )
_ , err = model . Insert ( & do . GameCcdUrl { ChannelType : req . ChannelType , Platform : req . Platform , HotfixVersion : req . HotfixVersion , VersionCompare : req . VersionCompare , Ip : req . Ip , VersionCode : req . VersionCode , VersionName : req . VersionName , State : req . State , Url : req . Url } )
}
} )
return
}
func ( c * gameCCDImpl ) DelCCD ( ctx context . Context , req * game . DelCCDReq ) ( res * game . DelCCDRes , err error ) {
err = g . Try ( ctx , func ( ctx context . Context ) {
if req . Id == 0 {
liberr . ErrIsNil ( ctx , err , "错误的id" )
return
}
_ , e := dao . GameCcdUrl . Ctx ( ctx ) . Where ( "id=" , req . Id ) . Delete ( )
if e != nil {
liberr . ErrIsNil ( ctx , e , "删除通知失败" )
return
}
} )
return
}
func getStateRes ( platform string , state int ) ( res string ) {
if platform == "Android" {
if state == 1 {
res = consts . ANDROID
} else {
res = consts . ANDROID_test
}
} else if platform == "IOS" {
if state == 1 {
res = consts . IOS
} else {
res = consts . ANDROID_test
}
}
return res
}
func ( c * gameCCDImpl ) GetGameCCD ( ctx context . Context , req * game . GetGameCCDReq ) ( res * game . GetGameCCDRes , err error ) {
res = new ( game . GetGameCCDRes )
ip := libUtils . GetClientIp ( ctx )
g . Log ( ) . Infof ( ctx , "0 ip= " , req . Platform , req . ChannelType , gjson . MustEncodeString ( req ) )
if req . ChannelType == "000005000000" {
res . AuditMode = 1
}
if req . Version == "" {
req . Version = req . VersionName
}
res . EnableAccountLogin = 0
res . EnableHotfix = 1
err = g . Try ( ctx , func ( ctx context . Context ) {
if internal . GetEnableHotfix ( ctx , req . ChannelType + req . Version ) != "" {
data , _ := strconv . Atoi ( internal . GetEnableHotfix ( ctx , req . ChannelType + req . Version ) )
res . EnableHotfix = int32 ( data )
} else if internal . GetEnableHotfix ( ctx , req . ChannelType ) != "" {
data , _ := strconv . Atoi ( internal . GetEnableHotfix ( ctx , req . ChannelType ) )
res . EnableHotfix = int32 ( data )
}
if internal . GetEnableAccountLogin ( ctx , req . ChannelType + req . Version ) != "" {
data , _ := strconv . Atoi ( internal . GetEnableAccountLogin ( ctx , req . ChannelType + req . Version ) )
res . EnableAccountLogin = int32 ( data )
} else if internal . GetEnableAccountLogin ( ctx , req . ChannelType ) != "" {
data , _ := strconv . Atoi ( internal . GetEnableAccountLogin ( ctx , req . ChannelType ) )
res . EnableAccountLogin = int32 ( data )
}
model := dao . GameCcdUrl . Ctx ( ctx )
model = model . Where ( "platform=? and (versionName='' or versionName>=?) and (channelType='' or channelType=?) or ip=?" , req . Platform , req . Version , req . ChannelType , ip )
model = model . Order ( "ip, versionName, channelType DESC" )
var ccdList [ ] * entity . GameCcdUrl
err = model . Scan ( & ccdList )
g . Log ( ) . Infof ( ctx , "1 ip=" + ip , len ( ccdList ) )
//res.Version = GameVersion().GetVersion(ctx, req.ChannelType)
ccdLen := len ( ccdList )
if err != nil || ccdLen == 0 {
liberr . ErrIsNil ( ctx , err , "插入通知日志失败" )
//g.Log().Debugf(ctx, "1.1 ip="+ip, len(ccdList))
return
}
//g.Log().Debugf(ctx, "2 ip="+ip, len(ccdList))
for _ , v := range ccdList {
versionWith := v . VersionName
if v . HotfixVersion != "" {
versionWith = v . HotfixVersion
}
url := v . Url + versionWith
//url := fmt.Sprintf("https://a.unity.cn/client_api/v1/buckets/%s/entry_by_path/content/?path=", getStateRes(v.Platform, v.State))
//g.Log().Debugf(ctx, "3 ip="+ip, url, gjson.MustEncodeString(v))
if v . Ip == ip {
res . Url = url
res . Version = v . VersionCompare
g . Log ( ) . Debugf ( ctx , "ccd ip -> ip=" + ip , gjson . MustEncodeString ( res ) )
return
} else {
if v . ChannelType == req . ChannelType {
res . Url = url
res . Version = v . VersionCompare
g . Log ( ) . Debugf ( ctx , "ccd ChannelType -> req.ChannelType=%s, " , req . ChannelType , gjson . MustEncodeString ( res ) )
return
} else if v . ChannelType == "" {
if v . HotfixVersion == req . HotfixVersion {
res . Url = url
res . Version = v . VersionCompare
g . Log ( ) . Debugf ( ctx , "ccd ChannelType == '' -> req.HotfixVersion=" + req . HotfixVersion , gjson . MustEncodeString ( res ) )
return
} else if v . HotfixVersion == "" {
res . Url = url
res . Version = v . VersionCompare
g . Log ( ) . Debugf ( ctx , "ccd ChannelType == '' -> " , gjson . MustEncodeString ( res ) )
return
}
}
}
}
g . Log ( ) . Debugf ( ctx , "ccd ip" + ip , res . Url )
} )
return
}
func ( c * gameCCDImpl ) GetGameCCD1 ( ctx context . Context , req * game . GetGameCCD1Req ) ( res * game . GetGameCCD1Res , err error ) {
res = new ( game . GetGameCCD1Res )
ip := libUtils . GetClientIp ( ctx )
g . Log ( ) . Debugf ( ctx , "0 ip= " , req . Platform , req . ChannelType , gjson . MustEncodeString ( req ) )
if req . Version == "" {
req . Version = req . VersionName
}
err = g . Try ( ctx , func ( ctx context . Context ) {
model := dao . GameCcdUrlCopy1 . Ctx ( ctx )
model = model . Where ( "platform=? and (versionName='' or versionName>=?) and (channelType='' or channelType=?) or ip=?" , req . Platform , req . Version , req . ChannelType , ip )
model = model . Order ( "ip, versionName, channelType DESC" )
var ccdList [ ] * entity . GameCcdUrl
err = model . Scan ( & ccdList )
g . Log ( ) . Debugf ( ctx , "1 ip=%s" , ip , len ( ccdList ) )
//res.Version = GameVersion().GetVersion(ctx, req.ChannelType)
ccdLen := len ( ccdList )
if err != nil || ccdLen == 0 {
liberr . ErrIsNil ( ctx , err , "插入通知日志失败" )
//g.Log().Debugf(ctx, "1.1 ip="+ip, len(ccdList))
return
}
for _ , v := range ccdList {
if v . Ip == ip {
res . Version = v . VersionCompare
g . Log ( ) . Infof ( ctx , "ccd ip -> ip=" + ip , gjson . MustEncodeString ( res ) , v )
return
} else {
if v . ChannelType == req . ChannelType {
res . Version = v . VersionCompare
g . Log ( ) . Infof ( ctx , "ccd ChannelType -> req.ChannelType=%s, " , req . ChannelType , gjson . MustEncodeString ( res ) )
return
} else if v . ChannelType == "" {
if v . HotfixVersion == req . HotfixVersion {
res . Version = v . VersionCompare
g . Log ( ) . Infof ( ctx , "ccd ChannelType == '' -> req.HotfixVersion=" + req . HotfixVersion , gjson . MustEncodeString ( res ) )
return
} else if v . HotfixVersion == "" {
res . Version = v . VersionCompare
g . Log ( ) . Infof ( ctx , "ccd ChannelType == '' -> " , gjson . MustEncodeString ( res ) )
return
}
}
}
}
g . Log ( ) . Infof ( ctx , "ccd ip" + ip )
} )
return
}
func ( c * gameCCDImpl ) GetGameCCD2 ( ctx context . Context , req * game . GetGameCCDReq ) ( res * game . GetGameCCDRes , err error ) {
res = new ( game . GetGameCCDRes )
ip := libUtils . GetClientIp ( ctx )
//fmt.Println("ip: ", ip)
err = g . Try ( ctx , func ( ctx context . Context ) {
model := dao . GameCcdUrl . Ctx ( ctx )
model = model . Where ( "Platform=? and (hotfixVersion='' or hotfixVersion>=?) and (channelType='' or channelType=?) or ip=?" , req . Platform , req . HotfixVersion , req . ChannelType , ip )
model = model . Order ( "Ip, HotfixVersion, ChannelType DESC" )
var ccdList [ ] * entity . GameCcdUrl
err = model . Scan ( & ccdList )
fmt . Println ( "ccd: " , ccdList )
res . Version = GameVersion ( ) . GetVersion ( ctx , req . ChannelType )
ccdLen := len ( ccdList )
if err != nil || ccdLen == 0 {
liberr . ErrIsNil ( ctx , err , "插入通知日志失败" )
return
}
for _ , v := range ccdList {
versionWith := v . VersionName
if v . HotfixVersion != "" {
versionWith = v . HotfixVersion
}
url := fmt . Sprintf ( "https://a.unity.cn/client_api/v1/buckets/%s/entry_by_path/content/?path=" , getStateRes ( v . Platform , v . State ) )
if v . Ip == ip {
res . Url = url + versionWith
res . Version = v . VersionCompare
return
} else {
if v . ChannelType == req . ChannelType {
res . Url = url + versionWith
res . Version = v . VersionCompare
return
} else if v . ChannelType == "" {
if v . HotfixVersion == req . HotfixVersion {
res . Url = url + versionWith
res . Version = v . VersionCompare
return
} else if v . HotfixVersion == "" {
res . Url = url + versionWith
res . Version = v . VersionCompare
return
}
}
}
}
} )
return
}