95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package biz
|
||
|
||
import (
|
||
"ai_scheduler/internal/data/impl"
|
||
"ai_scheduler/internal/data/model"
|
||
"ai_scheduler/internal/entitys"
|
||
"context"
|
||
)
|
||
|
||
type AdviceLabelBiz struct {
|
||
impl *impl.AdviceLabelImpl
|
||
}
|
||
|
||
func NewAdviceLabelBiz(impl *impl.AdviceLabelImpl) *AdviceLabelBiz {
|
||
return &AdviceLabelBiz{impl: impl}
|
||
}
|
||
|
||
// List 查询标签列表
|
||
func (a *AdviceLabelBiz) List(ctx context.Context, req *entitys.AdvicerLabelListReq) ([]model.AiAdviceLabel, error) {
|
||
return a.impl.ListBySelfWxid(ctx, req.SelfWxid)
|
||
}
|
||
|
||
// Sync 同步标签:与 DB 比对,新增/更新/删除
|
||
func (a *AdviceLabelBiz) Sync(ctx context.Context, req *entitys.AdvicerLabelSyncReq) error {
|
||
// 查询 DB 中已有标签
|
||
dbLabels, err := a.impl.ListBySelfWxid(ctx, req.SelfWxid)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 构建 DB 标签 map: labelId -> labelName
|
||
dbMap := make(map[int]string, len(dbLabels))
|
||
for _, l := range dbLabels {
|
||
dbMap[l.LabelId] = l.LabelName
|
||
}
|
||
|
||
// 构建微信标签 map: labelId -> labelName
|
||
wxMap := make(map[int]string, len(req.Labels))
|
||
for _, l := range req.Labels {
|
||
wxMap[l.LabelId] = l.LabelName
|
||
}
|
||
|
||
// 新增:微信有,DB 没有
|
||
var toInsert []model.AiAdviceLabel
|
||
for _, l := range req.Labels {
|
||
if _, ok := dbMap[l.LabelId]; !ok {
|
||
toInsert = append(toInsert, model.AiAdviceLabel{
|
||
SelfWxid: req.SelfWxid,
|
||
LabelId: l.LabelId,
|
||
LabelName: l.LabelName,
|
||
})
|
||
}
|
||
}
|
||
if len(toInsert) > 0 {
|
||
if err := a.impl.BatchInsert(ctx, toInsert); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
// 更新:两边都有,但名称变了
|
||
for _, l := range req.Labels {
|
||
dbName, ok := dbMap[l.LabelId]
|
||
if ok && dbName != l.LabelName {
|
||
if err := a.impl.UpdateName(ctx, req.SelfWxid, l.LabelId, l.LabelName); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
|
||
// 删除:DB 有,微信没有
|
||
var toDelete []int
|
||
for labelId := range dbMap {
|
||
if _, ok := wxMap[labelId]; !ok {
|
||
toDelete = append(toDelete, labelId)
|
||
}
|
||
}
|
||
if len(toDelete) > 0 {
|
||
if err := a.impl.DeleteByLabelIds(ctx, req.SelfWxid, toDelete); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// ToggleHosting 切换标签托管状态
|
||
func (a *AdviceLabelBiz) ToggleHosting(ctx context.Context, req *entitys.AdvicerLabelHostingReq) error {
|
||
return a.impl.UpdateHosting(ctx, req.SelfWxid, req.LabelId, req.Hosting)
|
||
}
|
||
|
||
// GetHostingLabelIds 返回某销售下 hosting=1 的标签 ID 列表
|
||
func (a *AdviceLabelBiz) GetHostingLabelIds(ctx context.Context, selfWxid string) ([]int, error) {
|
||
return a.impl.GetHostingLabelIds(ctx, selfWxid)
|
||
}
|