|
@@ -31,17 +31,17 @@ func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceCo
|
|
|
|
|
|
func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
|
|
func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
|
|
// 开始事务
|
|
// 开始事务
|
|
- //tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
|
- //if err != nil {
|
|
|
|
- // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
|
- //}
|
|
|
|
|
|
+ tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
|
+ }
|
|
|
|
|
|
// 获取联系人信息
|
|
// 获取联系人信息
|
|
- c, err := l.svcCtx.DB.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx)
|
|
|
|
|
|
+ c, err := tx.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx)
|
|
// 获取联系人当前已关联的标签
|
|
// 获取联系人当前已关联的标签
|
|
- currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
|
|
|
|
|
|
+ currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
- //_ = tx.Rollback()
|
|
|
|
|
|
+ _ = tx.Rollback()
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -58,7 +58,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
|
|
if req.UpdateType == nil || *req.UpdateType == "all" {
|
|
if req.UpdateType == nil || *req.UpdateType == "all" {
|
|
// 删除需要移除的标签关系
|
|
// 删除需要移除的标签关系
|
|
for _, id := range removeLabelIds {
|
|
for _, id := range removeLabelIds {
|
|
- _, err := l.svcCtx.DB.LabelRelationship.
|
|
|
|
|
|
+ _, err := tx.LabelRelationship.
|
|
Delete().
|
|
Delete().
|
|
Where(
|
|
Where(
|
|
labelrelationship.ContactID(*req.ContactId),
|
|
labelrelationship.ContactID(*req.ContactId),
|
|
@@ -66,7 +66,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
|
|
).
|
|
).
|
|
Exec(l.ctx)
|
|
Exec(l.ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
- //_ = tx.Rollback()
|
|
|
|
|
|
+ _ = tx.Rollback()
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -74,7 +74,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
|
|
|
|
|
|
// 创建需要新增的标签关系
|
|
// 创建需要新增的标签关系
|
|
for _, id := range addLabelIds {
|
|
for _, id := range addLabelIds {
|
|
- _, _ = l.svcCtx.DB.LabelRelationship.Create().
|
|
|
|
|
|
+ _, _ = tx.LabelRelationship.Create().
|
|
SetLabelID(id).
|
|
SetLabelID(id).
|
|
SetContactID(*req.ContactId).
|
|
SetContactID(*req.ContactId).
|
|
Save(l.ctx)
|
|
Save(l.ctx)
|
|
@@ -83,13 +83,10 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
|
|
//}
|
|
//}
|
|
}
|
|
}
|
|
|
|
|
|
- currentLabelIds = req.LabelIds
|
|
|
|
-
|
|
|
|
// 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
|
|
// 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
|
|
- sopTasks, err := l.svcCtx.DB.SopTask.Query().Where(soptask.Status(3)).All(l.ctx)
|
|
|
|
- l.Logger.Info("UpdateLabelRelationships sopTasks:", sopTasks)
|
|
|
|
|
|
+ sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3)).All(l.ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
- //_ = tx.Rollback()
|
|
|
|
|
|
+ _ = tx.Rollback()
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -108,28 +105,28 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
|
|
for _, task := range filteredSopTasks {
|
|
for _, task := range filteredSopTasks {
|
|
stages, err := task.QueryTaskStages().All(l.ctx)
|
|
stages, err := task.QueryTaskStages().All(l.ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
- //_ = tx.Rollback()
|
|
|
|
|
|
+ _ = tx.Rollback()
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
}
|
|
}
|
|
sopStages = append(sopStages, stages...)
|
|
sopStages = append(sopStages, stages...)
|
|
}
|
|
}
|
|
- err = l.AddLabelRelationships(sopStages, *c, currentLabelIds)
|
|
|
|
|
|
+ err = l.AddLabelRelationships(sopStages, *c, req.LabelIds)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
- //_ = tx.Rollback()
|
|
|
|
|
|
+ _ = tx.Rollback()
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- //// 所有操作成功,提交事务
|
|
|
|
- //err = tx.Commit()
|
|
|
|
- //if err != nil {
|
|
|
|
- // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
|
- //}
|
|
|
|
|
|
+ // 所有操作成功,提交事务
|
|
|
|
+ err = tx.Commit()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
|
+ }
|
|
|
|
|
|
return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
|
|
return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64) (err error) {
|
|
func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64) (err error) {
|
|
- // 开始事务
|
|
|
|
|
|
+ //// 开始事务
|
|
//tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
//tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
//if err != nil {
|
|
//if err != nil {
|
|
// return dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
// return dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
@@ -152,7 +149,7 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
|
|
// 判断ActionMessage是否为空
|
|
// 判断ActionMessage是否为空
|
|
sourceType := 3
|
|
sourceType := 3
|
|
if stage.ActionMessage != nil {
|
|
if stage.ActionMessage != nil {
|
|
- for _, message := range stage.ActionMessage {
|
|
|
|
|
|
+ for i, message := range stage.ActionMessage {
|
|
_, _ = l.svcCtx.DB.MessageRecords.Create().
|
|
_, _ = l.svcCtx.DB.MessageRecords.Create().
|
|
SetNotNilBotWxid(&contact.WxWxid).
|
|
SetNotNilBotWxid(&contact.WxWxid).
|
|
SetNotNilContactID(&contact.ID).
|
|
SetNotNilContactID(&contact.ID).
|
|
@@ -162,6 +159,7 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
|
|
SetNotNilContent(&message.Content).
|
|
SetNotNilContent(&message.Content).
|
|
SetNotNilSourceType(&sourceType).
|
|
SetNotNilSourceType(&sourceType).
|
|
SetNotNilSourceID(&stage.ID).
|
|
SetNotNilSourceID(&stage.ID).
|
|
|
|
+ SetSubSourceID(uint64(i)).
|
|
Save(l.ctx)
|
|
Save(l.ctx)
|
|
|
|
|
|
//if err != nil {
|
|
//if err != nil {
|
|
@@ -201,11 +199,8 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
|
|
// 合并 currentLabelIds 和 newLabelIds
|
|
// 合并 currentLabelIds 和 newLabelIds
|
|
currentLabelIds = append(currentLabelIds, newLabelIds...)
|
|
currentLabelIds = append(currentLabelIds, newLabelIds...)
|
|
// 递归调用 AddLabelRelationships
|
|
// 递归调用 AddLabelRelationships
|
|
- //err = l.AddLabelRelationships(sopStages, contact, currentLabelIds)
|
|
|
|
- //if err != nil {
|
|
|
|
- // //_ = tx.Rollback()
|
|
|
|
- // return err
|
|
|
|
- //}
|
|
|
|
|
|
+ _ = l.AddLabelRelationships(sopStages, contact, currentLabelIds)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|