From 2548908b68dfadaa16d6aa164539d4f82c21d89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Mon, 30 Dec 2019 17:08:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=B7=E6=A0=BC=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/cms/store_sku.go | 16 +++++---- business/jxstore/report/report.go | 28 ++++++++++------ business/model/dao/report.go | 22 +++++++----- business/model/dao/store_sku.go | 56 +++++++++++++++++++++---------- business/model/store.go | 1 + 5 files changed, 79 insertions(+), 44 deletions(-) diff --git a/business/jxstore/cms/store_sku.go b/business/jxstore/cms/store_sku.go index 100ad13be..8ead577a6 100644 --- a/business/jxstore/cms/store_sku.go +++ b/business/jxstore/cms/store_sku.go @@ -2247,9 +2247,9 @@ func GetTopSkusByCityCode(ctx *jxcontext.Context, cityCode, storeID int) (skuNam } for _, v := range skuNameAndPlace { if skuNameMap[v.ID] != nil { - midPrice, _ := dao.GetMidPriceByNameID(db, cityCode, v.ID, utils.Time2Date(time.Now().AddDate(0, 0, -1))) - if midPrice != 0 { - v.Price = midPrice + priceReferList, _, _ := dao.GetPriceReferSnapshot(db, []int{cityCode}, nil, v.ID, utils.Time2Date(time.Now().AddDate(0, 0, -1)), 0, 9999) + if len(priceReferList) > 0 { + v.Price = priceReferList[0].MidUnitPrice } v.Type = skuNameMap[v.ID].BrandID skuNameAndPlaceList = append(skuNameAndPlaceList, v) @@ -2813,7 +2813,7 @@ func FocusStoreSkusByExcelBin(ctx *jxcontext.Context, reader io.Reader, isAsync, focusList, _ := dao.GetStoreSkuBindByNameID(db, store.ID, v.NameID, model.StoreSkuBindStatusNormal) //有关注过 if len(focusList) > 0 { - price = 0 + price = focusList[0].UnitPrice skuInfoMap[v.NameID] = append(skuInfoMap[v.NameID], &StoreSkuBindSkuInfo{ SkuID: v.ID, IsSale: 1, @@ -2920,10 +2920,12 @@ func FocusStoreSkusBySku(ctx *jxcontext.Context, skuIDs []int, isAsync, isContin focusList, _ := dao.GetStoreSkuBindByNameID(db, store.ID, k, model.StoreSkuBindStatusNormal) //有关注过 if len(focusList) > 0 { - price = 0 + price = focusList[0].UnitPrice } else { - midPrice, _ := dao.GetMidPriceByNameID(db, store.CityCode, k, utils.Time2Date(time.Now().AddDate(0, 0, -1))) - price = midPrice + priceReferList, _, _ := dao.GetPriceReferSnapshot(db, []int{store.CityCode}, nil, k, utils.Time2Date(time.Now().AddDate(0, 0, -1)), 0, 9999) + if len(priceReferList) > 0 { + price = priceReferList[0].MidUnitPrice + } } skuBindInfo := &StoreSkuBindInfo{ NameID: k, diff --git a/business/jxstore/report/report.go b/business/jxstore/report/report.go index cc78fbe71..c9001fcbd 100644 --- a/business/jxstore/report/report.go +++ b/business/jxstore/report/report.go @@ -6,7 +6,7 @@ import ( "math" "time" - "git.rosy.net.cn/jx-callback/globals" + "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" @@ -44,7 +44,7 @@ func StatisticsReportForStoreSkusPrice(ctx *jxcontext.Context, cityCodes, skuIDs if snapDate != "" { snapDateParam = utils.Str2Time(snapDate) } - priceReferSnapshot, totalCount, err := dao.GetPriceReferSnapshot(db, cityCodes, skuIDs, snapDateParam, offset, pageSize) + priceReferSnapshot, totalCount, err := dao.GetPriceReferSnapshot(db, cityCodes, skuIDs, 0, snapDateParam, offset, pageSize) pagedInfo = &model.PagedInfo{ Data: priceReferSnapshot, TotalCount: totalCount, @@ -70,15 +70,23 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int) (err e priceReferSnapshotDelete := &model.PriceReferSnapshot{SnapshotAt: snapshotAt} dao.DeleteEntity(db, priceReferSnapshotDeleteHis, "SnapshotAt") dao.DeleteEntity(db, priceReferSnapshotDelete, "SnapshotAt") - for _, v := range priceReferSnapshot { - dao.WrapAddIDCULDEntity(v, ctx.GetUserName()) - v.SnapshotAt = snapshotAt - if err = dao.CreateEntity(db, v); err != nil { - return err - } - } + task := tasksch.NewParallelTask("生成价格统计", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + v := batchItemList[0].(*model.PriceReferSnapshot) + dao.WrapAddIDCULDEntity(v, ctx.GetUserName()) + v.SnapshotAt = snapshotAt + err = dao.CreateEntity(db, v) + priceRefer, err := dao.GetPriceReferUnitPrice(db, v.CityCode, v.NameID, v.SnapshotAt) + v.MidUnitPrice = priceRefer.MidUnitPrice + v.MaxUnitPrice = priceRefer.MaxUnitPrice + v.MinUnitPrice = priceRefer.MinUnitPrice + v.AvgUnitPrice = priceRefer.AvgUnitPrice + _, err = dao.UpdateEntity(db, v, "MidUnitPrice", "MaxUnitPrice", "AvgUnitPrice", "MinUnitPrice") + return retVal, err + }, priceReferSnapshot) + tasksch.HandleTask(task, nil, true).Run() + _, err = task.GetResult(0) dao.Commit(db) - globals.SugarLogger.Debugf("CreatePriceRefer") } return err } diff --git a/business/model/dao/report.go b/business/model/dao/report.go index 9826f390e..20ff39589 100644 --- a/business/model/dao/report.go +++ b/business/model/dao/report.go @@ -36,8 +36,11 @@ type StatisticsReportForOrdersList struct { type PriceReferSnapshotExt struct { model.PriceReferSnapshot - CityName string `json:"cityName"` - SkuName string `json:"skuName"` + CityName string `json:"cityName"` + SkuName string `json:"skuName"` + SpecQuality float32 + Unit string + SpecUnit string } //查询统计订单信息 @@ -187,15 +190,11 @@ func GetGetStatisticsReportForAfsOrders(db *DaoDB, storeIDs []int, fromDate time func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (priceReferSnapshot []*model.PriceReferSnapshot, err error) { sql := ` - SELECT b.city_code,a.sku_id, + SELECT b.city_code,a.sku_id,c.name_id, ROUND(MAX(a.price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) max_price, ROUND(MIN(a.price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) min_price, ROUND(AVG(a.price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) avg_price, ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT((a.price/IF(b.pay_percentage < 50,70,b.pay_percentage))*100 ORDER BY (a.price/IF(b.pay_percentage < 50,70,b.pay_percentage))*100),',',Count(1)/2),',',-1)) mid_price, - ROUND(MAX(a.unit_price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) max_unit_price, - ROUND(MIN(a.unit_price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) min_unit_price, - ROUND(AVG(a.unit_price/IF(b.pay_percentage < 50,70,b.pay_percentage)*100)) avg_unit_price, - ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT((a.unit_price/IF(b.pay_percentage < 50,70,b.pay_percentage))*100 ORDER BY (a.unit_price/IF(b.pay_percentage < 50,70,b.pay_percentage))*100),',',Count(1)/2),',',-1)) mid_unit_price, MAX(a.jd_price) max_jd_price, MIN(a.jd_price) min_jd_price, ROUND(AVG(a.jd_price)) avg_jd_price, @@ -216,6 +215,7 @@ func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (p t1.avg_vendor_price FROM store_sku_bind a JOIN store b ON a.store_id = b.id AND b.deleted_at = ? AND b.status != ? + JOIN sku c ON a.sku_id = c.id LEFT JOIN ( SELECT SUM(t1.count),t1.sku_id,MAX(t1.sale_price) max_sale_price,MIN(t1.sale_price) min_sale_price,ROUND(AVG(t1.sale_price)) avg_sale_price,MAX(t1.vendor_price) max_vendor_price,MIN(t1.vendor_price) min_vendor_price,ROUND(AVG(t1.vendor_price)) avg_vendor_price FROM order_sku t1 @@ -240,14 +240,14 @@ func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (p sql += " AND b.city_code IN (" + GenQuestionMarks(len(cityCodes)) + ")" sqlParams = append(sqlParams, cityCodes) } - sql += " GROUP BY 1,2" + sql += " GROUP BY 1,2,3" if err = GetRows(db, &priceReferSnapshot, sql, sqlParams...); err == nil { return priceReferSnapshot, nil } return nil, err } -func GetPriceReferSnapshot(db *DaoDB, cityCodes, skuIDs []int, snapDate time.Time, offset, pageSize int) (priceReferSnapshot []*PriceReferSnapshotExt, totalCount int, err error) { +func GetPriceReferSnapshot(db *DaoDB, cityCodes, skuIDs []int, skuNameID int, snapDate time.Time, offset, pageSize int) (priceReferSnapshot []*PriceReferSnapshotExt, totalCount int, err error) { sql := ` SELECT SQL_CALC_FOUND_ROWS a.*,b.name city_name FROM price_refer_snapshot a @@ -258,6 +258,10 @@ func GetPriceReferSnapshot(db *DaoDB, cityCodes, skuIDs []int, snapDate time.Tim sqlParams := []interface{}{ utils.DefaultTimeValue, } + if skuNameID > 0 { + sql += " AND a.name_id = ?" + sqlParams = append(sqlParams, skuNameID) + } if len(skuIDs) > 0 { sql += " AND a.sku_id IN (" + GenQuestionMarks(len(skuIDs)) + ")" sqlParams = append(sqlParams, skuIDs) diff --git a/business/model/dao/store_sku.go b/business/model/dao/store_sku.go index ca472fd39..9b0f83cd5 100644 --- a/business/model/dao/store_sku.go +++ b/business/model/dao/store_sku.go @@ -1367,33 +1367,42 @@ func GetStoreSkuBindByNameID(db *DaoDB, storeID, nameID, status int) (storeSkuBi return storeSkuBind, err } -func GetMidPriceByNameID(db *DaoDB, cityCode, skuNameID int, snapDate time.Time) (midPrice int, err error) { +func GetPriceReferUnitPrice(db *DaoDB, cityCode int, nameID int, snapDate time.Time) (result *PriceReferSnapshotExt, err error) { var ( - sku []*model.SkuAndName - skuMap = make(map[int]int) + pRefer []*PriceReferSnapshotExt + minUnitPrice int + maxUnitPrice int + midUnitPrice int + avgUnitPrice int ) sql := ` - SELECT a.mid_price price, a.sku_id id, b.spec_quality, c.unit, b.spec_unit + SELECT a.max_price, a.min_price, a.avg_price, a.mid_price, a.sku_id id, b.spec_quality, c.unit, b.spec_unit FROM price_refer_snapshot a JOIN sku b ON a.sku_id = b.id JOIN sku_name c ON c.id = b.name_id - WHERE c.id = ? + WHERE 1=1 AND a.snapshot_at = ? AND a.city_code = ? ` sqlParams := []interface{}{ - skuNameID, snapDate, cityCode, } - err = GetRows(db, &sku, sql, sqlParams...) - if err != nil { - return 0, err + if nameID > 0 { + sql += " AND a.id = ?" + sqlParams = append(sqlParams, nameID) } - if len(sku) > 0 { - for _, v := range sku { + err = GetRows(db, &pRefer, sql, sqlParams...) + if err != nil { + return nil, err + } + if len(pRefer) > 0 { + for _, v := range pRefer { var ( - price int + minPrice int + maxPrice int + midPrice int + avgPrice int specQuality float64 ) if v.Unit == model.SpecialUnit { @@ -1402,14 +1411,25 @@ func GetMidPriceByNameID(db *DaoDB, cityCode, skuNameID int, snapDate time.Time) } else { specQuality = float64(v.SpecQuality) } - price = int(utils.Float64TwoInt64(utils.Int2Float64(model.SpecialSpecQuality) / specQuality * utils.Int2Float64(v.Price))) + maxPrice = int(utils.Float64TwoInt64(utils.Int2Float64(model.SpecialSpecQuality) / specQuality * utils.Int2Float64(v.MaxPrice))) + minPrice = int(utils.Float64TwoInt64(utils.Int2Float64(model.SpecialSpecQuality) / specQuality * utils.Int2Float64(v.MinPrice))) + avgPrice = int(utils.Float64TwoInt64(utils.Int2Float64(model.SpecialSpecQuality) / specQuality * utils.Int2Float64(v.AvgPrice))) + midPrice = int(utils.Float64TwoInt64(utils.Int2Float64(model.SpecialSpecQuality) / specQuality * utils.Int2Float64(v.MidPrice))) } else { - price = v.Price - } - if skuMap[skuNameID] < price { - skuMap[skuNameID] = price + maxPrice = v.MaxPrice + minPrice = v.MinPrice + avgPrice = v.AvgPrice + midPrice = v.MidPrice } + minUnitPrice += minPrice + maxUnitPrice += maxPrice + midUnitPrice += midPrice + avgUnitPrice += avgPrice } + result.MinUnitPrice = minUnitPrice / len(pRefer) + result.MaxUnitPrice = maxUnitPrice / len(pRefer) + result.AvgUnitPrice = avgUnitPrice / len(pRefer) + result.MidUnitPrice = midUnitPrice / len(pRefer) } - return skuMap[skuNameID], err + return result, err } diff --git a/business/model/store.go b/business/model/store.go index c645da58e..553f564b9 100644 --- a/business/model/store.go +++ b/business/model/store.go @@ -444,6 +444,7 @@ type PriceReferSnapshot struct { SnapshotAt time.Time `orm:"type(datetime)" json:"snapshotAt"` // 这个不同于CreatedAt,SnapshotAt是逻辑上的时间,CreatedAt是实际存储的时间 CityCode int `json:"cityCode"` SkuID int `orm:"column(sku_id)" json:"skuId"` + NameID int `orm:"column(name_id)" json:"nameID"` MaxPrice int `json:"maxPrice"` MinPrice int `json:"minPrice"` AvgPrice int `json:"avgPrice"`