fusenapi/model/gmodel/fs_address_logic.go

147 lines
3.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-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-10-08 03:55:06 +00:00
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []*FsAddressWithDefault, err error) {
2023-10-10 03:21:43 +00:00
resp = make([]*FsAddressWithDefault, 0)
2023-10-08 04:04:02 +00:00
var dbresp []*FsAddress
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = 1", userId).Order("`ltime` DESC").Find(&dbresp).Error
2023-06-19 12:10:32 +00:00
if err != nil {
return nil, err
}
2023-10-08 03:55:06 +00:00
now := time.Now().UTC().AddDate(10, 0, 0).Unix()
2023-10-08 04:04:02 +00:00
for _, r := range dbresp {
rd := &FsAddressWithDefault{
FsAddress: r,
}
2023-10-08 03:55:06 +00:00
if r.Ltime.UTC().Unix() > now {
2023-10-08 04:04:02 +00:00
rd.IsDefault = 1
2023-10-08 03:55:06 +00:00
} else {
2023-10-08 04:04:02 +00:00
rd.IsDefault = 0
2023-10-08 03:55:06 +00:00
}
2023-10-08 04:04:02 +00:00
resp = append(resp, rd)
2023-10-08 03:55:06 +00:00
}
2023-06-19 12:10:32 +00:00
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
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-08 03:23:22 +00:00
func (a *FsAddressModel) SettingUserDefaultAddress(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-08 03:46:51 +00:00
var updates = map[string]interface{}{
"ltime": now,
"utime": now,
}
2023-09-25 10:42:45 +00:00
2023-10-08 03:23:22 +00:00
if isDefault == 1 {
err = tx.Where("`user_id` = ? and `status` = 1 and `ltime` > ? and `address_id` != ?", userId, now.AddDate(10, 0, 0), addressId).
2023-10-08 03:46:51 +00:00
UpdateColumns(updates).Error
2023-10-08 03:23:22 +00:00
if err != nil {
return err
}
2023-10-08 03:46:51 +00:00
updates["ltime"] = now.AddDate(250, 0, 0)
2023-09-27 03:55:21 +00:00
}
2023-10-08 03:38:10 +00:00
tr := tx.Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
2023-10-08 03:46:51 +00:00
UpdateColumns(updates)
2023-10-07 08:59:52 +00:00
2023-10-08 03:23:22 +00:00
err = tr.Error
2023-10-07 09:58:24 +00:00
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-10-08 03:46:51 +00:00
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-08 03:46:51 +00:00
var updates = map[string]interface{}{
"ltime": now,
"utime": now,
}
2023-10-07 09:58:24 +00:00
err = tx.Where("`address_id` = ? and `user_id` = ? and `status` = 1", addressId, userId).
2023-10-08 03:46:51 +00:00
UpdateColumns(updates).Error
2023-09-28 08:54:10 +00:00
if err != nil {
return err
}
return nil
})
return err
}