| | |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/mysqlx" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type ClientService struct{} |
| | |
| | | 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 { |
| | |
| | | |
| | | for _, contact := range client.Contacts { |
| | | contact.ClientId = client.Id |
| | | if contact.Id == 0 { |
| | | err := model.NewContactSearch(tx).Create(&contact) |
| | | |
| | | // 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.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 |
| | | } |