package gmodel import ( "context" "fusenapi/constants" "time" "gorm.io/gorm" ) func (o *FsOrderModel) FindOneBySn(ctx context.Context, userId int64, sn string) (resp *FsOrder, err error) { err = o.db.WithContext(ctx).Model(&FsOrder{}).Where(" `user_id` = ? and `sn` = ? ", userId, sn).Take(&resp).Error return resp, err } func (o *FsOrderModel) FindOne(ctx context.Context, userId int64, OrderId int64) (order *FsOrder, err error) { err = o.db.WithContext(ctx).Model(&FsOrder{}).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error if err != nil { return nil, err } return } func (o *FsOrderModel) Update(ctx context.Context, data *FsOrder) error { return o.db.WithContext(ctx).Model(&FsOrder{}).Where("`id` = ?", data.Id).Updates(&data).Error } func (o *FsOrderModel) Create(ctx context.Context, data *FsOrder) error { return o.db.WithContext(ctx).Model(&FsOrder{}).Create(&data).Error } func (o *FsOrderModel) FindOneAndCreateServiceContact(ctx context.Context, userId int64, OrderId int64, cs *FsContactService) (order *FsOrder, err error) { err = o.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { err = tx.Model(order).Select("id").Limit(1).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error if err != nil { return err } ctime := time.Now().Unix() cs.Ctime = &ctime if constants.ConcactService(*cs.Type) == constants.TYPE_DEFAULT { *cs.RelationId = 0 } return tx.Model(cs).Create(cs).Error }) return order, err }