| | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/mysqlx" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | |
| | | return code |
| | | } |
| | | |
| | | tx := model.NewContactSearch().Orm.Begin() |
| | | |
| | | tx := mysqlx.GetDB().Begin() |
| | | // check isFirst |
| | | errCode := setFirstContact(tx, contact) |
| | | if errCode != ecode.OK { |
| | | return errCode |
| | | } |
| | | |
| | | err := model.NewContactSearch().Create(contact) |
| | | err := model.NewContactSearch(tx).Create(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactExist |
| | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) DeleteContact(id int) int { |
| | | // check contact exist |
| | | _, err := model.NewContactSearch().SetId(id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | // delete contact |
| | | err = model.NewContactSearch().SetId(id).Delete() |
| | | if err != nil { |
| | | return ecode.ContactDeleteErr |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) GetContactList() ([]*model.Contact, int) { |
| | | // get contact list |
| | | contacts, err := model.NewContactSearch().FindAll() |
| | | if err != nil { |
| | | return nil, ecode.ContactListErr |
| | | } |
| | | return contacts, ecode.OK |
| | | } |
| | | |
| | | func (ContactService) UpdateContact(contact *model.Contact) int { |
| | | // check contact exist |
| | | _, err := model.NewContactSearch().SetId(contact.Id).First() |
| | | _, err := model.NewContactSearch(nil).SetId(contact.Id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | |
| | | return code |
| | | } |
| | | |
| | | tx := model.NewContactSearch().Orm.Begin() |
| | | tx := mysqlx.GetDB().Begin() |
| | | code = setFirstContact(tx, contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | // update contact |
| | | err = model.NewContactSearch().SetId(contact.Id).Update(contact) |
| | | err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactUpdateErr |
| | |
| | | |
| | | func setFirstContact(tx *gorm.DB, contact *model.Contact) int { |
| | | if contact.IsFirst && contact.ClientId != 0 { |
| | | err := model.NewContactSearch().SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{ |
| | | err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{ |
| | | "is_first": false, |
| | | }) |
| | | if err != nil { |
| | |
| | | |
| | | // CheckContactExist check contact exist |
| | | func CheckContactExist(id int) int { |
| | | tmp, err := model.NewContactSearch().SetId(id).First() |
| | | tmp, err := model.NewContactSearch(nil).SetId(id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) GetContactList(page, pageSize int, data map[string]interface{}) ([]*model.ContactDetail, int64, int) { |
| | | // get contact list |
| | | contacts, total, err := model.NewContactSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll() |
| | | if err != nil { |
| | | return nil, 0, ecode.ContactListErr |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (ContactService) DeleteContact(ids []int) int { |
| | | // delete client |
| | | err := model.NewContactSearch(nil).SetIds(ids).Delete() |
| | | if err != nil { |
| | | return ecode.ContactDeleteErr |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) Assign(ids []int, memberId int) int { |
| | | // check contact exist |
| | | //errCode := CheckContactExist(id) |
| | | //if errCode != ecode.OK { |
| | | // return errCode |
| | | //} |
| | | |
| | | // assign contact |
| | | err := model.NewContactSearch(nil).SetIds(ids).UpdateByMap(map[string]interface{}{ |
| | | "member_id": memberId, |
| | | }) |
| | | if err != nil { |
| | | return ecode.ContactAssignErr |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |