fusenapi/model/gmodel/fs_address_logic.go

138 lines
4.0 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-09-25 10:42:45 +00:00
"time"
2023-06-20 11:36:28 +00:00
2023-09-25 10:42:45 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-06-14 06:05:27 +00:00
"gorm.io/gorm"
)
2023-06-12 06:05:35 +00:00
2023-09-25 10:42:45 +00:00
func (a *FsAddressModel) GetOne(ctx context.Context, addressId int64, userId int64) (resp *FsAddress, err error) {
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, 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) {
2023-10-07 09:58:24 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1", userId).Order("`ctime` DESC").Find(&resp).Error
2023-06-19 12:10:32 +00:00
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
2023-09-25 10:42:45 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
now := time.Now().UTC()
2023-06-20 04:15:14 +00:00
result = &FsAddress{
2023-09-27 03:35:34 +00:00
UserId: address.UserId,
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,
2023-10-07 09:58:24 +00:00
IsDefault: address.IsDefault,
Ctime: &now,
Utime: &now,
Ltime: &now,
2023-06-19 10:27:31 +00:00
}
2023-09-25 10:42:45 +00:00
return tx.Model(&FsAddress{}).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-09-26 04:24:09 +00:00
func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress) (err error) {
2023-09-25 16:02:50 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
err = tx.
2023-09-28 09:27:52 +00:00
Where("address_id = ? and user_id = ? and status = 1 ", address.AddressId, address.UserId).
2023-09-25 16:02:50 +00:00
Updates(address).Error
if err != nil {
return err
2023-06-19 12:10:32 +00:00
}
2023-09-25 16:02:50 +00:00
return err
2023-06-19 12:10:32 +00:00
})
return err
2023-06-12 08:47:48 +00:00
}
2023-09-25 10:42:45 +00:00
2023-10-07 09:58:24 +00:00
func (a *FsAddressModel) SettingUserDefaultAddressMutex(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) {
2023-09-25 10:42:45 +00:00
2023-09-25 16:02:50 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
2023-09-25 10:42:45 +00:00
2023-10-07 08:59:52 +00:00
logx.Info("address_id:", addressId, " set default ", isDefault)
2023-09-25 10:42:45 +00:00
now := time.Now().UTC()
2023-10-07 09:58:24 +00:00
err = tx.Where("`user_id` = ? and `status` = 1 and `is_default` = 1", userId).
UpdateColumn("is_default", 0).
UpdateColumn("utime", now).Error
2023-09-27 03:55:21 +00:00
if err != nil {
return err
}
2023-10-07 09:58:24 +00:00
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
UpdateColumn("is_default", isDefault).
UpdateColumn("ltime", now).
UpdateColumn("utime", now).Error
if err != nil {
return err
}
2023-10-07 08:59:52 +00:00
2023-10-07 09:58:24 +00:00
return nil
})
2023-10-07 08:59:52 +00:00
2023-10-07 09:58:24 +00:00
return err
}
2023-10-07 08:59:52 +00:00
2023-10-07 09:58:24 +00:00
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64, isDefault int64) (err error) {
2023-10-07 08:59:52 +00:00
2023-10-07 09:58:24 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
2023-09-25 10:42:45 +00:00
2023-10-07 09:58:24 +00:00
logx.Info("address_id:", addressId, " set default ", isDefault)
now := time.Now().UTC()
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
UpdateColumn("is_default", isDefault).
UpdateColumn("ltime", now).
UpdateColumn("utime", now).Error
if err != nil {
return err
2023-09-25 10:42:45 +00:00
}
return nil
})
2023-09-28 08:54:10 +00:00
return err
2023-09-25 10:42:45 +00:00
}
2023-09-25 16:02:50 +00:00
func (a *FsAddressModel) DeleteOne(ctx context.Context, addressId int64, userId int64) (err error) {
2023-09-26 03:32:43 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).
2023-09-28 08:54:10 +00:00
Where("`address_id` = ? and `user_id` = ? and `status` = 1 ", addressId, userId).
2023-09-26 03:32:43 +00:00
UpdateColumn("status", 0).Error
2023-09-25 16:02:50 +00:00
return err
}
2023-09-28 08:54:10 +00:00
// UpdateUsedAddress 当每次订单成功后, 更新一次地址使用时间
func (a *FsAddressModel) UpdateUsedAddress(ctx context.Context, addressId int64, userId int64) (err error) {
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
logx.Info("address_id:", addressId, " update used")
now := time.Now().UTC()
2023-10-07 09:58:24 +00:00
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
UpdateColumn("utime", now).
2023-09-28 08:54:10 +00:00
UpdateColumn("ltime", now).Error
if err != nil {
return err
}
return nil
})
return err
}