| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | package userproductlogic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/sentinel/internal/model" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"tianyuan-api/apps/sentinel/internal/svc" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/sentinel/sentinel" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CreateUserProductLogic struct { | 
					
						
							|  |  |  | 	ctx    context.Context | 
					
						
							|  |  |  | 	svcCtx *svc.ServiceContext | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewCreateUserProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserProductLogic { | 
					
						
							|  |  |  | 	return &CreateUserProductLogic{ | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // UserProduct methods | 
					
						
							|  |  |  | func (l *CreateUserProductLogic) CreateUserProduct(in *sentinel.CreateUserProductRequest) (*sentinel.UserProductEmptyResponse, error) { | 
					
						
							|  |  |  | 	isExist, err := l.IsUserProductAssociated(l.ctx, in.UserId, in.ProductId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if isExist { | 
					
						
							|  |  |  | 		return nil, errors.New("该产品已定阅读,无法重复订阅") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-10-21 16:01:20 +08:00
										 |  |  | 	product, err := l.svcCtx.ProductsModel.FindOne(l.ctx, in.ProductId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_, err = l.svcCtx.UserProductsModel.Insert(l.ctx, &model.UserProducts{UserId: in.UserId, ProductId: in.ProductId, ProductPrice: product.ProductPrice}) | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &sentinel.UserProductEmptyResponse{}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func (l *CreateUserProductLogic) IsUserProductAssociated(ctx context.Context, userId, productId int64) (bool, error) { | 
					
						
							|  |  |  | 	_, err := l.svcCtx.UserProductsModel.FindOneUserProduct(ctx, userId, productId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if err == model.ErrNotFound { | 
					
						
							|  |  |  | 			return false, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true, nil | 
					
						
							|  |  |  | } |