Vestmore_GO/model/killara_customer_logic.go
2024-04-10 17:30:12 +08:00

303 lines
9.7 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
}
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
}