package channelSer import ( "context" "errors" "stat/api/proto" "stat/common/log" "stat/models/channelmod" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) // SearchChannelDeductionRule 查询渠道扣量规则信息 func SearchChannelDeductionRule(ctx context.Context, req *proto.ChannelDeductionRuleSearch) (*proto.ChannelDeductionRuleSearchResp, error) { resp := &proto.ChannelDeductionRuleSearchResp{} filter := bson.M{} opt := &options.FindOptions{} opt.SetSort(bson.M{channelmod.FN.ChannelDeductionRule.BeginTime: -1}) opt.SetSkip((req.PageNum - 1) * req.PageSize) opt.SetLimit(req.PageSize) if req.Channel != nil && *req.Channel != "" { filter[channelmod.FN.ChannelDeductionRule.Channel] = bson.M{"$regex": *req.Channel} } if req.AppId != nil { filter[channelmod.FN.ChannelDeductionRule.AppId] = *req.AppId } if req.Channel != nil && *req.Channel != "" { filter[channelmod.FN.ChannelDeductionRule.Channel] = *req.Channel } total, err := channelmod.CountChannelDeductionRules(ctx, filter) if err != nil { return nil, err } if total == 0 { return resp, nil } ChannelDeductionRuleList, err := channelmod.FindManyChannelDeductionRule(ctx, filter, opt) if err != nil { return nil, err } for _, rule := range ChannelDeductionRuleList { if rule.DivideRate == 0 { rule.DivideRate = 100 } } resp.Total = total resp.List = ChannelDeductionRuleList return resp, nil } // AddChannelDeductionRule 添加一个媒体视频 func AddChannelDeductionRule(ctx context.Context, req *proto.ChannelDeductionRuleAdd) (*channelmod.ChannelDeductionRule, error) { checkFilter := bson.M{channelmod.FN.ChannelDeductionRule.Channel: req.Channel, channelmod.FN.ChannelDeductionRule.AppId: req.AppId} ChannelDeductionRule, err := channelmod.FindOneChannelDeductionRule(ctx, checkFilter) if err != nil { return nil, err } if ChannelDeductionRule.Channel == req.Channel { return ChannelDeductionRule, errors.New("渠道扣量规则已经存在") } channelInfo, err := channelmod.FindOneChannelInfoEx(ctx, bson.M{channelmod.FN.ChannelInfo.Channel: req.Channel}) if err != nil { return nil, err } var one float64 if req.LessIncomeRate == nil { one = 0.0 } else { one = *req.LessIncomeRate } now := time.Now() rule := channelmod.ChannelDeductionRule{ AppId: req.AppId, AppType: req.AppType, PromoteMode: req.PromoteMode, Channel: req.Channel, Settlement: req.Settlement, First: req.First, Second: req.Second, Third: req.Third, Fourth: req.Fourth, Fifth: req.Fifth, CPAPrice: req.CPAPrice, CPSRate: req.CPSRate, BeginTime: req.BeginTime, CPSBegin: req.CPSBegin, CPSDeductionRate: req.CPSDeductionRate, DivideRate: req.DivideRate, LandingPage: req.LandingPage, ProxyDeduction: req.ProxyDeduction, AutoDeduction: req.AutoDeduction, LessIncomeRate: one, CreatedAt: now, UpdatedAt: now, } err = channelmod.InsertOneChannelDeductionRule(ctx, &rule) if err != nil { return nil, err } channelInfo.AppIds = append(channelInfo.AppIds, req.AppId) setdoc := bson.M{"$set": bson.M{channelmod.FN.ChannelInfo.AppIds: channelInfo.AppIds}} err = channelmod.UpdateOneChannelInfo(ctx, bson.M{channelmod.FN.ChannelInfo.Channel: req.Channel}, setdoc) if err != nil { return nil, err } return channelmod.FindOneChannelDeductionRuleEx(ctx, checkFilter) } // DelChannelDeductionRule 删除指定渠道扣量规则信息 func DelChannelDeductionRule(ctx context.Context, channelId string) error { id, err := primitive.ObjectIDFromHex(channelId) if err != nil { return errors.New("ID 格式错误") } ruleFilter := bson.M{channelmod.FN.ChannelDeductionRule.ID: id} channelRuleInfo, err := channelmod.FindOneChannelDeductionRuleEx(ctx, ruleFilter) if err != nil { return err } channelInfo, err := channelmod.FindOneChannelInfoEx(ctx, bson.M{channelmod.FN.ChannelInfo.Channel: channelRuleInfo.Channel}) if err != nil { return err } count, err := channelmod.CountChannelInfos(ctx, bson.M{channelmod.FN.ChannelInfo.ParentAgent: channelInfo.Channel}) if err != nil { log.Error("DelChannelDeductionRule CountChannelInfos", log.Any("channelId", channelId), log.E(err)) return err } if count > 0 { return errors.New("下级代理不为空,请先删除下级代理, 再删除应用规则") } _, err = channelmod.DelOneChannelDeductionRule(ctx, ruleFilter) if err != nil { return err } appids := make([]int32, 0) for _, appid := range channelInfo.AppIds { if appid != channelRuleInfo.AppId { appids = append(appids, appid) } } setdoc := bson.M{"$set": bson.M{channelmod.FN.ChannelInfo.AppIds: appids}} err = channelmod.UpdateOneChannelInfo(ctx, bson.M{channelmod.FN.ChannelInfo.Channel: channelRuleInfo.Channel}, setdoc) if err != nil { return err } return nil } // UpdateChannelDeductionRule 更新指定渠道扣量规则 func UpdateChannelDeductionRule(ctx context.Context, channelId string, info proto.ChannelDeductionRuleUpdate) error { id, err := primitive.ObjectIDFromHex(channelId) if err != nil { return errors.New("ID 格式错误") } filter := bson.M{channelmod.FN.ChannelDeductionRule.ID: id} //更新数据库 setDoc := bson.M{"$set": info} serr := channelmod.UpdateOneChannelDeductionRule(ctx, filter, setDoc) if serr != nil { return serr } return nil }