Go实战全家桶之58:goEs自动比对--源于实战项目

一键生成代码goweb: https://gitee.com/leijmdas/goweb/settings#index

type IwebFacade[T baseindex.IndexMapping] interface {
    SaveIndex(indexTable baseindex.IndexIface) *basedto.IchubResult
    UpdateStru(ids any, doc any) *basedto.IchubResult
    Update(ids any, doc map[string]any) *basedto.IchubResult
    DeleteByQuery() *basedto.IchubResult
    Delete(id any) *basedto.IchubResult
    BulkDelete(ids ...any) *basedto.IchubResult
    BulkIndex(docs ...[]*cmddto.BulkDoc) *basedto.IchubResult
    BulkCreate(docs ...[]*cmddto.BulkDoc) *basedto.IchubResult
    BulkUpdate(docs ...[]*cmddto.BulkDoc) *basedto.IchubResult

    LogQueryMaster(q *elastic.BoolQuery, msg string)
    LogInfoQuery(q *elastic.BoolQuery, prompt ...string)

    CacheFindId(id any) *pagemodel.IchubResult[T]
    CacheFindKey(key string, id any) *pagemodel.IchubResult[T]
    CacheListAll() *pagemodel.PageResult[T]
    CacheListAllOf(fieldName string, value any) *pagemodel.IchubResult[T]

    CheckRecord(keyAny any, exps string) *basedto.IchubResult
}

func Test002_Meta2CodeFile(t *testing.T) {

    var err = metafacade.FindBeanMetaFacade().MetaEs("test_website_utm")
    golog.Info(err)
}
package esutm

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/base/baseindex"
    "git.ichub.com/general/webcli120/goconfig/ichubconfig"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/pagemodel"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/webfacade"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/webfacade/webui"
    "github.com/gogf/gf/v2/util/gconv"
    jsoniter "github.com/json-iterator/go"
    "github.com/olivere/elastic/v7"
    "time"
    "website-utm/domain/db/dbentity/dbutm"
    "website-utm/domain/es/esbase"
)

/*
@Title    文件名称: website_visit_es.go
@Description  描述: 统一返回结构

@Author  作者: raymond@163.com  时间(2025-03-25 21:40:39)
@Update  作者: raymond@163.com  时间(2025-03-25 21:40:39)
*/

type WebsiteVisitEs struct {
    basedto.BaseEntity //basedto.BaseEntityIndex
    esbase.EsBase
    dbutm.Tdk

    Active        bool      `json:"active"`
    VisitDuration int32     `json:"visit_duration"`
    WebsiteId     int64     `json:"website_id,string"`
    VisitorId     string    `json:"visitor_id"`
    UtmId         int64     `json:"utm_id,string"` //推广标识
    UtmSource     string    `json:"utm_source"`
    Id            int64     `json:"id,string"`
    PageId        int64     `json:"page_id,string"`
    ShopId        int64     `json:"shop_id,string"`
    PageShopId    int64     `json:"page_shop_id,string"`    //page shopid
    VisitorShopId int64     `json:"visitor_shop_id,string"` //visit shopid
    VisitIp       string    `json:"visit_ip"`
    VisitorType   string    `json:"visitor_type"`
    VisitorName   string    `json:"visitor_name"`
    Uuid          string    `json:"uuid"`
    Url           string    `json:"url"` //URL
    Token         string    `json:"token"`
    State         string    `json:"state"`
    ReferUrl      string    `json:"refer_url"`
    Os            string    `json:"os"`
    Browser       string    `json:"browser"`
    PageType      string    `json:"page_type"`
    ObjectType    string    `json:"object_type"`
    ObjectCode    string    `json:"object_code"`
    MenuName      string    `json:"menu_name"`
    DomainName    string    `json:"domain_name"`
    ComponentName string    `json:"component_name"`
    ComponentId   string    `json:"component_id"`
    ColumnId      string    `json:"column_id"`
    ClickUrl      string    `json:"click_url"`
    VisitAt       time.Time `json:"visit_at"`            //访问时间
    VisitAtInt    int64     `json:"visit_at_int,string"` //访问时间

}

func NewWebsiteVisitEs() *WebsiteVisitEs {
    return &WebsiteVisitEs{}
}

type WebsiteVisitEsRequest struct {
    basedto.BaseEntity
    webui.WebuiRequest

    WebsiteVisitEs `json:"param"`
}

func NewWebsiteVisitEsRequest() *WebsiteVisitEsRequest {
    return &WebsiteVisitEsRequest{}
}

func (self *WebsiteVisitEs) PkeyName() string {
    return "id"
}
func (self *WebsiteVisitEs) PkeyValue() string {
    return gconv.String(self.Id)
}
func (self *WebsiteVisitEs) TableName() string {
    return ichubconfig.FindEnv() + "_website_visit"
}

func (self *WebsiteVisitEs) IndexName() string {
    return self.TableName()
}

