You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
6.5 KiB
194 lines
6.5 KiB
package customer
|
|
|
|
import (
|
|
"base/app/lib/bean"
|
|
"base/app/model"
|
|
"errors"
|
|
"fmt"
|
|
"git.oa00.com/go/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var CustomerLogic = &customerLogic{}
|
|
|
|
type customerLogic struct {
|
|
}
|
|
type customerItem struct {
|
|
Id uint `json:"id"`
|
|
Nickname string `json:"nickname"`
|
|
Mobile string `json:"mobile"`
|
|
BrokerStatus int `json:"brokerStatus"`
|
|
BrokerId uint `json:"brokerId"`
|
|
BrokerName string `json:"brokerName"`
|
|
BrokerPhone string `json:"brokerPhone"`
|
|
BrokerStoreName string `json:"brokerStoreName"`
|
|
BrokerBusinessName string `json:"brokerBusinessName"`
|
|
CreatedAt int64 `json:"createdAt"`
|
|
}
|
|
type CustomerListsSearch struct {
|
|
BrokerStatus uint
|
|
CustomerName string
|
|
CustomerPhone string
|
|
BrokerName string
|
|
BrokerPhone string
|
|
BrokerStore string
|
|
BrokerBusiness string
|
|
RegisterAtStart string
|
|
RegisterAtEnd string
|
|
}
|
|
|
|
// Lists @Title 客户列表
|
|
func (c *customerLogic) Lists(search CustomerListsSearch, page bean.Page) (lists []customerItem, total int64) {
|
|
lists = []customerItem{}
|
|
var customers []model.Customer
|
|
where := mysql.Db
|
|
if search.BrokerStatus > 0 {
|
|
switch search.BrokerStatus {
|
|
case 1: // 待分配
|
|
where = where.Where("broker_id = 0")
|
|
case 2: // 已分配
|
|
where = where.Where("broker_id > 0")
|
|
}
|
|
}
|
|
// 客户名称
|
|
if search.CustomerName != "" {
|
|
where = where.Where("nickname like ?", fmt.Sprintf("%%%s%%", search.CustomerName))
|
|
}
|
|
// 客户手机号
|
|
if search.CustomerPhone != "" {
|
|
where = where.Where("mobile like ?", fmt.Sprintf("%%%s%%", search.CustomerPhone))
|
|
}
|
|
// 经纪人
|
|
if search.BrokerName != "" || search.BrokerPhone != "" || search.BrokerStore != "" || search.BrokerBusiness != "" {
|
|
brokerWhere := mysql.Db.Model(&model.Broker{}).Select("id")
|
|
// 经纪人名称
|
|
if search.BrokerName != "" {
|
|
brokerWhere = brokerWhere.Where("nickname like ?", fmt.Sprintf("%%%s%%", search.BrokerName))
|
|
}
|
|
// 经纪人电话
|
|
if search.BrokerPhone != "" {
|
|
brokerWhere = brokerWhere.Where("phone like ?", fmt.Sprintf("%%%s%%", search.BrokerPhone))
|
|
}
|
|
// 经纪人门店
|
|
if search.BrokerStore != "" {
|
|
brokerWhere = brokerWhere.Where("business_id in (?)", mysql.Db.Model(&model.BrokerBusinessStore{}).Select("business_id").Where("name like ?", fmt.Sprintf("%%%s%%", search.BrokerStore)))
|
|
}
|
|
// 经纪人入驻商
|
|
if search.BrokerBusiness != "" {
|
|
brokerWhere = brokerWhere.Where("business_id in (?)", mysql.Db.Model(&model.BrokerBusiness{}).Select("id").Where("name like ?", fmt.Sprintf("%%%s%%", search.BrokerBusiness)))
|
|
}
|
|
|
|
where = where.Where("broker_id in (?)", brokerWhere)
|
|
}
|
|
// 注册日期
|
|
if search.RegisterAtStart != "" && search.RegisterAtEnd != "" {
|
|
where = where.Where("created_at > ? and created_at <= ?", search.RegisterAtStart, search.RegisterAtEnd+" 23:59:59")
|
|
}
|
|
mysql.Db.Model(&customers).Where(where).Count(&total)
|
|
if page.HasPage(total) {
|
|
mysql.Db.
|
|
Preload("Broker.Staff.Store").Preload("Broker.Staff.Business").
|
|
Where(where).Offset(page.GetStart()).Limit(page.GetLimit()).Find(&customers)
|
|
for _, customer := range customers {
|
|
brokerStatus := 0
|
|
if customer.BrokerId > 0 {
|
|
brokerStatus = 1
|
|
}
|
|
lists = append(lists, customerItem{
|
|
Id: customer.Id,
|
|
Nickname: customer.Nickname,
|
|
Mobile: customer.Mobile,
|
|
BrokerStatus: brokerStatus,
|
|
BrokerId: customer.BrokerId,
|
|
BrokerName: customer.Broker.Nickname,
|
|
BrokerPhone: customer.Broker.Phone,
|
|
BrokerStoreName: customer.Broker.Staff.Store.Name,
|
|
BrokerBusinessName: customer.Broker.Staff.Business.Name,
|
|
CreatedAt: customer.CreatedAt.Unix(),
|
|
})
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// Distribute @Title 分配经纪人
|
|
func (c *customerLogic) Distribute(customerIds []uint, broker model.Broker) error {
|
|
var customers []model.Customer
|
|
if mysql.Db.Where("id in ?", customerIds).Find(&customers).Error != nil {
|
|
return errors.New("客户错误")
|
|
}
|
|
//broker := model.Broker{Phone: phone}
|
|
if mysql.Db.Where(broker).First(&broker).Error != nil {
|
|
return errors.New("经纪人错误")
|
|
}
|
|
var upCustomerIds []uint
|
|
var customerBrokers []model.CustomerBroker
|
|
for _, customer := range customers {
|
|
if customer.BrokerId != broker.Id {
|
|
upCustomerIds = append(upCustomerIds, customer.Id)
|
|
customerBrokers = append(customerBrokers, model.CustomerBroker{
|
|
CustomerId: customer.Id,
|
|
BeforeBrokerId: customer.BrokerId,
|
|
BrokerId: broker.Id,
|
|
})
|
|
}
|
|
}
|
|
if len(upCustomerIds) == 0 {
|
|
return nil
|
|
}
|
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Model(&model.Customer{}).Where("id in ?", upCustomerIds).UpdateColumn("broker_id", broker.Id).Error != nil {
|
|
return errors.New("分配失败")
|
|
}
|
|
if tx.Create(&customerBrokers).Error != nil {
|
|
return errors.New("分配失败")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
type customerInfo struct {
|
|
Id uint `json:"id"`
|
|
Nickname string `json:"nickname"`
|
|
Mobile string `json:"mobile"`
|
|
Gender uint `json:"gender"`
|
|
Level uint `json:"level"`
|
|
IsImportant uint `json:"isImportant"`
|
|
CreatedAt int64 `json:"createdAt"`
|
|
BrokerId uint `json:"brokerId"`
|
|
Broker customerInfoBroker `json:"broker"`
|
|
}
|
|
type customerInfoBroker struct {
|
|
BusinessName string `json:"businessName"`
|
|
BusinessLiaisonPhone string `json:"businessLiaisonPhone"`
|
|
StoreName string `json:"storeName"`
|
|
StaffName string `json:"staffName"`
|
|
StaffPhone string `json:"staffPhone"`
|
|
}
|
|
|
|
// Info @Title 客户详情
|
|
func (c *customerLogic) Info(customerId uint) (result customerInfo, err error) {
|
|
customer := model.Customer{Id: customerId}
|
|
if mysql.Db.Preload("Broker.Business").Preload("Broker.Staff.Store").First(&customer).Error != nil {
|
|
return result, errors.New("客户错误")
|
|
}
|
|
result = customerInfo{
|
|
Id: customer.Id,
|
|
Nickname: customer.Nickname,
|
|
Mobile: customer.Mobile,
|
|
Gender: customer.Gender,
|
|
Level: customer.Level,
|
|
IsImportant: customer.IsImportant,
|
|
CreatedAt: customer.CreatedAt.Unix(),
|
|
BrokerId: customer.BrokerId,
|
|
Broker: customerInfoBroker{
|
|
BusinessName: customer.Broker.Business.Name,
|
|
BusinessLiaisonPhone: customer.Broker.Business.LiaisonPhone,
|
|
StoreName: customer.Broker.Staff.Store.Name,
|
|
StaffName: customer.Broker.Staff.Name,
|
|
StaffPhone: customer.Broker.Staff.Phone,
|
|
},
|
|
}
|
|
return
|
|
}
|