获取客户层级方法
This commit is contained in:
parent
737c73e914
commit
b7239af0e0
|
@ -99,6 +99,24 @@ func (m *KillaraCustomerModel) GetCustomerForBackEnd(customerID uint64) (*Killar
|
|||
return &customer, err
|
||||
}
|
||||
|
||||
// 可以用于代替下面的data结构
|
||||
type Filter struct {
|
||||
Type *int64 `json:"filter_type" form:"filter_type"`
|
||||
Email *string `json:"filter_email" form:"filter_email"`
|
||||
Telephone *string `json:"filter_telephone" form:"filter_telephone"`
|
||||
Nickname *string `json:"filter_nickname" form:"filter_nickname"`
|
||||
Name *string `json:"filter_name" form:"filter_name"`
|
||||
Code *string `json:"filter_code" form:"filter_code"`
|
||||
Realname *string `json:"filter_realname" form:"filter_realname"`
|
||||
Parents *[]int `json:"filter_parents" form:"filter_parents"`
|
||||
Status *int64 `json:"filter_status" form:"filter_status"`
|
||||
DisableA *bool `json:"filter_disable_a" form:"filter_disable_a"`
|
||||
Sort *string `json:"sort" form:"sort"`
|
||||
Order *string `json:"order" form:"order"`
|
||||
Start *int64 `json:"start" form:"start"`
|
||||
Limit *int64 `json:"limit" form:"limit"`
|
||||
}
|
||||
|
||||
func (m *KillaraCustomerModel) GetCustomersForBackEndForAutocomplete(data map[string]interface{}) ([]*KillaraCustomer, error) {
|
||||
var customers []*KillaraCustomer
|
||||
query := m.db.Model(&KillaraCustomer{})
|
||||
|
@ -300,3 +318,93 @@ func (m *KillaraCustomerModel) GetTotalCustomersForBackEnd(data map[string]inter
|
|||
}
|
||||
return count, err
|
||||
}
|
||||
|
||||
// 获取客户的所有上级
|
||||
func (m *KillaraCustomerModel) GetFullParents(parentID uint64) ([]*KillaraCustomer, error) {
|
||||
var parents []*KillaraCustomer
|
||||
|
||||
// 获取当前父级客户
|
||||
parent, err := m.GetCustomer(parentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if parent != nil {
|
||||
parents = append(parents, parent)
|
||||
|
||||
// 如果还有上级,则递归获取
|
||||
if parent.ParentId != nil {
|
||||
upperParents, err := m.GetFullParents(uint64(*parent.ParentId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parents = append(parents, upperParents...)
|
||||
}
|
||||
}
|
||||
|
||||
return parents, nil
|
||||
}
|
||||
|
||||
// 获取客户自身及所有上级
|
||||
func (m *KillaraCustomerModel) GetFullParentsFromSelf(customerID uint64) ([]*KillaraCustomer, error) {
|
||||
customer, err := m.GetCustomer(customerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var parents []*KillaraCustomer
|
||||
if customer != nil && customer.ParentId != nil {
|
||||
parents, err = m.GetFullParents(uint64(*customer.ParentId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
parents = append([]*KillaraCustomer{customer}, parents...)
|
||||
return parents, nil
|
||||
}
|
||||
|
||||
// 获取客户的所有下级
|
||||
func (m *KillaraCustomerModel) GetFullChildren(parentID uint64) ([]*KillaraCustomer, error) {
|
||||
var children []*KillaraCustomer
|
||||
|
||||
// 获取直接下级
|
||||
directChildren, err := m.GetCustomersForBackEnd(map[string]interface{}{
|
||||
"filter_parent_id": parentID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
children = append(children, directChildren...)
|
||||
|
||||
// 递归获取下级的下级
|
||||
for _, child := range directChildren {
|
||||
grandChildren, err := m.GetFullChildren(*child.CustomerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
children = append(children, grandChildren...)
|
||||
}
|
||||
|
||||
return children, nil
|
||||
}
|
||||
|
||||
// 获取客户自身及所有下级
|
||||
func (m *KillaraCustomerModel) GetFullChildrenWithSelf(customerID uint64) ([]*KillaraCustomer, error) {
|
||||
customer, err := m.GetCustomer(customerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var children []*KillaraCustomer
|
||||
if customer != nil {
|
||||
children = append(children, customer)
|
||||
childrenOfCustomer, err := m.GetFullChildren(*customer.CustomerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
children = append(children, childrenOfCustomer...)
|
||||
}
|
||||
|
||||
return children, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user