enhance(report):公示报表部分接口增加缓存支持。

This commit is contained in:
徐涛 2022-08-25 23:21:56 +08:00
parent cb492a31f7
commit 4baadda299
5 changed files with 180 additions and 34 deletions

View File

@ -1,9 +1,11 @@
package service package service
import ( import (
"electricity_bill_calc/cache"
"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"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"xorm.io/builder" "xorm.io/builder"
@ -241,5 +243,6 @@ func (_CalculateService) ComprehensivelyCalculateReport(reportId string) (err er
tx.Rollback() tx.Rollback()
return return
} }
cache.AbolishRelation(fmt.Sprintf("publicity_%s", reportId))
return return
} }

View File

@ -1,6 +1,7 @@
package service package service
import ( import (
"electricity_bill_calc/cache"
"electricity_bill_calc/config" "electricity_bill_calc/config"
"electricity_bill_calc/excel" "electricity_bill_calc/excel"
"electricity_bill_calc/exceptions" "electricity_bill_calc/exceptions"
@ -8,6 +9,7 @@ import (
"electricity_bill_calc/model" "electricity_bill_calc/model"
"fmt" "fmt"
"io" "io"
"strconv"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
@ -21,6 +23,8 @@ type _EndUserService struct{}
var EndUserService _EndUserService var EndUserService _EndUserService
func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) { func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) {
var conditions = make([]string, 0)
conditions = append(conditions, reportId, strconv.Itoa(page))
cond := builder.NewCond().And(builder.Eq{"report_id": reportId}) cond := builder.NewCond().And(builder.Eq{"report_id": reportId})
if len(keyword) > 0 { if len(keyword) > 0 {
cond = cond.And( cond = cond.And(
@ -29,39 +33,61 @@ func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) (
Or(builder.Like{"contact_phone", keyword}). Or(builder.Like{"contact_phone", keyword}).
Or(builder.Like{"meter_04kv_id", keyword}), Or(builder.Like{"meter_04kv_id", keyword}),
) )
conditions = append(conditions, keyword)
} }
total, err := global.DBConn. var (
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("end_user_detail", conditions...); cachedTotal != -1 {
total = cachedTotal
} else {
total, err = global.DBConn.
Table(&model.EndUserDetail{}). Table(&model.EndUserDetail{}).
Where(cond). Where(cond).
Count() Count()
if err != nil { if err != nil {
return make([]model.EndUserDetail, 0), -1, err return make([]model.EndUserDetail, 0), -1, err
} }
cache.CacheCount([]string{"end_user", "report", "park"}, "end_user_detail", total, conditions...)
}
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
if cachedEndUsers, _ := cache.RetreiveSearch[[]model.EndUserDetail]("end_user_detail", conditions...); cachedEndUsers != nil {
return *cachedEndUsers, total, nil
}
endUsers := make([]model.EndUserDetail, 0) endUsers := make([]model.EndUserDetail, 0)
err = global.DBConn. err = global.DBConn.
Where(cond). Where(cond).
Limit(config.ServiceSettings.ItemsPageSize, startItem). Limit(config.ServiceSettings.ItemsPageSize, startItem).
Asc("seq"). Asc("seq").
Find(&endUsers) Find(&endUsers)
cache.CacheSearch(endUsers, []string{"end_user", "report", "park"}, "end_user_detail", conditions...)
return endUsers, total, err return endUsers, total, err
} }
func (_EndUserService) AllEndUserRecord(reportId string) ([]model.EndUserDetail, error) { func (_EndUserService) AllEndUserRecord(reportId string) ([]model.EndUserDetail, error) {
if cachedEndUsers, _ := cache.RetreiveSearch[[]model.EndUserDetail]("end_user_detail", "report", reportId); cachedEndUsers != nil {
return *cachedEndUsers, nil
}
users := make([]model.EndUserDetail, 0) users := make([]model.EndUserDetail, 0)
err := global.DBConn. err := global.DBConn.
Where(builder.Eq{"report_id": reportId}). Where(builder.Eq{"report_id": reportId}).
Asc("seq"). Asc("seq").
Find(&users) Find(&users)
cache.CacheSearch(users, []string{"end_user_detail", "report", "park"}, "end_user_detail", "report", reportId)
return users, err return users, err
} }
func (_EndUserService) FetchSpecificEndUserRecord(reportId, parkId, meterId string) (*model.EndUserDetail, error) { func (_EndUserService) FetchSpecificEndUserRecord(reportId, parkId, meterId string) (*model.EndUserDetail, error) {
if cachedEndUser, _ := cache.RetreiveEntity[model.EndUserDetail]("end_user_detail", fmt.Sprintf("%s_%s_%s", reportId, parkId, meterId)); cachedEndUser != nil {
return cachedEndUser, nil
}
record := new(model.EndUserDetail) record := new(model.EndUserDetail)
_, err := global.DBConn. _, err := global.DBConn.
ID(schemas.NewPK(reportId, parkId, meterId)). ID(schemas.NewPK(reportId, parkId, meterId)).
NoAutoCondition(). NoAutoCondition().
Get(record) Get(record)
cache.CacheEntity(record, []string{"end_user_detail", "report", "park"}, "end_user_detail", fmt.Sprintf("%s_%s_%s", reportId, parkId, meterId))
return record, err return record, err
} }
@ -86,6 +112,7 @@ func (_EndUserService) UpdateEndUserRegisterRecord(tx *xorm.Session, record mode
"valley", "valley",
). ).
Update(record) Update(record)
cache.AbolishRelation("end_user_detail")
return return
} }
@ -155,6 +182,7 @@ func (es _EndUserService) BatchImportNonPVRegister(reportId string, file io.Read
tx.Rollback() tx.Rollback()
errs.AddError(es.newVirtualExcelAnalysisError(err)) errs.AddError(es.newVirtualExcelAnalysisError(err))
} }
cache.AbolishRelation("end_user_detail")
return errs return errs
} }
@ -220,5 +248,6 @@ func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader)
tx.Rollback() tx.Rollback()
errs.AddError(es.newVirtualExcelAnalysisError(err)) errs.AddError(es.newVirtualExcelAnalysisError(err))
} }
cache.AbolishRelation("end_user_detail")
return errs return errs
} }

