获取客户层级方法

This commit is contained in:
474420502 2024-04-11 10:14:48 +08:00
parent 737c73e914
commit b7239af0e0
2 changed files with 111 additions and 1 deletions

View File

@ -99,6 +99,24 @@ func (m *KillaraCustomerModel) GetCustomerForBackEnd(customerID uint64) (*Killar
return &customer, err 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) { func (m *KillaraCustomerModel) GetCustomersForBackEndForAutocomplete(data map[string]interface{}) ([]*KillaraCustomer, error) {
var customers []*KillaraCustomer var customers []*KillaraCustomer
query := m.db.Model(&KillaraCustomer{}) query := m.db.Model(&KillaraCustomer{})
@ -300,3 +318,93 @@ func (m *KillaraCustomerModel) GetTotalCustomersForBackEnd(data map[string]inter
} }
return count, err 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
}

View File

@ -8,4 +8,6 @@
4. 日志设计不完善。 4. 日志设计不完善。
5. 没事务回滚, 经过异步处理后, 事务无法一致 5. 没事务回滚, 经过异步处理后, 事务无法一致
6. 电话号码和邮件不是唯一, 帐号表数据可能混乱 6. 电话号码和邮件不是唯一, 帐号表数据可能混乱
7. 表绝大部分没建索引, 数量上去, 性能立马下降 7. 表绝大部分没建索引, 数量上去, 性能立马下降
8. 表的部分字段不对应, 如 parent_id 和 customer_id 是同一事物, 但是类型不一样. 可能会产生不可预料的错误
9. 数据库里递归查询, 一个查询n个query请求.出问题数据库死锁,整个项目崩溃. 例如 getFullParents