ai_scheduler/internal/data/impl/advice_label_impl.go

73 lines
2.1 KiB
Go

package impl
import (
"ai_scheduler/internal/data/model"
"ai_scheduler/tmpl/dataTemp"
"ai_scheduler/utils"
"context"
)
type AdviceLabelImpl struct {
dataTemp.DataTemp
}
func NewAdviceLabelImpl(db *utils.Db) *AdviceLabelImpl {
return &AdviceLabelImpl{
DataTemp: *dataTemp.NewDataTemp(db, new(model.AiAdviceLabel)),
}
}
// ListBySelfWxid 查询某销售下所有标签
func (a *AdviceLabelImpl) ListBySelfWxid(ctx context.Context, selfWxid string) ([]model.AiAdviceLabel, error) {
var list []model.AiAdviceLabel
err := a.Db.WithContext(ctx).
Where("self_wxid = ?", selfWxid).
Order("label_id ASC").
Find(&list).Error
return list, err
}
// BatchInsert 批量插入标签
func (a *AdviceLabelImpl) BatchInsert(ctx context.Context, records []model.AiAdviceLabel) error {
if len(records) == 0 {
return nil
}
return a.Db.WithContext(ctx).Create(&records).Error
}
// UpdateName 更新标签名称
func (a *AdviceLabelImpl) UpdateName(ctx context.Context, selfWxid string, labelId int, labelName string) error {
return a.Db.WithContext(ctx).
Model(&model.AiAdviceLabel{}).
Where("self_wxid = ? AND label_id = ?", selfWxid, labelId).
Update("label_name", labelName).Error
}
// DeleteByLabelIds 按标签 ID 批量删除
func (a *AdviceLabelImpl) DeleteByLabelIds(ctx context.Context, selfWxid string, labelIds []int) error {
if len(labelIds) == 0 {
return nil
}
return a.Db.WithContext(ctx).
Where("self_wxid = ? AND label_id IN ?", selfWxid, labelIds).
Delete(&model.AiAdviceLabel{}).Error
}
// UpdateHosting 更新标签托管状态
func (a *AdviceLabelImpl) UpdateHosting(ctx context.Context, selfWxid string, labelId int, hosting int8) error {
return a.Db.WithContext(ctx).
Model(&model.AiAdviceLabel{}).
Where("self_wxid = ? AND label_id = ?", selfWxid, labelId).
Update("hosting", hosting).Error
}
// GetHostingLabelIds 查询某销售下所有已托管的标签 ID
func (a *AdviceLabelImpl) GetHostingLabelIds(ctx context.Context, selfWxid string) ([]int, error) {
var ids []int
err := a.Db.WithContext(ctx).
Model(&model.AiAdviceLabel{}).
Where("self_wxid = ? AND hosting = 1", selfWxid).
Pluck("label_id", &ids).Error
return ids, err
}