179 lines
6.2 KiB
Go
179 lines
6.2 KiB
Go
package biz
|
||
|
||
import (
|
||
"ai_scheduler/internal/data/impl"
|
||
"ai_scheduler/internal/data/model"
|
||
"ai_scheduler/internal/entitys"
|
||
"ai_scheduler/tmpl/dataTemp"
|
||
"context"
|
||
|
||
"xorm.io/builder"
|
||
)
|
||
|
||
type AdviceCustomerBiz struct {
|
||
impl *impl.AdviceCustomerImpl
|
||
}
|
||
|
||
func NewAdviceCustomerBiz(impl *impl.AdviceCustomerImpl) *AdviceCustomerBiz {
|
||
return &AdviceCustomerBiz{impl: impl}
|
||
}
|
||
|
||
// Add 新增单条好友
|
||
func (a *AdviceCustomerBiz) Add(ctx context.Context, req *entitys.AdvicerCustomerAddReq) error {
|
||
record := a.toModel(req)
|
||
return a.impl.Add(ctx, &record)
|
||
}
|
||
|
||
// List 分页查询好友列表
|
||
func (a *AdviceCustomerBiz) List(ctx context.Context, req *entitys.AdvicerCustomerListReq) (list []model.AiAdviceCustomer, err error) {
|
||
list = make([]model.AiAdviceCustomer, 0)
|
||
cond := builder.NewCond()
|
||
if req.SelfWxid != "" {
|
||
cond = cond.And(builder.Eq{"self_wxid": req.SelfWxid})
|
||
}
|
||
if req.LabelId != "" {
|
||
// label_list 为逗号分隔的标签 ID 字符串,用 FIND_IN_SET 精确匹配
|
||
cond = cond.And(builder.Expr("FIND_IN_SET(?, label_list)", req.LabelId))
|
||
}
|
||
// page_size <= 0 时不分页,返回全量
|
||
var pageBo *dataTemp.ReqPageBo
|
||
if req.PageSize > 0 {
|
||
pageBo = &dataTemp.ReqPageBo{Page: req.Page, Limit: req.PageSize}
|
||
}
|
||
_, err = a.impl.GetListToStruct(ctx, &cond, pageBo, &list, "create_at desc")
|
||
return
|
||
}
|
||
|
||
// Del 删除好友
|
||
func (a *AdviceCustomerBiz) Del(ctx context.Context, req *entitys.AdvicerCustomerDelReq) error {
|
||
return a.impl.DelByPrimary(ctx, "customer_id", req.CustomerId)
|
||
}
|
||
|
||
// Diff 查询差集:返回库中已有好友记录 + 库中不存在的 userName 差集
|
||
func (a *AdviceCustomerBiz) Diff(ctx context.Context, req *entitys.AdvicerCustomerDiffReq) (*entitys.AdvicerCustomerDiffRes, error) {
|
||
res := &entitys.AdvicerCustomerDiffRes{
|
||
List: make([]model.AiAdviceCustomer, 0),
|
||
Diff: make([]string, 0),
|
||
}
|
||
if req.SelfWxid == "" {
|
||
return res, nil
|
||
}
|
||
// 查询该 selfWxid 下已有的全部好友记录
|
||
cond := builder.Eq{"self_wxid": req.SelfWxid}
|
||
condPtr := builder.Cond(cond)
|
||
err := a.impl.GetRangeToMapStruct(&condPtr, &res.List)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(req.UserNames) == 0 {
|
||
return res, nil
|
||
}
|
||
// 构建已有集合
|
||
existSet := make(map[string]struct{}, len(res.List))
|
||
for _, e := range res.List {
|
||
existSet[e.UserName] = struct{}{}
|
||
}
|
||
// 取差集
|
||
for _, name := range req.UserNames {
|
||
if _, ok := existSet[name]; !ok {
|
||
res.Diff = append(res.Diff, name)
|
||
}
|
||
}
|
||
return res, nil
|
||
}
|
||
|
||
// BatchAdd 批量新增好友
|
||
func (a *AdviceCustomerBiz) BatchAdd(ctx context.Context, req *entitys.AdvicerCustomerBatchAddReq) error {
|
||
if len(req.List) == 0 {
|
||
return nil
|
||
}
|
||
records := make([]model.AiAdviceCustomer, 0, len(req.List))
|
||
for _, item := range req.List {
|
||
records = append(records, a.toModel(&item))
|
||
}
|
||
return a.impl.BatchInsert(ctx, records)
|
||
}
|
||
|
||
// BatchUpdate 批量更新已有好友记录
|
||
func (a *AdviceCustomerBiz) BatchUpdate(ctx context.Context, req *entitys.AdvicerCustomerBatchAddReq) error {
|
||
if len(req.List) == 0 {
|
||
return nil
|
||
}
|
||
records := make([]model.AiAdviceCustomer, 0, len(req.List))
|
||
for _, item := range req.List {
|
||
records = append(records, a.toModel(&item))
|
||
}
|
||
return a.impl.BatchUpdateByWxid(ctx, records)
|
||
}
|
||
|
||
// UpdateLabels 仅更新好友的标签列表
|
||
func (a *AdviceCustomerBiz) UpdateLabels(ctx context.Context, req *entitys.AdvicerCustomerUpdateLabelsReq) error {
|
||
return a.impl.UpdateLabelList(ctx, req.SelfWxid, req.UserName, req.LabelList)
|
||
}
|
||
|
||
// FindByCustomerId 按主键查客户记录
|
||
func (a *AdviceCustomerBiz) FindByCustomerId(ctx context.Context, customerId int32) (model.AiAdviceCustomer, error) {
|
||
return a.impl.FindByCustomerId(ctx, customerId)
|
||
}
|
||
|
||
// UpdateNote 更新客户备注(description 列)
|
||
func (a *AdviceCustomerBiz) UpdateNote(ctx context.Context, req *entitys.AdvicerCustomerUpdateNoteReq) error {
|
||
return a.impl.UpdateNote(ctx, req.CustomerId, req.Description)
|
||
}
|
||
|
||
// UpdateLabelListById 按主键更新 label_list
|
||
func (a *AdviceCustomerBiz) UpdateLabelListById(ctx context.Context, customerId int32, labelList string) error {
|
||
return a.impl.UpdateLabelListById(ctx, customerId, labelList)
|
||
}
|
||
|
||
func (a *AdviceCustomerBiz) toModel(req *entitys.AdvicerCustomerAddReq) model.AiAdviceCustomer {
|
||
return model.AiAdviceCustomer{
|
||
SelfWxid: req.SelfWxid,
|
||
UserName: req.UserName,
|
||
NickName: req.NickName,
|
||
PyInitial: req.PyInitial,
|
||
QuanPin: req.QuanPin,
|
||
Sex: req.Sex,
|
||
Remark: req.Remark,
|
||
RemarkPyInitial: req.RemarkPyInitial,
|
||
RemarkQuanPin: req.RemarkQuanPin,
|
||
Signature: req.Signature,
|
||
Alias: req.Alias,
|
||
SnsBgImg: req.SnsBgImg,
|
||
Country: req.Country,
|
||
BigHeadImgUrl: req.BigHeadImgUrl,
|
||
SmallHeadImgUrl: req.SmallHeadImgUrl,
|
||
Description: req.Description,
|
||
CardImgUrl: req.CardImgUrl,
|
||
LabelList: req.LabelList,
|
||
Province: req.Province,
|
||
City: req.City,
|
||
PhoneNumList: req.PhoneNumList,
|
||
}
|
||
}
|
||
|
||
// FindBySelfWxidAndUserName 按登录wxid+客户wxid查单条客户记录
|
||
func (a *AdviceCustomerBiz) FindBySelfWxidAndUserName(ctx context.Context, selfWxid, userName string) (model.AiAdviceCustomer, error) {
|
||
return a.impl.FindBySelfWxidAndUserName(ctx, selfWxid, userName)
|
||
}
|
||
|
||
// UpdateSessionId 更新客户的当前活跃会话ID
|
||
func (a *AdviceCustomerBiz) UpdateSessionId(ctx context.Context, customerId int32, sessionId string) error {
|
||
return a.impl.UpdateSessionId(ctx, customerId, sessionId)
|
||
}
|
||
|
||
// FindBySelfWxids 查询多个 selfWxid 下的所有客户记录
|
||
func (a *AdviceCustomerBiz) FindBySelfWxids(ctx context.Context, selfWxids []string) ([]model.AiAdviceCustomer, error) {
|
||
return a.impl.FindBySelfWxids(ctx, selfWxids)
|
||
}
|
||
|
||
// FindBySelfWxidHostingLabels 在 SQL 层用 FIND_IN_SET 筛选出各 selfWxid 下命中其托管标签的客户记录
|
||
func (a *AdviceCustomerBiz) FindBySelfWxidHostingLabels(ctx context.Context, hostingLabelsMap map[string][]int) ([]model.AiAdviceCustomer, error) {
|
||
return a.impl.FindBySelfWxidHostingLabels(ctx, hostingLabelsMap)
|
||
}
|
||
|
||
// UpdateFromWxDetail 根据微信好友详情更新 ai_advice_customer 记录
|
||
func (a *AdviceCustomerBiz) UpdateFromWxDetail(ctx context.Context, selfWxid, userName string, updates map[string]interface{}) error {
|
||
return a.impl.UpdateFromWxDetail(ctx, selfWxid, userName, updates)
|
||
}
|