package consume import ( "applet/app/db" utils2 "applet/app/utils" "applet/app/utils/logx" "applet/consume/md" md2 "applet/es/md" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "context" "encoding/json" "errors" "fmt" "github.com/olivere/elastic/v7" "github.com/streadway/amqp" "time" ) // EggCanalViolateNumsConsume 更新违规次数 func EggCanalViolateNumsConsume(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>EggCanalViolateNumsConsume>>>>>>>>>>>>>") ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { logx.Error(err) return } defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) //2、取出数据进行消费 ch.Qos(1000) delivery := ch.Consume(queue.Name, true) //设置自动应答 var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { //fmt.Println(string(res.Body)) fmt.Println(">>>>>>>>>>>>>>>>>>EggCanalViolateNumsConsume<<<<<<<<<<<<<<<<<<<<<<<<<") err = handleEggCanalViolateNumsConsume(res.Body) if err != nil { fmt.Println("EggCanalViolateNumsConsume_ERR:::::", err.Error()) utils2.FilePutContents("EggCanalViolateNumsConsume_ERR", utils2.SerializeStr(map[string]interface{}{ "body": res.Body, "err": err.Error(), })) } //_ = res.Reject(false) //_ = res.Ack(true) } else { panic(errors.New("error getting message")) } } } func handleEggCanalViolateNumsConsume(msg []byte) error { //1、解析canal采集至mq中queue的数据结构体 var canalMsg *md.CanalTagRecordsMessage[md.CanalTagRecords] err := json.Unmarshal(msg, &canalMsg) if err != nil { return nil } year, week := time.Now().ISOWeek() yearStr := utils2.IntToStr(year) weekStr := utils2.IntToStr(week) index := es2.GetAppointIndexFromAlias(yearStr, weekStr) // 2. 监听插入信息 if canalMsg.Type == md2.CanalMsgInsertSqlType { tagDb := implement.NewUserTagDb(db.Db) for _, item := range canalMsg.Data { tag, err1 := tagDb.UserTagGetOneByParams(map[string]interface{}{ "key": "id", "value": item.TagId, }) if err1 != nil { return err1 } // 2.1. 判断是否为处罚标签 if tag.IsPunish == 0 { continue } uid := item.Uid id := fmt.Sprintf("%d%d_%s", year, week, uid) // 2.2. 增加违规次数记录 script := elastic.NewScript("ctx._source.violate_nums += params.inc").Param("inc", 1) _, err = es.EsClient.Update(). Index(index). Id(id). Script(script). Do(context.Background()) if err != nil { return err } } } // 3. 监听删除信息 if canalMsg.Type == md2.CanalMsgDeleteSqlType { tagDb := implement.NewUserTagDb(db.Db) for _, item := range canalMsg.Data { tag, err1 := tagDb.UserTagGetOneByParams(map[string]interface{}{ "key": "id", "value": item.TagId, }) if err1 != nil { return err1 } // 3.1 判断是否为处罚标签 if tag.IsPunish == 0 { continue } uid := item.Uid id := fmt.Sprintf("%d%d_%s", year, week, uid) // 3.2 减少违规次数记录 script := elastic.NewScript("ctx._source.violate_nums -= params.dec").Param("dec", 1) _, err = es.EsClient.Update(). Index(index). Id(id). Script(script). Do(context.Background()) if err != nil { return err } } } return nil }