From 20ca649b9f0fa0af0361024149dee6833858c2fa Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期二, 15 八月 2023 17:12:27 +0800
Subject: [PATCH] fix
---
service/client.go | 44 +++++++++++++++++++++++++++++++++++++++++---
1 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/service/client.go b/service/client.go
index 8743ee3..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{}
@@ -51,9 +52,36 @@
}
// update client
- err := model.NewClientSearch(nil).SetId(client.Id).Update(client)
- if err != nil {
- return ecode.ClientUpdateErr
+ if len(client.Contacts) == 0 {
+ err := model.NewClientSearch(nil).SetId(client.Id).Update(client)
+ if err != nil {
+ return ecode.ClientUpdateErr
+ }
+ } else {
+ tx := mysqlx.GetDB().Begin()
+ err := model.NewClientSearch(tx).SetId(client.Id).Update(client)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientUpdateErr
+ }
+
+ for _, contact := range client.Contacts {
+ contact.ClientId = client.Id
+ if contact.Id == 0 {
+ err := model.NewContactSearch(tx).Create(&contact)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientUpdateErr
+ }
+ } else {
+ err = model.NewContactSearch(tx).SetId(contact.Id).Update(&contact)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientUpdateErr
+ }
+ }
+ }
+ tx.Commit()
}
return ecode.OK
@@ -94,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