123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package sop_task
- import (
- "context"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "wechat-api/ent"
- "wechat-api/ent/sopnode"
- "wechat-api/ent/soptask"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "wechat-api/internal/utils/dberrorhandler"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type SopTaskCopyLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewSopTaskCopyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SopTaskCopyLogic {
- return &SopTaskCopyLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *SopTaskCopyLogic) SopTaskCopy(req *types.CopyReq) (resp *types.BaseMsgResp, err error) {
- isAdmin := l.ctx.Value("isAdmin").(bool)
- organizationId := l.ctx.Value("organizationId").(uint64)
- var targetOrganizationId uint64
- if isAdmin {
- if req.OrganizationId == 0 {
- targetOrganizationId = organizationId
- } else {
- targetOrganizationId = req.OrganizationId
- }
- } else {
- targetOrganizationId = organizationId
- }
- data, err := l.svcCtx.DB.SopTask.Query().
- Where(soptask.ID(req.Id), soptask.OrganizationIDEQ(organizationId)).
- WithTaskStages().
- Only(l.ctx)
- 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)
- }
- newTask, err := tx.SopTask.Create().
- SetName(*req.Name).
- SetType(data.Type).
- SetOrganizationID(targetOrganizationId).
- Save(l.ctx)
- if err != nil {
- _ = tx.Rollback()
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- for _, s := range data.Edges.TaskStages {
- newStage, err := tx.SopStage.Create().
- SetTaskID(newTask.ID).
- SetName(s.Name).
- SetConditionType(s.ConditionType).
- SetConditionOperator(s.ConditionOperator).
- SetConditionList(s.ConditionList).
- SetNotNilActionMessage(s.ActionMessage).
- SetNotNilActionLabel(s.ActionLabel).
- SetIndexSort(s.IndexSort).
- Save(l.ctx)
- if err != nil {
- _ = tx.Rollback()
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- err = CreateNodes(tx, s.ID, 0, newStage.ID, 0, l.ctx, l.Logger)
- if err != nil {
- _ = tx.Rollback()
- 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.CreateSuccess}, nil
- }
- func CreateNodes(tx *ent.Tx, stageId uint64, parentId uint64, newStageId uint64, newParentId uint64, ctx context.Context, logger logx.Logger) (err error) {
- data, err := tx.SopNode.Query().
- Where(sopnode.StageID(stageId), sopnode.ParentID(parentId)).All(ctx)
- if err != nil {
- return nil
- }
- for _, n := range data {
- newNode, err := tx.SopNode.Create().
- SetStageID(newStageId).
- SetParentID(newParentId).
- SetName(n.Name).
- SetConditionType(n.ConditionType).
- SetConditionList(n.ConditionList).
- SetNotNilNoReplyCondition(&n.NoReplyCondition).
- SetNotNilActionMessage(n.ActionMessage).
- SetNotNilActionLabel(n.ActionLabel).
- Save(ctx)
- if err != nil {
- return err
- }
- err = CreateNodes(tx, stageId, n.ID, newStageId, newNode.ID, ctx, logger)
- if err != nil {
- return err
- }
- }
- return nil
- }
|