diff --git a/business/jxstore/report/report.go b/business/jxstore/report/report.go index 692825794..bbfdf5de4 100644 --- a/business/jxstore/report/report.go +++ b/business/jxstore/report/report.go @@ -4,9 +4,11 @@ import ( "errors" "fmt" "math" + "time" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" + "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" ) @@ -34,7 +36,32 @@ func GetStatisticsReportForAfsOrders(ctx *jxcontext.Context, storeIDs []int, fro return statisticsReportForOrdersList, err } -func StatisticsReportForStoreSkusPrice(ctx *jxcontext.Context, cityCodes, skuIDs []int) (err error) { +func StatisticsReportForStoreSkusPrice(ctx *jxcontext.Context, cityCodes, skuIDs []int) (priceRefer []*model.PriceReferSnapshot, err error) { db := dao.GetDB() - return dao.GetStatisticsReportForStoreSkusPrice(db, cityCodes, skuIDs) + return dao.GetPriceReferSnapshot(db, cityCodes, skuIDs) +} + +func BeginSavePriceRefer(ctx *jxcontext.Context, cityCodes, skuIDs []int) (err error) { + db := dao.GetDB() + snapshotAt := utils.Str2Time(time.Now().Format("2006-01-02")) + priceReferSnapshot, err := dao.GetStatisticsReportForStoreSkusPrice(db, cityCodes, skuIDs) + dao.Begin(db) + defer func() { + if r := recover(); r != nil || err != nil { + dao.Rollback(db) + if r != nil { + panic(r) + } + } + }() + for _, v := range priceReferSnapshot { + dao.WrapAddIDCULDEntity(v, ctx.GetUserName()) + v.SnapshotAt = snapshotAt + dao.DeleteEntity(db, v, "CityCode", "SkuID", "SnapshotAt") + if err = dao.CreateEntity(db, v); err != nil { + return err + } + } + dao.Commit(db) + return err } diff --git a/business/model/dao/report.go b/business/model/dao/report.go index bf53ac726..00552e06a 100644 --- a/business/model/dao/report.go +++ b/business/model/dao/report.go @@ -178,7 +178,82 @@ func GetGetStatisticsReportForAfsOrders(db *DaoDB, storeIDs []int, fromDate time return nil, err } -func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (err error) { - - return err +func GetStatisticsReportForStoreSkusPrice(db *DaoDB, cityCodes, skuIDs []int) (priceReferSnapshot []*model.PriceReferSnapshot, err error) { + sql := ` + SELECT b.city_code,a.sku_id, + MAX(a.price/100) max_price, + MIN(a.price/100) min_price, + ROUND(AVG(a.price/100),2) avg_price, + ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(a.price/100 ORDER BY a.price/100),',',Count(1)/2),',',-1),2) mid_price, + MAX(a.jd_price/100) max_jd_price, + MIN(a.jd_price/100) min_jd_price, + ROUND(AVG(a.jd_price/100),2) avg_jd_price, + ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(a.jd_price/100 ORDER BY a.jd_price/100),',',Count(1)/2),',',-1),2) mid_jd_price, + MAX(a.ebai_price/100) max_ebai_price, + MIN(a.ebai_price/100) min_ebai_price, + ROUND(AVG(a.ebai_price/100),2) avg_ebai_price, + ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(a.ebai_price/100 ORDER BY a.ebai_price/100),',',Count(1)/2),',',-1),2) mid_ebai_price, + MAX(a.mtwm_price/100) max_mtwm_price, + MIN(a.mtwm_price/100) min_mtwm_price, + ROUND(AVG(a.mtwm_price/100),2) avg_mtwm_price, + ROUND(SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(a.mtwm_price/100 ORDER BY a.mtwm_price/100),',',Count(1)/2),',',-1),2) mid_mtwm_price, + t1.max_sale_price, + t1.min_sale_price, + t1.avg_sale_price, + t1.max_vendor_price, + t1.min_vendor_price, + t1.avg_vendor_price + FROM store_sku_bind a + JOIN store b ON a.store_id = b.id AND b.deleted_at = ? + JOIN sku d ON a.sku_id = d.id AND d.deleted_at = ? + LEFT JOIN ( + SELECT SUM(t1.count),t1.sku_id,MAX(t1.sale_price/100) max_sale_price,MIN(t1.sale_price/100) min_sale_price,ROUND(AVG(t1.sale_price/100),2) avg_sale_price,MAX(t1.vendor_price/100) max_vendor_price,MIN(t1.vendor_price/100) min_vendor_price,ROUND(AVG(t1.vendor_price/100),2) avg_vendor_price + FROM order_sku t1 + WHERE t1.order_created_at BETWEEN ? AND NOW() + GROUP BY 2 + )t1 ON t1.sku_id = a.sku_id + WHERE 1=1 + ` + sqlParams := []interface{}{ + utils.DefaultTimeValue, + utils.DefaultTimeValue, + time.Now().AddDate(0, -1, 0), + } + 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" + if err = GetRows(db, &priceReferSnapshot, sql, sqlParams...); err == nil { + return priceReferSnapshot, nil + } + return nil, err +} + +func GetPriceReferSnapshot(db *DaoDB, cityCodes, skuIDs []int) (priceReferSnapshot []*model.PriceReferSnapshot, err error) { + sql := ` + SELECT * + FROM price_refer_snapshot + WHERE 1=1 + AND delete_at = ? + ` + sqlParams := []interface{}{ + utils.DefaultTimeValue, + } + if len(skuIDs) > 0 { + sql += " AND sku_id IN (" + GenQuestionMarks(len(skuIDs)) + ")" + sqlParams = append(sqlParams, skuIDs) + } + if len(cityCodes) > 0 { + sql += " AND city_code IN (" + GenQuestionMarks(len(cityCodes)) + ")" + sqlParams = append(sqlParams, cityCodes) + } + if err = GetRows(db, &priceReferSnapshot, sql, sqlParams...); err == nil { + return priceReferSnapshot, nil + } + return nil, err } diff --git a/business/model/store.go b/business/model/store.go index 0250028f4..58ab96a50 100644 --- a/business/model/store.go +++ b/business/model/store.go @@ -422,6 +422,47 @@ func (*StoreCourierMap) TableUnique() [][]string { } } +type PriceReferSnapshot struct { + ModelIDCULD + SnapshotAt time.Time `orm:"type(datetime)" json:"snapshotAt"` // 这个不同于CreatedAt,SnapshotAt是逻辑上的时间,CreatedAt是实际存储的时间 + CityCode int `json:"cityCode"` + SkuID int `orm:"column(sku_id)" json:"skuId"` + MaxPrice float64 `json:"maxPrice"` + MinPrice float64 `json:"minPrice"` + AvgPrice float64 `json:"avgPrice"` + MidPrice float64 `json:"midPrice"` + MaxJdPrice float64 `json:"maxJdPrice"` + MinJdPrice float64 `json:"minJdPrice"` + AvgJdPrice float64 `json:"avgJdPrice"` + MidJdPrice float64 `json:"midJdPrice"` + MaxEbaiPrice float64 `json:"maxEbaiPrice"` + MinEbaiPrice float64 `json:"minEbaiPrice"` + AvgEbaiPrice float64 `json:"avgEbaiPrice"` + MidEbaiPrice float64 `json:"midEbaiPrice"` + MaxMtwmPrice float64 `json:"maxMtwmPrice"` + MinMtwmPrice float64 `json:"minMtwmPrice"` + AvgMtwmPrice float64 `json:"avgMtwmPrice"` + MidMtwmPrice float64 `json:"midMtwmPrice"` + MaxSalePrice float64 `json:"maxSalePrice"` + MinSalePrice float64 `json:"minSalePrice"` + AvgSalePrice float64 `json:"avgSalePrice"` + MaxVendorPrice float64 `json:"maxVendorPrice"` + MinVendorPrice float64 `json:"minVendorPrice"` + AvgVendorPrice float64 `json:"avgVendorPrice"` +} + +func (*PriceReferSnapshot) TableUnique() [][]string { + return [][]string{ + []string{"CityCode", "SkuID", "SnapshotAt"}, + } +} + +func (*PriceReferSnapshot) TableIndex() [][]string { + return [][]string{ + []string{"SnapshotAt"}, + } +} + type VendorStoreSnapshot struct { ModelIDCULD diff --git a/controllers/jx_report.go b/controllers/jx_report.go index c4ce656f4..7d81093c4 100644 --- a/controllers/jx_report.go +++ b/controllers/jx_report.go @@ -61,8 +61,21 @@ func (c *ReportController) StatisticsReportForStoreSkusPrice() { c.callStatisticsReportForStoreSkusPrice(func(params *tReportStatisticsReportForStoreSkusPriceParams) (retVal interface{}, errCode string, err error) { var cityCodeList, skuIDList []int if err = jxutils.Strings2Objs(params.CityCodes, &cityCodeList, params.SkuIDs, &skuIDList); err == nil { - err = report.StatisticsReportForStoreSkusPrice(params.Ctx, cityCodeList, skuIDList) + retVal, err = report.StatisticsReportForStoreSkusPrice(params.Ctx, cityCodeList, skuIDList) } return retVal, "", err }) } + +// @Title 生成价格参考数据 +// @Description 生成价格参考数据 +// @Param token header string true "认证token" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /PriceRefer [post] +func (c *ReportController) PriceRefer() { + c.callPriceRefer(func(params *tReportPriceReferParams) (retVal interface{}, errCode string, err error) { + report.BeginSavePriceRefer(params.Ctx, nil, nil) + return retVal, "", err + }) +} diff --git a/globals/beegodb/beegodb.go b/globals/beegodb/beegodb.go index d7a7a8f67..4a491fd0c 100644 --- a/globals/beegodb/beegodb.go +++ b/globals/beegodb/beegodb.go @@ -38,6 +38,7 @@ func Init() { orm.RegisterModel(&model.PageShop{}) orm.RegisterModel(&model.VendorStoreSnapshot{}) + orm.RegisterModel(&model.PriceReferSnapshot{}) // orm.RegisterModel(&model.ActivityForSku{}) // orm.RegisterModel(&legacymodel.JxBadComments2{}) diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 67c2124b1..d969eb1f5 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -1044,6 +1044,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:ReportController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:ReportController"], + beego.ControllerComments{ + Method: "PriceRefer", + Router: `/PriceRefer`, + AllowHTTPMethods: []string{"post"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:ReportController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:ReportController"], beego.ControllerComments{ Method: "StatisticsReportForAfsOrders",