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.

244 lines
8.3 KiB

4 years ago
package abroad
import (
"fmt"
mysql2 "git.oa00.com/go/mysql"
"github.com/golangkit/formatime"
"github.com/shopspring/decimal"
"recook/internal/cache"
"recook/internal/dbc"
"recook/internal/libs/bean"
"recook/internal/model/promotion"
"recook/internal/v2/model/http/goods"
goods2 "recook/internal/v2/model/recook/goods"
"strconv"
"strings"
"time"
)
type AbroadLogic struct {
}
var Logic = &AbroadLogic{}
//选择国家,
func (l *AbroadLogic) ViewCountry() (result []goods.CountryResult, err error) {
var cy []uint
mysql2.Db.Table((&goods2.RecookGoodsInfoModel{}).TableName()).Select("country").Group("country").Pluck("country", &cy)
var resul []goods.CountryResult
var t goods2.RecookAbroadCountryModel
if err = dbc.DB.Table(t.TableName()).Preload("Children", "id in(?)", cy).
Find(&resul, "parent_id = 0").Error; err != nil {
return
}
for _, v := range resul {
if len(v.Children) == 0 {
continue
} else {
result = append(result, v)
}
}
return
}
//选择商品类目
type Category struct {
First goods2.RecookGoodsCategoryModel `json:"first"`
Child []goods2.RecookGoodsCategoryModel `gorm:"foreignKey:parent_id" json:"child"`
}
func (l *AbroadLogic) ViewCategory(countryId uint) ([]Category, error) {
/*
step1
*/
var aboradGoods []goods2.RecookGoodsInfoModel
err := dbc.DB.Table("recook_goods_info").Where("is_import=? and country=?", 1, countryId).Find(&aboradGoods).Error
if err != nil {
return nil, err
}
/*
step2:,
*/
var cid []uint
maps := make(map[uint]bool)
for _, v := range aboradGoods {
_, ok := maps[v.SecondCategoryID]
if ok {
continue
} else {
maps[v.SecondCategoryID] = true
cid = append(cid, v.SecondCategoryID)
}
}
/*
id
*/
var cate []goods2.RecookGoodsCategoryModel
dbc.DB.Table("recook_goods_category").Find(&cate, "id in(?)", cid)
mapstr := make(map[uint]string)
for _, k := range cate {
_, ok := mapstr[k.ParentId]
if ok {
mapstr[k.ParentId] += strconv.FormatUint(uint64(k.Id), 10) + ","
} else {
mapstr[k.ParentId] = strconv.FormatUint(uint64(k.Id), 10) + ","
}
}
var rest []Category
for u, j := range mapstr {
var uu []uint
var first goods2.RecookGoodsCategoryModel
var second []goods2.RecookGoodsCategoryModel
ss := strings.Split(j, ",")
for _, k := range ss {
if k == "" {
continue
} else {
a, _ := strconv.ParseUint(k, 10, 64)
uu = append(uu, uint(a))
}
}
dbc.DB.Table("recook_goods_category").Find(&first, "id=?", u)
dbc.DB.Table("recook_goods_category").Find(&second, "id in(?)", ss)
re := Category{
First: first,
Child: second,
}
rest = append(rest, re)
}
return rest, nil
}
type GoodsSelect struct {
CountryId uint `json:"country_id"`
CategoryId uint `json:"category_id"`
Page bean.Page `json:"page"`
}
//显示进口商品
type ViewGoodsListVo struct {
Id uint `json:"id"` //商品idrecook表id
GoodsName string `json:"goods_name"` //商品名称
Subtitle string `json:"subtitle"` //商品的副标题
MainPhotoUrl string `json:"main_photo_url"` //产品主图
BrandName string `json:"brand_name"` //品牌名称
BrandLogo string `json:"brand_logo"` //品牌图标
Inventory uint `json:"inventory"` // 总库存
SalesVolume uint `json:"salesVolume"` // 总销量
PromotionName string `json:"promotionName"` //活动名称
OriginalPrice decimal.Decimal `json:"originalPrice"` // 原价
DiscountPrice decimal.Decimal `json:"discountPrice"` // 售价
Commission decimal.Decimal `json:"commission"` // 返利
Coupon decimal.Decimal `json:"coupon"` //商品劵
Tags []string `json:"tags"` // 为产品打上标签,比如 "新人特惠" "限时特卖"
Percent uint `json:"percent"` // 活动的%-1代表不是活动
StartTime formatime.Second `json:"startTime"` // 开始时间
EndTime formatime.Second `json:"endTime"` // 结束时间
Live live `json:"live"`
}
func (l *AbroadLogic) ViewGoods(p GoodsSelect) (rest []ViewGoodsListVo) {
var goodsList []goods2.RecookGoodsInfoModel
dbc.DB.Table("recook_goods_info").Where("country=? and second_category_id=? and publish_status=?", p.CountryId, p.CategoryId, 1).Limit(p.Page.GetLimit()).Offset(p.Page.GetStart()).Find(&goodsList)
var re ViewGoodsListVo
for _, v := range goodsList {
re.Id = v.Id
re.GoodsName = v.GoodsName
re.Subtitle = v.Description
//产品主图
var photo goods2.RecookGoodsMainPhotoModel
dbc.DB.Table(photo.TableName()).Where("goods_id=? and is_master=?", v.Id, 1).First(&photo)
re.MainPhotoUrl = photo.Url
// 品牌名称 品牌log
var brand goods2.RecookGoodsBrandModel
dbc.DB.Table(brand.TableName()).Find(&brand, "id=?", v.BrandID)
re.BrandName = brand.Name + "品牌馆"
re.BrandLogo = brand.LogoUrl
// sku相关
var sku goods2.RecookGoodsSkuModel
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)
re.SalesVolume = sku.SalesVolume //总销量
re.Inventory = sku.Inventory //总库存
re.OriginalPrice = sku.OriginalPrice //原价
re.DiscountPrice = sku.DiscountPrice //售价
re.Commission = sku.Commission //返利
re.Coupon = sku.Coupon //商品卷
// 是否活动状态
now := time.Now()
var tags = make([]string, 0, 0)
var promotionGoods promotion.Goods
dbc.DB.Table(promotionGoods.TableName()).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)
re.DiscountPrice = promotionSku.DiscountPrice //活动的价格
re.Commission = promotionSku.Commission //活动的佣金
re.Inventory = promotionSku.Inventory //活动的库存
//salesVolume = promotionSku.SalesVolume //活动的销量
//计算%
re.Percent = uint(promotionSku.SalesVolume / (re.Inventory + promotionSku.SalesVolume) * 100)
re.StartTime = promotionGoods.StartTime
re.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 //活动的佣金
re.StartTime = promotionGoods.StartTime
re.EndTime = promotionGoods.EndTime
}
}
// 查看是否是 新人特惠 商品
if isMember, _ := cache.IsMemberOfNewerTehui(strconv.Itoa(int(v.Id))); isMember {
tags = append(tags, "新人特惠")
}
//判断是否直播
var vo live
dbc.DB.Table("recook_live_live_item").Select("status , id as item_id").Where("main_goods_id=?", v.Id).Order("id desc").First(&vo)
re.Live = live{
Status: vo.Status,
ItemId: vo.ItemId,
}
rest = append(rest, re)
}
return rest
}
func (l *AbroadLogic) SearchCountry(name string) (result []goods.CountryResult) {
//var rest []goods2.RecookAbroadCountryModel
//dbc.DB.Table((&goods2.RecookAbroadCountryModel{}).TableName()).Where("name like ?", fmt.Sprintf("%%%v%%", name)).Where("parent_id!=0").Find(&rest)
//
//return
var t goods2.RecookAbroadCountryModel
dbc.DB.Table(t.TableName()).Preload("Children", "name like ?", fmt.Sprintf("%%%v%%", name)).
Find(&result, "parent_id = 0")
return
}
type live struct {
Status int `json:"status"`
ItemId uint `json:"item_id"`
}