83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hospital-open-api/api/model"
|
|
"hospital-open-api/global"
|
|
)
|
|
|
|
type CouponDao struct {
|
|
}
|
|
|
|
// GetCouponById 获取数据-id
|
|
func (r *CouponDao) GetCouponById(couponId int64) (m *model.Coupon, err error) {
|
|
err = global.Db.First(&m, couponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCouponPreloadById 获取数据-加载全部关联-id
|
|
func (r *CouponDao) GetCouponPreloadById(couponId int64) (m *model.Coupon, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, couponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// EditCoupon 修改
|
|
func (r *CouponDao) EditCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.Coupon{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCouponByOrderServicePackageId 修改-id
|
|
func (r *CouponDao) EditCouponByOrderServicePackageId(tx *gorm.DB, couponId int64, data interface{}) error {
|
|
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCouponList 获取列表
|
|
func (r *CouponDao) GetCouponList(maps interface{}) (m []*model.Coupon, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddCoupon 新增
|
|
func (r *CouponDao) AddCoupon(tx *gorm.DB, model *model.Coupon) (*model.Coupon, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// Inc 自增
|
|
func (r *CouponDao) Inc(tx *gorm.DB, couponId int64, field string, numeral int) error {
|
|
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" + ?", numeral)).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Dec 自减
|
|
func (r *CouponDao) Dec(tx *gorm.DB, couponId int64, field string, numeral int) error {
|
|
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" - ?", numeral)).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|