|
- 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"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
- 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"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/streadway/amqp"
- "strings"
- "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 || 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
- }
- // 2.1. 判断是否为处罚标签
- if tag.IsPunish == 0 {
- continue
- }
- uid := item.Uid
- id := fmt.Sprintf("%d%d_%s", year, week, uid)
-
- // 2.2 计算违规次数
- sql := "SELECT COUNT(*) AS total " +
- "From user_tag_records " +
- "left join user_tag on user_tag_records.tag_id = user_tag.id " +
- "WHERE user_tag_records.uid = %s " +
- "AND user_tag.is_punish = 1"
- sql = fmt.Sprintf(sql, uid)
- res, err1 := db.QueryNativeString(db.Db, sql)
- if err1 != nil {
- return err1
- }
-
- // 2.3 更新违规记录
- updateMap := make(map[string]interface{})
- updateMap["violate_nums"] = utils2.StrToInt(res[0]["total"])
- updateDocRet, err := es.UpdateDoc(index, id, updateMap)
- if err != nil {
- if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
- // 蛋蛋分数据还不存在,创建蛋蛋分数据
- now := time.Now().Format("2006-01-02 15:04:05")
- err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, utils2.StrToInt64(uid), enum.ViolateNums, "1", now)
- if err1 != nil {
- return err1
- }
- return nil
- }
- return err
- }
- fmt.Println("updateDocRet ========>", updateDocRet)
- }
- }
-
- return nil
- }
|