|
|
package product
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"math/rand"
|
|
|
"recook/internal/v2/model/product"
|
|
|
"recook/internal/v2/model/recook/goods"
|
|
|
manage "recook/internal/v2/model/recook/order"
|
|
|
user2 "recook/internal/v2/model/recook/user"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type productLogic struct {
|
|
|
}
|
|
|
|
|
|
var LogicProductPortrait = productLogic{}
|
|
|
|
|
|
type saleProduct struct {
|
|
|
SaleNum decimal.Decimal `json:"sale_num"`
|
|
|
SortId uint `json:"sort_id"`
|
|
|
}
|
|
|
|
|
|
type showRes struct {
|
|
|
BasicParameters basicParameters `json:"basic_parameters"`
|
|
|
SaleNum []saleProduct `json:"sale_num"`
|
|
|
TopTen []partThree `json:"top_ten"`
|
|
|
AgePort []agePort `json:"age_port"`
|
|
|
Gender gender `json:"gender"`
|
|
|
}
|
|
|
type basicParameters struct {
|
|
|
BrandName string `json:"brand_name"`
|
|
|
GoodsName string `json:"goods_name"`
|
|
|
MainMaterial string `json:"main_material"`
|
|
|
MainPhoto string `json:"main_photo"`
|
|
|
Weight decimal.Decimal `json:"weight"`
|
|
|
}
|
|
|
|
|
|
func (l productLogic) Show(gid uint, dayType uint) (rest showRes) {
|
|
|
|
|
|
//step1:商品参数
|
|
|
var info goods.RecookGoodsInfoModel
|
|
|
mysql2.Db.Table(info.TableName()).Where("id=?", gid).First(&info)
|
|
|
var brand goods.RecookGoodsBrandModel
|
|
|
mysql2.Db.Table(brand.TableName()).Where("id=?", info.BrandID).First(&brand)
|
|
|
var mphoto goods.RecookGoodsMainPhotoModel
|
|
|
mysql2.Db.Table(mphoto.TableName()).Where("goods_id=?", gid).Where("is_master=?", 1).First(&mphoto)
|
|
|
var basic = basicParameters{
|
|
|
BrandName: brand.Name,
|
|
|
GoodsName: info.GoodsName,
|
|
|
MainMaterial: info.Material,
|
|
|
Weight: info.Weight,
|
|
|
MainPhoto: mphoto.Url,
|
|
|
}
|
|
|
rest.BasicParameters = basic
|
|
|
//step2:累计销量
|
|
|
now := time.Now()
|
|
|
var start time.Time
|
|
|
var end time.Time
|
|
|
max := 0
|
|
|
switch dayType {
|
|
|
case 1:
|
|
|
max = int(now.Weekday()) //周
|
|
|
s := now.Day() - int(now.Weekday()-1)
|
|
|
start = time.Date(now.Year(), now.Month(), s, 0, 0, 0, 0, time.Local)
|
|
|
end = now
|
|
|
case 2:
|
|
|
//月
|
|
|
max = now.Day() / 5
|
|
|
start = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
|
|
|
end = now
|
|
|
case 3:
|
|
|
//年
|
|
|
max = int(now.Month())
|
|
|
start = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
|
|
|
end = now
|
|
|
case 4:
|
|
|
//总
|
|
|
max = now.Year() - 2019
|
|
|
start = time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local)
|
|
|
end = now
|
|
|
}
|
|
|
fmt.Println(max)
|
|
|
re, allnum := SaleNumCal(gid, max, dayType)
|
|
|
rest.SaleNum = re
|
|
|
|
|
|
//step3:销售top10
|
|
|
//增加虚拟销量
|
|
|
var fal product.RecookProductPortraitModel
|
|
|
mysql2.Db.Table(fal.TableName()).Where("goods_id=?", gid).Order("id desc").First(&fal)
|
|
|
//虚假销量值
|
|
|
//sales := fal.DayVirtualSales
|
|
|
//if fal.Id == 0 {
|
|
|
// sales = decimal.NewFromInt(11)
|
|
|
//}
|
|
|
var order manage.RecookOrderGoodsDetailModel
|
|
|
var ads manage.RecookOrderAddrModel
|
|
|
var citys []partThree
|
|
|
mysql2.Db.Table(fmt.Sprintf("%s as a ", order.TableName())).Joins(fmt.Sprintf("left join %v as b on a.order_id=b.order_id ", ads.TableName())).Group("b.province").Select("sum(a.quantity) as sum,"+
|
|
|
"b.province as province ").Where("a.goods_id=?", gid).Where("a.order_time between ? and ?", start, end).Limit(10).Find(&citys)
|
|
|
if len(citys) == 0 {
|
|
|
//没有城市,新增虚假城市
|
|
|
var cs []string
|
|
|
mysql2.Db.Table((&user2.RecookUserInfoModel{}).TableName()).Where("province!=\"\"").Group("province").Pluck("province", &cs)
|
|
|
for i := 0; i < 3+rand.Intn(8); i++ {
|
|
|
sj := rand.Intn(len(cs))
|
|
|
citys = append(citys, partThree{
|
|
|
Province: cs[sj],
|
|
|
Sum: decimal.NewFromInt(0),
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
tend := end.Day() - start.Day()
|
|
|
//all := decimal.NewFromInt(int64(tend)).Mul(fal.DayVirtualSales)
|
|
|
var tn = allnum
|
|
|
//tsn := 0
|
|
|
fmt.Println(citys)
|
|
|
fmt.Println(allnum)
|
|
|
for i, city := range citys {
|
|
|
//dv := decimal.NewFromInt(int64(10 * len(citys)))
|
|
|
if len(citys) < 10 {
|
|
|
|
|
|
if i == (len(citys) - 1) {
|
|
|
citys[i].Sum = tn
|
|
|
} else {
|
|
|
//n1 := rand.Intn(int(5*len(citys)) - tsn)
|
|
|
//tsn = (5 * len(citys)) - n1
|
|
|
//if tsn <= 0 {
|
|
|
// citys[i].Sum = decimal.NewFromInt(1)
|
|
|
// tn = tn.Sub(citys[i].Sum)
|
|
|
//} else {
|
|
|
// rd := decimal.NewFromInt(int64(n1))
|
|
|
// citys[i].Sum = allnum.Mul(rd).Div(dv).Truncate(0)
|
|
|
// tn = tn.Sub(citys[i].Sum)
|
|
|
// fmt.Println(tn)
|
|
|
//}
|
|
|
citys[i].Sum = allnum.Div(decimal.NewFromInt(int64(len(citys)))).Mul(decimal.NewFromInt(int64(rand.Intn(10)))).Div(decimal.NewFromInt(10)).Add(decimal.NewFromInt(1)).Truncate(0)
|
|
|
tn = tn.Sub(citys[i].Sum)
|
|
|
}
|
|
|
} else {
|
|
|
si := float64(rand.Intn(100)+10) / 100
|
|
|
one := allnum.Div(decimal.NewFromInt(20)).Mul(decimal.NewFromInt(int64(tend))).Mul(decimal.NewFromFloat(si)).Add(decimal.NewFromInt(1)).Truncate(0)
|
|
|
citys[i].Sum = city.Sum.Add(one)
|
|
|
}
|
|
|
}
|
|
|
rest.TopTen = citys
|
|
|
//step4 下单者的年龄段
|
|
|
var userlist []uint
|
|
|
var user []user2.RecookUserInfoModel
|
|
|
q1 := mysql2.Db.Table(order.TableName()).Where("goods_id=?", gid).Where("order_time between ? and ?", start, end).Select("user_id").Group("user_id").Pluck("user_id", &userlist)
|
|
|
mysql2.Db.Table((&user2.RecookUserInfoModel{}).TableName()).Where("id in(?)", q1).Find(&user)
|
|
|
//var num1 uint //20-30
|
|
|
//var num2 uint //31-40
|
|
|
//var num3 uint //41-50
|
|
|
//var num4 uint //51-60
|
|
|
//var num5 uint //其他年龄段
|
|
|
//var num6 uint //男
|
|
|
//var num7 uint //女
|
|
|
//var num8 uint //未知
|
|
|
agemap := make(map[uint]uint, 8)
|
|
|
|
|
|
for _, v := range user {
|
|
|
age := now.Year() - v.Birthday.Time.Year() + 1
|
|
|
if age >= 61 || age < 20 {
|
|
|
agemap[5]++
|
|
|
} else if age >= 51 {
|
|
|
agemap[4]++
|
|
|
} else if age >= 41 {
|
|
|
agemap[3]++
|
|
|
} else if age >= 31 {
|
|
|
agemap[2]++
|
|
|
} else if age >= 20 {
|
|
|
agemap[1]++
|
|
|
}
|
|
|
if v.Gender == 2 {
|
|
|
agemap[7]++
|
|
|
} else if v.Gender == 1 {
|
|
|
agemap[6]++
|
|
|
} else if v.Gender == 0 {
|
|
|
agemap[8]++
|
|
|
}
|
|
|
}
|
|
|
all := agemap[1] + agemap[2] + agemap[3] + agemap[4] + agemap[5]
|
|
|
alld := decimal.NewFromInt(int64(all))
|
|
|
var gend gender
|
|
|
var ages []agePort
|
|
|
var agetend decimal.Decimal
|
|
|
if all > 0 { //有订单真实数据
|
|
|
for i := 1; i < 6; i++ {
|
|
|
if i == 5 {
|
|
|
num := decimal.NewFromInt(100).Sub(agetend)
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
num := decimal.NewFromInt(int64(agemap[uint(i)])).Div(alld).Mul(decimal.NewFromInt(100)).Truncate(0)
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
agetend = agetend.Add(num)
|
|
|
}
|
|
|
}
|
|
|
allPeople := decimal.NewFromInt(int64(agemap[6] + agemap[7]))
|
|
|
if allPeople.GreaterThan(decimal.NewFromInt(0)) {
|
|
|
gend.Female = decimal.NewFromInt(int64(agemap[7])).Div(allPeople).Mul(decimal.NewFromInt(100)).Truncate(0)
|
|
|
//gend.Male = decimal.NewFromInt(int64(agemap[6])).Div(allPeople).Mul(decimal.NewFromInt(100)).Truncate(0)
|
|
|
gend.Male = decimal.NewFromInt(100).Sub(gend.Female)
|
|
|
//gend.Other = decimal.NewFromInt(100).Sub(gend.Male).Sub(gend.Female)
|
|
|
} else {
|
|
|
num := rand.Intn(16) + 64
|
|
|
gend.Female = decimal.NewFromInt(int64(num))
|
|
|
gend.Male = decimal.NewFromInt(100).Sub(gend.Female)
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
//没有订单,放假数据
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
|
|
if i == 5 {
|
|
|
|
|
|
num := decimal.NewFromInt(100).Sub(agetend)
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
|
|
|
} else if i == 1 {
|
|
|
sj := rand.Intn(10) + 40
|
|
|
num := decimal.NewFromInt(int64(sj))
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
|
|
|
agetend = agetend.Add(num)
|
|
|
} else if i == 2 {
|
|
|
sj := rand.Intn(10) + 20
|
|
|
num := decimal.NewFromInt(int64(sj))
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
agetend = agetend.Add(num)
|
|
|
} else if i == 3 {
|
|
|
sj := rand.Intn(10) + 5
|
|
|
num := decimal.NewFromInt(int64(sj))
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
agetend = agetend.Add(num)
|
|
|
} else if i == 4 {
|
|
|
sj := rand.Intn(5) + 1
|
|
|
num := decimal.NewFromInt(int64(sj))
|
|
|
ages = append(ages, agePort{
|
|
|
SortId: uint(i),
|
|
|
Num: num,
|
|
|
})
|
|
|
agetend = agetend.Add(num)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
n1 := int64(rand.Intn(15)) + 65
|
|
|
gend.Female = decimal.NewFromInt(n1)
|
|
|
gend.Male = decimal.NewFromInt(100).Sub(gend.Female)
|
|
|
//n2 := int64(rand.Intn(100 - int(n1)))
|
|
|
//gend.Male = decimal.NewFromInt(n2)
|
|
|
//gend.Other = decimal.NewFromInt(100).Sub(gend.Male).Sub(gend.Female)
|
|
|
|
|
|
}
|
|
|
rest.AgePort = ages
|
|
|
rest.Gender = gend
|
|
|
return rest
|
|
|
}
|
|
|
|
|
|
type agePort struct {
|
|
|
SortId uint `json:"sort_id"`
|
|
|
Num decimal.Decimal `json:"num"`
|
|
|
}
|
|
|
type gender struct {
|
|
|
Male decimal.Decimal `json:"male"`
|
|
|
Female decimal.Decimal `json:"female"`
|
|
|
Other decimal.Decimal `json:"_"`
|
|
|
}
|
|
|
|
|
|
type partThree struct {
|
|
|
Province string `json:"province"`
|
|
|
Sum decimal.Decimal `json:"sum"`
|
|
|
}
|
|
|
|
|
|
func SaleNumCal(goodsId uint, max int, status uint) ([]saleProduct, decimal.Decimal) {
|
|
|
var fal product.RecookProductPortraitModel
|
|
|
mysql2.Db.Table(fal.TableName()).Where("goods_id=?", goodsId).Order("id desc").First(&fal)
|
|
|
if fal.Id == 0 {
|
|
|
fal.DayVirtualSales = decimal.NewFromInt(11)
|
|
|
}
|
|
|
var now = time.Now()
|
|
|
var start time.Time
|
|
|
var end time.Time
|
|
|
var order manage.RecookOrderGoodsDetailModel
|
|
|
var sl []saleProduct
|
|
|
type num struct {
|
|
|
Sum decimal.Decimal `json:"sum"`
|
|
|
}
|
|
|
//wk := int(now.Weekday())
|
|
|
for i := max; i > 0; i-- {
|
|
|
var sj decimal.Decimal
|
|
|
switch status {
|
|
|
case 1:
|
|
|
//获取每日的数据
|
|
|
start = time.Date(now.Year(), now.Month(), now.Day()-i+1, 0, 0, 0, 0, time.Local)
|
|
|
end = start.AddDate(0, 0, 1).Add(-1 * time.Second)
|
|
|
sj = decimal.NewFromFloat(float64(rand.Intn(100)+10) / 100).Truncate(2)
|
|
|
sj = fal.DayVirtualSales.Mul(sj)
|
|
|
case 2:
|
|
|
//月度
|
|
|
start = time.Date(now.Year(), now.Month(), 5*(i-1)+1, 0, 0, 0, 0, time.Local)
|
|
|
end = start.AddDate(0, 0, 5).Add(-1 * time.Second)
|
|
|
sj = decimal.NewFromFloat(float64(rand.Intn(100)+10) / 100).Truncate(2)
|
|
|
sj = fal.DayVirtualSales.Mul(sj).Mul(decimal.NewFromInt(5))
|
|
|
case 3:
|
|
|
//年度
|
|
|
start = time.Date(now.Year(), time.Month(i), 1, 0, 0, 0, 0, time.Local)
|
|
|
end = start.AddDate(0, 1, 0).Add(-1 * time.Second)
|
|
|
sj = decimal.NewFromFloat(float64(rand.Intn(100)+10) / 100).Truncate(2)
|
|
|
sj = fal.DayVirtualSales.Mul(sj).Mul(decimal.NewFromInt(30))
|
|
|
case 4:
|
|
|
start = time.Date(now.Year()-i+1, 1, 1, 0, 0, 0, 0, time.Local)
|
|
|
end = start.AddDate(1, 0, 0).Add(-1 * time.Second)
|
|
|
sj = decimal.NewFromFloat(float64(rand.Intn(100)+10) / 100).Truncate(2)
|
|
|
sj = fal.DayVirtualSales.Mul(sj).Mul(decimal.NewFromInt(365))
|
|
|
}
|
|
|
if start.After(now) {
|
|
|
sj = decimal.NewFromFloat(0)
|
|
|
}
|
|
|
var nm num
|
|
|
mysql2.Db.Table(order.TableName()).Where("goods_id=?", goodsId).Where("order_time between ? and ?", start, end).Select("sum(quantity) as sum").Find(&nm)
|
|
|
|
|
|
sl = append(sl, saleProduct{SaleNum: nm.Sum.Add(sj).Truncate(0), SortId: uint(i)})
|
|
|
}
|
|
|
var all decimal.Decimal
|
|
|
for _, s := range sl {
|
|
|
all = all.Add(s.SaleNum)
|
|
|
}
|
|
|
|
|
|
return sl, all
|
|
|
}
|