2023-06-16 07:11:37 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-27 03:48:15 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fusenapi/constants"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-06-16 07:11:37 +00:00
|
|
|
)
|
|
|
|
|
2023-09-25 09:20:44 +00:00
|
|
|
// 阶梯价结构
|
|
|
|
type StepPriceJsonStruct struct {
|
|
|
|
PriceRange []struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
Price int64 `json:"price"`
|
|
|
|
EndQuantity int64 `json:"end_quantity"`
|
|
|
|
StartQuantity int64 `json:"start_quantity"`
|
|
|
|
} `json:"price_range"`
|
2023-09-25 10:44:47 +00:00
|
|
|
MinBuyUnitsNum int64 `json:"min_buy_units_num"`
|
2023-09-25 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:26:44 +00:00
|
|
|
func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProductModel3d, err error) {
|
2023-09-26 03:10:41 +00:00
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? and `status` =? ", id, 1)
|
2023-09-12 09:26:44 +00:00
|
|
|
if len(fields) > 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Take(&resp).Error
|
2023-06-20 09:28:28 +00:00
|
|
|
return resp, err
|
2023-06-19 06:47:54 +00:00
|
|
|
}
|
2023-07-21 04:27:37 +00:00
|
|
|
func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, orderBy string, fields ...string) (resp []FsProductModel3d, err error) {
|
2023-06-16 07:11:37 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-06-21 04:11:43 +00:00
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1)
|
|
|
|
if len(fields) > 0 {
|
|
|
|
db = db.Select(fields[0])
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-07-21 04:27:37 +00:00
|
|
|
if orderBy != "" {
|
|
|
|
db = db.Order(orderBy)
|
|
|
|
}
|
2023-06-21 04:11:43 +00:00
|
|
|
err = db.Find(&resp).Error
|
|
|
|
return resp, err
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-06-27 09:04:58 +00:00
|
|
|
func (d *FsProductModel3dModel) GetAllByIdsWithoutStatus(ctx context.Context, ids []int64, fields ...string) (resp []FsProductModel3d, err error) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-09-26 03:10:41 +00:00
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1)
|
2023-06-27 09:04:58 +00:00
|
|
|
if len(fields) > 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Find(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-09-18 04:31:51 +00:00
|
|
|
func (d *FsProductModel3dModel) GetAllByIdsTag(ctx context.Context, ids []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) {
|
2023-06-16 07:11:37 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-09-18 04:31:51 +00:00
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ? and `tag` = ?", ids, 1, tag)
|
|
|
|
if len(fields) > 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Find(&resp).Error
|
2023-06-21 04:11:43 +00:00
|
|
|
return resp, err
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-06-25 09:11:42 +00:00
|
|
|
|
|
|
|
type Get3dModelsByParamReq struct {
|
|
|
|
Tag int64
|
|
|
|
ProductId int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3dModelsByParamReq) (resp []FsProductModel3d, err error) {
|
|
|
|
if req.ProductId == 0 && req.Tag == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`status` = ?", 1)
|
|
|
|
if req.ProductId > 0 {
|
|
|
|
db = db.Where("`product_id` =? ", req.ProductId)
|
|
|
|
}
|
|
|
|
if req.Tag > 0 {
|
|
|
|
db = db.Where("`tag` =? ", req.Tag)
|
|
|
|
}
|
|
|
|
err = db.Find(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-06-26 08:53:36 +00:00
|
|
|
func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error {
|
2023-09-26 03:10:41 +00:00
|
|
|
return d.db.WithContext(ctx).Where("`id` = ? and `status` =? ", id, 1).Updates(&data).Error
|
2023-06-26 08:53:36 +00:00
|
|
|
}
|
2023-07-14 08:48:04 +00:00
|
|
|
func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) {
|
2023-07-04 08:48:56 +00:00
|
|
|
if len(sizeIds) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-09-26 03:10:41 +00:00
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ? and `status` = ?", sizeIds, tag, 1)
|
2023-07-14 08:48:04 +00:00
|
|
|
if len(fields) != 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Find(&resp).Error
|
2023-07-04 08:48:56 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
func (d *FsProductModel3dModel) GetAll(ctx context.Context) (resp []FsProductModel3d, err error) {
|
2023-09-26 03:10:41 +00:00
|
|
|
err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`status` = ?", 1).Find(&resp).Error
|
2023-07-04 08:48:56 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
2023-07-20 03:08:35 +00:00
|
|
|
|
|
|
|
type GetGroupPartListByProductIdsRsp struct {
|
|
|
|
PartList string `json:"part_list"`
|
|
|
|
ProductId int64 `json:"product_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FsProductModel3dModel) GetGroupPartListByProductIds(ctx context.Context, productIds []int64) (resp []GetGroupPartListByProductIdsRsp, err error) {
|
|
|
|
if len(productIds) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-09-26 03:10:41 +00:00
|
|
|
err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`product_id` in(?) and `status` =? ", productIds, 1).
|
2023-07-20 03:08:35 +00:00
|
|
|
Select("product_id,group_concat(part_list) as part_list").
|
|
|
|
Group("product_id").Find(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-08-08 04:22:15 +00:00
|
|
|
func (d *FsProductModel3dModel) FindOneJoinSize(ctx context.Context, productId int64) (resp FsProductModel3d, err error) {
|
|
|
|
err = d.db.WithContext(ctx).Table(d.name+"as m").Joins("left join fs_product_size as s on m.size_id = s.id").
|
|
|
|
Select("m.*").
|
|
|
|
Where("m.product_id = ?", productId).
|
|
|
|
Where("(s.status= ? and m.tag = ?)", 1, 1).
|
|
|
|
Order("s.sort ASC").Take(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-08-11 10:59:03 +00:00
|
|
|
|
|
|
|
func (d *FsProductModel3dModel) GetOneBySizeIdTag(ctx context.Context, sizeId int64, tag int64, fields ...string) (resp *FsProductModel3d, err error) {
|
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).
|
|
|
|
Where("`size_id` = ? and `tag` = ? and `status` = ?", sizeId, tag, 1).
|
|
|
|
Order("sort DESC")
|
|
|
|
if len(fields) != 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Take(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-09-25 09:20:44 +00:00
|
|
|
|
|
|
|
func (d *FsProductModel3dModel) GetAllByProductIdTag(ctx context.Context, productId int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) {
|
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).
|
|
|
|
Where("`product_id` = ? and `tag` = ? and `status` = ?", productId, tag, 1).
|
|
|
|
Order("sort DESC")
|
|
|
|
if len(fields) != 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Find(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-09-26 03:40:27 +00:00
|
|
|
func (d *FsProductModel3dModel) FindOneByProductIdSizeIdTag(ctx context.Context, productId, sizeId, tag int64, fields ...string) (resp *FsProductModel3d, err error) {
|
|
|
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).
|
|
|
|
Where("`product_id` = ? and `size_id` = ? and `tag` = ? and `status` = ?", productId, sizeId, tag, 1).
|
|
|
|
Order("sort DESC")
|
|
|
|
if len(fields) != 0 {
|
|
|
|
db = db.Select(fields[0])
|
|
|
|
}
|
|
|
|
err = db.Take(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
2023-09-27 03:13:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-09-27 03:48:15 +00:00
|
|
|
|
|
|
|
// 获取每个产品最低价格
|
|
|
|
func (d *FsProductModel3dModel) GetProductMinPrice(ctx context.Context, productIds []int64) (productMinPrice map[int64]int64, err 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
|
|
|
|
}
|
|
|
|
mapModelMinPrice := make(map[int64]int64)
|
|
|
|
//每个模型/配件存储最小价格
|
|
|
|
for _, modelInfo := range modelList {
|
|
|
|
switch *modelInfo.Tag {
|
|
|
|
case constants.TAG_MODEL: //模型
|
|
|
|
if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("model step price is not set:%d", modelInfo.Id))
|
|
|
|
}
|
|
|
|
var stepPrice StepPriceJsonStruct
|
|
|
|
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return nil, errors.New(fmt.Sprintf("failed to parse model step price:%d", modelInfo.Id))
|
|
|
|
}
|
|
|
|
lenRange := len(stepPrice.PriceRange)
|
|
|
|
if lenRange == 0 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("the count of step price is 0:%d", modelInfo.Id))
|
|
|
|
}
|
|
|
|
mapModelMinPrice[modelInfo.Id] = stepPrice.PriceRange[lenRange-1].Price
|
|
|
|
case constants.TAG_PARTS: //配件
|
|
|
|
mapModelMinPrice[modelInfo.Id] = *modelInfo.Price
|
|
|
|
}
|
|
|
|
}
|
|
|
|
productMinPrice = make(map[int64]int64)
|
|
|
|
//给产品存储最小价格
|
|
|
|
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 := productMinPrice[*v.ProductId]; ok {
|
|
|
|
if itemPrice < minPrice {
|
|
|
|
productMinPrice[*v.ProductId] = itemPrice
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
productMinPrice[*v.ProductId] = itemPrice
|
|
|
|
}
|
|
|
|
return productMinPrice, nil
|
|
|
|
}
|