func (self *WebsiteVisitEs) GetMapping() string {
    return `{
     "mappings": {
          "properties": {
               "active": {
                    "type": "boolean"
               },
               "uuid": {
                    "type": "keyword"
               },
              "browser": {
                    "type": "keyword"
               },
               "component_id": {
                    "type": "keyword"
               },
               "component_name": {
                    "type": "keyword"
               },
               "os": {
                    "type": "keyword"
               },
               "updated_at": {
                    "type": "date"
               },
               "visit_duration": {
                    "type": "integer"
               },
               "id": {
                    "type": "long"
               },
               "visitor_type": {
                    "type": "keyword"
               },
               "created_at": {
                    "type": "date"
               },
               "created_by": {
                    "type": "long"
               },
               "object_type": {
                    "type": "keyword"
               },
               "page_id": {
                    "type": "long"
               },
               "updated_by": {
                    "type": "long"
               },
               "url": {
                    "type": "keyword"
               },
               "utm_source": {
                    "type": "keyword"
               },
               "visitor_id": {
                    "type": "keyword"
               },
               "column_id": {
                    "type": "keyword"
               },               
               "menu_name": {
                    "type": "keyword"
               },
               "shop_id": {
                    "type": "long"
               }, 
             "page_shop_id": {
                            "type": "long"
                         },
             "visitor_shop_id": {
                            "type": "long"
                         },
               "token": {
                    "type": "keyword"
               },
               "utm_id": {
                    "type": "long"
               },
               "visitor_name": {
                    "type": "keyword"
               },
               "created_by_name": {
                    "type": "keyword"
               },
               "object_code": {
                    "type": "keyword"
               },
               "refer_url": {
                    "type": "keyword"
               },
               "state": {
                    "type": "keyword"
               },
               "visit_ip": {
                    "type": "keyword"
               },
               "website_id": {
                    "type": "long"
               },
               "click_url": {
                    "type": "keyword"
               },
               "domain_name": {
                    "type": "keyword"
               },
               "visit_at": {
                    "type": "date"
               },
             "title": {
                    "type": "keyword"
               },
             "description": {
                    "type": "keyword"
               },
             "keyword": {
                    "type": "keyword"
               }
          }
     },
     "settings": {
          "index": {
               "number_of_replicas": "2",
               "max_ngram_diff": "30",
               "number_of_shards": "2",
               "analysis": {
                    "analyzer": {
                         "part_number_analyzer": {
                              "filter": [
                                   "lowercase"
                              ],
                              "tokenizer": "letter"
                         },
                         "ngram_analyzer": {
                              "filter": [
                                   "lowercase"
                              ],
                              "tokenizer": "ngram_name_tokenizer"
                         }
                    },
                    "tokenizer": {
                         "ngram_name_tokenizer": {
                              "token_chars": [],
                              "min_gram": "2",
                              "type": "ngram",
                              "max_gram": "30"
                         }
                    }
               }
          }
     }
}`
}
func (self *WebsiteVisitEs) Mapping() string {
    return self.GetMapping()
}
func (self *WebsiteVisitEs) Unmarshal(data []byte) error {
    return jsoniter.Unmarshal(data, self)
}

func (self WebsiteVisitEs) IndexID() string {
    return self.PkeyName()
}

func (self WebsiteVisitEs) AliasName() string {
    return self.IndexName() + "_alias"
}

func (self *WebsiteVisitEs) InitDoc() baseindex.IndexEsObject {
    ss := *self
    return &ss
}

func (self *WebsiteVisitEs) EsFill(p any) error {
    var err = gconv.Struct(p, self)
    if err != nil {
       golog.Error("WebsiteVisitEs err:", err)
    }
    return err
}

func (self *WebsiteVisitEs) Init() {
}
func (self *WebsiteVisitEs) Shutdown() {

}

func NewWebFacadeWebsiteVisitEsOf(q elastic.Query) *webfacade.WebFacade[*WebsiteVisitEs] {
    var facade = webfacade.DefaultOf[*WebsiteVisitEs](q)
    facade.Query(q)
    return facade
}

func NewWebFacadeWebsiteVisitEs() *webfacade.WebFacade[*WebsiteVisitEs] {
    return webfacade.Default[*WebsiteVisitEs]()

}

func (self *WebsiteVisitEs) DefaultOf(q elastic.Query) *webfacade.WebFacade[*WebsiteVisitEs] {
    return NewWebFacadeWebsiteVisitEsOf(q)

}
func (self *WebsiteVisitEs) Default() *webfacade.WebFacade[*WebsiteVisitEs] {
    return NewWebFacadeWebsiteVisitEs()
}

func (self *WebsiteVisitEs) ObjectKey() string {
    return self.TableName() + ":" + gconv.String(self.PkeyValue())
}
func (self *WebsiteVisitEs) IfUser() bool {
    return self.VisitorType == "User"
}

