From 163257e81abbf954ba140ce22098fa78a66aa682 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期二, 25 七月 2023 19:47:59 +0800
Subject: [PATCH] add

---
 api/v1/client.go           |   49 ++++++-----
 service/client.go          |   19 ++--
 model/client.go            |   26 ++++++
 model/request/client.go    |    5 +
 docs/swagger.yaml          |   24 +++++
 router/client.go           |    4 
 docs/docs.go               |   34 ++++++++
 docs/swagger.json          |   34 ++++++++
 model/response/response.go |    3 
 9 files changed, 156 insertions(+), 42 deletions(-)

diff --git a/api/v1/client.go b/api/v1/client.go
index 96aa141..b691940 100644
--- a/api/v1/client.go
+++ b/api/v1/client.go
@@ -44,28 +44,6 @@
 	ctx.Ok()
 }
 
-// List
-//
-//	@Tags		Client
-//	@Summary	鑾峰彇瀹㈡埛鍒楄〃
-//	@Produce	application/json
-//	@Success	200	{object}	contextx.Response{data=response.ClientResponse}
-//	@Router		/api/client/list [get]
-func (cli *ClientApi) List(c *gin.Context) {
-	ctx, ok := contextx.NewContext(c, nil)
-	if !ok {
-		return
-	}
-
-	errCode, clients := clientService.GetClientList()
-	if errCode != ecode.OK {
-		ctx.Fail(errCode)
-		return
-	}
-
-	ctx.OkWithDetailed(response.ClientResponse{List: clients})
-}
-
 func checkClientParams(params request.Client) (int, *model.Client) {
 	client := new(model.Client)
 	//if params.Name == "" {
@@ -198,3 +176,30 @@
 
 	ctx.Ok()
 }
