114 lines
3.2 KiB
Go
Executable File
114 lines
3.2 KiB
Go
Executable File
package gmodel
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
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
|
|
return resp, err
|
|
}
|
|
|
|
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("`ltime` DESC").Find(&resp).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (result *FsAddress, err error) {
|
|
|
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
|
now := time.Now().UTC()
|
|
result = &FsAddress{
|
|
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,
|
|
IsDefault: address.IsDefault,
|
|
Ctime: &now,
|
|
Utime: &now,
|
|
Ltime: &now,
|
|
}
|
|
|
|
// 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 = <ime
|
|
return tx.Model(&FsAddress{}).Create(result).Error
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (a *FsAddressModel) UpdateAddress(ctx context.Context, address *FsAddress) (err error) {
|
|
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
|
|
}
|
|
return err
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId int64, addressId int64) (err error) {
|
|
|
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
err = tx.Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1 and `address_id` = ? ", userId, addressId).
|
|
UpdateColumn("ltime", now.AddDate(250, 0, 0)).
|
|
UpdateColumn("utime", now).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tx.Where(" `user_id` = ? and `status` = 1 and `address_id` != ? and `ltime` > ? ", userId, addressId, now.AddDate(10, 0, 0)).
|
|
UpdateColumn("ltime", now).Error
|
|
if err != nil {
|
|
logx.Error(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (a *FsAddressModel) DeleteOne(ctx context.Context, addressId int64, userId int64) (err error) {
|
|
err = a.db.WithContext(ctx).Model(&FsAddress{}).
|
|
Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, userId, 1).
|
|
UpdateColumn("status", 0).Error
|
|
return err
|
|
}
|