| | |
| | | package service
|
| | |
|
| | | import (
|
| | | "aps_crm/model"
|
| | | "aps_crm/pkg/ecode"
|
| | | "aps_crm/pkg/mysqlx"
|
| | | "gorm.io/gorm"
|
| | | )
|
| | |
|
| | | type ContactService struct{}
|
| | |
|
| | | func (ContactService) AddContact(contact *model.Contact) int {
|
| | |
|
| | | code := checkMemberAndCompany(contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | tx := mysqlx.GetDB().Begin()
|
| | | // check isFirst
|
| | | errCode := setFirstContact(tx, contact)
|
| | | if errCode != ecode.OK {
|
| | | return errCode
|
| | | }
|
| | |
|
| | | err := model.NewContactSearch(tx).Create(contact)
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactExist
|
| | | }
|
| | |
|
| | | tx.Commit()
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) UpdateContact(contact *model.Contact) int {
|
| | | // check contact exist
|
| | | _, err := model.NewContactSearch(nil).SetId(contact.Id).First()
|
| | | if err != nil {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | code := checkMemberAndCompany(contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | tx := mysqlx.GetDB().Begin()
|
| | | code = setFirstContact(tx, contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | // update contact
|
| | | err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact)
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactUpdateErr
|
| | | }
|
| | |
|
| | | tx.Commit()
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func checkMemberAndCompany(contact *model.Contact) int {
|
| | | // check client exist
|
| | | code := CheckClientExist(contact.ClientId)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | // check member exist
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func setFirstContact(tx *gorm.DB, contact *model.Contact) int {
|
| | | if contact.IsFirst && contact.ClientId != 0 {
|
| | | err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{
|
| | | "is_first": false,
|
| | | })
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactUpdateErr
|
| | | }
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | // CheckContactExist check contact exist
|
| | | func CheckContactExist(id int) int {
|
| | | tmp, err := model.NewContactSearch(nil).SetId(id).First()
|
| | | if err != nil {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | if tmp.Id == 0 {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) GetContactList(page, pageSize int, keyword string) ([]*model.ContactDetail, int64, int) {
|
| | | // get contact list
|
| | | contacts, total, err := model.NewContactSearch(nil).SetKeyword(keyword).SetPage(page, pageSize).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
|
| | | }
|
| | | package service |
| | | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/mysqlx" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type ContactService struct{} |
| | | |
| | | func (ContactService) AddContact(contact *model.Contact) int { |
| | | |
| | | code := checkMemberAndCompany(contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | tx := mysqlx.GetDB().Begin() |
| | | // check isFirst |
| | | errCode := setFirstContact(tx, contact) |
| | | if errCode != ecode.OK { |
| | | return errCode |
| | | } |
| | | |
| | | err := model.NewContactSearch(tx).Create(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactExist |
| | | } |
| | | |
| | | tx.Commit() |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) UpdateContact(contact *model.Contact) int { |
| | | // check contact exist |
| | | _, err := model.NewContactSearch(nil).SetId(contact.Id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | code := checkMemberAndCompany(contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | tx := mysqlx.GetDB().Begin() |
| | | code = setFirstContact(tx, contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | // update contact |
| | | err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactUpdateErr |
| | | } |
| | | |
| | | tx.Commit() |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func checkMemberAndCompany(contact *model.Contact) int { |
| | | // check client exist |
| | | code := CheckClientExist(contact.ClientId) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | // check member exist |
| | | return ecode.OK |
| | | } |
| | | |
| | | func setFirstContact(tx *gorm.DB, contact *model.Contact) int { |
| | | if contact.IsFirst && contact.ClientId != 0 { |
| | | err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{ |
| | | "is_first": false, |
| | | }) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactUpdateErr |
| | | } |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | // CheckContactExist check contact exist |
| | | func CheckContactExist(id int) int { |
| | | tmp, err := model.NewContactSearch(nil).SetId(id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | if tmp.Id == 0 { |
| | | 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 |
| | | } |