View File

@ -1,12 +1,14 @@
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"
"electricity_bill_calc/tools" "electricity_bill_calc/tools"
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/fufuok/utils" "github.com/fufuok/utils"
@ -21,6 +23,9 @@ type _ReportService struct{}
var ReportService _ReportService var ReportService _ReportService
func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewestReport, error) { func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewestReport, error) {
if cachedParks, _ := cache.RetreiveSearch[[]model.ParkNewestReport]("park_newest_report", uid); cachedParks != nil {
return *cachedParks, nil
}
parks := make([]model.ParkNewestReport, 0) parks := make([]model.ParkNewestReport, 0)
err := global.DBConn. err := global.DBConn.
Alias("p"). Alias("p").
@ -46,11 +51,15 @@ func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewest
}, },
make(map[string]model.ParkNewestReport, 0), make(map[string]model.ParkNewestReport, 0),
) )
cache.CacheSearch(reducedParks, []string{"park", "report"}, "park_newest_report", uid)
return lo.Values(reducedParks), nil return lo.Values(reducedParks), nil
} }
func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, error) { func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, error) {
reports := make([]model.Report, 0) reports := make([]model.Report, 0)
if cachedReport, _ := cache.RetreiveSearch[[]model.Report]("report", "user", uid); cachedReport != nil {
reports = *cachedReport
} else {
err := global.DBConn. err := global.DBConn.
Table("report").Alias("r"). Table("report").Alias("r").
Join("INNER", []string{"park", "p"}, "r.park_id=p.id"). Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
@ -59,6 +68,8 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro
if err != nil { if err != nil {
return false, nil return false, nil
} }
cache.CacheSearch(reports, []string{"report", "park"}, "park", "user", uid)
}
// 检查给定的期数在目前的记录中是否已经存在 // 检查给定的期数在目前的记录中是否已经存在
exists := lo.Reduce( exists := lo.Reduce(
reports, reports,
@ -223,16 +234,21 @@ func (_ReportService) InitializeNewReport(parkId string, period time.Time) (stri
tx.Rollback() tx.Rollback()
return "", err return "", err
} }
cache.AbolishRelation("report")
return newReport.Id, nil return newReport.Id, nil
} }
func (_ReportService) RetreiveReportIndex(rid string) (*model.Report, error) { func (_ReportService) RetreiveReportIndex(rid string) (*model.Report, error) {
if cachedReport, _ := cache.RetreiveEntity[model.Report]("report", rid); cachedReport != nil {
return cachedReport, nil
}
reports := make([]model.Report, 0) reports := make([]model.Report, 0)
err := global.DBConn.Where(builder.Eq{"id": rid}).Find(&reports) err := global.DBConn.Where(builder.Eq{"id": rid}).Find(&reports)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(reports) > 0 { if len(reports) > 0 {
cache.CacheEntity(reports[0], []string{fmt.Sprintf("report_%s", rid), "park"}, "report", rid)
return &reports[0], nil return &reports[0], nil
} else { } else {
return nil, nil return nil, nil
@ -240,16 +256,23 @@ func (_ReportService) RetreiveReportIndex(rid string) (*model.Report, error) {
} }
func (_ReportService) RetreiveReportSummary(rid string) (*model.ReportSummary, error) { func (_ReportService) RetreiveReportSummary(rid string) (*model.ReportSummary, error) {
if cachedSummary, _ := cache.RetreiveEntity[model.ReportSummary]("report_summary", rid); cachedSummary != nil {
return cachedSummary, nil
}
var summary = new(model.ReportSummary) var summary = new(model.ReportSummary)
_, err := global.DBConn.ID(rid).NoAutoCondition().Get(summary) _, err := global.DBConn.ID(rid).NoAutoCondition().Get(summary)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cache.CacheEntity(summary, []string{fmt.Sprintf("report_%s", rid), "park"}, "report_summary", rid)
return summary, nil return summary, nil
} }
func (_ReportService) UpdateReportSummary(summary *model.ReportSummary) error { func (_ReportService) UpdateReportSummary(summary *model.ReportSummary) error {
_, err := global.DBConn.ID(summary.ReportId).Cols("overall", "overall_fee", "critical", "critical_fee", "peak", "peak_fee", "valley", "valley_fee", "basic_fee", "adjust_fee").Update(summary) _, err := global.DBConn.ID(summary.ReportId).Cols("overall", "overall_fee", "critical", "critical_fee", "peak", "peak_fee", "valley", "valley_fee", "basic_fee", "adjust_fee").Update(summary)
if err == nil {
cache.AbolishRelation(fmt.Sprintf("report_%s", summary.ReportId))
}
return err return err
} }
@ -292,21 +315,27 @@ func (_ReportService) CalculateSummaryAndFinishStep(reportId string) error {
tx.Rollback() tx.Rollback()
return err return err
} }
cache.AbolishRelation(fmt.Sprintf("report_%s", summary.ReportId))
return nil return nil
} }
func (_ReportService) FetchWillDulutedMaintenanceFees(reportId string) ([]model.WillDilutedFee, error) { func (_ReportService) FetchWillDulutedMaintenanceFees(reportId string) ([]model.WillDilutedFee, error) {
if cachedFees, _ := cache.RetreiveSearch[[]model.WillDilutedFee]("will_diluted_fee", "report", reportId); cachedFees != nil {
return *cachedFees, nil
}
fees := make([]model.WillDilutedFee, 0) fees := make([]model.WillDilutedFee, 0)
err := global.DBConn.Where(builder.Eq{"report_id": reportId}).Asc("created_at").Find(&fees) err := global.DBConn.Where(builder.Eq{"report_id": reportId}).Asc("created_at").Find(&fees)
if err != nil { if err != nil {
return make([]model.WillDilutedFee, 0), nil return make([]model.WillDilutedFee, 0), nil
} }
cache.CacheSearch(fees, []string{"will_diluted_fee", fmt.Sprintf("report_%s", reportId), "park"}, "will_diluted_fee", "report", reportId)
return fees, nil return fees, nil
} }
func (_ReportService) CreateTemporaryWillDilutedMaintenanceFee(fee model.WillDilutedFee) error { func (_ReportService) CreateTemporaryWillDilutedMaintenanceFee(fee model.WillDilutedFee) error {
fee.Id = utils.UUIDString() fee.Id = utils.UUIDString()
_, err := global.DBConn.Insert(fee) _, err := global.DBConn.Insert(fee)
cache.AbolishRelation("will_diluted_fee")
return err return err
} }
@ -333,38 +362,47 @@ func (_ReportService) BatchSaveMaintenanceFee(reportId string, fees []model.Will
tx.Rollback() tx.Rollback()
return err return err
} }
cache.AbolishRelation("will_diluted_fee")
return nil return nil
} }
func (_ReportService) UpdateMaintenanceFee(feeId string, updates map[string]interface{}) (err error) { func (_ReportService) UpdateMaintenanceFee(feeId string, updates map[string]interface{}) (err error) {
_, err = global.DBConn.Table(new(model.WillDilutedFee)).ID(feeId).Update(updates) _, err = global.DBConn.Table(new(model.WillDilutedFee)).ID(feeId).Update(updates)
cache.AbolishRelation("will_diluted_fee")
return return
} }
func (_ReportService) DeleteWillDilutedFee(fee string) (err error) { func (_ReportService) DeleteWillDilutedFee(fee string) (err error) {
_, err = global.DBConn.ID(fee).NoAutoCondition().Delete(new(model.WillDilutedFee)) _, err = global.DBConn.ID(fee).NoAutoCondition().Delete(new(model.WillDilutedFee))
cache.AbolishRelation("will_diluted_fee")
return return
} }
func (_ReportService) ProgressReportWillDilutedFee(report model.Report) (err error) { func (_ReportService) ProgressReportWillDilutedFee(report model.Report) (err error) {
report.StepState.WillDiluted = true report.StepState.WillDiluted = true
_, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report) _, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report)
cache.AbolishRelation(fmt.Sprintf("report_%s", report.Id))
return return
} }
func (_ReportService) ProgressReportRegisterEndUser(report model.Report) (err error) { func (_ReportService) ProgressReportRegisterEndUser(report model.Report) (err error) {
report.StepState.Submeter = true report.StepState.Submeter = true
_, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report) _, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report)
cache.AbolishRelation(fmt.Sprintf("report_%s", report.Id))
return return
} }
func (_ReportService) ProgressReportCalculate(report model.Report) (err error) { func (_ReportService) ProgressReportCalculate(report model.Report) (err error) {
report.StepState.Calculate = true report.StepState.Calculate = true
_, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report) _, err = global.DBConn.ID(report.Id).Cols("step_state").Update(report)
cache.AbolishRelation(fmt.Sprintf("report_%s", report.Id))
return return
} }
func (_ReportService) RetreiveParkEndUserMeterType(reportId string) (int, error) { func (_ReportService) RetreiveParkEndUserMeterType(reportId string) (int, error) {
if cachedType, _ := cache.RetreiveEntity[int]("park_end_user_meter_type", fmt.Sprintf("report_%s", reportId)); cachedType != nil {
return *cachedType, nil
}
var types = make([]int, 0) var types = make([]int, 0)
err := global.DBConn. err := global.DBConn.
Table("park").Alias("p"). Table("park").Alias("p").
@ -378,6 +416,7 @@ func (_ReportService) RetreiveParkEndUserMeterType(reportId string) (int, error)
if len(types) == 0 { if len(types) == 0 {
return -1, nil return -1, nil
} }
cache.CacheEntity(types[0], []string{fmt.Sprintf("report_%s", reportId), "park"}, "park_end_user_meter_type", fmt.Sprintf("report_%s", reportId))
return types[0], nil return types[0], nil
} }
@ -386,19 +425,26 @@ func (_ReportService) PublishReport(report model.Report) (err error) {
report.PublishedAt = lo.ToPtr(time.Now()) report.PublishedAt = lo.ToPtr(time.Now())
report.StepState.Publish = true report.StepState.Publish = true
_, err = global.DBConn.ID(report.Id).Cols("step_state", "published", "published_at").Update(report) _, err = global.DBConn.ID(report.Id).Cols("step_state", "published", "published_at").Update(report)
cache.AbolishRelation("report")
cache.AbolishRelation(fmt.Sprintf("report_%s", report.Id))
return return
} }
func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword string, requestPeriod *time.Time, requestPage int) ([]model.JoinedReportForWithdraw, int64, error) { func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword string, requestPeriod *time.Time, requestPage int) ([]model.JoinedReportForWithdraw, int64, error) {
var conditions = make([]string, 0)
conditions = append(conditions, strconv.Itoa(requestPage))
cond := builder.NewCond().And(builder.Eq{"r.published": true}) cond := builder.NewCond().And(builder.Eq{"r.published": true})
if len(requestUser) > 0 { if len(requestUser) > 0 {
cond = cond.And(builder.Eq{"u.id": requestUser}) cond = cond.And(builder.Eq{"u.id": requestUser})
conditions = append(conditions, requestUser)
} }
if len(requestPark) > 0 { if len(requestPark) > 0 {
cond = cond.And(builder.Eq{"p.id": requestPark}) cond = cond.And(builder.Eq{"p.id": requestPark})
conditions = append(conditions, requestPark)
} }
if requestPeriod != nil { if requestPeriod != nil {
cond = cond.And(builder.Eq{"r.period": *requestPeriod}) cond = cond.And(builder.Eq{"r.period": *requestPeriod})
conditions = append(conditions, strconv.FormatInt(requestPeriod.Unix(), 10))
} }
if len(requestKeyword) > 0 { if len(requestKeyword) > 0 {
cond = cond.And( cond = cond.And(
@ -409,7 +455,15 @@ func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword stri
Or(builder.Like{"u.address", requestKeyword}). Or(builder.Like{"u.address", requestKeyword}).
Or(builder.Like{"p.address", requestKeyword}), Or(builder.Like{"p.address", requestKeyword}),
) )
conditions = append(conditions, requestKeyword)
} }
var (
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 {
total = cachedTotal
} else {
total, err := global.DBConn. total, err := global.DBConn.
Table("report").Alias("r"). Table("report").Alias("r").
Join("INNER", []string{"park", "p"}, "p.id=r.park_id"). Join("INNER", []string{"park", "p"}, "p.id=r.park_id").
@ -419,7 +473,12 @@ func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword stri
if err != nil { if err != nil {
return make([]model.JoinedReportForWithdraw, 0), -1, err return make([]model.JoinedReportForWithdraw, 0), -1, err
} }
cache.CacheCount([]string{"report", "park"}, "join_report_for_withdraw", total, conditions...)
}
startItem := (requestPage - 1) * config.ServiceSettings.ItemsPageSize startItem := (requestPage - 1) * config.ServiceSettings.ItemsPageSize
if cachedRecords, _ := cache.RetreiveSearch[[]model.JoinedReportForWithdraw]("join_report_for_withdraw", conditions...); cachedRecords != nil {
return *cachedRecords, total, nil
}
records := make([]model.JoinedReportForWithdraw, 0) records := make([]model.JoinedReportForWithdraw, 0)
err = global.DBConn. err = global.DBConn.
Table("report").Alias("r"). Table("report").Alias("r").
@ -428,10 +487,14 @@ func (_ReportService) SearchReport(requestUser, requestPark, requestKeyword stri
Where(cond). Where(cond).
Limit(config.ServiceSettings.ItemsPageSize, startItem). Limit(config.ServiceSettings.ItemsPageSize, startItem).
Find(&records) Find(&records)
cache.CacheSearch(records, []string{"report", "park"}, "join_report_for_withdraw", conditions...)
return records, total, err return records, total, err
} }
func (_ReportService) AssembleReportPublicity(reportId string) (*model.Publicity, error) { func (_ReportService) AssembleReportPublicity(reportId string) (*model.Publicity, error) {
if cachedPublicity, _ := cache.RetreiveEntity[model.Publicity]("publicity", reportId); cachedPublicity != nil {
return cachedPublicity, nil
}
// 资料准备 // 资料准备
var reportIndex = new(model.Report) var reportIndex = new(model.Report)
has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(reportIndex) has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(reportIndex)
@ -570,7 +633,7 @@ func (_ReportService) AssembleReportPublicity(reportId string) (*model.Publicity
}, },
) )
return &model.Publicity{ publicity := &model.Publicity{
Report: *reportIndex, Report: *reportIndex,
Park: *parkDetail, Park: *parkDetail,
User: *userDetail, User: *userDetail,
@ -581,5 +644,8 @@ func (_ReportService) AssembleReportPublicity(reportId string) (*model.Publicity
OtherCollections: otherCollection, OtherCollections: otherCollection,
Maintenance: maintenanceFees, Maintenance: maintenanceFees,
EndUserDetails: endUsers, EndUserDetails: endUsers,
}, nil }
cache.CacheEntity(publicity, []string{fmt.Sprintf("publicity_%s", reportId), "report", "park"}, "publicity", reportId)
return publicity, nil
} }

