74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hospital-admin-api/api/model"
|
|
"hospital-admin-api/global"
|
|
)
|
|
|
|
type ProductDao struct {
|
|
}
|
|
|
|
// GetProductById 获取商品数据-商品id
|
|
func (r *ProductDao) GetProductById(productId int64) (m *model.Product, err error) {
|
|
err = global.Db.First(&m, productId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetProductPreloadById 获取商品数据-加载全部关联-商品id
|
|
func (r *ProductDao) GetProductPreloadById(productId int64) (m *model.Product, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, productId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteProduct 删除商品
|
|
func (r *ProductDao) DeleteProduct(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.Product{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditProduct 修改商品
|
|
func (r *ProductDao) EditProduct(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.Product{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditProductByUserId 修改商品-商品id
|
|
func (r *ProductDao) EditProductByUserId(tx *gorm.DB, productId int64, data interface{}) error {
|
|
err := tx.Model(&model.Product{}).Where("product_id = ?", productId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetProductList 获取商品列表
|
|
func (r *ProductDao) GetProductList(maps interface{}) (m []*model.Product, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddProduct 新增商品
|
|
func (r *ProductDao) AddProduct(tx *gorm.DB, model *model.Product) (*model.Product, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|