+
+// List
+//
+//	@Tags		Client
+//	@Summary	瀹㈡埛鍒楄〃
+//	@Produce	application/json
+//	@Param		object	body		request.GetClientList	true	"鍙傛暟"
+//	@Success	200		{object}	contextx.Response{data=response.ClientResponse}
+//	@Router		/api/client/list [post]
+func (cli *ClientApi) List(c *gin.Context) {
+	var params request.GetClientList
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+
+	clients, errCode := clientService.GetClientList(params.Page, params.PageSize, params.Keyword)
+	if errCode != ecode.OK {
+		ctx.Fail(errCode)
+		return
+	}
+
+	ctx.OkWithDetailed(response.ClientResponse{
+		List:  clients,
+		Count: len(clients),
+	})
+}
diff --git a/docs/docs.go b/docs/docs.go
index bc4cbff..717105a 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -500,14 +500,25 @@
             }
         },
         "/api/client/list": {
-            "get": {
+            "post": {
                 "produces": [
                     "application/json"
                 ],
                 "tags": [
                     "Client"
                 ],
-                "summary": "鑾峰彇瀹㈡埛鍒楄〃",
+                "summary": "瀹㈡埛鍒楄〃",
+                "parameters": [
+                    {
+                        "description": "鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.GetClientList"
+                        }
+                    }
+                ],
                 "responses": {
                     "200": {
                         "description": "OK",
@@ -9725,6 +9736,22 @@
                 }
             }
         },
+        "request.GetClientList": {
+            "type": "object",
+            "properties": {
+                "keyword": {
+                    "type": "string"
+                },
+                "page": {
+                    "description": "椤电爜",
+                    "type": "integer"
+                },
+                "pageSize": {
+                    "description": "姣忛〉澶у皬",
+                    "type": "integer"
+                }
+            }
+        },
         "request.GetContactList": {
             "type": "object",
             "properties": {
@@ -11834,6 +11861,9 @@
         "response.ClientResponse": {
             "type": "object",
             "properties": {
+                "count": {
+                    "type": "integer"
+                },
                 "list": {
                     "type": "array",
                     "items": {
diff --git a/docs/swagger.json b/docs/swagger.json
index 1500782..43f1388 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -488,14 +488,25 @@
             }
         },
         "/api/client/list": {
-            "get": {
+            "post": {
                 "produces": [
                     "application/json"
                 ],
                 "tags": [
                     "Client"
                 ],
-                "summary": "鑾峰彇瀹㈡埛鍒楄〃",
+                "summary": "瀹㈡埛鍒楄〃",
+                "parameters": [
+                    {
+                        "description": "鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.GetClientList"
+                        }
+                    }
+                ],
                 "responses": {
                     "200": {
                         "description": "OK",
@@ -9713,6 +9724,22 @@
                 }
             }
         },
+        "request.GetClientList": {
+            "type": "object",
+            "properties": {
+                "keyword": {
+                    "type": "string"
+                },
+                "page": {
+                    "description": "椤电爜",
+                    "type": "integer"
+                },
+                "pageSize": {
+                    "description": "姣忛〉澶у皬",
+                    "type": "integer"
+                }
+            }
+        },
         "request.GetContactList": {
             "type": "object",
             "properties": {
@@ -11822,6 +11849,9 @@
         "response.ClientResponse": {
             "type": "object",
             "properties": {
+                "count": {
+                    "type": "integer"
+                },
                 "list": {
                     "type": "array",
                     "items": {
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 4dbfe02..260e944 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -1907,6 +1907,17 @@
       topic:
         type: string
     type: object
+  request.GetClientList:
+    properties:
+      keyword:
+        type: string
+      page:
+        description: 椤电爜
+        type: integer
+      pageSize:
+        description: 姣忛〉澶у皬
+        type: integer
+    type: object
   request.GetContactList:
     properties:
       keyword:
@@ -3319,6 +3330,8 @@
     type: object
   response.ClientResponse:
     properties:
+      count:
+        type: integer
       list:
         items:
           $ref: '#/definitions/model.Client'
@@ -4136,7 +4149,14 @@
       tags:
       - Client
   /api/client/list:
-    get:
+    post:
+      parameters:
+      - description: 鍙傛暟
+        in: body
+        name: object
+        required: true
+        schema:
+          $ref: '#/definitions/request.GetClientList'
       produces:
       - application/json
       responses:
@@ -4149,7 +4169,7 @@
                 data:
                   $ref: '#/definitions/response.ClientResponse'
               type: object
-      summary: 鑾峰彇瀹㈡埛鍒楄〃
+      summary: 瀹㈡埛鍒楄〃
       tags:
       - Client
   /api/client/update:
diff --git a/model/client.go b/model/client.go
index c764ea9..e76a52d 100644
--- a/model/client.go
+++ b/model/client.go
@@ -34,7 +34,13 @@
 
 	ClientSearch struct {
 		Client
-		Orm *gorm.DB
+
+				Orm      *gorm.DB
+		Keyword  string
+		OrderBy  string
+		PageNum  int
+		PageSize int
+
 	}
 )
 
@@ -54,6 +60,9 @@
 
 func (slf *ClientSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&Client{})
+	if slf.Keyword != "" {
+		db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
+	}
 	if slf.Id != 0 {
 		db.Where("id = ?", slf.Id)
 	}
@@ -112,3 +121,18 @@
 	var db = slf.build()
 	return db.Updates(data).Error
 }
+
+func (slf *ClientSearch) SetKeyword(keyword string) *ClientSearch {
+	slf.Keyword = keyword
+	return slf
+}
+
+func (slf *ClientSearch) SetPage(page, size int) *ClientSearch {
+	slf.PageNum, slf.PageSize = page, size
+	return slf
+}
+
+func (slf *ClientSearch) SetOrder(order string) *ClientSearch {
+	slf.OrderBy = order
+	return slf
+}
diff --git a/model/request/client.go b/model/request/client.go
index 7e547f5..e27bb01 100644
--- a/model/request/client.go
+++ b/model/request/client.go
@@ -26,3 +26,8 @@
 	Id int `json:"id"`
 	Client
 }
+
+type GetClientList struct {
+	PageInfo
+	Keyword string `json:"keyword"`
+}
diff --git a/model/response/response.go b/model/response/response.go
index 3a75735..bb66ace 100644
--- a/model/response/response.go
+++ b/model/response/response.go
@@ -52,7 +52,8 @@
 	}
 
 	ClientResponse struct {
-		List []*model.Client `json:"list"`
+		List  []*model.Client `json:"list"`
+		Count int             `json:"count"`
 	}
 
 	ClientStatusResponse struct {
diff --git a/router/client.go b/router/client.go
index 7111685..1302ef8 100644
--- a/router/client.go
+++ b/router/client.go
@@ -14,6 +14,6 @@
 		clientRouter.POST("add", clientApi.Add)             // 娣诲姞瀹㈡埛
 		clientRouter.DELETE("delete/:id", clientApi.Delete) // 鍒犻櫎瀹㈡埛
 		clientRouter.PUT("update", clientApi.Update)        // 鏇存柊瀹㈡埛
-		clientRouter.GET("list", clientApi.List)            // 鑾峰彇瀹㈡埛鍒楄〃
+		clientRouter.POST("list", clientApi.List)            // 鑾峰彇瀹㈡埛鍒楄〃
 	}
-}
+}
\ No newline at end of file
diff --git a/service/client.go b/service/client.go
index 260ef66..2c9a1cd 100644
--- a/service/client.go
+++ b/service/client.go
@@ -30,16 +30,6 @@
 	return ecode.OK
 }
 
-func (ClientService) GetClientList() (int, []*model.Client) {
-	// get client list
-	clients, err := model.NewClientSearch(nil).Find()
-	if err != nil {
-		return ecode.ClientListErr, nil
-	}
-
-	return ecode.OK, clients
-}
-
 // CheckClientExist check client exist
 func CheckClientExist(id int) int {
 	_, err := model.NewClientSearch(nil).SetId(id).First()
@@ -65,3 +55,12 @@
 
 	return ecode.OK
 }
+
+func (ClientService) GetClientList(page, pageSize int, keyword string) ([]*model.Client, int) {
+	// get contact list
+	contacts, err := model.NewClientSearch(nil).SetKeyword(keyword).SetPage(page, pageSize).Find()
+	if err != nil {
+		return nil, ecode.ClientListErr
+	}
+	return contacts, ecode.OK
+}

--
Gitblit v1.8.0