enhance(charge):为订阅费用管理增加缓存控制功能。
This commit is contained in:
parent
8d3ce16ce0
commit
4f722f2701
@ -1,10 +1,13 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"electricity_bill_calc/cache"
|
||||||
"electricity_bill_calc/config"
|
"electricity_bill_calc/config"
|
||||||
"electricity_bill_calc/exceptions"
|
"electricity_bill_calc/exceptions"
|
||||||
"electricity_bill_calc/global"
|
"electricity_bill_calc/global"
|
||||||
"electricity_bill_calc/model"
|
"electricity_bill_calc/model"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fufuok/utils"
|
"github.com/fufuok/utils"
|
||||||
@ -18,18 +21,6 @@ type _ChargeService struct{}
|
|||||||
var ChargeService _ChargeService
|
var ChargeService _ChargeService
|
||||||
|
|
||||||
func (c _ChargeService) CreateChargeRecord(charge *model.UserCharge, extendWithIgnoreSettle bool) error {
|
func (c _ChargeService) CreateChargeRecord(charge *model.UserCharge, extendWithIgnoreSettle bool) error {
|
||||||
// var seqs = make([]int64, 0)
|
|
||||||
// err := global.DBConn.Table(&model.UserCharge{}).Cols("seq").Find(&seqs)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// maxSeq := utils.Reduce(seqs, 0, func(acc, elem int64) int64 {
|
|
||||||
// if elem > acc {
|
|
||||||
// return elem
|
|
||||||
// } else {
|
|
||||||
// return acc
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
tx := global.DBConn.NewSession()
|
tx := global.DBConn.NewSession()
|
||||||
defer tx.Close()
|
defer tx.Close()
|
||||||
if err := tx.Begin(); err != nil {
|
if err := tx.Begin(); err != nil {
|
||||||
@ -52,6 +43,7 @@ func (c _ChargeService) CreateChargeRecord(charge *model.UserCharge, extendWithI
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cache.AbolishRelation("charge")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +76,8 @@ func (c _ChargeService) SettleCharge(seq int64, uid string) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cache.AbolishRelation("charge")
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +111,8 @@ func (c _ChargeService) RefundCharge(seq int64, uid string) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cache.AbolishRelation("charge")
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,6 +155,8 @@ func (c _ChargeService) CancelCharge(seq int64, uid string) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cache.AbolishRelation("charge")
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,13 +165,20 @@ func (_ChargeService) updateUserExpiration(tx *xorm.Session, uid string, expirat
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
}
|
}
|
||||||
|
cache.AbolishRelation("user")
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string, page int) ([]model.ChargeWithName, int64, error) {
|
func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string, page int) ([]model.ChargeWithName, int64, error) {
|
||||||
var cond = builder.NewCond()
|
var (
|
||||||
|
cond = builder.NewCond()
|
||||||
|
condition = make([]string, 0)
|
||||||
|
)
|
||||||
|
condition = append(condition, strconv.Itoa(page))
|
||||||
if len(keyword) != 0 {
|
if len(keyword) != 0 {
|
||||||
cond = cond.And(builder.Like{"d.name", keyword}.Or(builder.Like{"d.abbr", keyword}))
|
cond = cond.And(builder.Like{"d.name", keyword}.Or(builder.Like{"d.abbr", keyword}))
|
||||||
|
condition = append(condition, keyword)
|
||||||
}
|
}
|
||||||
if len(beginDate) != 0 {
|
if len(beginDate) != 0 {
|
||||||
beginTime, err := time.ParseInLocation("2006-01-02", beginDate, time.Local)
|
beginTime, err := time.ParseInLocation("2006-01-02", beginDate, time.Local)
|
||||||
@ -182,6 +187,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||||||
return make([]model.ChargeWithName, 0), 0, err
|
return make([]model.ChargeWithName, 0), 0, err
|
||||||
}
|
}
|
||||||
cond = cond.And(builder.Gte{"c.created_at": beginTime})
|
cond = cond.And(builder.Gte{"c.created_at": beginTime})
|
||||||
|
condition = append(condition, strconv.FormatInt(beginTime.Unix(), 10))
|
||||||
}
|
}
|
||||||
if len(endDate) != 0 {
|
if len(endDate) != 0 {
|
||||||
endTime, err := time.ParseInLocation("2006-01-02", endDate, time.Local)
|
endTime, err := time.ParseInLocation("2006-01-02", endDate, time.Local)
|
||||||
@ -190,9 +196,17 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||||||
return make([]model.ChargeWithName, 0), 0, err
|
return make([]model.ChargeWithName, 0), 0, err
|
||||||
}
|
}
|
||||||
cond = cond.And(builder.Lte{"c.created_at": endTime})
|
cond = cond.And(builder.Lte{"c.created_at": endTime})
|
||||||
|
condition = append(condition, strconv.FormatInt(endTime.Unix(), 10))
|
||||||
}
|
}
|
||||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||||
total, err := global.DBConn.
|
var (
|
||||||
|
total int64
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if cachedTotal, _ := cache.RetreiveCount("charge_with_name", condition...); cachedTotal != -1 {
|
||||||
|
total = cachedTotal
|
||||||
|
} else {
|
||||||
|
total, err = global.DBConn.
|
||||||
Alias("d").
|
Alias("d").
|
||||||
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
|
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
|
||||||
Where(cond).
|
Where(cond).
|
||||||
@ -201,7 +215,12 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, -1, err
|
return nil, -1, err
|
||||||
}
|
}
|
||||||
|
cache.CacheCount("charge", "charge_with_name", total, condition...)
|
||||||
|
}
|
||||||
charges := make([]model.ChargeWithName, 0)
|
charges := make([]model.ChargeWithName, 0)
|
||||||
|
if cachedCharges, _ := cache.RetreiveSearch[[]model.ChargeWithName]("charge_with_name", condition...); cachedCharges != nil {
|
||||||
|
return *cachedCharges, total, nil
|
||||||
|
}
|
||||||
err = global.DBConn.
|
err = global.DBConn.
|
||||||
Alias("d").
|
Alias("d").
|
||||||
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
|
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
|
||||||
@ -209,10 +228,14 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||||
NoAutoCondition().
|
NoAutoCondition().
|
||||||
Find(&charges)
|
Find(&charges)
|
||||||
|
cache.CacheSearch(charges, "charge", "charge_with_name", condition...)
|
||||||
return charges, total, err
|
return charges, total, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
|
func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
|
||||||
|
if cachedValid, _ := cache.RetreiveSearch[time.Time]("last_valid_charge", uid); cachedValid != nil {
|
||||||
|
return *cachedValid, nil
|
||||||
|
}
|
||||||
veryBlankTime, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
|
veryBlankTime, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
|
||||||
var records []string
|
var records []string
|
||||||
err := global.DBConn.
|
err := global.DBConn.
|
||||||
@ -234,5 +257,6 @@ func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
|
|||||||
return acc
|
return acc
|
||||||
}
|
}
|
||||||
}, veryBlankTime)
|
}, veryBlankTime)
|
||||||
|
cache.CacheSearch(lastValid, "charge", "last_valid_charge", uid)
|
||||||
return lastValid, nil
|
return lastValid, nil
|
||||||
}
|
}
|
||||||
|
@ -262,7 +262,7 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return make([]model.JoinedUserDetail, 0), err
|
return make([]model.JoinedUserDetail, 0), err
|
||||||
}
|
}
|
||||||
cache.CacheSearch(users, "user", "join_user_detail", strconv.Itoa(limit))
|
cache.CacheSearch(users, "user", "join_user_detail", keyword, strconv.Itoa(limit))
|
||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user