From fbb3b8c3eb3b48772dc1123561e68741d05dfffa Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 30 八月 2023 13:31:35 +0800
Subject: [PATCH] 统一记录编号
---
service/client.go | 80 ++++++++++++++++++++++++++++++++++++----
1 files changed, 72 insertions(+), 8 deletions(-)
diff --git a/service/client.go b/service/client.go
index 5243a3c..20cd752 100644
--- a/service/client.go
+++ b/service/client.go
@@ -3,15 +3,34 @@
import (
"aps_crm/model"
"aps_crm/pkg/ecode"
+ "aps_crm/pkg/mysqlx"
+ "gorm.io/gorm"
)
type ClientService struct{}
-func (ClientService) AddClient(client *model.Client) int {
- err := model.NewClientSearch(nil).Create(client)
- if err != nil {
- return ecode.ClientExist
+func (ClientService) AddClient(client *model.Client, sId int) int {
+ if sId == 0 {
+ err := model.NewClientSearch(nil).Create(client)
+ if err != nil {
+ return ecode.ClientExist
+ }
+ } else {
+ tx := mysqlx.GetDB().Begin()
+ err := model.NewClientSearch(tx).Create(client)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientExist
+ }
+
+ err = model.NewSalesLeadsSearch(tx).SetId(sId).Delete()
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientExist
+ }
+ tx.Commit()
}
+
return ecode.OK
}
@@ -25,7 +44,7 @@
return ecode.OK
}
-func (ClientService) UpdateClient(client *model.Client) int {
+func (ClientService) UpdateClient(client *model.Client, conId int) int {
// check client exist
errCode := CheckClientExist(client.Id)
if errCode != ecode.OK {
@@ -33,9 +52,44 @@
}
// 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
+
+ // check isFirst
+ errCode = setFirstContact(tx, &contact)
+ if errCode != ecode.OK {
+ return errCode
+ }
+
+ if conId == 0 {
+ err = model.NewContactSearch(tx).Create(&contact)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientUpdateErr
+ }
+ } else {
+ contact.Id = conId
+ err = model.NewContactSearch(tx).SetId(contact.Id).Update(&contact)
+ if err != nil {
+ tx.Rollback()
+ return ecode.ClientUpdateErr
+ }
+ }
+ }
+ tx.Commit()
}
return ecode.OK
@@ -76,3 +130,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