first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package mysqldao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqlmodel"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GoodsDao 商品数据访问
|
||||
type GoodsDao struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGoodsDao 创建商品 DAO
|
||||
func NewGoodsDao() *GoodsDao {
|
||||
return &GoodsDao{db: global.MysqlEngine}
|
||||
}
|
||||
|
||||
// GoodsGetByCode 按商品编码查询商品
|
||||
func (d *GoodsDao) GoodsGetByCode(ctx context.Context, code string) (*mysqlmodel.Goods, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsGetByCode")
|
||||
defer span.End()
|
||||
|
||||
var goods mysqlmodel.Goods
|
||||
if err := d.db.WithContext(ctx).Where("goods_code = ?", code).First(&goods).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
return &goods, nil
|
||||
}
|
||||
|
||||
// GoodsGetByID 按主键 ID 查询商品
|
||||
func (d *GoodsDao) GoodsGetByID(ctx context.Context, id int64) (*mysqlmodel.Goods, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsGetByID")
|
||||
defer span.End()
|
||||
|
||||
var goods mysqlmodel.Goods
|
||||
if err := d.db.WithContext(ctx).First(&goods, id).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
return &goods, nil
|
||||
}
|
||||
|
||||
// GoodsList 分页查询上架商品(goodsType 为 0 不筛选)
|
||||
func (d *GoodsDao) GoodsList(ctx context.Context, goodsType int8, page, pageSize int) ([]mysqlmodel.Goods, int64, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsList")
|
||||
defer span.End()
|
||||
|
||||
var list []mysqlmodel.Goods
|
||||
var total int64
|
||||
q := d.db.WithContext(ctx).Model(&mysqlmodel.Goods{}).Where("status = ?", int8(1))
|
||||
if goodsType != 0 {
|
||||
q = q.Where("goods_type = ?", goodsType)
|
||||
}
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := q.Offset((page - 1) * pageSize).Limit(pageSize).Order("id DESC").Find(&list).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user