Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop
This commit is contained in:
@@ -1,29 +1,28 @@
|
||||
package gmodel
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// fs_address 用户地址表
|
||||
type FsAddress struct {
|
||||
AddressId int64 `gorm:"primary_key;default:0;auto_increment;" json:"address_id"` //
|
||||
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户ID
|
||||
AddressName *string `gorm:"default:'';" json:"address_name"` //
|
||||
FirstName *string `gorm:"default:'';" json:"first_name"` // FirstName
|
||||
LastName *string `gorm:"default:'';" json:"last_name"` // LastName
|
||||
Mobile *string `gorm:"default:'';" json:"mobile"` // 手机号码
|
||||
Street *string `gorm:"default:'';" json:"street"` // 街道
|
||||
Suite *string `gorm:"default:'';" json:"suite"` // 房号
|
||||
City *string `gorm:"default:'';" json:"city"` // 城市
|
||||
State *string `gorm:"default:'';" json:"state"` //
|
||||
Country *string `gorm:"default:'';" json:"country"` //
|
||||
ZipCode *string `gorm:"default:'';" json:"zip_code"` //
|
||||
Status *int64 `gorm:"default:0;" json:"status"` // 1正常 0异常
|
||||
IsDefault *int64 `gorm:"index;default:0;" json:"is_default"` // 1默认地址,0非默认地址
|
||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // 创建时间
|
||||
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // 更新时间
|
||||
Ltime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"ltime"` // 上次被使用的时间
|
||||
AddressId int64 `gorm:"primary_key;default:0;auto_increment;" json:"address_id"` //
|
||||
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户ID
|
||||
FirstName *string `gorm:"default:'';" json:"first_name"` // FirstName
|
||||
LastName *string `gorm:"default:'';" json:"last_name"` // LastName
|
||||
Mobile *string `gorm:"default:'';" json:"mobile"` // 手机号码
|
||||
Street *string `gorm:"default:'';" json:"street"` // 街道
|
||||
Suite *string `gorm:"default:'';" json:"suite"` // 房号
|
||||
City *string `gorm:"default:'';" json:"city"` // 城市
|
||||
State *string `gorm:"default:'';" json:"state"` //
|
||||
Country *string `gorm:"default:'';" json:"country"` //
|
||||
ZipCode *string `gorm:"default:'';" json:"zip_code"` //
|
||||
Status *int64 `gorm:"default:0;" json:"status"` // 1正常 0异常
|
||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // 创建时间
|
||||
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // 更新时间
|
||||
Ltime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"ltime"` // 上次被使用的时间
|
||||
}
|
||||
type FsAddressModel struct {
|
||||
db *gorm.DB
|
||||
|
||||
@@ -26,22 +26,21 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res
|
||||
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
||||
now := time.Now().UTC()
|
||||
result = &FsAddress{
|
||||
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,
|
||||
Ctime: &now,
|
||||
Utime: &now,
|
||||
Ltime: &now,
|
||||
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,
|
||||
|
||||
Ctime: &now,
|
||||
Utime: &now,
|
||||
Ltime: &now,
|
||||
}
|
||||
|
||||
// lastOne := &FsAddress{}
|
||||
@@ -85,16 +84,23 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i
|
||||
|
||||
err = a.db.WithContext(ctx).Model(&FsAddress{}).Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
logx.Info("address_id:", addressId, " set default ")
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId).
|
||||
err = tx.Where("`user_id` = ? and `status` = 1 and `address_id` = ? ", userId, addressId).Take(nil).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.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` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.AddDate(10, 0, 0)).
|
||||
err = tx.Model(&FsAddress{}).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)
|
||||
|
||||
@@ -2,6 +2,11 @@ package gmodel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// 阶梯价结构
|
||||
@@ -152,3 +157,72 @@ func (d *FsProductModel3dModel) FindOneByProductIdSizeIdTag(ctx context.Context,
|
||||
err = db.Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (d *FsProductModel3dModel) GetAllByProductIdsTags(ctx context.Context, productIds []int64, tags []int, fields ...string) (resp []FsProductModel3d, err error) {
|
||||
if len(productIds) == 0 || len(tags) == 0 {
|
||||
return
|
||||
}
|
||||
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).
|
||||
Where("`product_id` in (?) and `tag` in (?) and `status` = ?", productIds, tags, 1).
|
||||
Order("sort DESC")
|
||||
if len(fields) != 0 {
|
||||
db = db.Select(fields[0])
|
||||
}
|
||||
err = db.Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// 获取每个产品最低价格
|
||||
func (d *FsProductModel3dModel) GetProductMinPrice(ctx context.Context, productIds []int64, mapProductMinPrice map[int64]int64) error {
|
||||
//获取产品模型价格列表
|
||||
modelList, err := d.GetAllByProductIdsTags(ctx, productIds, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,product_id,price,tag,part_id,step_price")
|
||||
if err != nil {
|
||||
return errors.New("failed to get model list")
|
||||
}
|
||||
mapModelMinPrice := make(map[int64]int64)
|
||||
//每个模型/配件存储最小价格
|
||||
for _, modelInfo := range modelList {
|
||||
switch *modelInfo.Tag {
|
||||
case constants.TAG_MODEL: //模型
|
||||
if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 {
|
||||
mapModelMinPrice[modelInfo.Id] = 0
|
||||
continue
|
||||
}
|
||||
var stepPrice StepPriceJsonStruct
|
||||
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
||||
return errors.New(fmt.Sprintf("failed to parse model step price:%d", modelInfo.Id))
|
||||
}
|
||||
lenRange := len(stepPrice.PriceRange)
|
||||
if lenRange == 0 {
|
||||
mapModelMinPrice[modelInfo.Id] = 0
|
||||
continue
|
||||
}
|
||||
sort.SliceStable(stepPrice.PriceRange, func(i, j int) bool {
|
||||
return stepPrice.PriceRange[i].Price > stepPrice.PriceRange[j].Price
|
||||
})
|
||||
mapModelMinPrice[modelInfo.Id] = stepPrice.PriceRange[lenRange-1].Price
|
||||
case constants.TAG_PARTS: //配件
|
||||
mapModelMinPrice[modelInfo.Id] = *modelInfo.Price
|
||||
}
|
||||
}
|
||||
//给产品存储最小价格
|
||||
for _, v := range modelList {
|
||||
if *v.Tag != constants.TAG_MODEL {
|
||||
continue
|
||||
}
|
||||
itemPrice := mapModelMinPrice[v.Id]
|
||||
if *v.PartId > 0 {
|
||||
if fittingPrice, ok := mapModelMinPrice[*v.PartId]; ok {
|
||||
itemPrice += fittingPrice
|
||||
}
|
||||
}
|
||||
if minPrice, ok := mapProductMinPrice[*v.ProductId]; ok {
|
||||
if itemPrice < minPrice {
|
||||
mapProductMinPrice[*v.ProductId] = itemPrice
|
||||
}
|
||||
continue
|
||||
}
|
||||
mapProductMinPrice[*v.ProductId] = itemPrice
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user