门店价格插叙

This commit is contained in:
suyl
2019-12-07 20:26:52 +08:00
parent 6eb984987d
commit 4caf019e70
7 changed files with 124 additions and 26 deletions

View File

@@ -2413,16 +2413,43 @@ func StoreStatus2Chinese(status int) (str string) {
}
}
func GetStorePriceScore(ctx *jxcontext.Context, storeIDs []int, snapDate string, offset, pageSize int) (storePriceScoreEx *dao.StorePriceScoreEx, err error) {
func GetStorePriceScore(ctx *jxcontext.Context, storeIDs []int, fromScore, toScore, sort int, snapDate string, offset, pageSize int) (storePriceScoreEx *dao.StorePriceScoreEx, err error) {
var snapDateParam time.Time
db := dao.GetDB()
if snapDate != "" {
snapDateParam = utils.Str2Time(snapDate)
}
storePriceScore, totalCount, err := dao.GetStorePriceScore(db, storeIDs, snapDateParam, offset, pageSize)
storePriceScore, totalCount, err := dao.GetStorePriceScore(db, storeIDs, fromScore, toScore, sort, snapDateParam, offset, pageSize)
storePriceScoreEx = &dao.StorePriceScoreEx{
StorePriceScoreList: storePriceScore,
TotalCount: totalCount,
}
return storePriceScoreEx, err
}
func CreateStorePriceScore(ctx *jxcontext.Context) (err error) {
db := dao.GetDB()
snapshotAt := utils.Time2Date(time.Now())
storePriceScoreSnapshot, err := dao.GetStorePriceScoreSnapshot(db, snapshotAt)
if len(storePriceScoreSnapshot) > 0 {
dao.Begin(db)
defer func() {
if r := recover(); r != nil || err != nil {
dao.Rollback(db)
if r != nil {
panic(r)
}
}
}()
dao.DeleteEntity(db, storePriceScoreSnapshot[0], "SnapshotAt")
for _, v := range storePriceScoreSnapshot {
dao.WrapAddIDCULDEntity(v, ctx.GetUserName())
v.SnapshotAt = snapshotAt
if err = dao.CreateEntity(db, v); err != nil {
return err
}
}
dao.Commit(db)
}
return err
}

View File

@@ -134,6 +134,9 @@ func Init() {
ScheduleTimerFunc("BeginSavePriceRefer", func() {
report.BeginSavePriceRefer(jxcontext.AdminCtx, nil, nil)
}, createStorePriceTimeList)
ScheduleTimerFunc("CreateStorePriceScore", func() {
cms.CreateStorePriceScore(jxcontext.AdminCtx)
}, openRemoteStoreTimeList)
}
ScheduleTimerFunc("AutoSaleStoreSku", func() {
cms.AutoSaleStoreSku(jxcontext.AdminCtx, nil, false)

View File

@@ -57,7 +57,7 @@ type CityBrankBranch struct {
}
type StorePriceScore struct {
StoreID int `json:"storeID"`
StoreID int `orm:"column(store_id)" json:"storeID"`
StoreName string `json:"storeName"`
StoreScore float64 `json:"storeScore"`
CityName string `json:"cityName"`
@@ -475,20 +475,18 @@ func GetStoreMapsListWithoutDisabled(db *DaoDB, vendorIDs []int, status int) (st
return storeMapList, err
}
func GetStorePriceScore(db *DaoDB, storeIDs []int, snapDate time.Time, offset, pageSize int) (StorePriceScore []*StorePriceScore, totalCount int, err error) {
func GetStorePriceScore(db *DaoDB, storeIDs []int, fromScore, toScore, sort int, snapDate time.Time, offset, pageSize int) (StorePriceScore []*StorePriceScore, totalCount int, err error) {
sql := `
SELECT SQL_CALC_FOUND_ROWS
t1.* FROM(
SELECT c.store_id,d.name store_name,e.name city_name,ROUND(count(c.price/100 <= a.mid_price or NULL)/count(*)*100,2) store_score
FROM price_refer_snapshot a
JOIN store_sku_bind c ON c.sku_id = a.sku_id AND c.status = 1 AND c.deleted_at = ?
JOIN store d ON c.store_id = d.id AND d.city_code = a.city_code AND d.deleted_at = ? AND d.status !=-2
JOIN place e ON e.code = d.city_code
SELECT SQL_CALC_FOUND_ROWS a.store_id,score store_score,e.name city_name,b.name store_name
FROM store_price_score_snapshot a
JOIN store b ON b.id = a.store_id
JOIN place e ON e.code = b.city_code
WHERE 1=1
`
AND a.score BETWEEN ? AND ?
`
sqlParams := []interface{}{
utils.DefaultTimeValue,
utils.DefaultTimeValue,
fromScore,
toScore,
}
if len(storeIDs) > 0 {
sql += " AND c.store_id IN(" + GenQuestionMarks(len(storeIDs)) + ")"
@@ -498,11 +496,12 @@ func GetStorePriceScore(db *DaoDB, storeIDs []int, snapDate time.Time, offset, p
sql += " AND a.snapshot_at = ?"
sqlParams = append(sqlParams, snapDate)
}
sql += `
GROUP BY c.store_id)t1
ORDER BY t1.store_score
LIMIT ? OFFSET ?
`
if sort == 0 {
sql += " ORDER BY a.score"
} else {
sql += " ORDER BY a.score DESC"
}
sql += " LIMIT ? OFFSET ?"
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Commit(db)
@@ -511,3 +510,27 @@ func GetStorePriceScore(db *DaoDB, storeIDs []int, snapDate time.Time, offset, p
}
return StorePriceScore, totalCount, err
}
func GetStorePriceScoreSnapshot(db *DaoDB, snapDate time.Time) (storePriceScoreSnapshot []*model.StorePriceScoreSnapshot, err error) {
sql := `
SELECT c.store_id,ROUND(count(c.price/100 <= a.mid_price or NULL)/count(*)*100,2) score
FROM price_refer_snapshot a
JOIN store_sku_bind c ON c.sku_id = a.sku_id AND c.status = 1 AND c.deleted_at = ?
JOIN store d ON c.store_id = d.id AND d.city_code = a.city_code AND d.deleted_at = ? AND d.status != ?
WHERE 1=1
`
sqlParams := []interface{}{
utils.DefaultTimeValue,
utils.DefaultTimeValue,
model.StoreStatusDisabled,
}
if !utils.IsTimeZero(snapDate) {
sql += " AND a.snapshot_at = ?"
sqlParams = append(sqlParams, snapDate)
}
sql += `
GROUP BY c.store_id
`
err = GetRows(db, &storePriceScoreSnapshot, sql, sqlParams...)
return storePriceScoreSnapshot, err
}

View File

@@ -465,6 +465,25 @@ func (*PriceReferSnapshot) TableIndex() [][]string {
}
}
type StorePriceScoreSnapshot struct {
ModelIDCULD
SnapshotAt time.Time `orm:"type(datetime)" json:"snapshotAt"` // 这个不同于CreatedAtSnapshotAt是逻辑上的时间CreatedAt是实际存储的时间
StoreID int `orm:"column(store_id)" json:"storeID"`
Score float64 `json:"score"`
}
func (*StorePriceScoreSnapshot) TableUnique() [][]string {
return [][]string{
[]string{"StoreID", "Score", "SnapshotAt"},
}
}
func (*StorePriceScoreSnapshot) TableIndex() [][]string {
return [][]string{
[]string{"SnapshotAt"},
}
}
type VendorStoreSnapshot struct {
ModelIDCULD