|
|
package shopping
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
|
"github.com/golangkit/formatime"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"math/rand"
|
|
|
goods3 "recook/internal/api/mobile/goods"
|
|
|
"recook/internal/dbc"
|
|
|
"recook/internal/model/goods"
|
|
|
"recook/internal/v2/model/keywords"
|
|
|
goods2 "recook/internal/v2/model/recook/goods"
|
|
|
)
|
|
|
|
|
|
type logic struct {
|
|
|
}
|
|
|
|
|
|
var LogicShoppingCart = &logic{}
|
|
|
|
|
|
type queryCategoryGoodsListResp struct {
|
|
|
ID uint `json:"id"`
|
|
|
GoodsName string `json:"goodsName"`
|
|
|
BrandImg string `json:"brandImg"`
|
|
|
BrandName string `json:"brandName"`
|
|
|
BrandId uint `json:"brandId"`
|
|
|
Description string `json:"description"`
|
|
|
Inventory uint `json:"inventory"` // 总库存
|
|
|
SalesVolume uint `json:"salesVolume"` // 总销量
|
|
|
MainPhotoURL string `json:"mainPhotoUrl"` // 主图链接
|
|
|
PromotionName string `json:"promotionName"`
|
|
|
OriginalPrice decimal.Decimal `json:"originalPrice"`
|
|
|
DiscountPrice decimal.Decimal `json:"discountPrice"`
|
|
|
Commission decimal.Decimal `json:"commission"`
|
|
|
Tags []string `json:"tags"` // 为产品打上标签,比如 "新人特惠" "限时特卖"
|
|
|
Percent uint `json:"percent"` // 活动的%-1代表不是活动
|
|
|
StartTime formatime.Second `json:"startTime"` // 开始时间
|
|
|
EndTime formatime.Second `json:"endTime"` // 结束时间
|
|
|
Coupon decimal.Decimal `gorm:"coupon" json:"coupon"`
|
|
|
IsImport int `json:"isImport"` //是否进口商品
|
|
|
Storehouse int `json:"storehouse"` //进口商品仓库
|
|
|
IsFerme int `json:"isFerme"` //是否包税
|
|
|
HasCoin bool `json:"hasCoin"` //是否支持瑞币抵扣
|
|
|
HasBalance bool `json:"hasBalance"` //是否支持余额支付
|
|
|
Living live `json:"living"` //直播中
|
|
|
GysId uint `json:"gys_id"` //供应商id
|
|
|
SpecIcon []string `json:"spec_icon"` //特卖活动图标数组
|
|
|
CountryIcon string `json:"country_icon"` //进口国家图标
|
|
|
}
|
|
|
type live struct {
|
|
|
Status int `json:"status"`
|
|
|
RoomId int `json:"room_id"`
|
|
|
}
|
|
|
|
|
|
//ViewLike 找相似商品
|
|
|
func (l logic) ViewLike(id uint, userID uint, isSale bool) []goods3.QueryCategoryGoodsListResp {
|
|
|
//相似商品
|
|
|
if isSale {
|
|
|
return nil
|
|
|
}
|
|
|
var likeList []int
|
|
|
dbc.DB.Table((&goods2.Associate{}).TableName()).Where("goods_id=?", id).Pluck("other_goods_id", &likeList)
|
|
|
var newLike []int
|
|
|
num := rand.Perm(len(likeList))
|
|
|
for _, v := range num {
|
|
|
newLike = append(newLike, likeList[v])
|
|
|
if len(newLike) == 10 {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var goodsList []goods.Information
|
|
|
dbc.DB.Table((&goods.Information{}).TableName()).Where("publish_status = 1").Where("id in(?)", newLike).Find(&goodsList)
|
|
|
return goods3.GetGoodsRespByInfoList(goodsList, userID, false)
|
|
|
}
|
|
|
|
|
|
//ViewLikeMayBe 可能喜欢的商品
|
|
|
func (l logic) ViewLikeMayBe(userId uint, isSale bool) []goods3.QueryCategoryGoodsListResp {
|
|
|
//浏览过的商品
|
|
|
var kw []string
|
|
|
mysql2.Db.Table((&keywords.RecookUserKeywordsModel{}).TableName()).Where("user_id=?", userId).Limit(10).Pluck("keywords", &kw)
|
|
|
where := mysql2.Db.Where("publish_status = 1")
|
|
|
if isSale {
|
|
|
where = mysql2.Db.Where("sale_publish = 1 and is_sale=1")
|
|
|
}
|
|
|
for _, v := range kw {
|
|
|
where = where.Where("goods_name like ?", fmt.Sprintf("%%%v%%", v))
|
|
|
}
|
|
|
var goodsList []goods.Information
|
|
|
if len(kw) > 0 {
|
|
|
mysql2.Db.Table((&goods.Information{}).TableName()).Where(where).Find(&goodsList)
|
|
|
}
|
|
|
fmt.Println("sql:", len(goodsList))
|
|
|
var newGoodsList []goods.Information
|
|
|
if len(goodsList) >= 20 {
|
|
|
num := rand.Perm(len(goodsList))
|
|
|
num1 := num[:20]
|
|
|
for _, v := range num1 {
|
|
|
newGoodsList = append(newGoodsList, goodsList[v])
|
|
|
}
|
|
|
}
|
|
|
return goods3.GetGoodsRespByInfoList(newGoodsList, userId, isSale)
|
|
|
}
|
|
|
|
|
|
//func getFillList(c *gin.Context, goodsList []goods.Information, volSort string) []goods.Information {
|
|
|
// // 显示记录太少
|
|
|
// // 1.用浏览历史记录填充
|
|
|
// // 2.用T1 - T4商品记录填充
|
|
|
//
|
|
|
// uid, _ := strconv.Atoi(c.Request.Header.Get("X-Recook-ID"))
|
|
|
// deviceType := cache.GetDeviceType(c)
|
|
|
// recookUserLoginModel := user.RecookUserLoginModel{}
|
|
|
// tokenInfo := recookUserLoginModel.FindByIdAndDeviceType(uint(uid), deviceType)
|
|
|
//
|
|
|
// // 购买记录
|
|
|
// s11 := mysql2.Db.Select("goods_id").Table("recook_order_goods_detail").
|
|
|
// Where("user_id = ?", tokenInfo.UserId)
|
|
|
//
|
|
|
// // 已经存在的数据
|
|
|
// var id []uint
|
|
|
// for _, v := range goodsList {
|
|
|
// id = append(id, v.ID)
|
|
|
// }
|
|
|
//
|
|
|
// add := 20 - len(goodsList)
|
|
|
// // 浏览记录
|
|
|
// sh1 := mysql2.Db.Table("recook_app_user_history").Select("MAX(id)").
|
|
|
// Where("user_id = ?", tokenInfo.UserId).
|
|
|
// Where("goods_id not in (?)", s11).
|
|
|
// Group("goods_id")
|
|
|
//
|
|
|
// if len(id) != 0 {
|
|
|
// sh1 = sh1.Where("goods_id not in (?)", id)
|
|
|
// }
|
|
|
//
|
|
|
// var args []uint
|
|
|
// mysql2.Db.Table("recook_app_user_history").
|
|
|
// Select("goods_id").Where("id in (?)", sh1).Order("id DESC").Pluck("goods_id", &args)
|
|
|
//
|
|
|
// var history []goods.Information
|
|
|
// query := mysql2.Db.Table("recook_goods_info").Clauses(clause.OrderBy{
|
|
|
// Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{args}, WithoutParentheses: true}}).
|
|
|
// Where("id in (?) AND publish_status = 1", args)
|
|
|
//
|
|
|
// if volSort != "" {
|
|
|
// query = query.Order("sales_volume " + volSort)
|
|
|
// }
|
|
|
// query.Limit(add).Find(&history)
|
|
|
//
|
|
|
// goodsList = append(goodsList, history...)
|
|
|
// id = []uint{}
|
|
|
// for _, v := range goodsList {
|
|
|
// id = append(id, v.ID)
|
|
|
// }
|
|
|
//
|
|
|
// if len(goodsList) < 20 {
|
|
|
// // 检索 t1 - t4
|
|
|
// add = 20 - len(goodsList)
|
|
|
// s1 := mysql2.Db.Table("recook_activity_info").Select("MAX(id)").
|
|
|
// Where("is_active = 1 AND type > 0").Group("type")
|
|
|
//
|
|
|
// s2 := mysql2.Db.Table("recook_activity_goods").
|
|
|
// Select("goods_id").
|
|
|
// Where("activity_id IN (?)", s1).
|
|
|
// Where("goods_id NOT IN (?)", s11)
|
|
|
//
|
|
|
// if len(id) != 0 {
|
|
|
// s2 = s2.Where("goods_id not in (?)", id)
|
|
|
// }
|
|
|
//
|
|
|
// var remain []goods.Information
|
|
|
// query = mysql2.Db.Table("recook_goods_info").
|
|
|
// Where("id in (?) AND publish_status = 1", s2)
|
|
|
//
|
|
|
// if volSort != "" {
|
|
|
// query = query.Order("sales_volume " + volSort)
|
|
|
// }
|
|
|
//
|
|
|
// query.Limit(add).Find(&remain)
|
|
|
//
|
|
|
// goodsList = append(goodsList, remain...)
|
|
|
// }
|
|
|
// return goodsList
|
|
|
//}
|
|
|
|
|
|
//func getGoodsRespByInfoList(goodsList []goods.Information) []queryCategoryGoodsListResp {
|
|
|
// list := make([]queryCategoryGoodsListResp, 0, 0)
|
|
|
// for _, v := range goodsList {
|
|
|
// var mainPhoto goods.MainPhoto
|
|
|
// dbc.DB.Select("url").First(&mainPhoto, "goods_id = ? AND is_master = 1", v.ID)
|
|
|
// gb := goods.Brand{}
|
|
|
// dbc.DB.First(&gb, "id = ?", v.BrandID)
|
|
|
// var sku goods.Sku
|
|
|
// dbc.DB.Select("SUM(inventory) AS inventory, "+
|
|
|
// "SUM(sales_volume) AS sales_volume, "+
|
|
|
// "MIN(original_price) AS original_price, "+
|
|
|
// "MIN(discount_price) AS discount_price, "+
|
|
|
// "MIN(commission) AS commission,"+
|
|
|
// "coupon").
|
|
|
// First(&sku, "goods_id = ?", v.ID)
|
|
|
// var inventory uint
|
|
|
// var percent uint
|
|
|
//
|
|
|
// var startTime = formatime.Second{}
|
|
|
// var endTime = formatime.Second{}
|
|
|
//
|
|
|
// inventory = sku.Inventory //sku库存,这个其实用不到的
|
|
|
// salesVolume := v.SalesVolume //这个用主产品的已卖
|
|
|
// discountPrice := sku.DiscountPrice //sku 价格
|
|
|
// commission := sku.Commission //sku佣金
|
|
|
//
|
|
|
// now := time.Now()
|
|
|
// var tags = make([]string, 0, 0)
|
|
|
// var promotionGoods promotion.Goods
|
|
|
// dbc.DB.Select("id, promotion_name, start_time, end_time").First(&promotionGoods, "goods_id = ? AND start_time <= ? AND end_time >= ?", v.ID, now, now)
|
|
|
// if promotionGoods.ID > 0 {
|
|
|
// var promotionSku promotion.Sku
|
|
|
// dbc.DB.Select("SUM(inventory) AS inventory, SUM(sales_volume) AS sales_volume, discount_price,commission").First(&promotionSku, "promotion_goods_id = ?", promotionGoods.ID)
|
|
|
//
|
|
|
// discountPrice = promotionSku.DiscountPrice //活动的价格
|
|
|
// commission = promotionSku.Commission //活动的佣金
|
|
|
// inventory = promotionSku.Inventory //活动的库存
|
|
|
// //salesVolume = promotionSku.SalesVolume //活动的销量
|
|
|
// //计算%
|
|
|
// percent = uint(promotionSku.SalesVolume / (inventory + promotionSku.SalesVolume) * 100)
|
|
|
// startTime = promotionGoods.StartTime
|
|
|
// endTime = promotionGoods.EndTime
|
|
|
// // 打上特卖标签
|
|
|
// tags = append(tags, "限时特卖")
|
|
|
// } else { // 再判断商品明天会不会参加活动
|
|
|
// dbc.DB.Select("id, promotion_name").First(&promotionGoods, "goods_id = ? AND start_time > ? ", v.ID, now)
|
|
|
// if promotionGoods.ID > 0 {
|
|
|
// var promotionSku promotion.Sku
|
|
|
// dbc.DB.Select("MIN(discount_price) AS discount_price, MIN(commission) AS commission").Find(&promotionSku, "promotion_goods_id = ?", promotionGoods.ID)
|
|
|
//
|
|
|
// //inventory = promotionSku.Inventory //活动的库存
|
|
|
// //salesVolume = promotionSku.SalesVolume //活动的销量
|
|
|
// //discountPrice = promotionSku.DiscountPrice //活动的价格
|
|
|
// //commission = promotionSku.Commission //活动的佣金
|
|
|
// startTime = promotionGoods.StartTime
|
|
|
// endTime = promotionGoods.EndTime
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
// // 查看是否是 新人特惠 商品
|
|
|
// if isMember, _ := cache.IsMemberOfNewerTehui(strconv.Itoa(int(v.ID))); isMember {
|
|
|
// tags = append(tags, "新人特惠")
|
|
|
// }
|
|
|
//
|
|
|
// recookGoodsInfoModel := &goods2.RecookGoodsInfoModel{}
|
|
|
// list = append(list, queryCategoryGoodsListResp{
|
|
|
// ID: v.ID,
|
|
|
// GoodsName: v.GoodsName,
|
|
|
// BrandName: gb.Name + "品牌馆",
|
|
|
// BrandImg: gb.LogoURL,
|
|
|
// BrandId: gb.ID,
|
|
|
// Description: v.Description,
|
|
|
// Inventory: inventory,
|
|
|
// SalesVolume: salesVolume,
|
|
|
// MainPhotoURL: mainPhoto.URL,
|
|
|
// PromotionName: promotionGoods.PromotionName,
|
|
|
// OriginalPrice: sku.OriginalPrice,
|
|
|
// DiscountPrice: discountPrice,
|
|
|
// Commission: commission,
|
|
|
// Tags: tags,
|
|
|
// Percent: percent, //百分比,-1不显示
|
|
|
// StartTime: startTime,
|
|
|
// EndTime: endTime,
|
|
|
// Coupon: sku.Coupon,
|
|
|
// IsImport: v.IsImport,
|
|
|
// Storehouse: v.Storehouse,
|
|
|
// IsFerme: v.IsFerme,
|
|
|
// HasCoin: recookGoodsInfoModel.HasCoin(v.Storehouse),
|
|
|
// HasBalance: recookGoodsInfoModel.HasBalance(v.Storehouse),
|
|
|
// GysId: v.VendorID,
|
|
|
// })
|
|
|
// }
|
|
|
// /*
|
|
|
// ****************************新增添加直播状态********************************************
|
|
|
// */
|
|
|
//
|
|
|
// for i, k := range list {
|
|
|
// var vo live
|
|
|
// dbc.DB.Table("recook_live_live_item").Where("main_goods_id=?", k.ID).Select("status,id as item_id").Order("id desc").First(&vo)
|
|
|
// if vo.Status == 1 {
|
|
|
// list[i].Living = live{
|
|
|
// Status: vo.Status,
|
|
|
// RoomId: vo.RoomId,
|
|
|
// }
|
|
|
//
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// /*
|
|
|
// 修改特卖要求,给商品添加特卖图标
|
|
|
// */
|
|
|
// var sp special_sale.SpecialSaleModel
|
|
|
// var spes special_sale.SpecialSaleAssociationModel
|
|
|
// var spess []special_sale.SpecialSaleAssociationModel
|
|
|
// var spc []special_sale.SpecialSaleModel
|
|
|
// //获取全部的特卖活动
|
|
|
// dbc.DB.Table(sp.TableName()).Find(&spc)
|
|
|
// //获取活动规则,活动商品或供应商
|
|
|
// fmt.Println("特卖活动规则", spc)
|
|
|
// for i, v := range list {
|
|
|
// var spList []string
|
|
|
// for _, k := range spc {
|
|
|
// if k.Type == 1 { //全部商品
|
|
|
// if k.GysId == v.GysId {
|
|
|
// if k.EndTime.Time.Unix() > time.Now().Unix() {
|
|
|
// spList = append(spList, k.Url)
|
|
|
// }
|
|
|
// }
|
|
|
// } else { //部分商品
|
|
|
// dbc.DB.Table(spes.TableName()).Where("special_sale_id=?", k.Id).Find(&spess)
|
|
|
// for _, j := range spess {
|
|
|
// if v.ID == j.GoodsId {
|
|
|
// if k.EndTime.Time.Unix() > time.Now().Unix() {
|
|
|
// spList = append(spList, k.Url)
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// list[i].SpecIcon = spList
|
|
|
// }
|
|
|
// list2 := []queryCategoryGoodsListResp{}
|
|
|
// num := rand.Perm(len(list))
|
|
|
// for _, v := range num {
|
|
|
// list2 = append(list2, list[v])
|
|
|
// }
|
|
|
//
|
|
|
// return list2
|
|
|
//}
|