| 
									
										
										
										
											2023-06-13 12:15:06 +08:00
										 |  |  | // Code generated by goctl. DO NOT EDIT. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package gmodel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"gorm.io/gorm" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type FindOneCartByParamsReq struct { | 
					
						
							|  |  |  | 	UserId     *int64 | 
					
						
							|  |  |  | 	ProductId  *int64 | 
					
						
							|  |  |  | 	TemplateId *int64 | 
					
						
							|  |  |  | 	PriceId    *int64 | 
					
						
							|  |  |  | 	DesignId   *int64 | 
					
						
							|  |  |  | 	MaterialId *int64 | 
					
						
							|  |  |  | 	Status     *int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *FsCartModel) FindOne(ctx context.Context, id int64) (resp FsCart, err error) { | 
					
						
							|  |  |  | 	err = c.db.WithContext(ctx).Model(&FsCart{}).Where("`id` = ?", id).First(&resp).Error | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { | 
					
						
							|  |  |  | 		return FsCart{}, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-13 13:07:09 +08:00
										 |  |  | 	return resp, nil | 
					
						
							| 
									
										
										
										
											2023-06-13 12:15:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | func (c *FsCartModel) FindOneCartByParams(ctx context.Context, req FindOneCartByParamsReq) (resp FsCart, err error) { | 
					
						
							|  |  |  | 	db := c.db.WithContext(ctx).Model(&FsCart{}) | 
					
						
							|  |  |  | 	if req.UserId != nil { | 
					
						
							|  |  |  | 		db = db.Where("`user_id` = ?", req.UserId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.ProductId != nil { | 
					
						
							|  |  |  | 		db = db.Where("`product_id` = ?", req.ProductId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.TemplateId != nil { | 
					
						
							|  |  |  | 		db = db.Where("`template_id` = ?", req.TemplateId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.PriceId != nil { | 
					
						
							| 
									
										
										
										
											2023-06-13 13:07:09 +08:00
										 |  |  | 		db = db.Where("`price_id` = ?", req.PriceId) | 
					
						
							| 
									
										
										
										
											2023-06-13 12:15:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if req.DesignId != nil { | 
					
						
							|  |  |  | 		db = db.Where("`design_id` = ?", req.DesignId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.MaterialId != nil { | 
					
						
							|  |  |  | 		db = db.Where("`material_id` = ?", req.MaterialId) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if req.Status != nil { | 
					
						
							|  |  |  | 		db = db.Where("`status` = ?", req.Status) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err = db.First(&resp).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { | 
					
						
							|  |  |  | 		return FsCart{}, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-13 13:07:09 +08:00
										 |  |  | 	return resp, nil | 
					
						
							| 
									
										
										
										
											2023-06-13 12:15:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | func (c *FsCartModel) Create(ctx context.Context, data FsCart) error { | 
					
						
							| 
									
										
										
										
											2023-06-13 13:07:09 +08:00
										 |  |  | 	return c.db.WithContext(ctx).Model(&FsCart{}).Create(&data).Error | 
					
						
							| 
									
										
										
										
											2023-06-13 12:15:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | func (c *FsCartModel) Update(ctx context.Context, id int64, data FsCart) error { | 
					
						
							|  |  |  | 	return c.db.WithContext(ctx).Model(&FsCart{}).Where("`id` = ?", id).Updates(data).Error | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-13 14:13:40 +08:00
										 |  |  | func (c *FsCartModel) UpdateByIdUserId(ctx context.Context, id int64, userId int64, data FsCart) error { | 
					
						
							|  |  |  | 	return c.db.WithContext(ctx).Model(&FsCart{}).Where("`id` = ? and `user_id` = ?", id, userId).Updates(data).Error | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-13 14:29:44 +08:00
										 |  |  | func (c *FsCartModel) CountUserCart(ctx context.Context, userId int64) (total int64, err error) { | 
					
						
							|  |  |  | 	err = c.db.WithContext(ctx).Model(&FsCart{}).Where("`user_id` = ? and `status` = ? limit 1", userId, 1).Count(&total).Error | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-13 17:47:48 +08:00
										 |  |  | func (c *FsCartModel) GetAllByUserId(ctx context.Context, userId int64, sort string) (resp []FsCart, err error) { | 
					
						
							|  |  |  | 	db := c.db.WithContext(ctx).Model(&FsCart{}).Where("`user_id` = ? and `status` = ?", userId, 1) | 
					
						
							|  |  |  | 	switch sort { | 
					
						
							|  |  |  | 	case "ctime-desc": | 
					
						
							|  |  |  | 		db = db.Order("`ctime` DESC") | 
					
						
							|  |  |  | 	case "ctime-asc": | 
					
						
							|  |  |  | 		db = db.Order("`ctime` ASC") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = db.Find(&resp).Error | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } |