82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hospital-open-api/api/model"
|
|
"hospital-open-api/global"
|
|
)
|
|
|
|
type UserCouponDao struct {
|
|
}
|
|
|
|
// GetUserCouponById 获取用户优惠卷数据-用户优惠卷id
|
|
func (r *UserCouponDao) GetUserCouponById(userCouponId int64) (m *model.UserCoupon, err error) {
|
|
err = global.Db.First(&m, userCouponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (r *UserCouponDao) GetUserCouponByUserId(userId int64) (m *model.UserCoupon, err error) {
|
|
err = global.Db.Where("user_id = ?", userId).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetUserCouponPreloadById 获取用户优惠卷数据-加载全部关联-用户优惠卷id
|
|
func (r *UserCouponDao) GetUserCouponPreloadById(userCouponId int64) (m *model.UserCoupon, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, userCouponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteUserCoupon 删除用户优惠卷
|
|
func (r *UserCouponDao) DeleteUserCoupon(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.UserCoupon{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditUserCoupon 修改用户优惠卷
|
|
func (r *UserCouponDao) EditUserCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.UserCoupon{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditUserCouponById 修改用户优惠卷-用户优惠卷id
|
|
func (r *UserCouponDao) EditUserCouponById(tx *gorm.DB, userCouponId int64, data interface{}) error {
|
|
err := tx.Model(&model.UserCoupon{}).Where("user_coupon_id = ?", userCouponId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetUserCouponList 获取用户优惠卷列表
|
|
func (r *UserCouponDao) GetUserCouponList(maps interface{}) (m []*model.UserCoupon, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddUserCoupon 新增用户优惠卷
|
|
func (r *UserCouponDao) AddUserCoupon(tx *gorm.DB, model *model.UserCoupon) (*model.UserCoupon, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|