package serviceGame import ( "context" "encoding/json" "errors" "log" "math" "strconv" "sync" "tyj_admin/api/v1/game" "tyj_admin/internal/dao" "tyj_admin/internal/model/do" "tyj_admin/internal/model/entity" "tyj_admin/internal/serviceGame/internal" ) type IAdvertisement interface { Advertise(ctx context.Context, req *game.ADReq) (res *game.ADRes, err error) DeepAdvertise(ctx context.Context, req *game.DeepADReq) (res *game.DeepADRes, err error) AttributionHugeAmount(ctx context.Context, req *game.ATHAReq) (res *game.ATHARes, err error) ConversionHugeAmount(ctx context.Context, req *game.CSHAReq) (res *game.CSHARes, err error) AdvertiseHugeAmount(ctx context.Context, req *game.AdvertiseHAReq) (res *game.AdvertiseHARes, err error) AdvertiseHugeAmount1(ctx context.Context, androidId, idfa, os, callBack, timestamp, caid, ip string) (err error) AdvertiseHugeAmountReadOnly(ctx context.Context, osInt int, list []map[string]string, caid, androidId, idfa, os, callBack, timestamp, ip string) (err error) GetAccessToken(ctx context.Context, req *game.GetAccessTokenReq) (res *game.GetAccessTokenRes, err error) RefreshAccessToken(ctx context.Context, req *game.RefreshAccessTokenReq) (res *game.RefreshAccessTokenRes, err error) RefreshAdvertisementOceanegine(ctx context.Context, req *game.RefreshAdvertisementOceanegineReq) (res *game.RefreshAdvertisementOceanegineRes, err error) } type advertisementImpl struct { sync.RWMutex } var advertisementService = advertisementImpl{} func Advertisement() IAdvertisement { return &advertisementService } func (g *advertisementImpl) Advertise(ctx context.Context, req *game.ADReq) (res *game.ADRes, err error) { res = new(game.ADRes) res, err = internal.Advertise(ctx, req) return } func (g *advertisementImpl) DeepAdvertise(ctx context.Context, req *game.DeepADReq) (res *game.DeepADRes, err error) { res = new(game.DeepADRes) res, err = internal.DeepAdvertise(ctx, req) return } func (g *advertisementImpl) AttributionHugeAmount(ctx context.Context, req *game.ATHAReq) (res *game.ATHARes, err error) { res = new(game.ATHARes) res, err = internal.AttributionHugeAmount(ctx, req) return } func (g *advertisementImpl) ConversionHugeAmount(ctx context.Context, req *game.CSHAReq) (res *game.CSHARes, err error) { res = new(game.CSHARes) res, err = internal.ConversionHugeAmount(ctx, req) return } func (g *advertisementImpl) AdvertiseHugeAmount(ctx context.Context, req *game.AdvertiseHAReq) (res *game.AdvertiseHARes, err error) { res = new(game.AdvertiseHARes) res, err = internal.AdvertiseHugeAmount(ctx, req) return } func (g *advertisementImpl) AdvertiseHugeAmount1(ctx context.Context, androidId, idfa, os, callBack, timestamp, caidList, ip string) (err error) { osInt, err := strconv.Atoi(os) if err != nil { return } if osInt != 0 && osInt != 1 { return errors.New("系统类型错误!") } if callBack == "__CALLBACK_PARAM__" { return } if osInt == 1 && caidList == "" && (idfa == "__IDFA__" || idfa == "00000000-0000-0000-0000-000000000000") { return } if osInt == 0 && (androidId == "__ANDROIDID__" || androidId == "") { return } caid := "" var list []map[string]string if caidList != "" { //var list []map[string]string _ = json.Unmarshal([]byte(caidList), &list) versionNow := int64(0) for _, v := range list { version, _ := strconv.ParseInt(v["version"], 10, 64) if versionNow < version { caid = v["caid"] versionNow = version } } } err = g.AdvertiseHugeAmountReadOnly(ctx, osInt, list, caid, androidId, idfa, os, callBack, timestamp, ip) return } func (g *advertisementImpl) AdvertiseHugeAmountReadOnly(ctx context.Context, osInt int, list []map[string]string, caid, androidId, idfa, os, callBack, timestamp, ip string) (err error) { //g.Lock() model := dao.AdvertisementOceanegine1.Ctx(ctx) lastTouchTime, _ := strconv.ParseInt(timestamp, 10, 64) adDatas := []entity.AdvertisementOceanegine1{} if osInt == 0 { _ = model.Where("adv_android_id=?", androidId).Where("os=?", os).Scan(&adDatas) } else if osInt == 1 { for _, v := range list { _ = model.Where("caid=?", v["caid"]).Where("os=?", os).Scan(&adDatas) if len(adDatas) > 0 { break } } if idfa != "" && len(adDatas) == 0 { _ = model.Where("idfa=?", idfa).Where("os=?", os).Scan(&adDatas) } } if len(adDatas) > 0 { var adData entity.AdvertisementOceanegine1 for _, v := range adDatas { if adData.Id == 0 || v.LastTouchTime > adData.LastTouchTime || (v.LastTouchTime == adData.LastTouchTime && v.CDate.Unix() > adData.CDate.Unix()) { adData = v } } if adData.LastTouchTime > lastTouchTime { err = errors.New("不是最后一次!") //g.Unlock() return } _, err = model.WherePri(adData.Id).Update(do.AdvertisementOceanegine1{CallbackParam: callBack, LastTouchTime: timestamp, Caid: caid}) //g.Unlock() return } _, err = model.Insert(do.AdvertisementOceanegine1{ AdvAndroidId: androidId, Idfa: idfa, Os: os, CallbackParam: callBack, Caid: caid, LastTouchTime: timestamp, Ip: ip, }) //g.Unlock() return } func (g *advertisementImpl) GetAccessToken(ctx context.Context, req *game.GetAccessTokenReq) (res *game.GetAccessTokenRes, err error) { res = new(game.GetAccessTokenRes) res.Msg = internal.GetAccessToken(ctx, req.AuthCode) return } func (g *advertisementImpl) RefreshAccessToken(ctx context.Context, req *game.RefreshAccessTokenReq) (res *game.RefreshAccessTokenRes, err error) { internal.RefreshAccessToken(ctx) return } func (g *advertisementImpl) RefreshAdvertisementOceanegine(ctx context.Context, req *game.RefreshAdvertisementOceanegineReq) (res *game.RefreshAdvertisementOceanegineRes, err error) { if req.AD != "idfa" && req.AD != "caid" && req.AD != "advAndroidId" && req.AD != "advIdfv" { return } adDatas := []entity.AdvertisementOceanegine{} adDataMaps := map[string]entity.AdvertisementOceanegine{} model := dao.AdvertisementOceanegine.Ctx(ctx) idList := [][]int64{} ids := []int64{} deleteCount := 50000 limit := 1000000 if req.Limit > 0 { limit = req.Limit } log.Printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&& RefreshAdvertisementOceanegine start %s &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&", req.AD) if req.AD == "idfa" { _ = model.Where("idfa <> '' ").Where("os=?", 1).Limit(limit).Offset(req.Page).Scan(&adDatas) if len(adDatas) > 0 { for _, v := range adDatas { if v.Os == 0 { continue } if v.Idfa == "00000000-0000-0000-0000-000000000000" || v.Idfa == "__IDFA__" { ids = append(ids, v.Id) } else if adDataMaps[v.Idfa].Id == 0 { adDataMaps[v.Idfa] = v } else if v.LastTouchTime > adDataMaps[v.Idfa].LastTouchTime || (v.LastTouchTime == adDataMaps[v.Idfa].LastTouchTime && v.CDate.Unix() > adDataMaps[v.Idfa].CDate.Unix()) { ids = append(ids, adDataMaps[v.Idfa].Id) adDataMaps[v.Idfa] = v } if len(ids) > deleteCount { idList = append(idList, ids) ids = []int64{} } } } } else if req.AD == "caid" { _ = model.Where("caid <> ''").Where("os=?", 1).Limit(limit).Offset(req.Page).Scan(&adDatas) if len(adDatas) > 0 { for _, v := range adDatas { if v.Os == 0 { continue } if v.Caid == "00000000-0000-0000-0000-000000000000" || v.Caid == "" { ids = append(ids, v.Id) } else if adDataMaps[v.Caid].Id == 0 { adDataMaps[v.Caid] = v } else if v.LastTouchTime > adDataMaps[v.Caid].LastTouchTime || (v.LastTouchTime == adDataMaps[v.Caid].LastTouchTime && v.CDate.Unix() > adDataMaps[v.Caid].CDate.Unix()) { ids = append(ids, adDataMaps[v.Caid].Id) adDataMaps[v.Caid] = v } if len(ids) > deleteCount { idList = append(idList, ids) ids = []int64{} } } } } else if req.AD == "advAndroidId" { _ = model.Where("adv_android_id <> ''").Where("os=?", 0).Limit(limit).Offset(req.Page).Scan(&adDatas) if len(adDatas) > 0 { for _, v := range adDatas { if v.Os == 1 { continue } if v.AdvAndroidId == "00000000-0000-0000-0000-000000000000" || v.AdvAndroidId == "__ANDROIDID__" { ids = append(ids, v.Id) } else if adDataMaps[v.AdvAndroidId].Id == 0 { adDataMaps[v.AdvAndroidId] = v } else if v.LastTouchTime > adDataMaps[v.AdvAndroidId].LastTouchTime || (v.LastTouchTime == adDataMaps[v.AdvAndroidId].LastTouchTime && v.CDate.Unix() > adDataMaps[v.AdvAndroidId].CDate.Unix()) { ids = append(ids, adDataMaps[v.AdvAndroidId].Id) adDataMaps[v.AdvAndroidId] = v } if len(ids) > deleteCount { idList = append(idList, ids) ids = []int64{} } } } } else if req.AD == "advIdfv" { _ = model.Where("adv_idfv <> ''").Where("os=?", 1).Limit(limit).Offset(req.Page).Scan(&adDatas) if len(adDatas) > 0 { for _, v := range adDatas { if v.Os == 0 { continue } if adDataMaps[v.AdvIdfv].Id == 0 { adDataMaps[v.AdvIdfv] = v } else if v.LastTouchTime > adDataMaps[v.AdvIdfv].LastTouchTime || (v.LastTouchTime == adDataMaps[v.AdvIdfv].LastTouchTime && v.CDate.Unix() > adDataMaps[v.AdvIdfv].CDate.Unix()) { ids = append(ids, adDataMaps[v.AdvIdfv].Id) adDataMaps[v.AdvIdfv] = v } if len(ids) > deleteCount { idList = append(idList, ids) ids = []int64{} } } } } if len(ids) > 0 { idList = append(idList, ids) } if len(idList) > 0 { for _, v := range idList { _, _ = model.Where("id in (?)", v).Delete() } } log.Printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&& RefreshAdvertisementOceanegine over %s ids: %d idList: %d count: %f &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&", req.AD, len(ids), len(idList), math.Max(0, float64(len(idList)-1))*float64(deleteCount)+float64(len(ids))) return }