View File

@ -1,6 +1,7 @@
package service package service
import ( import (
"electricity_bill_calc/cache"
"electricity_bill_calc/global" "electricity_bill_calc/global"
"electricity_bill_calc/model" "electricity_bill_calc/model"
@ -13,24 +14,41 @@ type _StatisticsService struct{}
var StatisticsService _StatisticsService var StatisticsService _StatisticsService
func (_StatisticsService) EnabledEnterprises() (int64, error) { func (_StatisticsService) EnabledEnterprises() (int64, error) {
return global.DBConn. if cachedCount, _ := cache.RetreiveCount("enabled_ent"); cachedCount != -1 {
return cachedCount, nil
}
c, err := global.DBConn.
Table(new(model.User)). Table(new(model.User)).
Where(builder.Eq{"type": 0, "enabled": true}). Where(builder.Eq{"type": 0, "enabled": true}).
Count() Count()
if err == nil {
cache.CacheCount([]string{"user"}, "enabled_ent", c)
}
return c, err
} }
func (_StatisticsService) EnabledParks(userIds ...string) (int64, error) { func (_StatisticsService) EnabledParks(userIds ...string) (int64, error) {
if cachedParks, _ := cache.RetreiveCount("enabled_parks", userIds...); cachedParks != -1 {
return cachedParks, nil
}
cond := builder.NewCond().And(builder.Eq{"enabled": true}) cond := builder.NewCond().And(builder.Eq{"enabled": true})
if len(userIds) > 0 { if len(userIds) > 0 {
cond = cond.And(builder.Eq{"user_id": userIds}) cond = cond.And(builder.Eq{"user_id": userIds})
} }
return global.DBConn. c, err := global.DBConn.
Table(new(model.Park)). Table(new(model.Park)).
Where(cond). Where(cond).
Count() Count()
if err == nil {
cache.CacheCount([]string{"user", "park"}, "enabled_parks", c, userIds...)
}
return c, err
} }
func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPeriodStatistics, error) { func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPeriodStatistics, error) {
if cachedState, _ := cache.RetreiveSearch[[]model.ParkPeriodStatistics]("park_period_stat", userIds...); cachedState != nil {
return *cachedState, nil
}
cond := builder.NewCond().And(builder.Eq{"p.enabled": true, "r.published": true}) cond := builder.NewCond().And(builder.Eq{"p.enabled": true, "r.published": true})
if len(userIds) > 0 { if len(userIds) > 0 {
cond = cond.And(builder.Eq{"p.user_id": userIds}) cond = cond.And(builder.Eq{"p.user_id": userIds})
@ -58,5 +76,6 @@ func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPerio
groupedParks[p.Id] = p groupedParks[p.Id] = p
} }
} }
cache.CacheSearch(lo.Values(groupedParks), []string{"user", "park"}, "park_period_stat", userIds...)
return lo.Values(groupedParks), nil return lo.Values(groupedParks), nil
} }

