From dd39909b3d8172856313ea7b2e19d1d7a039a582 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期二, 15 八月 2023 16:43:22 +0800
Subject: [PATCH] fix

---
 api/v1/client.go        |   24 ++++++++
 service/client.go       |   11 +++
 model/client.go         |   12 +++
 model/request/client.go |    4 +
 docs/swagger.yaml       |   24 ++++++++
 router/client.go        |    5 +
 docs/docs.go            |   38 ++++++++++++
 docs/swagger.json       |   38 ++++++++++++
 8 files changed, 153 insertions(+), 3 deletions(-)

diff --git a/api/v1/client.go b/api/v1/client.go
index 1162b78..c4da6b7 100644
--- a/api/v1/client.go
+++ b/api/v1/client.go
@@ -201,3 +201,27 @@
 		Count: int(total),
 	})
 }
+
+// CheckName
+//
+//	@Tags		Client
+//	@Summary	妫�鏌ュ鎴峰悕绉版槸鍚﹂噸澶�
+//	@Produce	application/json
+//	@Param		object	body		request.CheckClientName	true	"鍙傛暟"
+//	@Success	200		{object}	contextx.Response{}
+//	@Router		/api/client/checkName [post]
+func (cli *ClientApi) CheckName(c *gin.Context) {
+	var params request.CheckClientName
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+
+	errCode := clientService.CheckName(params.Name)
+	if errCode != ecode.OK {
+		ctx.Fail(errCode)
+		return
+	}
+
+	ctx.Ok()
+}
diff --git a/docs/docs.go b/docs/docs.go
index 0236c2f..8d18427 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -664,6 +664,36 @@
                 }
             }
         },
+        "/api/client/checkName": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Client"
+                ],
+                "summary": "妫�鏌ュ鎴峰悕绉版槸鍚﹂噸澶�",
+                "parameters": [
+                    {
+                        "description": "鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.CheckClientName"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/contextx.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/api/client/delete": {
             "delete": {
                 "produces": [
@@ -14515,6 +14545,14 @@
                 }
             }
         },
+        "request.CheckClientName": {
+            "type": "object",
+            "properties": {
+                "name": {
+                    "type": "string"
+                }
+            }
+        },
         "request.Contact": {
             "type": "object",
             "properties": {
diff --git a/docs/swagger.json b/docs/swagger.json
index 1465934..f5753ae 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -652,6 +652,36 @@
                 }
             }
         },
+        "/api/client/checkName": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Client"
+                ],
+                "summary": "妫�鏌ュ鎴峰悕绉版槸鍚﹂噸澶�",
+                "parameters": [
+                    {
+                        "description": "鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.CheckClientName"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/contextx.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/api/client/delete": {
             "delete": {
                 "produces": [
@@ -14503,6 +14533,14 @@
                 }
             }
         },
+        "request.CheckClientName": {
+            "type": "object",
+            "properties": {
+                "name": {
+                    "type": "string"
+                }
+            }
+        },
         "request.Contact": {
             "type": "object",
             "properties": {
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 6628e6d..24f3e2d 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -3103,6 +3103,11 @@
         description: 鐢ㄦ埛宀椾綅
         type: string
     type: object
+  request.CheckClientName:
+    properties:
+      name:
+        type: string
+    type: object
   request.Contact:
     properties:
       birthday:
@@ -6286,6 +6291,25 @@
       summary: 娣诲姞瀹㈡埛
       tags:
       - Client
+  /api/client/checkName:
+    post:
+      parameters:
+      - description: 鍙傛暟
+        in: body
+        name: object
+        required: true
+        schema:
+          $ref: '#/definitions/request.CheckClientName'
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/contextx.Response'
+      summary: 妫�鏌ュ鎴峰悕绉版槸鍚﹂噸澶�
+      tags:
+      - Client
   /api/client/delete:
     delete:
       parameters:
diff --git a/model/client.go b/model/client.go
index f68f542..f7c2b5e 100644
--- a/model/client.go
+++ b/model/client.go
@@ -10,7 +10,8 @@
 type (
 	Client struct {
 		Id                int            `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"`
-		Name              string         `json:"name" gorm:"column:name;unique;type:varchar(255);comment:瀹㈡埛鍚嶇О"`
+		Name              string         `json:"name" gorm:"column:name;uniqueIndex:name_isDeleted_idx;type:varchar(255);comment:瀹㈡埛鍚嶇О"`
+		IsDeleted         bool           `json:"-" gorm:"column:is_deleted;uniqueIndex:name_isDeleted_idx;type:tinyint(1);comment:鏄惁鍒犻櫎"`
 		Number            string         `json:"number" gorm:"column:number;type:varchar(255);comment:瀹㈡埛缂栧彿"`
 		ClientStatusId    int            `json:"client_status_id" gorm:"column:client_status_id;type:int(11);comment:瀹㈡埛鐘舵�両D"`
 		ClientStatus      ClientStatus   `json:"client_status" gorm:"foreignKey:ClientStatusId"`
@@ -59,6 +60,15 @@
 	}
 }
 
+func (c *Client) BeforeSave(tx *gorm.DB) (err error) {
+	if c.DeletedAt.Valid {
+		c.IsDeleted = true
+	} else {
+		c.IsDeleted = false
+	}
+	return
+}
+
 func (slf *ClientSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&Client{})
 	if slf.Id != 0 {
diff --git a/model/request/client.go b/model/request/client.go
index fb41a58..29303bf 100644
--- a/model/request/client.go
+++ b/model/request/client.go
@@ -36,3 +36,7 @@
 type DeleteClient struct {
 	Ids []int `json:"ids"`
 }
+
+type CheckClientName struct {
+	Name string `json:"name"`
+}
diff --git a/router/client.go b/router/client.go
index 3563b8d..b63dfaa 100644
--- a/router/client.go
+++ b/router/client.go
@@ -14,6 +14,7 @@
 		clientRouter.POST("add", clientApi.Add)             // 娣诲姞瀹㈡埛
 		clientRouter.DELETE("delete", clientApi.Delete)     // 鍒犻櫎瀹㈡埛
 		clientRouter.PUT("update", clientApi.Update)        // 鏇存柊瀹㈡埛
-		clientRouter.POST("list", clientApi.List)            // 鑾峰彇瀹㈡埛鍒楄〃
+		clientRouter.POST("list", clientApi.List)           // 鑾峰彇瀹㈡埛鍒楄〃
+		clientRouter.POST("checkName", clientApi.CheckName) // 妫�鏌ュ鎴峰悕绉版槸鍚﹂噸澶�
 	}
-}
\ No newline at end of file
+}
diff --git a/service/client.go b/service/client.go
index 523e368..e889606 100644
--- a/service/client.go
+++ b/service/client.go
@@ -4,6 +4,7 @@
 	"aps_crm/model"
 	"aps_crm/pkg/ecode"
 	"aps_crm/pkg/mysqlx"
+	"gorm.io/gorm"
 )
 
 type ClientService struct{}
@@ -121,3 +122,13 @@
 	}
 	return ecode.OK
 }
+
+func (ClientService) CheckName(name string) int {
+	// check client exist
+	_, err := model.NewClientSearch(nil).SetName(name).First()
+	if err != gorm.ErrRecordNotFound {
+		return ecode.ClientExist
+	}
+
+	return ecode.OK
+}

--
Gitblit v1.8.0