Browse Source

数据去重封装api接口

fuwencai 4 years ago
parent
commit
220d52e030

+ 34 - 0
api/deduplication.api

@@ -0,0 +1,34 @@
+syntax = "v1"
+
+info(
+	title: // TODO: add title
+	desc: // TODO: add description
+	author: "fuwencai"
+	email: "fuwencai@topnet.net.cn"
+)
+
+type (
+	Info {
+		TotalCount int64 `json:"totalCount"` // 本次查询info_id总量
+		ExistCount int64 `json:"existCount"` // 已存在的info_id的数量
+		NewCount   int64 `json:"NewCount"`   // 不存在的info_id的数量
+	}
+
+	DedupReq {
+		PersonId string `form:"personId"` //人员id
+		InfoId   string `form:"infoId"`   //信息id  逗号分隔
+		EntId    string `form:"entId"`    // 企业id
+		IsInsert bool   `form:"isInsert"` // 是否插入不重复的数据
+		IsEnt    bool   `form:"isEnt"`    //是否按企业id判重
+	}
+	DedupResp {
+		Code int64  `json:"code"`
+		Msg  string `json:"msg"`
+		Data Info   `json:"data"`
+	}
+)
+
+service deduplication-api {
+	@handler Datadeduplication // 调用数据去重
+	post /data/deduplication (DedupReq) returns (DedupResp)
+}

+ 31 - 0
api/deduplication.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/config"
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/handler"
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/svc"
+
+	"github.com/tal-tech/go-zero/core/conf"
+	"github.com/tal-tech/go-zero/rest"
+)
+
+var configFile = flag.String("f", "etc/deduplication-api.yaml", "the config file")
+
+func main() {
+	flag.Parse()
+
+	var c config.Config
+	conf.MustLoad(*configFile, &c)
+
+	ctx := svc.NewServiceContext(c)
+	server := rest.MustNewServer(c.RestConf)
+	defer server.Stop()
+
+	handler.RegisterHandlers(server, ctx)
+
+	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
+	server.Start()
+}

+ 8 - 0
api/etc/deduplication-api.yaml

@@ -0,0 +1,8 @@
+Name: deduplication-api
+Host: 0.0.0.0
+Port: 8888
+Deduplication:
+  Etcd:
+    Hosts:
+      - localhost:2379
+    Key: deduplication.rpc

+ 12 - 0
api/internal/config/config.go

@@ -0,0 +1,12 @@
+package config
+
+import (
+	"github.com/tal-tech/go-zero/rest"
+	"github.com/tal-tech/go-zero/zrpc"
+)
+
+type Config struct {
+	rest.RestConf
+	Deduplication   zrpc.RpcClientConf     // 手动代码
+
+}

+ 29 - 0
api/internal/handler/datadeduplicationhandler.go

@@ -0,0 +1,29 @@
+package handler
+
+import (
+	"net/http"
+
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/logic"
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/svc"
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/types"
+
+	"github.com/tal-tech/go-zero/rest/httpx"
+)
+
+func DatadeduplicationHandler(ctx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.DedupReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewDatadeduplicationLogic(r.Context(), ctx)
+		resp, err := l.Datadeduplication(req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 22 - 0
api/internal/handler/routes.go

@@ -0,0 +1,22 @@
+// Code generated by goctl. DO NOT EDIT.
+package handler
+
+import (
+	"net/http"
+
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/svc"
+
+	"github.com/tal-tech/go-zero/rest"
+)
+
+func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
+	engine.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodPost,
+				Path:    "/data/deduplication",
+				Handler: DatadeduplicationHandler(serverCtx),
+			},
+		},
+	)
+}

+ 48 - 0
api/internal/logic/datadeduplicationlogic.go

@@ -0,0 +1,48 @@
+package logic
+
+import (
+	"app.yhyue.com/moapp/dataDeduplication/rpc/deduplication"
+	"context"
+
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/svc"
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/types"
+
+	"github.com/tal-tech/go-zero/core/logx"
+)
+
+type DatadeduplicationLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewDatadeduplicationLogic(ctx context.Context, svcCtx *svc.ServiceContext) DatadeduplicationLogic {
+	return DatadeduplicationLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *DatadeduplicationLogic) Datadeduplication(req types.DedupReq) (*types.DedupResp, error) {
+	// todo: add your logic here and delete this line
+	res,err:=l.svcCtx.Dedup.DataDeduplication(l.ctx,&deduplication.Request{
+		InfoId: req.InfoId,
+		EntId: req.EntId,
+		IsInsert:req.IsInsert,
+		IsEnt: req.IsEnt,
+		PersonId: req.PersonId,
+	})
+	if err != nil {
+		return nil, err
+	}
+	return &types.DedupResp{
+		Code: 0,
+		Msg: "请求成功",
+		Data: types.Info{
+			TotalCount: res.Data.TotalCount,
+			ExistCount :res.Data.ExistCount,
+			NewCount   :res.Data.NewCount,
+		},
+	}, nil
+}

+ 19 - 0
api/internal/svc/servicecontext.go

@@ -0,0 +1,19 @@
+package svc
+
+import (
+	"app.yhyue.com/moapp/dataDeduplication/api/internal/config"
+	"app.yhyue.com/moapp/dataDeduplication/rpc/deduplicationclient"
+	"github.com/tal-tech/go-zero/zrpc"
+)
+
+type ServiceContext struct {
+	Config config.Config
+	Dedup  deduplicationclient.Deduplication
+}
+
+func NewServiceContext(c config.Config) *ServiceContext {
+	return &ServiceContext{
+		Config: c,
+		Dedup: deduplicationclient.NewDeduplication(zrpc.MustNewClient(c.Deduplication)),
+	}
+}

+ 22 - 0
api/internal/types/types.go

@@ -0,0 +1,22 @@
+// Code generated by goctl. DO NOT EDIT.
+package types
+
+type Info struct {
+	TotalCount int64 `json:"totalCount"` // 本次查询info_id总量
+	ExistCount int64 `json:"existCount"` // 已存在的info_id的数量
+	NewCount   int64 `json:"NewCount"`   // 不存在的info_id的数量
+}
+
+type DedupReq struct {
+	PersonId string `form:"personId"` //人员id
+	InfoId   string `form:"infoId"`   //信息id  逗号分隔
+	EntId    string `form:"entId"`    // 企业id
+	IsInsert bool   `form:"isInsert"` // 是否插入不重复的数据
+	IsEnt    bool   `form:"isEnt"`    //是否按企业id判重
+}
+
+type DedupResp struct {
+	Code int64  `json:"code"`
+	Msg  string `json:"msg"`
+	Data Info   `json:"data"`
+}