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/internal/v2/model/special_sale" "recook/tools" "strconv" "strings" "time" "github.com/gin-gonic/gin" "github.com/golangkit/formatime" "github.com/shopspring/decimal" ) 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"` } 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) //rate1 := decimal.NewFromFloat32(0.4) //rate2 := decimal.NewFromFloat32(0.1) //rate3 := decimal.Zero //if u.Level == 2 { // rate3 = decimal.NewFromFloat(0.2) //} //p.TimeItemID=1275 var promotionGoodsList []promotion.Goods dbc.DB.Order("`order` asc").Find(&promotionGoodsList, "promotion_time_item_id = ?", p.TimeItemID) gList := make([]promotionGoods, 0) aList := make([]activityList, 0) /* 秒杀部分 */ //获取当前时间段的秒杀goodsid //gmp := GetSecKillGoods() for _, v := range promotionGoodsList { if v.GoodsID > 0 { var 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 dbc.DB.Select("SUM(sales_volume) AS sales_volume, SUM(inventory) AS inventory, MIN(discount_price) AS discount_price, MIN(commission) AS commission").First(&promotionSku, "promotion_goods_id = ?", v.ID) salesVolume := 0 if promotionSku.SalesVolume > 0 { salesVolume = int(promotionSku.SalesVolume) } var goodsSkuVolume goods.Sku dbc.DB.Select("SUM(sales_volume) AS sales_volume, SUM(sales_volume_inc) AS sales_volume_inc,SUM(inventory) AS inventory"). Find(&goodsSkuVolume, "goods_id = ?", v.GoodsID) salesVolume1 := int(goodsSkuVolume.SalesVolume) + int(goodsSkuVolume.SalesVolumeInc) fmt.Println(v.ID) fmt.Println(goodsSkuVolume) // salesVolumeDesc := "已抢" + fmt.Sprintf("%d", salesVolume) + "件" salesVolumeDesc := "累计已售" + fmt.Sprintf("%d", salesVolume1) + "件" if salesVolume == int(goodsSkuVolume.Inventory) { //salesVolumeDesc = "已抢光" salesVolumeDesc = "" } // todo:已售未改成sku的已售,仍然是活动的已售 //percentage := decimal.NewFromInt(int64(salesVolume)).Div(decimal.NewFromInt(int64(v.TotalInventory))).Round(2) // //percentageDesc := percentage.Mul(decimal.NewFromInt(100)).String() + "%" inventoryDesc := "库存" + fmt.Sprintf("%d", goodsSkuVolume.Inventory) if goodsSkuVolume.Inventory == 0 { inventoryDesc = "已售罄" } var goodsSkus []goods.Sku dbc.DB.Order("discount_price asc").Where("goods_id = ?", v.GoodsID).Find(&goodsSkus) goodsSku := goods.Sku{} if len(goodsSkus) > 0 { goodsSku = goodsSkus[0] } inventory := uint(0) for _, skus := range goodsSkus { inventory += skus.Inventory } //后面把这个代码删除 //if c.Request.Header.Get("X-Recook-System") == "ios" { // mytemp := goodsSku.DiscountPrice // goodsSku.DiscountPrice = goodsSku.OriginalPrice // goodsSku.OriginalPrice = mytemp //} var country goods2.RecookAbroadCountryModel dbc.DB.Table(country.TableName()).First(&country, "id = ?", gi.Country) var one SecKillDetail //if _, ok := gmp[v.ID]; ok { // //存着有秒杀商品 // one = GetSecKillDetail(v.GoodsID) //} //base := goodsSku.GetBase() //commission1 := base.Mul(rate1).Round(2) //commission2 := base.Mul(rate2).Round(2) //commission3 := base.Mul(rate3).Round(2) //cm := commission1.Add(commission2).Add(commission3) //if u.Level == 10 { // cm = goodsSku.DiscountPrice.Sub(goodsSku.PurchasePrice.Mul(decimal.NewFromFloat(1.03))).Mul(decimal.NewFromFloat(define.Coefficient)).Round(2) //} cm := goodsSku.GetSelfProfit(u.Level) gList = append(gList, promotionGoods{ GoodsID: gi.ID, GoodsName: strings.Join([]string{gi.GoodsName, strings.Replace(goodsSku.Name, "+", " ", -1)}, " "), 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: inventory, InventoryDesc: inventoryDesc, TotalInventory: v.TotalInventory, TotalInventoryDesc: "限购" + fmt.Sprintf("%d", v.TotalInventory) + "件", TotalSalesVolume: uint(salesVolume1), SalesVolumeDesc: salesVolumeDesc, StartTime: v.StartTime, EndTime: v.EndTime, Percentage: decimal.Zero, PercentageDesc: "", Coupon: goodsSku.Coupon, IsImport: gi.IsImport, Storehouse: gi.Storehouse, IsFerme: gi.IsFerme, GysId: gi.VendorID, //添加供应商id CountryIcon: country.Icon, SecKill: one, }) } 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, }) } } /* 在原有的gList上查询该goodsId是否在直播,有就添加直播表示 */ //for i, k := range gList { // var vo live // dbc.DB.Table("recook_live_live_item").Where("main_goods_id=?", k.GoodsID).Select("status").Order("id desc").First(&vo) // if vo.Status == 1 { // gList[i].Living.Status = 1 // gList[i].Living.RoomId = vo.RoomId // } // //} /* 新增:前五条新增特外图标,该功能暂时关闭 */ //var SIcon kingKongIcon.SpecialSaleIconModel //dbc.DB.Table(SIcon.TableName()).First(&SIcon) //for i, _ := range gList { // if i < 5 { // gList[i].SpecialSale = SIcon.SpecialSaleIcon // } else { // break // } //} /* 修改特卖要求,给商品添加特卖图标 */ 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) //获取活动规则,活动商品或供应商 for i, v := range gList { 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.GoodsID == j.GoodsId { if k.EndTime.Time.Unix() > time.Now().Unix() { spList = append(spList, k.Url) } } } } } fmt.Println("图标路由为:", spList) gList[i].SpecialSale = spList } back.Suc(c, "", &promotionList{ GoodsList: gList, ActivityList: aList, }) }