244 lines
5.6 KiB
Go
244 lines
5.6 KiB
Go
package model
|
|
|
|
import (
|
|
"github.com/codinl/go-logger"
|
|
"mh-server/lib/utils"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
SaleStatusUnknown = iota
|
|
SaleStatusYes
|
|
SaleStatusNo
|
|
)
|
|
|
|
const (
|
|
PayTypeUnknown = iota
|
|
PayTypeRm // 人民币
|
|
PayTypeVm // 积分
|
|
)
|
|
|
|
const (
|
|
PayStatusUnknown = iota
|
|
PayStatusInit
|
|
PayStatusOK
|
|
PayStatusFail
|
|
)
|
|
|
|
// 商品
|
|
//go:generate goqueryset -in mall.go
|
|
// gen:qs
|
|
type Goods struct {
|
|
Model
|
|
|
|
GoodsId uint32 `json:"goods_id" gorm:"unique_index"`
|
|
SerialNo string `json:"serial_no" gorm:"unique_index"` // 序列号
|
|
CatId uint32 `json:"cat_id"` // 分类
|
|
Name string `json:"name"` // 名称
|
|
Title string `json:"title"` // 标题
|
|
MainImage string `json:"main_image"` // 主图
|
|
Images string `json:"images"` // 图片列表,用,隔开
|
|
Stock uint32 `json:"stock"` // 库存
|
|
Detail string `json:"detail" gorm:"type:text;"` // 详情, 富文本
|
|
|
|
SoldCount uint32 `json:"sold_count"` // 已销售数量
|
|
SaleStatus uint32 `json:"sale_status"` // 在售状态 1-在售 2-下架
|
|
|
|
PriceVm uint32 `json:"price_vm"` // 积分价格
|
|
PriceRm uint32 `json:"price_rm"` // 人民币价格
|
|
PriceOriginal uint32 `json:"price_original"` // 市场价
|
|
DeliveryFee uint32 `json:"delivery_fee"` // 邮费
|
|
|
|
VersionId uint64 `json:"version_id"` // 乐观锁
|
|
}
|
|
|
|
//// gen:qs
|
|
//type GoodsCat struct {
|
|
// Model
|
|
// Color string `json:"color"`
|
|
//}
|
|
|
|
func CreateGoodsSerialNo() string {
|
|
for {
|
|
serialNo := utils.GenSerialNo()
|
|
if count, err := NewGoodsQuerySet(DB).SerialNoEq(serialNo).Count(); err == nil && count > 0 {
|
|
continue
|
|
}
|
|
return serialNo
|
|
}
|
|
}
|
|
|
|
func CreateGoodsId() uint32 {
|
|
for {
|
|
orderId := utils.GenUid()
|
|
if count, err := NewGoodsQuerySet(DB).GoodsIdEq(orderId).Count(); err == nil && count > 0 {
|
|
continue
|
|
}
|
|
return orderId
|
|
}
|
|
}
|
|
|
|
type GoodsListReq struct {
|
|
PageIdx int `json:"page_idx"`
|
|
PageSize int `json:"page_size"`
|
|
}
|
|
|
|
func (m *GoodsListReq) GoodsList() ([]Goods, int, error) {
|
|
page := m.PageIdx - 1
|
|
if page < 0 {
|
|
page = 0
|
|
}
|
|
if m.PageSize == 0 {
|
|
m.PageSize = 10
|
|
}
|
|
var goodsList []Goods
|
|
qs := NewGoodsQuerySet(DB)
|
|
|
|
count, err := qs.Count()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, 0, err
|
|
}
|
|
totalPage := count/m.PageSize + 1
|
|
|
|
err = qs.Offset(page * m.PageSize).Limit(m.PageSize).All(&goodsList)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
return goodsList, totalPage, nil
|
|
}
|
|
|
|
type GoodsDetailReq struct {
|
|
GoodsId uint32 `json:"goods_id"`
|
|
}
|
|
|
|
func (m *GoodsDetailReq) GoodsDetail() (*Goods, error) {
|
|
var goods Goods
|
|
err := NewGoodsQuerySet(DB).GoodsIdEq(m.GoodsId).One(&goods)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, err
|
|
}
|
|
return &goods, nil
|
|
}
|
|
|
|
// 商品分类
|
|
// gen:qs
|
|
type GoodsCategory struct {
|
|
Model
|
|
|
|
CatId uint32 `json:"cat_id"`
|
|
ParentCatId uint32 `json:"parent_cat_id"`
|
|
Name string `json:"name"`
|
|
Sort uint32 `json:"sort"` // 排序
|
|
Level uint32 `json:"level"`
|
|
State uint32 `json:"state"`
|
|
}
|
|
|
|
// 商品订单
|
|
// gen:qs
|
|
type GoodsOrder struct {
|
|
Model
|
|
|
|
OrderId uint32 `json:"order_id" gorm:"unique_index"` // 订单 id
|
|
SerialNo string `json:"serial_no" gorm:"unique_index"` // 序列号
|
|
|
|
Uid uint32 `json:"uid" gorm:"index"`
|
|
GoodsId uint32 `json:"goods_id" gorm:"index"` // 商品id
|
|
Amount uint32 `json:"amount"` // 订单金额
|
|
Quantity uint32 `json:"quantity"` // 购买商品的数量
|
|
|
|
PayType uint32 `json:"pay_type"` // 支付方式 1-rm 2-vm
|
|
PayTime time.Time `json:"pay_time"` // 支付时间
|
|
PayStatus uint32 `json:"pay_status"` // 支付状态 1-待支付 2-已支付 3-失败
|
|
|
|
AddressId uint32 `json:"address_id"` // 收货地址
|
|
DeliveryExtraInfo string `json:"delivery_extra_info"` // 物流备注
|
|
DeliveryFee uint32 `json:"delivery_fee"` // 物流费用
|
|
DeliveryTrackingNo string `json:"delivery_tracking_no"` // 物流单号
|
|
DeliveryStatus uint32 `json:"delivery_status"` // 物流状态 1-待发货 2-已发货 3-已收货
|
|
|
|
VersionId uint64 `json:"version_id"` // 乐观锁
|
|
}
|
|
|
|
func CreateGoodsOrderSerialNo() string {
|
|
for {
|
|
serialNo := utils.GenSerialNo()
|
|
if count, err := NewGoodsOrderQuerySet(DB).SerialNoEq(serialNo).Count(); err == nil && count > 0 {
|
|
continue
|
|
}
|
|
return serialNo
|
|
}
|
|
}
|
|
|
|
func CreateGoodsOrderId() uint32 {
|
|
for {
|
|
orderId := utils.GenUid()
|
|
if count, err := NewGoodsOrderQuerySet(DB).OrderIdEq(orderId).Count(); err == nil && count > 0 {
|
|
continue
|
|
}
|
|
return orderId
|
|
}
|
|
}
|
|
|
|
type GoodsOrderListReq struct {
|
|
PageIdx int `json:"page_idx"`
|
|
PageSize int `json:"page_size"`
|
|
}
|
|
|
|
func (m *GoodsOrderListReq) OrderList(uid uint32) ([]GoodsOrder, int, error) {
|
|
page := m.PageIdx - 1
|
|
if page < 0 {
|
|
page = 0
|
|
}
|
|
if m.PageSize == 0 {
|
|
m.PageSize = 10
|
|
}
|
|
|
|
var list []GoodsOrder
|
|
qs := NewGoodsOrderQuerySet(DB)
|
|
|
|
if uid != 0 {
|
|
qs = qs.UidEq(uid)
|
|
}
|
|
|
|
count, err := qs.Count()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, 0, err
|
|
}
|
|
totalPage := count/m.PageSize + 1
|
|
|
|
err = qs.Offset(page * m.PageSize).Limit(m.PageSize).All(&list)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
return list, totalPage, nil
|
|
}
|
|
|
|
type GoodsOrderDetailReq struct {
|
|
OrderId uint32 `json:"order_id"`
|
|
}
|
|
|
|
func (m *GoodsOrderDetailReq) OrderDetail(uid uint32) (*GoodsOrder, error) {
|
|
var order GoodsOrder
|
|
qs := NewGoodsOrderQuerySet(DB).
|
|
OrderIdEq(m.OrderId)
|
|
|
|
if uid != 0 {
|
|
qs = qs.UidEq(uid)
|
|
}
|
|
|
|
err := qs.One(&order)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &order, nil
|
|
}
|