| | |
| | | package service
|
| | |
|
| | | import (
|
| | | "aps_crm/model"
|
| | | "aps_crm/pkg/ecode"
|
| | | "aps_crm/pkg/mysqlx"
|
| | | )
|
| | |
|
| | | type ClientService struct{}
|
| | |
|
| | | 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
|
| | | }
|
| | |
|
| | | // CheckClientExist check client exist
|
| | | func CheckClientExist(id int) int {
|
| | | _, err := model.NewClientSearch(nil).SetId(id).First()
|
| | | if err != nil {
|
| | | return ecode.ClientNotExist
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ClientService) UpdateClient(client *model.Client) int {
|
| | | // check client exist
|
| | | errCode := CheckClientExist(client.Id)
|
| | | if errCode != ecode.OK {
|
| | | return errCode
|
| | | }
|
| | |
|
| | | // update client
|
| | | err := model.NewClientSearch(nil).SetId(client.Id).Update(client)
|
| | | if err != nil {
|
| | | return ecode.ClientUpdateErr
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ClientService) GetClientList(page, pageSize int, data map[string]interface{}) ([]*model.Client, int64, int) {
|
| | | // get contact list
|
| | | contacts, total, err := model.NewClientSearch(nil).SetPage(page, pageSize).SetSearchMap(data).Find()
|
| | | if err != nil {
|
| | | return nil, 0, ecode.ClientListErr
|
| | | }
|
| | | return contacts, total, ecode.OK
|
| | | }
|
| | |
|
| | | func (ClientService) Assign(ids []int, memberId int) int {
|
| | | // check client exist
|
| | | //errCode := CheckClientExist(id)
|
| | | //if errCode != ecode.OK {
|
| | | // return errCode
|
| | | //}
|
| | |
|
| | | // assign client
|
| | | err := model.NewClientSearch(nil).SetIds(ids).UpdateByMap(map[string]interface{}{
|
| | | "member_id": memberId,
|
| | | })
|
| | | if err != nil {
|
| | | return ecode.AssignErr
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ClientService) DeleteClient(ids []int) int {
|
| | | // delete client
|
| | | err := model.NewClientSearch(nil).SetIds(ids).Delete()
|
| | | if err != nil {
|
| | | return ecode.ClientDeleteErr
|
| | | }
|
| | | return ecode.OK
|
| | | }
|
| | | package service |
| | | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/mysqlx" |
| | | ) |
| | | |
| | | type ClientService struct{} |
| | | |
| | | 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 |
| | | } |
| | | |
| | | // CheckClientExist check client exist |
| | | func CheckClientExist(id int) int { |
| | | _, err := model.NewClientSearch(nil).SetId(id).First() |
| | | if err != nil { |
| | | return ecode.ClientNotExist |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ClientService) UpdateClient(client *model.Client) int { |
| | | // check client exist |
| | | errCode := CheckClientExist(client.Id) |
| | | if errCode != ecode.OK { |
| | | return errCode |
| | | } |
| | | |
| | | // update client |
| | | 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 |
| | | } |
| | | |
| | | func (ClientService) GetClientList(page, pageSize int, data map[string]interface{}) ([]*model.Client, int64, int) { |
| | | // get contact list |
| | | contacts, total, err := model.NewClientSearch(nil).SetPage(page, pageSize).SetSearchMap(data).Find() |
| | | if err != nil { |
| | | return nil, 0, ecode.ClientListErr |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (ClientService) Assign(ids []int, memberId int) int { |
| | | // check client exist |
| | | //errCode := CheckClientExist(id) |
| | | //if errCode != ecode.OK { |
| | | // return errCode |
| | | //} |
| | | |
| | | // assign client |
| | | err := model.NewClientSearch(nil).SetIds(ids).UpdateByMap(map[string]interface{}{ |
| | | "member_id": memberId, |
| | | }) |
| | | if err != nil { |
| | | return ecode.AssignErr |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ClientService) DeleteClient(ids []int) int { |
| | | // delete client |
| | | err := model.NewClientSearch(nil).SetIds(ids).Delete() |
| | | if err != nil { |
| | | return ecode.ClientDeleteErr |
| | | } |
| | | return ecode.OK |
| | | } |