|
@@ -33,23 +33,44 @@ func NewPublishSopTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Pu
|
|
|
}
|
|
|
|
|
|
func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
|
|
|
+ // 开始事务
|
|
|
+ tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
+
|
|
|
// 根据 id 查询 sop_task
|
|
|
- sopTask, err := l.svcCtx.DB.SopTask.Query().
|
|
|
+ sopTask, err := tx.SopTask.Query().
|
|
|
Where(
|
|
|
soptask.ID(req.Id),
|
|
|
- soptask.Status(3),
|
|
|
+ soptask.Status(1),
|
|
|
).
|
|
|
WithTaskStages().
|
|
|
Only(l.ctx)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
|
|
|
// 判断 sop_task 是否存在
|
|
|
if sopTask != nil {
|
|
|
+ if sopTask.BotWxidList == nil {
|
|
|
+ return nil, errors.New(errormsg.ValidationError)
|
|
|
+ }
|
|
|
+
|
|
|
// 查询任务的所有 sop_stages
|
|
|
- sopStages, err := l.svcCtx.DB.SopStage.Query().All(l.ctx)
|
|
|
+ err = tx.SopTask.UpdateOneID(req.Id).
|
|
|
+ SetStatus(3).
|
|
|
+ Exec(l.ctx)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
+
|
|
|
+ sopStages, err := tx.SopStage.Query().All(l.ctx)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
|
|
@@ -72,12 +93,13 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
|
|
|
var err error
|
|
|
sourceType := 3
|
|
|
if stage.ConditionOperator == 1 {
|
|
|
- contacts, err = l.svcCtx.DB.Contact.Query().Where(contact.And(predicates...)).All(l.ctx)
|
|
|
+ contacts, err = tx.Contact.Query().Where(contact.And(predicates...)).All(l.ctx)
|
|
|
} else {
|
|
|
- contacts, err = l.svcCtx.DB.Contact.Query().Where(contact.Or(predicates...)).All(l.ctx)
|
|
|
+ contacts, err = tx.Contact.Query().Where(contact.Or(predicates...)).All(l.ctx)
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
@@ -86,7 +108,7 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
|
|
|
// 判断联系人所属微信是否包含在任务当中
|
|
|
if sopTask.BotWxidList == nil || (sopTask.BotWxidList != nil && valueInArray(c.WxWxid, sopTask.BotWxidList)) {
|
|
|
for _, message := range stage.ActionMessage {
|
|
|
- _, _ = l.svcCtx.DB.MessageRecords.Create().
|
|
|
+ _, _ = tx.MessageRecords.Create().
|
|
|
SetNotNilBotWxid(&c.WxWxid).
|
|
|
SetNotNilContactID(&c.ID).
|
|
|
SetNotNilContactType(&c.Type).
|
|
@@ -103,8 +125,9 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
|
|
|
}
|
|
|
|
|
|
// 查询当前联系人的标签关系
|
|
|
- currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(c.ID)).All(l.ctx)
|
|
|
+ currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(c.ID)).All(l.ctx)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
|
|
@@ -118,6 +141,7 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
|
|
|
// 递归调用 AddLabelRelationships
|
|
|
err = l.AddLabelRelationships(sopStages, *c, currentLabelIds, stage.ActionLabel)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
@@ -125,14 +149,30 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 所有操作成功,提交事务
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
return &types.BaseMsgResp{Msg: errormsg.Success}, nil
|
|
|
} else {
|
|
|
+ // 所有操作成功,提交事务
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
// 返回错误信息:任务不存在
|
|
|
return nil, errors.New(errormsg.TargetNotFound)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64) (err error) {
|
|
|
+ // 开始事务
|
|
|
+ tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
|
+ }
|
|
|
+
|
|
|
// 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
|
|
|
var newLabelIds []uint64
|
|
|
// 创建一个映射,用于快速查找 currentLabelIds 中的元素
|
|
@@ -154,11 +194,12 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
|
|
|
|
|
|
// 创建需要新增的标签关系
|
|
|
for _, id := range newLabelIds {
|
|
|
- _, err = l.svcCtx.DB.LabelRelationship.Create().
|
|
|
+ _, err = tx.LabelRelationship.Create().
|
|
|
SetLabelID(id).
|
|
|
SetContactID(contact.ID).
|
|
|
Save(l.ctx)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
|
}
|
|
|
}
|
|
@@ -170,7 +211,7 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
|
|
|
for _, stage := range sopStages {
|
|
|
if stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) {
|
|
|
// 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
|
|
|
- _, err = l.svcCtx.DB.MessageRecords.Query().
|
|
|
+ _, err := tx.MessageRecords.Query().
|
|
|
Where(
|
|
|
messagerecords.ContactWxid(contact.Wxid),
|
|
|
messagerecords.SourceType(3),
|
|
@@ -178,14 +219,14 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
|
|
|
messagerecords.SubSourceID(0),
|
|
|
).
|
|
|
Only(l.ctx)
|
|
|
- if err == nil {
|
|
|
+ if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
// 判断ActionMessage是否为空
|
|
|
sourceType := 3
|
|
|
if stage.ActionMessage != nil {
|
|
|
for _, message := range stage.ActionMessage {
|
|
|
- _, _ = l.svcCtx.DB.MessageRecords.Create().
|
|
|
+ _, _ = tx.MessageRecords.Create().
|
|
|
SetNotNilBotWxid(&contact.WxWxid).
|
|
|
SetNotNilContactID(&contact.ID).
|
|
|
SetNotNilContactType(&contact.Type).
|
|
@@ -205,12 +246,19 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
|
|
|
// 递归调用 AddLabelRelationships
|
|
|
err = l.AddLabelRelationships(sopStages, contact, currentLabelIds, stage.ActionLabel)
|
|
|
if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ // 所有操作成功,提交事务
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ return dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|