fusenapi/model/gmodel/fs_address_logic.go

115 lines
3.3 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-09-25 10:42:45 +00:00
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`ltime` 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-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-09-25 10:42:45 +00:00
Ctime: &now,
Utime: &now,
Ltime: &now,
2023-06-19 10:27:31 +00:00
}
2023-09-25 10:42:45 +00:00
// lastOne := &FsAddress{}
// err = tx.Where("user_id = ?", lastOne.UserId).Order("ltime ASC").Take(&lastOne).Error
// if err == gorm.ErrRecordNotFound {
// result.Ltime = &now
// return tx.Model(&FsAddress{}).Create(result).Error
// }
// if err != nil {
// return err
// }
// // 根据lastOne处理时间
// ltime := (*lastOne.Ltime).Add(-time.Second)
// result.Ltime = &ltime
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.
Where("user_id = ? and address_id = ? and status = 1 ", address.UserId, address.AddressId).
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-09-25 16:02:50 +00:00
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId 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
now := time.Now().UTC()
2023-09-26 03:01:42 +00:00
err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId).
2023-09-26 03:21:43 +00:00
UpdateColumn("ltime", now.AddDate(250, 0, 0)).
2023-09-25 10:42:45 +00:00
UpdateColumn("utime", now).Error
if err != nil {
return err
}
2023-09-26 03:21:43 +00:00
err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.AddDate(10, 0, 0)).
2023-09-25 10:42:45 +00:00
UpdateColumn("ltime", now).Error
if err != nil {
logx.Error(err)
}
return nil
})
return
}
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{}).
Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, userId, 1).
UpdateColumn("status", 0).Error
2023-09-25 16:02:50 +00:00
return err
}