66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"muyu-apiserver/model"
|
|
"muyu-apiserver/rpc/production/internal/svc"
|
|
"muyu-apiserver/rpc/production/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type ConfirmShareLinkLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewConfirmShareLinkLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ConfirmShareLinkLogic {
|
|
return &ConfirmShareLinkLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *ConfirmShareLinkLogic) ConfirmShareLink(in *pb.ConfirmShareLinkReq) (*pb.Empty, error) {
|
|
link, err := l.svcCtx.ShareLinkModel.FindByShortCode(l.ctx, in.ShortCode)
|
|
if err != nil {
|
|
return nil, ErrLinkNotFound
|
|
}
|
|
|
|
// Check not expired
|
|
if time.Now().After(link.ExpiresAt) {
|
|
return nil, ErrLinkExpired
|
|
}
|
|
|
|
// Check status is 0 (created) or 1 (clicked)
|
|
if link.Status != 0 && link.Status != 1 {
|
|
return nil, ErrLinkAlreadyUsed
|
|
}
|
|
|
|
// Create plan_factory record linking factory to the plan
|
|
_, err = l.svcCtx.PlanFactoryModel.Insert(l.ctx, &model.ProPlanFactory{
|
|
PlanId: link.PlanId,
|
|
FactoryTenantId: in.FactoryTenantId,
|
|
FactoryType: link.FactoryType,
|
|
ShareLinkId: link.LinkId,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Update share link status to 2 (confirmed)
|
|
if err := l.svcCtx.ShareLinkModel.UpdateStatus(l.ctx, in.ShortCode, 2, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Update plan status to 2 (producing) if currently 1 (pending)
|
|
plan, err := l.svcCtx.PlanModel.FindOneByPlanId(l.ctx, link.PlanId)
|
|
if err == nil && plan.Status == 1 {
|
|
if err := l.svcCtx.PlanModel.UpdateStatus(l.ctx, link.PlanId, 2); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|