fusenapi/model/gmodel/fs_address_logic.go

64 lines
1.9 KiB
Go
Raw Normal View History

2023-06-12 08:47:48 +00:00
package gmodel
2023-06-14 06:05:27 +00:00
import (
2023-06-16 10:48:05 +00:00
"context"
2023-06-20 11:36:28 +00:00
2023-06-14 06:05:27 +00:00
"gorm.io/gorm"
)
2023-06-12 06:05:35 +00:00
2023-06-20 09:28:28 +00:00
func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (resp *FsAddress, err error) {
2023-06-21 04:34:57 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`id` = ? and `user_id` = ? and `status` = ? ", id, userId, 1).Take(&resp).Error
2023-06-20 09:28:28 +00:00
return resp, err
2023-06-12 06:05:35 +00:00
}
2023-06-12 08:47:48 +00:00
2023-06-19 12:10:32 +00:00
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) {
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error
if err != nil {
return nil, err
}
return
}
2023-06-20 04:15:14 +00:00
func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (result *FsAddress, err error) {
2023-06-19 10:27:31 +00:00
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
2023-08-28 06:21:06 +00:00
// now := time.Now().UTC().Unix()
2023-06-20 04:15:14 +00:00
result = &FsAddress{
2023-09-25 03:16:55 +00:00
UserId: address.UserId,
AddressName: address.AddressName,
FirstName: address.FirstName,
LastName: address.LastName,
Mobile: address.Mobile,
Street: address.Street,
Suite: address.Suite,
City: address.City,
State: address.State,
Country: address.Country,
ZipCode: address.ZipCode,
Status: address.Status,
IsDefault: address.IsDefault,
2023-06-19 10:27:31 +00:00
}
2023-06-20 04:15:14 +00:00
return tx.Create(result).Error
2023-06-19 10:27:31 +00:00
})
if err != nil {
return nil, err
}
2023-06-20 04:15:14 +00:00
return result, nil
2023-06-19 10:27:31 +00:00
}
2023-06-19 12:10:32 +00:00
func (a *FsAddressModel) UpdateAddAddress(ctx context.Context, address *FsAddress) (err error) {
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if *address.IsDefault > 0 {
2023-07-11 06:16:13 +00:00
err = tx.Model(&FsAddress{}).Where("user_id = ? and is_default = 1 ", address.UserId).Update("is_default", 0).Error
2023-06-19 12:10:32 +00:00
if err != nil {
return err
}
}
2023-09-25 03:16:55 +00:00
return tx.Model(&FsAddress{}).Where("id = ? and user_id = ?", address.AddressId, address.UserId).Omit("id", "user_id").Updates(address).Error
2023-06-19 12:10:32 +00:00
})
return err
2023-06-12 08:47:48 +00:00
}