View File

@ -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/samber/lo" "github.com/samber/lo"
@ -56,11 +59,15 @@ func (_WithdrawService) ApplyWithdraw(reportId string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
cache.AbolishRelation("report")
cache.AbolishRelation(fmt.Sprintf("publicity_%s", reportId))
return true, nil return true, nil
} }
func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) { func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) {
var conditions = make([]string, 0)
cond := builder.NewCond() cond := builder.NewCond()
conditions = append(conditions, strconv.Itoa(int(model.REPORT_WITHDRAW_APPLIED)), strconv.Itoa(page))
cond = cond.And(builder.Eq{"r.withdraw": model.REPORT_WITHDRAW_APPLIED}) cond = cond.And(builder.Eq{"r.withdraw": model.REPORT_WITHDRAW_APPLIED})
if len(keyword) > 0 { if len(keyword) > 0 {
cond = cond.And( cond = cond.And(
@ -71,9 +78,17 @@ func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]m
builder.Like{"u.abbr", keyword}, builder.Like{"u.abbr", keyword},
), ),
) )
conditions = append(conditions, keyword)
} }
var reports = make([]model.JoinedReportForWithdraw, 0) var reports = make([]model.JoinedReportForWithdraw, 0)
total, err := global.DBConn. var (
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 {
total = cachedTotal
} else {
total, err = global.DBConn.
Table(new(model.JoinedReportForWithdraw)).Alias("r"). Table(new(model.JoinedReportForWithdraw)).Alias("r").
Join("INNER", []string{"park", "p"}, "r.park_id=p.id"). Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
Join("INNER", []string{"user_detail", "u"}, "p.user_id=u.id"). Join("INNER", []string{"user_detail", "u"}, "p.user_id=u.id").
@ -82,7 +97,12 @@ func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]m
if err != nil { if err != nil {
return nil, -1, err return nil, -1, err
} }
cache.CacheCount([]string{"report", "park"}, "join_report_for_withdraw", total, conditions...)
}
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
if cachedReports, _ := cache.RetreiveSearch[[]model.JoinedReportForWithdraw]("join_user_detail", conditions...); cachedReports != nil {
return *cachedReports, total, err
}
err = global.DBConn. err = global.DBConn.
Alias("r"). Alias("r").
Join("INNER", []string{"park", "p"}, "r.park_id=p.id"). Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
@ -90,6 +110,7 @@ func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]m
Where(cond). Where(cond).
Limit(config.ServiceSettings.ItemsPageSize, startItem). Limit(config.ServiceSettings.ItemsPageSize, startItem).
Find(&reports) Find(&reports)
cache.CacheSearch(reports, []string{"report", "park"}, "join_report_for_withdraw", conditions...)
return reports, total, err return reports, total, err
} }
@ -108,14 +129,22 @@ func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
report.Published = false report.Published = false
} }
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_audit_at", "published").Update(report) _, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_audit_at", "published").Update(report)
cache.AbolishRelation("report")
return err return err
} }
func (_WithdrawService) AuditWaits() (int64, error) { func (_WithdrawService) AuditWaits() (int64, error) {
if cachedWaits, _ := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 {
return cachedWaits, nil
}
cond := builder.NewCond() cond := builder.NewCond()
cond = cond.And(builder.Eq{"withdraw": model.REPORT_WITHDRAW_APPLIED}) cond = cond.And(builder.Eq{"withdraw": model.REPORT_WITHDRAW_APPLIED})
return global.DBConn. total, err := global.DBConn.
Table(new(model.JoinedReportForWithdraw)). Table(new(model.JoinedReportForWithdraw)).
Where(cond). Where(cond).
Count() Count()
if err == nil {
cache.CacheCount([]string{"report", "park"}, "withdraw_waits", total)
}
return total, err
} }