2023-09-13 08:06:05 +00:00
|
|
|
|
package gmodel
|
2023-09-14 06:11:22 +00:00
|
|
|
|
|
2023-09-15 09:58:45 +00:00
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-09-26 07:14:08 +00:00
|
|
|
|
"time"
|
2023-09-15 09:58:45 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-20 10:04:33 +00:00
|
|
|
|
func (m *FsShoppingCartModel) TableName() string {
|
|
|
|
|
return m.name
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 07:21:42 +00:00
|
|
|
|
// 关联查询
|
2023-09-15 09:58:45 +00:00
|
|
|
|
type RelaFsShoppingCart struct {
|
|
|
|
|
FsShoppingCart
|
2023-09-26 08:53:34 +00:00
|
|
|
|
ShoppingCartProduct *RelaFsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"`
|
|
|
|
|
ShoppingCartProductModel3d *FsProductModel3d `json:"shopping_cart_product_model3d_list" gorm:"foreignkey:model_id;references:id"`
|
|
|
|
|
ShoppingCartProductModel3dFitting *FsProductModel3d `json:"shopping_cart_product_model3d_list_fitting" gorm:"foreignkey:fitting_id;references:id"`
|
2023-09-15 09:58:45 +00:00
|
|
|
|
}
|
2023-09-26 07:14:08 +00:00
|
|
|
|
type FsShoppingCartData struct {
|
|
|
|
|
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // id
|
|
|
|
|
UserId *int64 `gorm:"default:0;" json:"user_id"` // 用户id
|
|
|
|
|
ProductId *int64 `gorm:"default:0;" json:"product_id"` // 产品id
|
|
|
|
|
TemplateId *int64 `gorm:"default:0;" json:"template_id"` // 模板id
|
|
|
|
|
ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型id
|
|
|
|
|
SizeId *int64 `gorm:"default:0;" json:"size_id"` // 尺寸id
|
|
|
|
|
LightId *int64 `gorm:"default:0;" json:"light_id"` // 灯光id
|
|
|
|
|
FittingId *int64 `gorm:"default:0;" json:"fitting_id"` // 配件id
|
|
|
|
|
PurchaseQuantity *int64 `gorm:"default:0;" json:"purchase_quantity"` // 购买数量
|
|
|
|
|
Snapshot *map[string]interface{} `gorm:"default:'';" json:"snapshot"` //
|
|
|
|
|
SnapshotData *string `gorm:"default:'';" json:"snapshot_data"` //
|
|
|
|
|
IsSelected *int64 `gorm:"default:0;" json:"is_selected"` // 是否被选中 0非 1是
|
|
|
|
|
IsHighlyCustomized *int64 `gorm:"default:0;" json:"is_highly_customized"` // 是否高度定制 0非 1是(针对客人高度定制只能后台增加如购物车)
|
|
|
|
|
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
|
|
|
|
|
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
|
|
|
|
|
}
|
2023-09-14 06:11:22 +00:00
|
|
|
|
|
2023-09-20 07:21:42 +00:00
|
|
|
|
// 快照json数据结构
|
|
|
|
|
// 购物车快照数据结构
|
|
|
|
|
type CartSnapshot struct {
|
|
|
|
|
Logo string `json:"logo"` //logo地址
|
|
|
|
|
CombineImage string `json:"combine_image"` //刀版图地址
|
|
|
|
|
RenderImage string `json:"render_image"` //渲染结果图
|
|
|
|
|
TemplateInfo TemplateInfo `json:"template_info"` //模板数据
|
|
|
|
|
ModelInfo ModelInfo `json:"model_info"` //模型的数据
|
|
|
|
|
FittingInfo FittingInfo `json:"fitting_info"` //配件信息
|
|
|
|
|
SizeInfo SizeInfo `json:"size_info"` //尺寸基本信息
|
|
|
|
|
ProductInfo ProductInfo `json:"product_info"` //产品基本信息(只记录不要使用)
|
|
|
|
|
UserDiyInformation UserDiyInformation `json:"user_diy_information"` //用户diy数据
|
|
|
|
|
LightInfo LightInfo `json:"light_info"` //灯光数据
|
|
|
|
|
}
|
|
|
|
|
type ProductInfo struct {
|
|
|
|
|
ProductName string `json:"product_name"`
|
|
|
|
|
ProductSn string `json:"product_sn"`
|
|
|
|
|
}
|
|
|
|
|
type ModelInfo struct {
|
|
|
|
|
ModelJson string `json:"model_json"` //模型设计json数据
|
|
|
|
|
}
|
|
|
|
|
type FittingInfo struct {
|
|
|
|
|
FittingJson string `json:"fitting_json"` //配件设计json数据
|
|
|
|
|
FittingName string `json:"fitting_name"` //配件名称
|
2023-09-20 10:04:33 +00:00
|
|
|
|
|
2023-09-20 07:21:42 +00:00
|
|
|
|
}
|
|
|
|
|
type TemplateInfo struct {
|
|
|
|
|
TemplateJson string `json:"template_json"` //模板设计json数据
|
|
|
|
|
TemplateTag string `json:"template_tag"` //模板标签
|
|
|
|
|
}
|
|
|
|
|
type SizeInfo struct {
|
|
|
|
|
Inch string `json:"inch"`
|
|
|
|
|
Cm string `json:"cm"`
|
|
|
|
|
Capacity string `json:"capacity"`
|
|
|
|
|
}
|
|
|
|
|
type UserDiyInformation struct {
|
|
|
|
|
Phone string `json:"phone"` //电话
|
|
|
|
|
Address string `json:"address"` //地址
|
|
|
|
|
Website string `json:"website"` //网站
|
|
|
|
|
Qrcode string `json:"qrcode"` //二维码
|
|
|
|
|
Slogan string `json:"slogan"` //slogan
|
|
|
|
|
}
|
|
|
|
|
type LightInfo struct {
|
|
|
|
|
LightJson string `json:"light_json"` //灯光设计json数据
|
|
|
|
|
LightName string `json:"light_name"` //名称
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 06:11:22 +00:00
|
|
|
|
// 获取单个
|
|
|
|
|
func (s *FsShoppingCartModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsShoppingCart, err error) {
|
|
|
|
|
db := s.db.WithContext(ctx).Where("id = ?", id)
|
|
|
|
|
if len(fields) > 0 {
|
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
|
}
|
|
|
|
|
err = db.Take(&resp).Error
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 07:46:37 +00:00
|
|
|
|
// 获取用户购物车指定item
|
|
|
|
|
func (s *FsShoppingCartModel) FineOneUserCart(ctx context.Context, id, userId int64, fields ...string) (resp *FsShoppingCart, err error) {
|
|
|
|
|
db := s.db.WithContext(ctx).Where("user_id = ? and id = ?", userId, id)
|
|
|
|
|
if len(fields) > 0 {
|
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
|
}
|
|
|
|
|
err = db.Take(&resp).Error
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 07:20:37 +00:00
|
|
|
|
// 创建
|
|
|
|
|
func (s *FsShoppingCartModel) Create(ctx context.Context, data *FsShoppingCart) error {
|
2023-09-15 06:19:14 +00:00
|
|
|
|
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Create(&data).Error
|
2023-09-14 07:46:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 02:39:08 +00:00
|
|
|
|
// 删除
|
2023-09-20 07:52:34 +00:00
|
|
|
|
func (s *FsShoppingCartModel) Delete(ctx context.Context, userId, id int64) error {
|
2023-09-15 06:19:14 +00:00
|
|
|
|
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ? and id = ?", userId, id).Delete(&FsShoppingCart{}).Error
|
2023-09-14 07:46:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新
|
|
|
|
|
func (s *FsShoppingCartModel) Update(ctx context.Context, id, userId int64, data *FsShoppingCart) error {
|
2023-09-15 06:19:14 +00:00
|
|
|
|
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ? and id = ?", userId, id).Updates(&data).Error
|
2023-09-14 07:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户购物车数量
|
|
|
|
|
func (s *FsShoppingCartModel) CountUserCart(ctx context.Context, userId int64) (total int64, err error) {
|
2023-09-15 06:19:14 +00:00
|
|
|
|
err = s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ?", userId).Limit(1).Count(&total).Error
|
2023-09-14 07:20:37 +00:00
|
|
|
|
return total, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 06:11:22 +00:00
|
|
|
|
// 获取多个
|
|
|
|
|
func (s *FsShoppingCartModel) GetAllByIds(ctx context.Context, ids []int64, sort string, fields ...string) (resp []FsShoppingCart, err error) {
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-15 06:19:14 +00:00
|
|
|
|
db := s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("id in (?)", ids)
|
2023-09-14 06:11:22 +00:00
|
|
|
|
if len(fields) > 0 {
|
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
|
}
|
|
|
|
|
if sort != "" {
|
|
|
|
|
db = db.Order(sort)
|
|
|
|
|
}
|
|
|
|
|
err = db.Find(&resp).Error
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户所有的购物车
|
2023-09-14 10:33:29 +00:00
|
|
|
|
type GetAllCartsByParamReq struct {
|
|
|
|
|
Ids []int64 //id集合
|
|
|
|
|
UserId int64 //用户id
|
|
|
|
|
Fields string //筛选的字段
|
|
|
|
|
Sort string //排序
|
|
|
|
|
Page int //当前页
|
|
|
|
|
Limit int //每页数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *FsShoppingCartModel) GetAllCartsByParam(ctx context.Context, req GetAllCartsByParamReq) (resp []FsShoppingCart, total int64, err error) {
|
2023-09-15 06:19:14 +00:00
|
|
|
|
db := s.db.WithContext(ctx).Model(&FsShoppingCart{})
|
2023-09-14 10:33:29 +00:00
|
|
|
|
if req.UserId > 0 {
|
|
|
|
|
db = db.Where("user_id = ?", req.UserId)
|
2023-09-14 06:11:22 +00:00
|
|
|
|
}
|
2023-09-14 10:33:29 +00:00
|
|
|
|
if len(req.Ids) > 0 {
|
|
|
|
|
db = db.Where("id in (?)", req.Ids)
|
2023-09-14 06:11:22 +00:00
|
|
|
|
}
|
2023-09-14 10:33:29 +00:00
|
|
|
|
if req.Fields != "" {
|
|
|
|
|
db = db.Select(req.Fields)
|
|
|
|
|
}
|
|
|
|
|
if req.Sort != "" {
|
|
|
|
|
db = db.Order(req.Sort)
|
|
|
|
|
}
|
|
|
|
|
//查询数量
|
|
|
|
|
if err = db.Limit(1).Count(&total).Error; err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
offset := (req.Page - 1) * req.Limit
|
|
|
|
|
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
|
|
|
|
|
return resp, total, err
|
2023-09-14 06:11:22 +00:00
|
|
|
|
}
|