func (self *WebsiteVisitEs) FillName() {
    if self.IfUser() {
       self.VisitorName = self.FindUserNameById(gconv.Int64(self.VisitorId))
    }
    self.VisitAtInt = self.VisitAt.UnixMilli()
    self.Fill()
}
func (self *WebsiteVisitEs) EsFindId() *pagemodel.IchubResult[*WebsiteVisitEs] {
    var ret = NewWebFacadeWebsiteVisitEs().EsFindKeyId(self.PkeyName(), self.PkeyValue())
    if ret.IsFailed() {
       golog.Error("WebsiteVisitEs [EsFindKey] ret:", ret)
    }
    return ret
}

执行测试代码

func (self *TestWebsiteUtmSuite) Test032_CreateVisitUi() {
    var req = uidto.FindBeanWebsiteVisitCreate()
    req.Url = "https://testcloud.ichub.com/portal/sku?slug=c7fd8b97-7d21-4f64-b1b1-a2c381ace5f7"
    req.UseNats = false
    req.ObjectType = "SHOP_SPU"
    req.ObjectCode = "-1"
    req.PageType = "list"
    req.MenuName = "商品"
    req.Title = "123"
    var ret = req.CreateVisitUi(false, apiutils.UserIDOf(webdto.UserTest102()))
    golog.Info(ret)
    var q = esutm.NewWebFacadeWebsiteVisitEs()
    var check = q.EsCheckRecord("id="+ret.Data.(string), "object_code=-1|page_type=list|object_type=SHOP_SPU")
    golog.Info(check)
    self.Equal(check.Code, 200)
}

结果

INFO[2025-04-412 14:37:00]C:/Users/admin/go/pkg/mod/git.ichub.com/general/webcli120@v1.1.504-dev-03/goconfig/basedi/bean_info.go:25 git.ichub.com/general/webcli120/goconfig/basedi.(*BeanInfo).CreateBean() you new a single bean :EsWebClient           
2025-04-12 14:37:00.781 [INFO] {
     "code": 200,
     "msg": "成功",
     "data": "1062893715104497667",
     "exist": false
}
 git.ichub.com/general/webcli120/goconfig/basedi.(*BeanInfo).CreateBean() you new a single bean :PageEsCheck           
  git.ichub.com/general/webcli120/goconfig/base/basedto.(*IchubResult).CheckRecord() 
CheckRecord object_code=-1 OK              
  git.ichub.com/general/webcli120/goconfig/base/basedto.(*IchubResult).CheckRecord() 
CheckRecord page_type=list OK              
  git.ichub.com/general/webcli120/goconfig/base/basedto.(*IchubResult).CheckRecord() 
CheckRecord object_type=SHOP_SPU OK        
2025-04-12 14:37:00.826 [INFO] {
     "code": 200,
     "msg": "成功",
     "data": "id=1062893715104497667",
     "exist": false
}

检查代码实现:

EsCheckRecord

// pkey=f1=1
func (self *WebRequest[T]) EsCheckRecord(pkeys string, exps string) *basedto.IchubResult {
    var pkey, pvalue string
    var pks = strings.Split(pkeys, "=")
    if len(pks) < 2 {
       pkeys = "id=" + pkeys
       pkey = "id"
       pvalue = pkey
    } else {
       pkey = pks[0]
       pvalue = pks[1]
    }
    var ret = self.EsFindKeyId(pkey, pvalue)
    if ret.IsFailed() {
       return ret.To()
    }
    if !ret.ExistRecord() {
       return basedto.ResultFailMsg("pkey not exist!" + pkeys)
    }
    var err = ret.CheckRecord(exps)
    if err != nil {
       golog.Error("record=", ret)
       return basedto.ResultFailErr(err)
    }
    return basedto.ResultSuccessData(pkeys)
}
func (self *WebRequest[T]) EsFindKeyId(key string, id any) *pagemodel.IchubResult[T] {
    var q = elastic.NewBoolQuery()
    q.Must(elastic.NewMatchQuery(key, id))
    self.HttpCliQuery.Query(q)

    self.SetPageSize(1)
    var ret = self.GeneralQuery()
    if ret.IsFailed() {
       golog.Error("WebRequest[T] [FindId] ret:", ret)
    }
    return ret.ToResult()
}
func (self *IchubResult) CheckRecord(exp string) error {

    var recordExp = strings.Split(exp, "|")

    var maps = jsonutils.Stru2MapUnderscore(self.Data)
    for i := range recordExp {
       var items = strings.Split(recordExp[i], "=")
       if items[1] == gconv.String(maps[items[0]]) {
          logrus.Info("\r\nCheckRecord ", recordExp[i], " OK")
          continue
       } else {
          var err = errors.New("CheckRecord " + recordExp[i] + " failed!")
          logrus.Error("\r\n", err)
          return err
       }
    }
    return nil

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值