fusenapi/model/gmodel/fs_feishu_user_logic.go

31 lines
1.0 KiB
Go
Raw Normal View History

2023-11-07 10:29:13 +00:00
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
// TODO: 使用model的属性做你想做的
func (u *FsFeishuUserModel) Create(ctx context.Context, data *FsFeishuUser) error {
return u.db.WithContext(ctx).Model(&FsFeishuUser{}).Create(&data).Error
}
func (u *FsFeishuUserModel) Update(ctx context.Context, data *FsFeishuUser) error {
return u.db.WithContext(ctx).Model(&FsFeishuUser{}).Where("`app_id` = ? and `open_id` = ?", data.AppId, data.OpenId).Updates(&data).Error
}
func (u *FsFeishuUserModel) Find(ctx context.Context, appId, openId string) (resp *FsFeishuUser, err error) {
err = u.db.WithContext(ctx).Model(&FsFeishuUser{}).Where("`app_id` = ? and `open_id` = ?", appId, openId).Take(&resp).Error
return resp, err
}
func (u *FsFeishuUserModel) CreateOrUpdate(ctx context.Context, appId, openId string, data *FsFeishuUser) error {
_, err := u.Find(ctx, appId, openId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return u.Create(ctx, data)
}
return err
}
return u.Update(ctx, data)
}