价格统计
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
||||
@@ -61,6 +62,7 @@ func StatisticsReportForStoreSkusPrice(ctx *jxcontext.Context, cityCodes, skuIDs
|
||||
}
|
||||
|
||||
func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
var priceReferSnapshotList []*model.PriceReferSnapshot
|
||||
db := dao.GetDB()
|
||||
snapshotAt := utils.Time2Date(time.Now().AddDate(0, 0, -1))
|
||||
taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||
@@ -93,9 +95,11 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn
|
||||
dao.Commit(db)
|
||||
}
|
||||
case 1:
|
||||
priceReferSnapshotList, err = dao.GetPriceReferSnapshotNoPage(db, nil, nil, nil, snapshotAt)
|
||||
var (
|
||||
// citySkuMap = make(map[int]map[int][]int)
|
||||
tList []*tStoreSkuBindAndSkuName
|
||||
citySkuMap = make(map[int]map[int][]int)
|
||||
resultMap = make(map[int]map[int]*model.PriceReferSnapshot)
|
||||
tList []*tStoreSkuBindAndSkuName
|
||||
)
|
||||
storeList, err := dao.GetStoreList(db, nil, nil, "")
|
||||
if err != nil {
|
||||
@@ -121,6 +125,7 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn
|
||||
AND a.store_id = ?
|
||||
AND a.status = ?)
|
||||
)
|
||||
AND a.deleted_at = ?
|
||||
`
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
@@ -133,18 +138,68 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn
|
||||
utils.DefaultTimeValue,
|
||||
v.ID,
|
||||
model.StoreSkuBindStatusNormal,
|
||||
utils.DefaultTimeValue,
|
||||
}
|
||||
dao.GetRows(db, &tList, sql, sqlParams...)
|
||||
// for _, vv := range tList {
|
||||
// if citySkuMap[vv.CityCode] != nil {
|
||||
|
||||
// } else {
|
||||
// citySkuMap[vv.CityCode] =
|
||||
// }
|
||||
// }
|
||||
skuNameMap := make(map[int][]int)
|
||||
for _, vv := range tList {
|
||||
if citySkuMap[v.CityCode] != nil {
|
||||
if citySkuMap[v.CityCode][vv.NameID] != nil {
|
||||
citySkuMap[v.CityCode][vv.NameID] = append(citySkuMap[v.CityCode][vv.NameID], vv.UnitPrice)
|
||||
} else {
|
||||
citySkuMap[v.CityCode][vv.NameID] = []int{vv.UnitPrice}
|
||||
}
|
||||
} else {
|
||||
skuNameMap[vv.NameID] = append(skuNameMap[vv.NameID], vv.UnitPrice)
|
||||
citySkuMap[v.CityCode] = skuNameMap
|
||||
}
|
||||
}
|
||||
}
|
||||
for k1, v := range citySkuMap {
|
||||
skuNameMap := make(map[int]*model.PriceReferSnapshot)
|
||||
for k2, _ := range v {
|
||||
var midUnitPrice int
|
||||
var avgUnitPrice int
|
||||
sort.Ints(v[k2])
|
||||
if len(v[k2])%2 == 0 {
|
||||
midUnitPrice = v[k2][len(v[k2])/2-1]
|
||||
} else {
|
||||
midUnitPrice = v[k2][len(v[k2])/2]
|
||||
}
|
||||
for _, vv := range v[k2] {
|
||||
avgUnitPrice += vv
|
||||
}
|
||||
skuNameMap[k2] = &model.PriceReferSnapshot{
|
||||
MidUnitPrice: midUnitPrice,
|
||||
MaxUnitPrice: v[k2][len(v[k2])-1],
|
||||
MinUnitPrice: v[k2][0],
|
||||
AvgUnitPrice: avgUnitPrice / len(v[k2]),
|
||||
}
|
||||
}
|
||||
resultMap[k1] = skuNameMap
|
||||
}
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil || err != nil {
|
||||
dao.Rollback(db)
|
||||
if r != nil {
|
||||
panic(r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if len(priceReferSnapshotList) > 0 {
|
||||
for _, v := range priceReferSnapshotList {
|
||||
if resultMap[v.CityCode][v.NameID] != nil {
|
||||
v.MidUnitPrice = resultMap[v.CityCode][v.NameID].MidUnitPrice
|
||||
v.MaxUnitPrice = resultMap[v.CityCode][v.NameID].MaxUnitPrice
|
||||
v.AvgUnitPrice = resultMap[v.CityCode][v.NameID].AvgUnitPrice
|
||||
v.MaxUnitPrice = resultMap[v.CityCode][v.NameID].MaxUnitPrice
|
||||
dao.UpdateEntity(db, v, "MidUnitPrice", "MaxUnitPrice", "MinUnitPrice", "AvgUnitPrice")
|
||||
}
|
||||
}
|
||||
}
|
||||
dao.Commit(db)
|
||||
case 2:
|
||||
priceReferSnapshotList, err := dao.GetPriceReferSnapshotNoPage(db, nil, nil, nil, snapshotAt)
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil || err != nil {
|
||||
@@ -168,7 +223,7 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
taskSeq := tasksch.NewSeqTask2("生成每日价格统计", ctx, isContinueWhenError, taskSeqFunc, 4)
|
||||
taskSeq := tasksch.NewSeqTask2("生成每日价格统计", ctx, isContinueWhenError, taskSeqFunc, 3)
|
||||
tasksch.HandleTask(taskSeq, nil, true).Run()
|
||||
if !isAsync {
|
||||
_, err = taskSeq.GetResult(0)
|
||||
|
||||
Reference in New Issue
Block a user