123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package label_relationship
- import (
- "context"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "wechat-api/ent/labelrelationship"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "wechat-api/internal/utils/dberrorhandler"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type UpdateLabelRelationshipsLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLabelRelationshipsLogic {
- return &UpdateLabelRelationshipsLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
- l.Logger.Info("Start processing UpdateLabelRelationships request", "request", req)
- defer func() {
- if err != nil {
- l.Logger.Error("Error occurred in UpdateLabelRelationships", "error", err)
- } else {
- l.Logger.Info("Successfully processed UpdateLabelRelationships request")
- }
- }()
- // 获取联系人当前已关联的标签
- currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- // 提取当前标签ID
- var currentLabelIds []uint64
- for _, relationship := range currentLabelRelationships {
- currentLabelIds = append(currentLabelIds, relationship.LabelID)
- }
- // 对比新旧标签ID,找出需要新增和移除的标签
- addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
- // 删除需要移除的标签关系
- for _, id := range removeLabelIds {
- _, err := l.svcCtx.DB.LabelRelationship.
- Delete().
- Where(
- labelrelationship.ContactID(*req.ContactId),
- labelrelationship.LabelID(id),
- ).
- Exec(l.ctx)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- }
- // 创建需要新增的标签关系
- for _, id := range addLabelIds {
- _, err := l.svcCtx.DB.LabelRelationship.Create().
- SetLabelID(id).
- SetContactID(*req.ContactId).
- SetStatus(*req.Status).
- Save(l.ctx)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- }
- return &types.BaseMsgResp{
- Code: 0,
- Msg: errormsg.Success,
- }, nil
- }
- // compareLabelIds compares the new label ids with the current ones and returns the ids to be added and removed
- func compareLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
- newLabelIdSet := make(map[uint64]struct{}, len(newLabelIds))
- for _, id := range newLabelIds {
- newLabelIdSet[id] = struct{}{}
- }
- for _, id := range currentLabelIds {
- if _, ok := newLabelIdSet[id]; ok {
- delete(newLabelIdSet, id)
- } else {
- removeLabelIds = append(removeLabelIds, id)
- }
- }
- for id := range newLabelIdSet {
- addLabelIds = append(addLabelIds, id)
- }
- return
- }
|