first commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package mysqldao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/global"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/mysqlmodel"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GoodsDao 商品数据访问
|
||||
type GoodsDao struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGoodsDao 创建商品 DAO
|
||||
func NewGoodsDao() *GoodsDao {
|
||||
return &GoodsDao{db: global.MysqlEngine}
|
||||
}
|
||||
|
||||
// GoodsListParams 商品分页列表筛选参数。
|
||||
type GoodsListParams struct {
|
||||
GoodsType int8 // 商品类型筛选,0 表示全部
|
||||
Status int8 // 状态筛选,0 表示全部(含下架)
|
||||
Keyword string // 模糊匹配商品名称/编码
|
||||
Page int
|
||||
Size int
|
||||
}
|
||||
|
||||
// GoodsList 分页查询商品(管理端含下架商品)
|
||||
func (d *GoodsDao) GoodsList(ctx context.Context, p GoodsListParams) ([]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{})
|
||||
if p.GoodsType != 0 {
|
||||
q = q.Where("goods_type = ?", p.GoodsType)
|
||||
}
|
||||
if p.Status != 0 {
|
||||
q = q.Where("status = ?", p.Status)
|
||||
}
|
||||
if p.Keyword != "" {
|
||||
kw := "%" + p.Keyword + "%"
|
||||
q = q.Where("goods_name LIKE ? OR goods_code LIKE ?", kw, kw)
|
||||
}
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := q.Offset((p.Page - 1) * p.Size).Limit(p.Size).Order("id DESC").Find(&list).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return list, total, 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
|
||||
}
|
||||
|
||||
// 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).Unscoped().Where("goods_code = ?", code).First(&goods).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
return &goods, nil
|
||||
}
|
||||
|
||||
// GoodsCreate 创建商品
|
||||
func (d *GoodsDao) GoodsCreate(ctx context.Context, goods *mysqlmodel.Goods) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsCreate")
|
||||
defer span.End()
|
||||
|
||||
if err := d.db.WithContext(ctx).Create(goods).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GoodsUpdate 按主键更新商品字段
|
||||
func (d *GoodsDao) GoodsUpdate(ctx context.Context, id int64, updates map[string]any) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsUpdate")
|
||||
defer span.End()
|
||||
|
||||
if err := d.db.WithContext(ctx).Model(&mysqlmodel.Goods{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GoodsSoftDelete 软删除商品
|
||||
func (d *GoodsDao) GoodsSoftDelete(ctx context.Context, id int64) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.GoodsSoftDelete")
|
||||
defer span.End()
|
||||
|
||||
if err := d.db.WithContext(ctx).Where("id = ?", id).Delete(&mysqlmodel.Goods{}).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user