diff --git a/business/jxstore/cms/store_sku.go b/business/jxstore/cms/store_sku.go index ebf34ea1f..f44c3e467 100644 --- a/business/jxstore/cms/store_sku.go +++ b/business/jxstore/cms/store_sku.go @@ -262,7 +262,7 @@ func getGetStoresSkusBaseSQL(db *dao.DaoDB, storeIDs, skuIDs []int, isFocus bool sql += ` JOIN store_sku_bind t4 ON t4.store_id = t3.id AND t4.sku_id = t2.id AND t4.deleted_at = ? LEFT JOIN sku_name_place_bind t5 ON t1.id = t5.name_id AND t3.city_code = t5.place_code - JOIN price_refer_snapshot t6 ON t6.city_code = t3.city_code AND t6.sku_id = t2.id AND t6.snapshot_at = ? + LEFT JOIN price_refer_snapshot t6 ON t6.city_code = 0 AND t6.sku_id = t2.id AND t6.snapshot_at = ? WHERE t1.deleted_at = ? AND (t1.is_global = 1 OR t5.id IS NOT NULL OR 1 = ?)/* AND t1.status = ?*/ ` sqlParams = append(sqlParams, []interface{}{ diff --git a/business/jxstore/report/report.go b/business/jxstore/report/report.go index 997dcce74..e2b9b8f78 100644 --- a/business/jxstore/report/report.go +++ b/business/jxstore/report/report.go @@ -97,8 +97,10 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn case 1: priceReferSnapshotList, err = dao.GetPriceReferSnapshotNoPage(db, nil, nil, nil, snapshotAt) var ( - citySkuMap = make(map[int]map[int][]int) - resultMap = make(map[int]map[int]*model.PriceReferSnapshot) + citySkuMap = make(map[int]map[int][]int) + countryMap = make(map[int][]int) + resultMap = make(map[int]map[int]*model.PriceReferSnapshot) + resultCountryMap = make(map[int]*model.PriceReferSnapshot) ) storeList, err := dao.GetStoreList(db, nil, nil, "") if err != nil { @@ -145,6 +147,7 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn if len(tList) > 0 { for _, vv := range tList { skuNameMap[vv.NameID] = append(skuNameMap[vv.NameID], vv.UnitPrice) + countryMap[vv.NameID] = append(countryMap[vv.NameID], vv.UnitPrice) } if citySkuMap[v.CityCode] != nil { for nameID, unitPriceList := range skuNameMap { @@ -159,6 +162,26 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn } } } + for k, v := range countryMap { + var midUnitPrice int + var avgUnitPrice int + sort.Ints(v) + if len(v)%2 == 0 { + midUnitPrice = v[len(v)/2-1] + } else { + midUnitPrice = v[len(v)/2] + } + for _, vv := range v { + avgUnitPrice += vv + } + priceRefer := &model.PriceReferSnapshot{ + MidUnitPrice: midUnitPrice, + MaxUnitPrice: v[len(v)-1], + MinUnitPrice: v[0], + AvgUnitPrice: avgUnitPrice / len(v), + } + resultCountryMap[k] = priceRefer + } for k1, v := range citySkuMap { skuNameMap := make(map[int]*model.PriceReferSnapshot) for k2, _ := range v { @@ -193,6 +216,16 @@ func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int, isAsyn }() if len(priceReferSnapshotList) > 0 { for _, v := range priceReferSnapshotList { + if v.CityCode == 0 { + if resultCountryMap[v.NameID] != nil { + v.MidUnitPrice = resultCountryMap[v.NameID].MidUnitPrice + v.MaxUnitPrice = resultCountryMap[v.NameID].MaxUnitPrice + v.AvgUnitPrice = resultCountryMap[v.NameID].AvgUnitPrice + v.MinUnitPrice = resultCountryMap[v.NameID].MinUnitPrice + dao.UpdateEntity(db, v, "MidUnitPrice", "MaxUnitPrice", "MinUnitPrice", "AvgUnitPrice") + } + continue + } if resultMap[v.CityCode][v.NameID] != nil { v.MidUnitPrice = resultMap[v.CityCode][v.NameID].MidUnitPrice v.MaxUnitPrice = resultMap[v.CityCode][v.NameID].MaxUnitPrice diff --git a/business/model/dao/report.go b/business/model/dao/report.go index e65873b69..1d47e3c70 100644 --- a/business/model/dao/report.go +++ b/business/model/dao/report.go @@ -189,8 +189,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,c.name_id, + var sql string + sql1 := ` + SELECT a.sku_id, c.name_id, + ` + sql2 := ` MAX(a.jd_price) max_jd_price, MIN(a.jd_price) min_jd_price, ROUND(AVG(a.jd_price)) avg_jd_price, @@ -220,6 +223,7 @@ func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (p )t1 ON t1.sku_id = a.sku_id WHERE a.deleted_at = ? ` + sql = sql1 + "b.city_code, " + sql2 sqlParams := []interface{}{ utils.DefaultTimeValue, model.StoreStatusDisabled, @@ -234,7 +238,19 @@ 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,3" + sql += ` GROUP BY 1,2,3 + UNION ` + sql += sql1 + "0 city_code," + sql2 + if len(skuIDs) > 0 { + sql += " AND a.sku_id IN (" + GenQuestionMarks(len(skuIDs)) + ")" + sqlParams = append(sqlParams, skuIDs) + } + if len(cityCodes) > 0 { + sql += " AND b.city_code IN (" + GenQuestionMarks(len(cityCodes)) + ")" + sqlParams = append(sqlParams, cityCodes) + } + sql += " GROUP BY 1,2" + sqlParams = append(sqlParams, sqlParams...) if err = GetRows(db, &priceReferSnapshot, sql, sqlParams...); err == nil { return priceReferSnapshot, nil } @@ -243,9 +259,9 @@ func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (p 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 + SELECT SQL_CALC_FOUND_ROWS a.*,IF(a.city_code = 0,'全国',b.name) city_name FROM price_refer_snapshot a - JOIN place b ON a.city_code = b.code + LEFT JOIN place b ON a.city_code = b.code WHERE 1=1 AND a.deleted_at = ? `