164 lines
5.5 KiB
Go
164 lines
5.5 KiB
Go
package impl
|
||
|
||
import (
|
||
"ai_scheduler/internal/data/model"
|
||
"ai_scheduler/tmpl/dataTemp"
|
||
"ai_scheduler/utils"
|
||
"context"
|
||
"strings"
|
||
)
|
||
|
||
type AdviceCustomerImpl struct {
|
||
dataTemp.DataTemp
|
||
}
|
||
|
||
func NewAdviceCustomerImpl(db *utils.Db) *AdviceCustomerImpl {
|
||
return &AdviceCustomerImpl{
|
||
DataTemp: *dataTemp.NewDataTemp(db, new(model.AiAdviceCustomer)),
|
||
}
|
||
}
|
||
|
||
// BatchInsert 批量插入好友记录
|
||
func (a *AdviceCustomerImpl) BatchInsert(ctx context.Context, records []model.AiAdviceCustomer) error {
|
||
if len(records) == 0 {
|
||
return nil
|
||
}
|
||
return a.Db.WithContext(ctx).Create(&records).Error
|
||
}
|
||
|
||
// BatchUpdateByWxid 按 self_wxid + user_name 批量更新已有好友记录
|
||
func (a *AdviceCustomerImpl) BatchUpdateByWxid(ctx context.Context, records []model.AiAdviceCustomer) error {
|
||
for _, r := range records {
|
||
err := a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("self_wxid = ? AND user_name = ?", r.SelfWxid, r.UserName).
|
||
Updates(map[string]interface{}{
|
||
"nick_name": r.NickName,
|
||
"py_initial": r.PyInitial,
|
||
"quan_pin": r.QuanPin,
|
||
"sex": r.Sex,
|
||
"remark": r.Remark,
|
||
"remark_py_initial": r.RemarkPyInitial,
|
||
"remark_quan_pin": r.RemarkQuanPin,
|
||
"signature": r.Signature,
|
||
"alias": r.Alias,
|
||
"big_head_img_url": r.BigHeadImgUrl,
|
||
"small_head_img_url": r.SmallHeadImgUrl,
|
||
"label_list": r.LabelList,
|
||
"province": r.Province,
|
||
"city": r.City,
|
||
}).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// UpdateLabelList 仅更新好友的标签列表
|
||
func (a *AdviceCustomerImpl) UpdateLabelList(ctx context.Context, selfWxid, userName, labelList string) error {
|
||
return a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("self_wxid = ? AND user_name = ?", selfWxid, userName).
|
||
Update("label_list", labelList).Error
|
||
}
|
||
|
||
// FindByCustomerId 按主键查客户记录
|
||
func (a *AdviceCustomerImpl) FindByCustomerId(ctx context.Context, customerId int32) (model.AiAdviceCustomer, error) {
|
||
var record model.AiAdviceCustomer
|
||
err := a.Db.WithContext(ctx).
|
||
Where("customer_id = ?", customerId).
|
||
First(&record).Error
|
||
return record, err
|
||
}
|
||
|
||
// UpdateNote 更新客户备注(description 列)
|
||
func (a *AdviceCustomerImpl) UpdateNote(ctx context.Context, customerId int32, description string) error {
|
||
return a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("customer_id = ?", customerId).
|
||
Update("description", description).Error
|
||
}
|
||
|
||
// UpdateLabelListById 按主键更新 label_list
|
||
func (a *AdviceCustomerImpl) UpdateLabelListById(ctx context.Context, customerId int32, labelList string) error {
|
||
return a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("customer_id = ?", customerId).
|
||
Update("label_list", labelList).Error
|
||
}
|
||
|
||
// FindBySelfWxidAndUserName 查询单个客户记录
|
||
func (a *AdviceCustomerImpl) FindBySelfWxidAndUserName(ctx context.Context, selfWxid, userName string) (model.AiAdviceCustomer, error) {
|
||
var record model.AiAdviceCustomer
|
||
err := a.Db.WithContext(ctx).
|
||
Where("self_wxid = ? AND user_name = ?", selfWxid, userName).
|
||
First(&record).Error
|
||
return record, err
|
||
}
|
||
|
||
// UpdateSessionId 更新客户的会话 ID
|
||
func (a *AdviceCustomerImpl) UpdateSessionId(ctx context.Context, customerId int32, sessionId string) error {
|
||
return a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("customer_id = ?", customerId).
|
||
Update("session_id", sessionId).Error
|
||
}
|
||
|
||
// FindBySelfWxids 查询多个 selfWxid 下的所有客户记录
|
||
func (a *AdviceCustomerImpl) FindBySelfWxids(ctx context.Context, selfWxids []string) ([]model.AiAdviceCustomer, error) {
|
||
if len(selfWxids) == 0 {
|
||
return nil, nil
|
||
}
|
||
var list []model.AiAdviceCustomer
|
||
err := a.Db.WithContext(ctx).
|
||
Where("self_wxid IN ?", selfWxids).
|
||
Order("create_at desc").
|
||
Find(&list).Error
|
||
return list, err
|
||
}
|
||
|
||
// FindBySelfWxidHostingLabels 在 SQL 层用 FIND_IN_SET 直接筛选出各 selfWxid 下命中其托管标签的客户记录。
|
||
// hostingLabelsMap: selfWxid -> 该销售已开启托管(hosting=1)的标签 ID 列表;每个销售用各自的托管标签过滤。
|
||
func (a *AdviceCustomerImpl) FindBySelfWxidHostingLabels(ctx context.Context, hostingLabelsMap map[string][]int) ([]model.AiAdviceCustomer, error) {
|
||
if len(hostingLabelsMap) == 0 {
|
||
return nil, nil
|
||
}
|
||
var conditions []string
|
||
var args []interface{}
|
||
for selfWxid, labelIds := range hostingLabelsMap {
|
||
if len(labelIds) == 0 {
|
||
continue
|
||
}
|
||
findInSet := make([]string, 0, len(labelIds))
|
||
labelArgs := make([]interface{}, 0, len(labelIds))
|
||
for _, id := range labelIds {
|
||
findInSet = append(findInSet, "FIND_IN_SET(?, label_list)")
|
||
labelArgs = append(labelArgs, id)
|
||
}
|
||
conditions = append(conditions, "(self_wxid = ? AND ("+strings.Join(findInSet, " OR ")+"))")
|
||
args = append(args, selfWxid)
|
||
args = append(args, labelArgs...)
|
||
}
|
||
if len(conditions) == 0 {
|
||
return nil, nil
|
||
}
|
||
var list []model.AiAdviceCustomer
|
||
err := a.Db.WithContext(ctx).
|
||
Where("("+strings.Join(conditions, " OR ")+")", args...).
|
||
Order("create_at desc").
|
||
Find(&list).Error
|
||
return list, err
|
||
}
|
||
|
||
// UpdateFromWxDetail 根据微信好友详情更新客户记录(回调时保持客户信息最新)
|
||
func (a *AdviceCustomerImpl) UpdateFromWxDetail(ctx context.Context, selfWxid, userName string, updates map[string]interface{}) error {
|
||
if len(updates) == 0 {
|
||
return nil
|
||
}
|
||
return a.Db.WithContext(ctx).
|
||
Model(&model.AiAdviceCustomer{}).
|
||
Where("self_wxid = ? AND user_name = ?", selfWxid, userName).
|
||
Updates(updates).Error
|
||
}
|