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.

371 lines
12 KiB

package goods
import (
"fmt"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/domain"
"recook/internal/model/activity"
"recook/internal/model/goods"
"recook/internal/model/promotion"
goods2 "recook/internal/v2/model/recook/goods"
user2 "recook/internal/v2/model/recook/user"
"recook/tools"
"strconv"
"strings"
"time"
"git.oa00.com/go/mysql"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"github.com/shopspring/decimal"
"gorm.io/gorm/clause"
)
type queryPromotionGoodsParam struct {
TimeItemID uint `json:"timeItemID" validate:"required"`
UserID uint `json:"user_id"`
}
type promotionTimeItemResp struct {
promotion.TimeItem
ShowName string `json:"showName"`
}
type promotionReq struct {
IsSale bool `json:"is_sale"`
}
// QueryPromotionTimeItems 获取首页活动列表
func QueryPromotionTimeItems(c *gin.Context) {
var p promotionReq
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
list := make([]promotionTimeItemResp, 0)
var yesterdayPromInfo promotion.Information // 昨天的
today := time.Now()
d, _ := time.ParseDuration("-24h")
yesterday := today.Add(d)
dbc.DB.Select("id").First(&yesterdayPromInfo, "is_sale = ? AND start_date = ?", p.IsSale, yesterday.Format("2006-01-02"))
{
yesterdayTimeItems := make([]promotion.TimeItem, 0)
dbc.DB.Order("start_time asc").Find(&yesterdayTimeItems, "promotion_id = ?", yesterdayPromInfo.ID)
for i, _ := range yesterdayTimeItems {
showName := "昨日精选"
list = append(list, promotionTimeItemResp{
TimeItem: yesterdayTimeItems[i],
ShowName: showName,
})
}
}
var todayPromInfo promotion.Information // 今天的
fmt.Println(time.Now().Format("2006-01-02 15:04:06"))
dbc.DB.Select("id").First(&todayPromInfo, "is_sale=? AND start_date = ?", p.IsSale, time.Now().Format("2006-01-02"))
{
todayTimeItems := make([]promotion.TimeItem, 0)
dbc.DB.Order("start_time asc").Find(&todayTimeItems, "promotion_id = ?", todayPromInfo.ID)
length := len(todayTimeItems)
var nextPtr *promotion.TimeItem
now := time.Now().Unix()
for i, cur := range todayTimeItems {
nextPtr = nil
if i+2 <= length {
nextPtr = &todayTimeItems[i+1]
}
showName := ""
if now >= cur.StartTime.Time.Unix() && now < cur.EndTime.Time.Unix() { // 已经开始了
if nextPtr != nil && (now >= nextPtr.StartTime.Time.Unix() && now < nextPtr.EndTime.Time.Unix()) {
showName = "今日特推"
} else {
showName = "今日特推"
}
} else if cur.StartTime.Time.Unix() > now { // 没开始
showName = "今日特推"
} else {
showName = "今日特推"
}
list = append(list, promotionTimeItemResp{
TimeItem: todayTimeItems[i],
ShowName: showName,
})
}
}
var tomorrowPromInfo promotion.Information // 明天的
today = time.Now()
d, _ = time.ParseDuration("24h")
tomorrow := today.Add(d)
dbc.DB.Select("id").First(&tomorrowPromInfo, "is_sale=? AND start_date = ?", p.IsSale, tomorrow.Format("2006-01-02"))
{
tomorrowTimeItems := make([]promotion.TimeItem, 0)
dbc.DB.Order("start_time asc").Find(&tomorrowTimeItems, "promotion_id = ?", tomorrowPromInfo.ID)
for i, _ := range tomorrowTimeItems {
showName := "明日优选"
list = append(list, promotionTimeItemResp{
TimeItem: tomorrowTimeItems[i],
ShowName: showName,
})
}
}
back.Suc(c, "", &list)
}
type promotionList struct {
GoodsList []promotionGoods `json:"goodsList"`
ActivityList []activityList `json:"activityList"`
}
type activityList struct {
LogoUrl string `json:"logoUrl"`
TopUrl string `json:"topUrl"`
ActivityUrl string `json:"activityUrl"`
ActivitySortId uint `json:"activity_sort_id"`
}
type promotionGoods struct {
GoodsID uint `json:"goodsId"`
GoodsName string `json:"goodsName"`
BrandName string `json:"brandName"`
BrandImg string `json:"brandImg"`
BrandId uint `json:"brandId"`
Description string `json:"description"`
Price decimal.Decimal `json:"price"`
PrimePrice decimal.Decimal `json:"primePrice"` // 划线价格
PriceDesc string `json:"priceDesc"`
Commission decimal.Decimal `json:"commission"`
CommissionDesc string `json:"commissionDesc"`
Picture picture `json:"picture"`
Inventory uint `json:"inventory"`
InventoryDesc string `json:"inventoryDesc"`
TotalInventory uint `json:"totalInventory"` // 总库存
TotalInventoryDesc string `json:"totalInventoryDesc"`
TotalSalesVolume uint `json:"totalSalesVolume"` // 总销量
SalesVolumeDesc string `json:"salesVolumeDesc"`
StartTime formatime.Second `json:"startTime"`
EndTime formatime.Second `json:"endTime"`
Percentage decimal.Decimal `json:"percentage"`
PercentageDesc string `json:"percentageDesc"`
Coupon decimal.Decimal `json:"coupon"`
IsImport int `json:"isImport"` //是否进口商品
Storehouse int `json:"storehouse"` //进口商品仓库
IsFerme int `json:"isFerme"` //是否包税
Living live `json:"living"` //是否在直播
SpecialSale []string `json:"special_sale"` //特卖位图标
GysId uint `json:"gys_id"` //供应商id
CountryIcon string `json:"country_icon"`
SecKill SecKillDetail `json:"sec_kill"`
SalePrice decimal.Decimal `json:"sale_price"`
}
type picture struct {
URL string `gorm:"column:url" json:"url,omitempty"`
Width uint `gorm:"column:width" json:"width,omitempty"`
Height uint `gorm:"column:height" json:"height,omitempty"`
}
// QueryPromotionGoodsListYesterday 获取昨日的活动列表
func QueryPromotionGoodsListYesterday(c *gin.Context) {
var p queryPromotionGoodsParam
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var u user2.RecookUserInfoModel
dbc.DB.First(&u, "id = ?", p.UserID)
rate := decimal.Zero
{
switch u.Level {
case 1:
rate = decimal.NewFromFloat(0.5)
case 2:
rate = decimal.NewFromFloat(0.7)
}
}
nTime := time.Now()
yesTime := nTime.AddDate(0, 0, -1)
logDay := yesTime.Format("2006-01-02")
var promotionGoodsList []promotion.Goods
dbc.DB.Order("start_time desc").Find(&promotionGoodsList, `start_time like "`+logDay+`%"`)
fmt.Println(logDay)
gList := make([]promotionGoods, 0, 0)
for _, v := range promotionGoodsList {
if v.GoodsID > 0 {
gi := goods.Information{}
dbc.DB.First(&gi, "id = ? AND publish_status = 1", v.GoodsID)
gb := goods.Brand{}
dbc.DB.First(&gb, "id = ?", gi.BrandID)
if gi.ID == 0 {
continue
}
var pic picture
dbc.DB.Table((&goods.MainPhoto{}).TableName()).First(&pic, "goods_id = ? AND is_master = 1", v.GoodsID)
var promotionSku promotion.Sku
var sku goods.Sku
dbc.DB.First(&sku, "promotion_goods_id = ?", v.ID)
dbc.DB.Select("SUM(sales_volume) AS sales_volume, SUM(inventory) AS inventory, MIN(discount_price) AS discount_price, MIN(purchase_price) AS purchase_price, MIN(commission) AS commission").Find(&promotionSku, "promotion_goods_id = ?", v.ID)
salesVolume := 0
if sku.SalesVolume > 0 {
salesVolume = int(sku.SalesVolume)
}
// salesVolumeDesc := "已抢" + fmt.Sprintf("%d", salesVolume) + "件"
salesVolumeDesc := "累计已售" + fmt.Sprintf("%d", 1000+salesVolume) + "件"
if salesVolume == int(sku.Inventory) {
//salesVolumeDesc = "已抢光"
salesVolumeDesc = ""
}
//percentage := decimal.NewFromInt(int64(salesVolume)).Div(decimal.NewFromInt(int64(v.TotalInventory))).Round(2)
//
//percentageDesc := percentage.Mul(decimal.NewFromInt(100)).String() + "%"
inventoryDesc := "库存" + fmt.Sprintf("%d", sku.Inventory)
if sku.Inventory == 0 {
inventoryDesc = "已售罄"
}
var country goods2.RecookAbroadCountryModel
dbc.DB.Table(country.TableName()).First(&country, "id = ?", gi.Country)
var goodsSku goods.Sku
dbc.DB.Where("goods_id = ?", v.GoodsID).Find(&goodsSku)
cm := goodsSku.DiscountPrice.Sub(goodsSku.PurchasePrice).Mul(rate)
gList = append(gList, promotionGoods{
GoodsID: gi.ID,
GoodsName: gi.GoodsName,
BrandName: gb.Name + "品牌馆",
BrandImg: gb.LogoURL,
BrandId: gb.ID,
Description: v.Subtitle,
Price: goodsSku.DiscountPrice,
PrimePrice: goodsSku.OriginalPrice,
PriceDesc: goodsSku.DiscountPrice.String(),
Commission: cm,
CommissionDesc: "赚" + cm.String(),
Picture: pic,
Inventory: sku.Inventory,
InventoryDesc: inventoryDesc,
TotalInventory: v.TotalInventory,
TotalInventoryDesc: "限购" + fmt.Sprintf("%d", v.TotalInventory) + "件",
TotalSalesVolume: uint(salesVolume),
SalesVolumeDesc: salesVolumeDesc,
StartTime: v.StartTime,
EndTime: v.EndTime,
Percentage: decimal.Zero,
PercentageDesc: "",
Coupon: sku.Coupon,
IsImport: gi.IsImport,
Storehouse: gi.Storehouse,
IsFerme: gi.IsFerme,
GysId: gi.VendorID, //供应商id
CountryIcon: country.Icon,
})
}
}
//fmt.Println(promotionGoodsList)
back.Suc(c, "", &promotionGoodsList)
}
// QueryPromotionGoodsList 精品推荐
func QueryPromotionGoodsList(c *gin.Context) {
var p queryPromotionGoodsParam
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var u user2.RecookUserInfoModel
dbc.DB.First(&u, "id = ?", p.UserID)
var promotionGoodsList []promotion.Goods
dbc.DB.Order("`order` asc").Find(&promotionGoodsList, "promotion_time_item_id = ?", p.TimeItemID)
if len(promotionGoodsList) == 0 {
back.Suc(c, "", nil)
return
}
var p1 promotion.Information
dbc.DB.First(&p1, "id = ?", promotionGoodsList[0].PromotionID)
gList := make([]promotionGoods, 0)
aList := make([]activityList, 0)
ids := make([]interface{}, 0)
place := make([]string, 0)
for _, v := range promotionGoodsList {
if v.GoodsID > 0 {
ids = append(ids, v.GoodsID)
place = append(place, "?")
}
if v.ActivityID > 0 {
var activityInfo activity.Info
dbc.DB.First(&activityInfo, v.ActivityID)
aList = append(aList, activityList{
LogoUrl: activityInfo.LogoUrl,
TopUrl: activityInfo.TopUrl,
ActivityUrl: domain.GetCDN() + "/website/www/activity/t.html?id=" + strconv.Itoa(int(v.ActivityID)),
ActivitySortId: v.ActivitySortId,
})
}
}
var goodsList []goods.Information
field := fmt.Sprintf("FIELD(id, %s)", strings.Join(place, ","))
mysql.Db.Where("id in (?)", ids).Clauses(clause.OrderBy{
Expression: clause.Expr{SQL: field, Vars: ids, WithoutParentheses: true},
}).Find(&goodsList)
res := GetGoodsRespByInfoList(goodsList, p.UserID, p1.IsSale)
for _, v := range res {
gList = append(gList, promotionGoods{
GoodsID: v.ID,
GoodsName: v.GoodsName,
BrandName: v.BrandName,
BrandImg: v.BrandImg,
BrandId: v.BrandId,
Description: v.Description,
Price: v.DiscountPrice,
PrimePrice: v.OriginalPrice,
PriceDesc: v.DiscountPrice.String(),
Commission: v.Commission,
CommissionDesc: "赚" + v.Commission.String(),
Picture: picture{
URL: v.MainPhotoURL,
Width: 0,
Height: 0,
},
Inventory: v.Inventory,
InventoryDesc: "",
TotalInventory: v.Inventory,
TotalInventoryDesc: "",
TotalSalesVolume: uint(v.SalesVolume),
SalesVolumeDesc: "",
StartTime: v.StartTime,
EndTime: v.EndTime,
Percentage: decimal.Zero,
PercentageDesc: "",
Coupon: v.Coupon,
SalePrice: v.SalePrice,
})
}
back.Suc(c, "", &promotionList{
GoodsList: gList,
ActivityList: aList,
})
}