411 lines
13 KiB
Go
411 lines
13 KiB
Go
package model
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type KillaraCustomerModel struct {
|
|
// fields ...
|
|
db *gorm.DB
|
|
TableName string // 表名
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) InsertCustomer(customer *KillaraCustomer) (uint64, error) {
|
|
|
|
if err := m.db.Model(&KillaraCustomer{}).Create(customer).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return *customer.CustomerId, nil
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) UpdateCustomer(customer *KillaraCustomer) error {
|
|
return m.db.Model(&KillaraCustomer{}).Where("customer_id = ?", customer.CustomerId).Updates(customer).Error
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) DeleteCustomer(customer *KillaraCustomer) error {
|
|
return m.db.Where("customer_id = ?", customer.CustomerId).Delete(&KillaraCustomer{}).Error
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomer(customerID uint64) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("customer_id = ? AND status = 1", customerID).First(&customer).Error
|
|
return &customer, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) CheckTelephoneExists(telephone string) (bool, error) {
|
|
// var count int64
|
|
// err := m.db.Model(&KillaraCustomer{}).Where("telephone = ?", telephone).Count(&count).Error
|
|
err := m.db.Model(&KillaraCustomer{}).Where("telephone = ?", telephone).First(nil).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return false, nil
|
|
}
|
|
return err != nil, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) CheckEmailExists(email string) (bool, error) {
|
|
// var count int64
|
|
err := m.db.Model(&KillaraCustomer{}).Where("email = ?", email).First(nil).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return false, nil
|
|
}
|
|
return err != nil, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomerByCode(code string) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("code = ? AND status = 1", code).First(&customer).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return &customer, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomerByTelephone(telephone string) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("telephone = ? AND status = 1", telephone).Order("customer_id").First(&customer).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return &customer, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomerByTelephoneForBackEnd(telephone string) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("telephone = ?", telephone).First(&customer).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return &customer, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomerByEmailForBackEnd(email string) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("email = ?", email).First(&customer).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return &customer, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomerForBackEnd(customerID uint64) (*KillaraCustomer, error) {
|
|
var customer KillaraCustomer
|
|
err := m.db.Where("customer_id = ?", customerID).First(&customer).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
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{})
|
|
|
|
// 过滤客户 ID
|
|
if filterParents, ok := data["filter_parents"]; ok {
|
|
if parentIDs, ok := filterParents.([]uint64); ok && len(parentIDs) > 0 {
|
|
query = query.Where("customer_id IN (?)", parentIDs)
|
|
}
|
|
}
|
|
|
|
// 构建模糊搜索条件
|
|
conditions := make([]interface{}, 0)
|
|
|
|
if filterTelephone, ok := data["filter_telephone"].(string); ok && filterTelephone != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, telephone) > 0", filterTelephone))
|
|
}
|
|
|
|
if filterEmail, ok := data["filter_email"].(string); ok && filterEmail != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, email) > 0", filterEmail))
|
|
}
|
|
|
|
if filterNickname, ok := data["filter_nickname"].(string); ok && filterNickname != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, nickname) > 0", filterNickname))
|
|
}
|
|
|
|
if len(conditions) > 0 {
|
|
query = query.Where(strings.Join(make([]string, len(conditions)), " OR "), conditions...)
|
|
}
|
|
|
|
// 排序和分页
|
|
query = query.Order("customer_id DESC")
|
|
|
|
if start, ok := data["start"].(int); ok && start > 0 {
|
|
query = query.Offset(start)
|
|
}
|
|
|
|
if limit, ok := data["limit"].(int); ok && limit > 0 {
|
|
query = query.Limit(limit)
|
|
} else {
|
|
query = query.Limit(10)
|
|
}
|
|
|
|
err := query.Find(&customers).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return customers, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetCustomersForBackEnd(data map[string]interface{}) ([]*KillaraCustomer, error) {
|
|
var customers []*KillaraCustomer
|
|
query := m.db.Model(&KillaraCustomer{})
|
|
|
|
conditions := make([]interface{}, 0)
|
|
|
|
if filterType, ok := data["filter_type"]; ok {
|
|
conditions = append(conditions, m.db.Where("`type` = ?", filterType))
|
|
}
|
|
|
|
if filterRealname, ok := data["filter_realname"].(string); ok && filterRealname != "" {
|
|
conditions = append(conditions, m.db.Where("`realname` = ?", filterRealname))
|
|
}
|
|
|
|
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, code) > 0", filterCode))
|
|
}
|
|
|
|
if filterTelephone, ok := data["filter_telephone"].(string); ok && filterTelephone != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, telephone) > 0", filterTelephone))
|
|
}
|
|
|
|
if filterEmail, ok := data["filter_email"].(string); ok && filterEmail != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, email) > 0", filterEmail))
|
|
}
|
|
|
|
if filterNickname, ok := data["filter_nickname"].(string); ok && filterNickname != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, nickname) > 0", filterNickname))
|
|
}
|
|
|
|
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, name) > 0", filterName))
|
|
}
|
|
|
|
if filterStatus, ok := data["filter_status"]; ok {
|
|
conditions = append(conditions, m.db.Where("`status` = ?", filterStatus))
|
|
}
|
|
|
|
if filterDisableA, ok := data["filter_disable_a"]; ok {
|
|
conditions = append(conditions, m.db.Where("`disable_a` = ?", filterDisableA))
|
|
}
|
|
|
|
if filterParentID, ok := data["filter_parent_id"]; ok {
|
|
conditions = append(conditions, m.db.Where("`parent_id` = ?", filterParentID))
|
|
}
|
|
|
|
if filterParents, ok := data["filter_parents"]; ok {
|
|
if parentIDs, ok := filterParents.([]uint64); ok && len(parentIDs) > 0 {
|
|
conditions = append(conditions, m.db.Where("`parent_id` IN (?)", parentIDs))
|
|
}
|
|
}
|
|
|
|
if filterInsertDate, ok := data["filter_insert_date"].(string); ok && filterInsertDate != "" {
|
|
conditions = append(conditions, m.db.Where("DATE(`insert_date`) = ?", filterInsertDate))
|
|
}
|
|
|
|
if len(conditions) > 0 {
|
|
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
|
|
}
|
|
|
|
if sort, ok := data["sort"].(string); ok && sort != "" {
|
|
order := "DESC"
|
|
if sortOrder, ok := data["order"].(string); ok {
|
|
order = sortOrder
|
|
}
|
|
query = query.Order(sort + " " + order)
|
|
}
|
|
|
|
if start, ok := data["start"].(int); ok && start > 0 {
|
|
query = query.Offset(start)
|
|
}
|
|
|
|
if limit, ok := data["limit"].(int); ok && limit > 0 {
|
|
query = query.Limit(limit)
|
|
} else {
|
|
query = query.Limit(10)
|
|
}
|
|
|
|
err := query.Find(&customers).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return customers, err
|
|
}
|
|
|
|
func (m *KillaraCustomerModel) GetTotalCustomersForBackEnd(data map[string]interface{}) (int64, error) {
|
|
var count int64
|
|
query := m.db.Model(&KillaraCustomer{})
|
|
|
|
var conditions []interface{}
|
|
|
|
if filterType, ok := data["filter_type"]; ok {
|
|
conditions = append(conditions, m.db.Where("`type` = ?", filterType))
|
|
}
|
|
|
|
if filterRealname, ok := data["filter_realname"].(string); ok && filterRealname != "" {
|
|
conditions = append(conditions, m.db.Where("`realname` = ?", filterRealname))
|
|
}
|
|
|
|
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, code) > 0", filterCode))
|
|
}
|
|
|
|
if filterTelephone, ok := data["filter_telephone"].(string); ok && filterTelephone != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, telephone) > 0", filterTelephone))
|
|
}
|
|
|
|
if filterEmail, ok := data["filter_email"].(string); ok && filterEmail != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, email) > 0", filterEmail))
|
|
}
|
|
|
|
if filterNickname, ok := data["filter_nickname"].(string); ok && filterNickname != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, nickname) > 0", filterNickname))
|
|
}
|
|
|
|
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
|
|
conditions = append(conditions, m.db.Where("LOCATE(?, name) > 0", filterName))
|
|
}
|
|
|
|
if filterStatus, ok := data["filter_status"]; ok {
|
|
conditions = append(conditions, m.db.Where("`status` = ?", filterStatus))
|
|
}
|
|
|
|
if filterDisableA, ok := data["filter_disable_a"]; ok {
|
|
conditions = append(conditions, m.db.Where("`disable_a` = ?", filterDisableA))
|
|
}
|
|
|
|
if filterParentID, ok := data["filter_parent_id"]; ok {
|
|
conditions = append(conditions, m.db.Where("`parent_id` = ?", filterParentID))
|
|
}
|
|
|
|
if filterParents, ok := data["filter_parents"]; ok {
|
|
if parentIDs, ok := filterParents.([]uint64); ok && len(parentIDs) > 0 {
|
|
conditions = append(conditions, m.db.Where("`parent_id` IN (?)", parentIDs))
|
|
}
|
|
}
|
|
|
|
if filterInsertDate, ok := data["filter_insert_date"].(string); ok && filterInsertDate != "" {
|
|
conditions = append(conditions, m.db.Where("DATE(`insert_date`) = ?", filterInsertDate))
|
|
}
|
|
|
|
if len(conditions) > 0 {
|
|
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
|
|
}
|
|
|
|
err := query.Count(&count).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return 0, nil
|
|
}
|
|
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
|
|
}
|