You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.5 KiB

package service
import (
"encoding/json"
"errors"
"fmt"
"strings"
"gorm.io/gorm"
"shop-api/global"
"shop-api/model"
"shop-api/model/request"
)
func GetTbGoods(info request.Goods) (error, model.TbGoodsDetail) {
var (
err error
result model.TbGoodsDetail
)
if info.ID == 0 && info.SpuNo == "" {
err = errors.New("参数有误")
return err, result
}
db := global.MG_DB.Table("tb_goods").Select("id,spu_no,category_id,title,title_eng,images,content,content_text,list_price,retail_price,price_min,price_max,tags,online").
Preload("Specs", func(db *gorm.DB) *gorm.DB {
return db.Select("id,goods_id,specs,sku_no,stock,price,image,goods_no").Where("status = 1")
})
if info.ID != 0 {
db = db.Where("id = ?", info.ID)
}
if info.SpuNo != "" {
db = db.Where("spu_no = ?", info.SpuNo)
}
err = db.First(&result).Error
if err != nil {
return err, result
}
attributeValues := make(map[uint]*model.TbAttributeValueJson)
for i := 0; i < len(result.Specs); i++ {
err = json.Unmarshal([]byte(result.Specs[i].Specs), &result.Specs[i].Attributes)
if err != nil {
return err, result
}
if i == 0 {
var attributes []uint
for j := 0; j < len(result.Specs[i].Attributes); j++ {
attributes = append(attributes, result.Specs[i].Attributes[j].AttributeId)
}
orderSql := "FIELD(id"
for _, attribute := range attributes {
orderSql += fmt.Sprintf(",'%v'", attribute)
}
orderSql += ")"
err = global.MG_DB.Model(&model.TbAttribute{}).Select("id,`name`").Where("id IN (?)", attributes).Order(orderSql).Find(&result.Attributes).Error
if err != nil {
return err, result
}
}
for _, attribute := range result.Specs[i].Attributes {
if _, ok := attributeValues[attribute.AttributeId]; ok {
if !strings.Contains(attributeValues[attribute.AttributeId].Str, attribute.Value) {
attributeValues[attribute.AttributeId].Str += attribute.Value + ","
attributeValues[attribute.AttributeId].Items = append(attributeValues[attribute.AttributeId].Items, model.TbAttributeValueItem{Name: attribute.Value, Image: result.Specs[i].Image})
}
} else {
attributeValues[attribute.AttributeId] = &model.TbAttributeValueJson{Items: []model.TbAttributeValueItem{{Name: attribute.Value, Image: result.Specs[i].Image}}, Str: attribute.Value + ","}
}
}
}
for i := 0; i < len(result.Attributes); i++ {
if val, ok := attributeValues[result.Attributes[i].ID]; ok {
result.Attributes[i].Values = val.Items
}
}
_, result.Breadcrumb = GetTbCategoryBreadcrumb(result.CategoryId) // 获取分类面包屑
return nil, result
}
func GetTbGoodsWithMission(uuid string, info request.Goods) (error, model.TbGoodsDetail) {
var (
err error
result model.TbGoodsDetail
)
if info.ID == 0 && info.SpuNo == "" {
err = errors.New("参数有误")
return err, result
}
db := global.MG_DB.Table("tb_goods").Select("id,spu_no,category_id,title,title_eng,images,content,content_text,list_price,retail_price,price_min,price_max,tags,online").
Preload("Specs", func(db *gorm.DB) *gorm.DB {
return db.Select("id,goods_id,specs,sku_no,stock,price,image").Where("status = 1")
})
if info.ID != 0 {
db = db.Where("id = ?", info.ID)
}
if info.SpuNo != "" {
db = db.Where("spu_no = ?", info.SpuNo)
}
err = db.First(&result).Error
if err != nil {
return err, result
}
attributeValues := make(map[uint]*model.TbAttributeValueJson)
for i := 0; i < len(result.Specs); i++ {
err = json.Unmarshal([]byte(result.Specs[i].Specs), &result.Specs[i].Attributes)
if err != nil {
return err, result
}
if i == 0 {
var attributes []uint
for j := 0; j < len(result.Specs[i].Attributes); j++ {
attributes = append(attributes, result.Specs[i].Attributes[j].AttributeId)
}
orderSql := "FIELD(id"
for _, attribute := range attributes {
orderSql += fmt.Sprintf(",'%v'", attribute)
}
orderSql += ")"
err = global.MG_DB.Model(&model.TbAttribute{}).Select("id,`name`").Where("id IN (?)", attributes).Order(orderSql).Find(&result.Attributes).Error
if err != nil {
return err, result
}
}
for _, attribute := range result.Specs[i].Attributes {
if _, ok := attributeValues[attribute.AttributeId]; ok {
if !strings.Contains(attributeValues[attribute.AttributeId].Str, attribute.Value) {
attributeValues[attribute.AttributeId].Str += attribute.Value + ","
attributeValues[attribute.AttributeId].Items = append(attributeValues[attribute.AttributeId].Items, model.TbAttributeValueItem{Name: attribute.Value, Image: result.Specs[i].Image})
}
} else {
attributeValues[attribute.AttributeId] = &model.TbAttributeValueJson{Items: []model.TbAttributeValueItem{{Name: attribute.Value, Image: result.Specs[i].Image}}, Str: attribute.Value + ","}
}
}
}
for i := 0; i < len(result.Attributes); i++ {
if val, ok := attributeValues[result.Attributes[i].ID]; ok {
result.Attributes[i].Values = val.Items
}
}
_, result.Breadcrumb = GetTbCategoryBreadcrumb(result.CategoryId) // 获取分类面包屑
result.CollectStatus = getGoodsCollectStatus(uuid, info.SpuNo)
CreateGoodsVisit(info.ID, uuid,info.ClaimNo)
return nil, result
}
// 统计商品总销售量
func updateTbGoodsSalesByGoodsId(goodsId uint, count int) {
_ = global.MG_DB.Model(&model.TbGoods{}).Where("id = ?", goodsId).UpdateColumn("sales", gorm.Expr("sales + ?", count)).Error
}
func CreateGoodsVisit(goodsId uint, uuid,claimNo string) error {
err := global.MG_DB.Create(&model.GoodsVisit{
GoodsID: goodsId,
UserID: uuid,
ClaimNo: claimNo,
}).Error
return err
}