refactor(cache):缓存系统降级,停用全部数据缓存。

This commit is contained in:
徐涛 2023-06-19 20:56:31 +08:00
parent 316553d81a
commit 0d73665313
11 changed files with 0 additions and 529 deletions

View File

@ -2,12 +2,10 @@ package repository
import (
"context"
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
"electricity_bill_calc/tools"
"electricity_bill_calc/types"
"fmt"
@ -32,16 +30,6 @@ var ChargeRepository = &_ChargeRepository{
// 分页查询用户的充值记录
func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Date, keyword *string) ([]*model.UserChargeDetail, int64, error) {
cr.log.Info("查询用户的充值记录。", logger.DateFieldp("beginTime", beginTime), logger.DateFieldp("endTime", endTime), zap.Stringp("keyword", keyword), zap.Uint("page", page))
cacheConditions := []string{
fmt.Sprintf("%d", page),
tools.DefaultTo(keyword, ""),
tools.CondFn(func(t *types.Date) bool { return t != nil }, beginTime, beginTime.Format("2006-01-02"), "UNDEF"),
tools.CondFn(func(t *types.Date) bool { return t != nil }, endTime, endTime.Format("2006-01-02"), "UNDEF"),
}
if charges, total, err := cache.RetrievePagedSearch[[]*model.UserChargeDetail]("charges", cacheConditions...); err == nil && charges != nil {
cr.log.Info("从缓存中获取用户的充值记录成功。", zap.Int("count", len(*charges)), zap.Int64("total", total))
return *charges, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -103,7 +91,6 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat
cr.log.Error("查询用户的充值记录总数失败。", zap.Error(err))
return make([]*model.UserChargeDetail, 0), 0, err
}
cache.CachePagedSearch(charges, total, []string{"charges"}, "charges", cacheConditions...)
return charges, total, nil
}

View File

@ -2,7 +2,6 @@ package repository
import (
"context"
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
@ -10,7 +9,6 @@ import (
"electricity_bill_calc/types"
"errors"
"fmt"
"strings"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
@ -33,17 +31,6 @@ var InvoiceRepository = _InvoiceRepository{
// 查询指定园区中符合条件的发票
func (ir _InvoiceRepository) ListInvoice(pid *string, startDate, endDate *types.Date, keyword *string, page uint) ([]*model.Invoice, int64, error) {
ir.log.Info("查询指定园区的发票。", zap.Stringp("Park", pid), logger.DateFieldp("StartDate", startDate), logger.DateFieldp("EndDate", endDate), zap.Stringp("Keyword", keyword), zap.Uint("Page", page))
cacheCondition := []string{
cache.NullableStringKey(pid),
fmt.Sprintf("%d", page),
cache.NullableStringKey(keyword),
cache.NullableConditionKey(startDate),
cache.NullableConditionKey(endDate),
}
if invoices, total, err := cache.RetrievePagedSearch[[]*model.Invoice]("invoice", cacheCondition...); err != nil && invoices != nil && len(*invoices) > 0 {
ir.log.Info("从缓存中获取到了符合条件的发票记录。", zap.Int("Count", len(*invoices)), zap.Int64("Total", total))
return *invoices, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -112,26 +99,12 @@ func (ir _InvoiceRepository) ListInvoice(pid *string, startDate, endDate *types.
ir.log.Error("查询发票记录数失败。", zap.Error(err))
return invoices, 0, err
}
var relationName string
if pid != nil && len(*pid) > 0 {
relationName = fmt.Sprintf("invoice:%s", *pid)
} else {
relationName = "invoice"
}
cache.CachePagedSearch(invoices, total, []string{relationName}, "invoice", cacheCondition...)
return invoices, total, nil
}
// 查询指定商户未开票的核算记录,改记录将只包括商户整体核算,不包括商户各个表计的详细
func (ir _InvoiceRepository) ListUninvoicedTenementCharges(tid string) ([]*model.SimplifiedTenementCharge, error) {
ir.log.Info("查询指定商户的未开票核算记录", zap.String("Tenement", tid))
cacheConditions := []string{
tid,
}
if records, err := cache.RetrieveSearch[[]*model.SimplifiedTenementCharge]("uninvoiced_tenement_charge", cacheConditions...); err == nil && records != nil && len(*records) > 0 {
ir.log.Info("从缓存中获取到了符合条件的未开票核算记录。", zap.Int("Count", len(*records)))
return *records, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -154,11 +127,6 @@ func (ir _InvoiceRepository) ListUninvoicedTenementCharges(tid string) ([]*model
ir.log.Error("查询未开票核算记录失败。", zap.Error(err))
return charges, err
}
cache.CacheSearch(charges, []string{fmt.Sprintf("uninvoiced_tenement_charge:%s", tid)}, "uninvoiced_tenement_charge", cacheConditions...)
searchKey := cache.AssembleSearchKey("uninvoiced_tenement_charge", cacheConditions...)
for _, charge := range charges {
cache.CacheRelation(fmt.Sprintf("report:%s", charge.ReportId), cache.STORE_TYPE_KEY, searchKey)
}
return charges, nil
}
@ -186,10 +154,6 @@ func (ir _InvoiceRepository) UpdateTenementInvoicedState(tx pgx.Tx, ctx context.
// 查询指定发票的详细记录信息
func (ir _InvoiceRepository) GetInvoiceDetail(invoiceNo string) (*model.Invoice, error) {
ir.log.Info("查询指定发票的详细信息", zap.String("InvoiceNo", invoiceNo))
if invoice, err := cache.RetrieveEntity[model.Invoice]("invoice", invoiceNo); err == nil && invoice != nil {
ir.log.Info("从缓存中获取到了符合条件的发票记录。")
return invoice, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -203,21 +167,12 @@ func (ir _InvoiceRepository) GetInvoiceDetail(invoiceNo string) (*model.Invoice,
ir.log.Error("查询发票记录失败。", zap.Error(err))
return nil, err
}
cache.CacheEntity(invoice, []string{fmt.Sprintf("invoice:%s", invoiceNo)}, "invoice", invoiceNo)
return &invoice, nil
}
// 获取指定商户的简化核算记录
func (ir _InvoiceRepository) GetSimplifiedTenementCharges(tid string, rids []string) ([]*model.SimplifiedTenementCharge, error) {
ir.log.Info("查询庄园商户的简化核算记录", zap.String("Tenement", tid), zap.Strings("Reports", rids))
cacheConditions := []string{
tid,
strings.Join(rids, ":"),
}
if records, err := cache.RetrieveSearch[[]*model.SimplifiedTenementCharge]("simplified_tenement_charge", cacheConditions...); err == nil && records != nil && len(*records) > 0 {
ir.log.Info("从缓存中获取到了符合条件的简化核算记录。", zap.Int("Count", len(*records)))
return *records, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -240,11 +195,6 @@ func (ir _InvoiceRepository) GetSimplifiedTenementCharges(tid string, rids []str
ir.log.Error("查询简化核算记录失败。", zap.Error(err))
return charges, err
}
cache.CacheSearch(charges, []string{fmt.Sprintf("tenement:%s", tid)}, "simplified_tenement_charge", cacheConditions...)
searchKey := cache.AssembleSearchKey("simplified_tenement_charge", cacheConditions...)
for _, charge := range charges {
cache.CacheRelation(fmt.Sprintf("report:%s", charge.ReportId), cache.STORE_TYPE_KEY, searchKey)
}
return charges, nil
}
@ -391,10 +341,5 @@ func (ir _InvoiceRepository) Create(pid, tid, invoiceNo string, invoiceType *str
tx.Rollback(ctx)
return err
}
for _, rid := range *covers {
cache.AbolishRelation(fmt.Sprintf("report:%s", rid))
}
cache.AbolishRelation(fmt.Sprintf("invoice:%s", pid))
cache.AbolishRelation("invoice")
return nil
}

View File

@ -35,11 +35,6 @@ var MeterRepository = _MeterRepository{
// 获取指定园区中所有的表计信息
func (mr _MeterRepository) AllMeters(pid string) ([]*model.MeterDetail, error) {
mr.log.Info("列出指定园区中的所有表计", zap.String("park id", pid))
cacheConditions := []string{pid}
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("all_meters_in", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的表计信息", zap.Int("count", len(*meters)))
return *meters, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -62,19 +57,12 @@ func (mr _MeterRepository) AllMeters(pid string) ([]*model.MeterDetail, error) {
return make([]*model.MeterDetail, 0), err
}
cache.CacheSearch(meters, []string{fmt.Sprintf("meter:%s", pid)}, "all_meters_in", cacheConditions...)
return meters, nil
}
// 列出指定园区下的所有表计信息,包含已经拆除的表计
func (mr _MeterRepository) AllUsedMeters(pid string) ([]*model.MeterDetail, error) {
mr.log.Info("列出指定园区中的所有使用过的表计", zap.String("park id", pid))
cacheConditions := []string{pid}
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("all_used_meters_in", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的表计信息", zap.Int("count", len(*meters)))
return *meters, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -96,8 +84,6 @@ func (mr _MeterRepository) AllUsedMeters(pid string) ([]*model.MeterDetail, erro
return make([]*model.MeterDetail, 0), err
}
cache.CacheSearch(meters, []string{fmt.Sprintf("meter:%s", pid)}, "all_used_meters_in", cacheConditions...)
return meters, nil
}
@ -138,15 +124,6 @@ func (mr _MeterRepository) AllUsedMetersInReport(rid string) ([]*model.MeterDeta
// 分页列出指定园区下的表计信息
func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]*model.MeterDetail, int64, error) {
mr.log.Info("分页列出指定园区下的表计信息", zap.String("park id", pid), zap.Uint("page", page), zap.String("keyword", tools.DefaultTo(keyword, "")))
cacheConditions := []string{
pid,
tools.DefaultOrEmptyStr(keyword, "UNDEF"),
fmt.Sprintf("%d", page),
}
if meters, total, err := cache.RetrievePagedSearch[[]*model.MeterDetail]("meter", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的表计信息", zap.Int("count", len(*meters)), zap.Int64("total", total))
return *meters, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -203,22 +180,12 @@ func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]*
return make([]*model.MeterDetail, 0), 0, err
}
cache.CachePagedSearch(meters, total, []string{fmt.Sprintf("meter:%s", pid)}, "meter", cacheConditions...)
return meters, total, nil
}
// 列出指定园区中指定列表中所有表计的详细信息,将忽略所有表计的当前状态
func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.MeterDetail, error) {
mr.log.Info("列出指定园区中指定列表中所有表计的详细信息", zap.String("park id", pid), zap.Strings("meter ids", ids))
cacheConditions := []string{
pid,
strings.Join(ids, ","),
}
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("meter_slice", cacheConditions...); err == nil && meters != nil {
mr.log.Info("从缓存中获取到了指定园区中所需的表计信息", zap.Int("count", len(*meters)))
return *meters, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -241,19 +208,12 @@ func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.M
return make([]*model.MeterDetail, 0), err
}
cache.CacheSearch(meters, []string{fmt.Sprintf("meter:%s", pid), fmt.Sprintf("meter_slice:%s", pid)}, "meter_slice", cacheConditions...)
return meters, nil
}
// 获取指定表计的详细信息
func (mr _MeterRepository) FetchMeterDetail(pid, code string) (*model.MeterDetail, error) {
mr.log.Info("获取指定表计的详细信息", zap.String("park id", pid), zap.String("meter code", code))
cacheConditions := fmt.Sprintf("%s:%s", pid, code)
if meter, err := cache.RetrieveEntity[*model.MeterDetail]("meter", cacheConditions); err == nil {
mr.log.Info("从缓存中获取到了指定表计的详细信息", zap.String("code", (**meter).Code))
return *meter, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -275,8 +235,6 @@ func (mr _MeterRepository) FetchMeterDetail(pid, code string) (*model.MeterDetai
return nil, err
}
cache.CacheEntity(meter, []string{fmt.Sprintf("meter:%s", pid), "park"}, "meter", cacheConditions)
return &meter, nil
}
@ -302,12 +260,6 @@ func (mr _MeterRepository) CreateMeter(tx pgx.Tx, ctx context.Context, pid strin
mr.log.Error("创建表计信息失败", zap.Error(err))
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
cache.AbolishRelation(fmt.Sprintf("meter_relations:%s:%s", pid, meter.Code))
}
return ok.RowsAffected() > 0, nil
}
@ -368,10 +320,6 @@ func (mr _MeterRepository) RecordReading(tx pgx.Tx, ctx context.Context, pid, co
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter_reading:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -406,10 +354,6 @@ func (mr _MeterRepository) UpdateMeter(tx pgx.Tx, ctx context.Context, pid, code
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -439,8 +383,6 @@ func (mr _MeterRepository) ListMeterCodes(pid string) ([]string, error) {
return make([]string, 0), err
}
cache.CacheSearch(codes, []string{fmt.Sprintf("meter:%s", pid), fmt.Sprintf("park:%s", pid)}, "meter_codes", cacheConditions...)
return codes, nil
}
@ -468,11 +410,6 @@ func (mr _MeterRepository) DetachMeter(tx pgx.Tx, ctx context.Context, pid, code
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
cache.AbolishRelation(fmt.Sprintf("meter_relations:%s:%s", pid, code))
}
return ok.RowsAffected() > 0, nil
}
@ -523,10 +460,6 @@ func (mr _MeterRepository) BindMeter(tx pgx.Tx, ctx context.Context, pid, master
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter_relations:%s:%s", pid, masterMeter))
}
return ok.RowsAffected() > 0, nil
}
@ -554,10 +487,6 @@ func (mr _MeterRepository) UnbindMeter(tx pgx.Tx, ctx context.Context, pid, mast
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter_relations:%s:%s", pid, masterMeter))
}
return ok.RowsAffected() > 0, nil
}
@ -647,15 +576,6 @@ func (mr _MeterRepository) ListMeterRelations(pid, code string) ([]*model.MeterR
// 列出指定园区中的所有公摊表计
func (mr _MeterRepository) ListPoolingMeters(pid string, page uint, keyword *string) ([]*model.MeterDetail, int64, error) {
mr.log.Info("列出指定园区中的所有公摊表计", zap.String("park id", pid), zap.Uint("page", page), zap.String("keyword", tools.DefaultTo(keyword, "")))
cacheConditions := []string{
pid,
tools.DefaultOrEmptyStr(keyword, "UNDEF"),
fmt.Sprintf("%d", page),
}
if meters, total, err := cache.RetrievePagedSearch[[]*model.MeterDetail]("pooling_meters", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的公摊表计信息", zap.Int("count", len(*meters)), zap.Int64("total", total))
return *meters, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -714,23 +634,12 @@ func (mr _MeterRepository) ListPoolingMeters(pid string, page uint, keyword *str
return make([]*model.MeterDetail, 0), 0, err
}
cache.CachePagedSearch(meters, total, []string{fmt.Sprintf("meter:%s", pid), "park"}, "pooling_meters", cacheConditions...)
return meters, total, nil
}
// 列出目前尚未绑定到公摊表计的商户表计
func (mr _MeterRepository) ListUnboundMeters(uid string, pid *string, keyword *string, limit *uint) ([]*model.MeterDetail, error) {
mr.log.Info("列出目前尚未绑定到公摊表计的商户表计", zap.Stringp("park id", pid), zap.String("user id", uid), zap.String("keyword", tools.DefaultTo(keyword, "")), zap.Uint("limit", tools.DefaultTo(limit, 0)))
cacheConditions := []string{
tools.DefaultTo(pid, "UNDEF"),
tools.DefaultOrEmptyStr(keyword, "UNDEF"),
tools.DefaultStrTo("%d", limit, "0"),
}
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("unbound_pooling_meters", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的商户表计信息", zap.Int("count", len(*meters)))
return *meters, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -796,23 +705,12 @@ func (mr _MeterRepository) ListUnboundMeters(uid string, pid *string, keyword *s
return make([]*model.MeterDetail, 0), err
}
cache.CacheSearch(meters, []string{fmt.Sprintf("meter:%s", tools.DefaultTo(pid, "ALL")), "park"}, "unbound_pooling_meters", cacheConditions...)
return meters, nil
}
// 列出目前未绑定到商户的商户表计
func (mr _MeterRepository) ListUnboundTenementMeters(uid string, pid *string, keyword *string, limit *uint) ([]*model.MeterDetail, error) {
mr.log.Info("列出目前未绑定到商户的商户表计", zap.Stringp("park id", pid), zap.String("user id", uid), zap.String("keyword", tools.DefaultTo(keyword, "")), zap.Uint("limit", tools.DefaultTo(limit, 0)))
cacheConditions := []string{
tools.DefaultTo(pid, "UNDEF"),
tools.DefaultOrEmptyStr(keyword, "UNDEF"),
tools.DefaultStrTo("%d", limit, "0"),
}
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("unbound_tenement_meters", cacheConditions...); err == nil {
mr.log.Info("从缓存中获取到了指定园区中的商户表计信息", zap.Int("count", len(*meters)))
return *meters, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -878,26 +776,12 @@ func (mr _MeterRepository) ListUnboundTenementMeters(uid string, pid *string, ke
return make([]*model.MeterDetail, 0), err
}
cache.CacheSearch(meters, []string{fmt.Sprintf("meter:%s", tools.DefaultTo(pid, "ALL")), "park"}, "unbound_tenement_meters", cacheConditions...)
return meters, nil
}
// 查询指定园区中的符合条件的抄表记录
func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page uint, start, end *types.Date, buidling *string) ([]*model.MeterReading, int64, error) {
mr.log.Info("查询指定园区中的符合条件的抄表记录", zap.String("park id", pid), zap.String("keyword", tools.DefaultTo(keyword, "")), zap.Uint("page", page), logger.DateFieldp("start", start), logger.DateFieldp("end", end), zap.String("building", tools.DefaultTo(buidling, "")))
cacheConditions := []string{
pid,
cache.NullableStringKey(keyword),
fmt.Sprintf("%d", page),
cache.NullableConditionKey(start),
cache.NullableConditionKey(end),
cache.NullableStringKey(buidling),
}
if readings, total, err := cache.RetrievePagedSearch[[]*model.MeterReading]("meter_reading", cacheConditions...); err == nil && readings != nil && total != -1 {
mr.log.Info("从缓存中获取到了指定园区中的抄表记录", zap.Int("count", len(*readings)), zap.Int64("total", total))
return *readings, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -978,8 +862,6 @@ func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page u
return make([]*model.MeterReading, 0), 0, err
}
cache.CachePagedSearch(readings, total, []string{fmt.Sprintf("meter_reading:%s", pid), "park"}, "meter_reading", cacheConditions...)
return readings, total, nil
}
@ -1013,10 +895,6 @@ func (mr _MeterRepository) UpdateMeterReading(pid, mid string, readAt types.Date
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation(fmt.Sprintf("meter_reading:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -1079,11 +957,6 @@ func (mr _MeterRepository) ListLastMeterReading(pid string, date types.Date) ([]
// 列出指定园区中的表计与商户的关联详细记录用于写入Excel模板文件
func (mr _MeterRepository) ListMeterDocForTemplate(pid string) ([]*model.SimpleMeterDocument, error) {
mr.log.Info("列出指定园区中的表计与商户的关联详细记录", zap.String("park id", pid))
cacheConditions := []string{pid}
if docs, err := cache.RetrieveSearch[[]*model.SimpleMeterDocument]("simple_meter_doc", cacheConditions...); err == nil && docs != nil {
mr.log.Info("从缓存中获取到了指定园区中的表计与商户的关联详细记录", zap.Int("count", len(*docs)))
return *docs, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -1120,7 +993,5 @@ func (mr _MeterRepository) ListMeterDocForTemplate(pid string) ([]*model.SimpleM
return make([]*model.SimpleMeterDocument, 0), err
}
cache.CacheSearch(docs, []string{fmt.Sprintf("park:%s", pid), fmt.Sprintf("meter:%s", pid), "park"}, "simple_meter_doc", cacheConditions...)
return docs, nil
}

View File

@ -2,15 +2,12 @@ package repository
import (
"context"
"electricity_bill_calc/cache"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
"electricity_bill_calc/tools"
"electricity_bill_calc/tools/serial"
"electricity_bill_calc/types"
"fmt"
"strings"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
@ -32,13 +29,6 @@ var ParkRepository = _ParkRepository{
// 列出指定用户下的所有园区
func (pr _ParkRepository) ListAllParks(uid string) ([]*model.Park, error) {
pr.log.Info("列出指定用户下的所有园区", zap.String("uid", uid))
cacheConditions := []string{
uid,
}
if parks, err := cache.RetrieveSearch[[]*model.Park]("park_belongs", cacheConditions...); err == nil && parks != nil {
pr.log.Info("已经从缓存获取到了指定用户下的所有园区。")
return *parks, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -61,22 +51,12 @@ func (pr _ParkRepository) ListAllParks(uid string) ([]*model.Park, error) {
pr.log.Error("列出指定用户下的所有园区失败!", zap.Error(err))
return make([]*model.Park, 0), err
}
cache.CacheSearch(parks, []string{"park", fmt.Sprintf("park:%s", uid)}, "park_belongs", cacheConditions...)
return parks, nil
}
// 检查并确定指定园区的归属情况
func (pr _ParkRepository) IsParkBelongs(pid, uid string) (bool, error) {
pr.log.Info("检查并确定指定园区的归属情况", zap.String("pid", pid), zap.String("uid", uid))
cacheConditions := []string{
pid, "belongs", uid,
}
if exists, err := cache.CheckExists("park", cacheConditions...); err == nil && exists {
pr.log.Info("已经从缓存获取到了指定园区的归属情况。")
return true, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -93,11 +73,6 @@ func (pr _ParkRepository) IsParkBelongs(pid, uid string) (bool, error) {
pr.log.Error("检查并确定指定园区的归属情况失败!", zap.Error(err))
return false, err
}
if count > 0 {
cache.CacheExists([]string{"park", fmt.Sprintf("park:%s", uid)}, "park", cacheConditions...)
}
return count > 0, nil
}
@ -130,19 +105,12 @@ func (pr _ParkRepository) CreatePark(ownerId string, park *model.Park) (bool, er
pr.log.Error("创建一个属于指定用户的新园区失败!", zap.Error(err))
return false, err
}
cache.AbolishRelation("park")
return rs.RowsAffected() > 0, nil
}
// 获取指定园区的详细信息
func (pr _ParkRepository) RetrieveParkDetail(pid string) (*model.Park, error) {
pr.log.Info("获取指定园区的详细信息", zap.String("pid", pid))
if park, err := cache.RetrieveEntity[model.Park]("park", pid); err == nil && park != nil {
pr.log.Info("已经从缓存获取到了指定园区的详细信息。")
return park, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -161,19 +129,12 @@ func (pr _ParkRepository) RetrieveParkDetail(pid string) (*model.Park, error) {
pr.log.Error("获取指定园区的详细信息失败!", zap.Error(err))
return nil, err
}
cache.CacheEntity(park, []string{"park", fmt.Sprintf("park:%s", pid)}, "park", pid)
return &park, nil
}
// 获取园区对应的用户ID
func (pr _ParkRepository) RetrieveParkBelongs(pid string) (string, error) {
pr.log.Info("获取园区对应的用户ID", zap.String("pid", pid))
if uid, err := cache.RetrieveEntity[string]("park_belongs", pid); err == nil && uid != nil {
pr.log.Info("已经从缓存获取到了园区对应的用户ID。")
return *uid, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -187,9 +148,6 @@ func (pr _ParkRepository) RetrieveParkBelongs(pid string) (string, error) {
pr.log.Error("获取园区对应的用户ID失败", zap.Error(err))
return "", err
}
cache.CacheEntity(uid, []string{"park", fmt.Sprintf("park:%s", pid)}, "park_belongs", pid)
return uid, nil
}
@ -230,11 +188,6 @@ func (pr _ParkRepository) UpdatePark(pid string, park *model.Park) (bool, error)
pr.log.Error("更新指定园区的信息失败!", zap.Error(err))
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -259,11 +212,6 @@ func (pr _ParkRepository) EnablingPark(pid string, enabled bool) (bool, error) {
pr.log.Error("设定园区的可用状态失败!", zap.Error(err))
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -288,11 +236,6 @@ func (pr _ParkRepository) DeletePark(pid string) (bool, error) {
pr.log.Error("删除指定园区(软删除)失败!", zap.Error(err))
return false, err
}
if ok.RowsAffected() > 0 {
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
}
return ok.RowsAffected() > 0, nil
}
@ -303,11 +246,6 @@ func (pr _ParkRepository) RetrieveParks(pids []string) ([]*model.Park, error) {
pr.log.Info("给定要检索的园区ID列表为空执行快速返回。")
return make([]*model.Park, 0), nil
}
cacheConditions := []string{strings.Join(pids, ",")}
if parks, err := cache.RetrieveSearch[[]*model.Park]("park", cacheConditions...); err == nil && parks != nil {
pr.log.Info("已经从缓存获取到了给定的园区详细信息列表。")
return *parks, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -320,19 +258,12 @@ func (pr _ParkRepository) RetrieveParks(pids []string) ([]*model.Park, error) {
pr.log.Error("检索给定的园区详细信息列表失败!", zap.Error(err))
return nil, err
}
cache.CacheSearch(parks, []string{"park", fmt.Sprintf("park:%s", strings.Join(pids, ":"))}, "park", cacheConditions...)
return parks, nil
}
// 获取指定园区中的建筑
func (pr _ParkRepository) RetrieveParkBuildings(pid string) ([]*model.ParkBuilding, error) {
pr.log.Info("获取指定园区中的建筑", zap.String("pid", pid))
if buildings, err := cache.RetrieveSearch[[]*model.ParkBuilding]("park_building", pid); err == nil && buildings != nil {
pr.log.Info("已经从缓存获取到了指定园区中的建筑。")
return *buildings, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -350,9 +281,6 @@ func (pr _ParkRepository) RetrieveParkBuildings(pid string) ([]*model.ParkBuildi
pr.log.Error("获取指定园区中的建筑失败!", zap.Error(err))
return nil, err
}
cache.CacheSearch(buildings, []string{"park_building", fmt.Sprintf("park_building:%s", pid)}, "park_building", pid)
return buildings, nil
}
@ -381,12 +309,6 @@ func (pr _ParkRepository) CreateParkBuilding(pid, name string, floor *string) (b
pr.log.Error("在指定园区中创建一个新建筑失败!", zap.Error(err))
return false, err
}
if rs.RowsAffected() > 0 {
cache.AbolishRelation("park_building")
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
}
return rs.RowsAffected() > 0, nil
}
@ -411,12 +333,6 @@ func (pr _ParkRepository) CreateParkBuildingWithTransaction(tx pgx.Tx, ctx conte
pr.log.Error("在指定园区中创建一个新建筑失败!", zap.Error(err))
return false, err
}
if rs.RowsAffected() > 0 {
cache.AbolishRelation("park_building")
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
}
return rs.RowsAffected() > 0, nil
}
@ -445,12 +361,6 @@ func (pr _ParkRepository) ModifyParkBuilding(id, pid, name string, floor *string
pr.log.Error("修改指定园区中指定建筑的信息失败!", zap.Error(err))
return false, err
}
if rs.RowsAffected() > 0 {
cache.AbolishRelation("park_building")
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
}
return rs.RowsAffected() > 0, nil
}
@ -478,12 +388,6 @@ func (pr _ParkRepository) EnablingParkBuilding(id, pid string, enabled bool) (bo
pr.log.Error("修改指定建筑的可以状态失败!", zap.Error(err))
return false, err
}
if rs.RowsAffected() > 0 {
cache.AbolishRelation("park_building")
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
}
return rs.RowsAffected() > 0, nil
}
@ -511,11 +415,5 @@ func (pr _ParkRepository) DeleteParkBuilding(id, pid string) (bool, error) {
pr.log.Error("删除指定建筑(软删除)失败!", zap.Error(err))
return false, err
}
if rs.RowsAffected() > 0 {
cache.AbolishRelation("park_building")
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
}
return rs.RowsAffected() > 0, nil
}

View File

@ -1,7 +1,6 @@
package repository
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
@ -25,14 +24,6 @@ var RegionRepository = _RegionRepository{
// 获取指定行政区划下所有直接子级行政区划
func (r *_RegionRepository) FindSubRegions(parent string) ([]model.Region, error) {
r.log.Info("获取指定行政区划下所有直接子级行政区划", zap.String("parent", parent))
cacheConditions := []string{
"parent", parent,
}
if regions, err := cache.RetrieveSearch[[]model.Region]("region", cacheConditions...); err == nil && regions != nil {
r.log.Info("已经从缓存获取到了指定的子级行政区划。")
return *regions, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -45,21 +36,12 @@ func (r *_RegionRepository) FindSubRegions(parent string) ([]model.Region, error
r.log.Error("获取指定行政区划下所有直接子级行政区划失败!", zap.Error(err))
return nil, err
}
if len(regions) > 0 {
cache.CacheSearch(regions, []string{"region"}, "region", cacheConditions...)
}
return regions, nil
}
// 获取一个指定编号的行政区划详细信息
func (r *_RegionRepository) FindRegion(code string) (*model.Region, error) {
r.log.Info("获取指定行政区划信息", zap.String("code", code))
if region, err := cache.RetrieveEntity[model.Region]("region", code); err == nil && region != nil {
r.log.Info("已经从缓存获取到了指定的行政区划详细信息。")
return region, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -73,22 +55,12 @@ func (r *_RegionRepository) FindRegion(code string) (*model.Region, error) {
r.log.Error("获取指定行政区划信息失败!", zap.Error(err))
return nil, err
}
cache.CacheEntity(region, []string{"region"}, "region", code)
return &region, nil
}
// 获取指定行政区划的所有直接和非直接父级
func (r *_RegionRepository) FindParentRegions(code string) ([]*model.Region, error) {
r.log.Info("获取指定行政区划的所有直接和非直接父级", zap.String("code", code))
cacheConditions := []string{
"parent", code,
}
if regions, err := cache.RetrieveSearch[[]*model.Region]("region", cacheConditions...); err == nil && regions != nil {
r.log.Info("已经从缓存获取到了指定的父级行政区划。")
return *regions, nil
}
var (
regionsScanTask = []string{code}
regions = make([]*model.Region, 0)
@ -103,9 +75,5 @@ func (r *_RegionRepository) FindParentRegions(code string) ([]*model.Region, err
}
}
}
if len(regions) > 0 {
cache.CacheSearch(regions, []string{"region"}, "region", cacheConditions...)
}
return regions, nil
}

View File

@ -2,7 +2,6 @@ package repository
import (
"context"
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
@ -33,13 +32,6 @@ var TenementRepository = _TenementRepository{
// 判断指定商户是否属于指定用户的管辖
func (tr _TenementRepository) IsTenementBelongs(tid, uid string) (bool, error) {
tr.log.Info("检查指定商户是否属于指定企业管辖", zap.String("Tenement", tid), zap.String("Enterprise", uid))
cacheConditions := []string{
tid, "belongs", uid,
}
if exists, err := cache.CheckExists("tenement", cacheConditions...); err != nil && exists {
tr.log.Error("检查指定商户是否属于指定企业管辖失败", zap.Error(err))
return exists, err
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -57,10 +49,6 @@ func (tr _TenementRepository) IsTenementBelongs(tid, uid string) (bool, error) {
tr.log.Error("检查指定商户是否属于指定企业管辖失败", zap.Error(err))
return false, err
}
if count > 0 {
cache.CacheExists([]string{fmt.Sprintf("tenement:%s", tid)}, "tenement", cacheConditions...)
}
return count > 0, nil
}
@ -76,21 +64,6 @@ func (tr _TenementRepository) ListTenements(pid string, page uint, keyword, buil
logger.DateFieldp("EndDate", endDate),
zap.Stringp("State", state),
)
cacheConditions := []string{
pid,
fmt.Sprintf("%d", page),
cache.NullableStringKey(keyword),
cache.NullableStringKey(building),
cache.NullableConditionKey(startDate),
cache.NullableConditionKey(endDate),
cache.NullableStringKey(state),
}
if tenements, total, err := cache.RetrievePagedSearch[[]*model.Tenement]("tenements", cacheConditions...); err != nil && tenements != nil {
tr.log.Info("从缓存中获取到了符合条件的商户记录", zap.Int64("Total", total), zap.Int("Count", len(*tenements)))
return *tenements, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -188,22 +161,12 @@ func (tr _TenementRepository) ListTenements(pid string, page uint, keyword, buil
tr.log.Error("检索查询指定园区中符合条件的商户总数量失败", zap.Error(err))
return tenements, 0, err
}
cache.CachePagedSearch(tenements, total, []string{fmt.Sprintf("tenements:%s", pid)}, "tenements", cacheConditions...)
return tenements, total, nil
}
// 查询指定园区中某一商户下的所有表计编号,不包含公摊表计
func (tr _TenementRepository) ListMeterCodesBelongsTo(pid, tid string) ([]string, error) {
tr.log.Info("查询指定商户下所有的表计编号", zap.String("Park", pid), zap.String("Tenement", tid))
cacheConditions := []string{
pid, tid,
}
if meterCodes, err := cache.RetrieveSearch[[]string]("tenement_submeter", cacheConditions...); err != nil && meterCodes != nil {
tr.log.Info("从缓存中获取到了指定商户下所有的表计编号", zap.Int("Count", len(*meterCodes)))
return *meterCodes, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -222,9 +185,6 @@ func (tr _TenementRepository) ListMeterCodesBelongsTo(pid, tid string) ([]string
tr.log.Error("查询指定商户下所有的表计编号失败", zap.Error(err))
return meterCodes, err
}
cache.CacheSearch(&meterCodes, []string{"tenement", "tenement_submeter", "meter", fmt.Sprintf("tenement:%s", pid)}, "tenement_submeter", cacheConditions...)
return meterCodes, nil
}
@ -362,7 +322,6 @@ func (tr _TenementRepository) UpdateTenement(pid, tid string, tenement *vo.Tenem
tr.log.Error("修改指定商户的信息失败", zap.Error(err))
return err
}
cache.AbolishRelation(fmt.Sprintf("tenement:%s", pid))
return nil
}
@ -392,16 +351,6 @@ func (tr _TenementRepository) MoveOut(tx pgx.Tx, ctx context.Context, pid, tid s
// 列出用于下拉列表的符合指定条件的商户信息
func (tr _TenementRepository) ListForSelect(uid string, pid, keyword *string, limit *uint) ([]*model.Tenement, error) {
tr.log.Info("列出用于下拉列表的符合指定条件的商户信息", zap.String("Ent", uid), zap.String("Park", tools.DefaultOrEmptyStr(pid, "All")), zap.Stringp("Keyword", keyword), zap.Uintp("Limit", limit))
cacheConditions := []string{
uid,
cache.NullableStringKey(pid),
cache.NullableStringKey(keyword),
fmt.Sprintf("%d", tools.DefaultTo(limit, 0)),
}
if tenements, err := cache.RetrieveSearch[[]*model.Tenement]("tenement_choice", cacheConditions...); err != nil && tenements != nil {
tr.log.Info("从缓存中获取到了用于下拉列表的符合指定条件的商户信息", zap.Int("Count", len(*tenements)))
return *tenements, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -445,9 +394,6 @@ func (tr _TenementRepository) ListForSelect(uid string, pid, keyword *string, li
tr.log.Error("列出用于下拉列表的符合指定条件的商户信息失败", zap.Error(err))
return tenements, err
}
cache.CacheSearch(&tenements, []string{"tenement"}, "tenement_choice", cacheConditions...)
return tenements, nil
}
@ -487,10 +433,6 @@ func (tr _TenementRepository) ListTenementsInTimeRange(pid string, start, end ty
// 获取指定园区中指定商户的详细信息
func (tr _TenementRepository) RetrieveTenementDetail(pid, tid string) (*model.Tenement, error) {
tr.log.Info("获取指定园区中指定商户的详细信息", zap.String("Park", pid), zap.String("Tenement", tid))
if tenement, err := cache.RetrieveEntity[model.Tenement](fmt.Sprintf("tenement:%s", pid), tid); err != nil && tenement != nil {
tr.log.Info("从缓存中获取到了指定园区中指定商户的详细信息")
return tenement, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -510,8 +452,5 @@ func (tr _TenementRepository) RetrieveTenementDetail(pid, tid string) (*model.Te
tr.log.Error("获取指定园区中指定商户的详细信息失败", zap.Error(err))
return nil, err
}
cache.CacheEntity(&tenement, []string{"tenement", fmt.Sprintf("tenement:%s", pid)}, fmt.Sprintf("tenement:%s", pid), tid)
return &tenement, nil
}

View File

@ -1,7 +1,6 @@
package repository
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
@ -30,17 +29,6 @@ var TopUpRepository = _TopUpRepository{
// 检索符合条件的商户充值记录
func (tur _TopUpRepository) ListTopUps(pid string, startDate, endDate *types.Date, keyword *string, page uint) ([]*model.TopUp, int64, error) {
tur.log.Info("查询符合条件的商户充值记录", zap.String("Park", pid), logger.DateFieldp("StartDate", startDate), logger.DateFieldp("EndDate", endDate), zap.Stringp("keyword", keyword), zap.Uint("page", page))
cacheConditions := []string{
pid,
cache.NullableStringKey(keyword),
cache.NullableConditionKey(startDate),
cache.NullableConditionKey(endDate),
fmt.Sprintf("%d", page),
}
if topUps, total, err := cache.RetrievePagedSearch[[]*model.TopUp]("top_ups", cacheConditions...); err == nil && topUps != nil && len(*topUps) > 0 {
tur.log.Info("从缓存中获取到商户充值记录", zap.Int("Count", len(*topUps)), zap.Int64("Total", total))
return *topUps, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -103,17 +91,12 @@ func (tur _TopUpRepository) ListTopUps(pid string, startDate, endDate *types.Dat
tur.log.Error("查询商户充值记录总数失败", zap.Error(err))
return topUps, 0, err
}
cache.CachePagedSearch(topUps, total, []string{fmt.Sprintf("top_up:%s", pid), fmt.Sprintf("tenement:%s", pid), fmt.Sprintf("meter:%s", pid)}, "top_ups", cacheConditions...)
return topUps, total, nil
}
// 取得一个充值记录的详细信息
func (tur _TopUpRepository) GetTopUp(pid, topUpCode string) (*model.TopUp, error) {
tur.log.Info("查询充值记录", zap.String("Park", pid), zap.String("TopUpCode", topUpCode))
if topUp, err := cache.RetrieveEntity[model.TopUp]("top_up", topUpCode); err == nil && topUp != nil {
tur.log.Info("从缓存中获取到充值记录")
return topUp, err
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -130,7 +113,6 @@ func (tur _TopUpRepository) GetTopUp(pid, topUpCode string) (*model.TopUp, error
tur.log.Error("查询充值记录失败", zap.Error(err))
return nil, err
}
cache.CacheEntity(&topUp, []string{fmt.Sprintf("top_up:%s", pid), fmt.Sprintf("tenement:%s", pid), fmt.Sprintf("meter:%s", pid)}, "top_up", topUpCode)
return &topUp, nil
}
@ -161,7 +143,6 @@ func (tur _TopUpRepository) CreateTopUp(pid string, form *vo.TopUpCreationForm)
tur.log.Error("创建充值记录失败", zap.Error(err))
return err
}
cache.AbolishRelation(fmt.Sprintf("top_ups:%s", pid))
return nil
}
@ -181,6 +162,5 @@ func (tur _TopUpRepository) DeleteTopUp(pid, topUpCode string) error {
tur.log.Error("删除充值记录失败", zap.Error(err))
return err
}
cache.AbolishRelation(fmt.Sprintf("top_ups:%s", pid))
return nil
}

View File

@ -2,7 +2,6 @@ package repository
import (
"context"
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
@ -10,7 +9,6 @@ import (
"electricity_bill_calc/tools"
"electricity_bill_calc/types"
"fmt"
"strings"
"time"
"github.com/doug-martin/goqu/v9"
@ -35,10 +33,6 @@ var UserRepository = _UserRepository{
// 使用用户名查询指定用户的基本信息
func (ur _UserRepository) FindUserByUsername(username string) (*model.User, error) {
ur.log.Info("根据用户名查询指定用户的基本信息。", zap.String("username", username))
if cachedUser, _ := cache.RetrieveEntity[model.User]("user", username); cachedUser != nil {
ur.log.Info("已经从缓存获取到了符合指定用户名条件的用户基本信息。", zap.String("username", username))
return cachedUser, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -48,17 +42,12 @@ func (ur _UserRepository) FindUserByUsername(username string) (*model.User, erro
ur.log.Error("从数据库查询指定用户名的用户基本信息失败。", zap.String("username", username), zap.Error(err))
return nil, err
}
cache.CacheEntity(user, []string{"user", fmt.Sprintf("user:%s", username), fmt.Sprintf("user:%s", user.Id)}, "user", username)
return &user, nil
}
// 使用用户唯一编号查询指定用户的基本信息
func (ur _UserRepository) FindUserById(uid string) (*model.User, error) {
ur.log.Info("根据用户唯一编号查询指定用户的基本信息。", zap.String("user id", uid))
if cachedUser, _ := cache.RetrieveEntity[model.User]("user", uid); cachedUser != nil {
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户基本信息。")
return cachedUser, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -68,17 +57,12 @@ func (ur _UserRepository) FindUserById(uid string) (*model.User, error) {
ur.log.Error("从数据库查询指定用户唯一编号的用户基本信息失败。", zap.String("user id", uid), zap.Error(err))
return nil, err
}
cache.CacheEntity(user, []string{"user", fmt.Sprintf("user:%s", uid)}, "user", uid)
return &user, nil
}
// 使用用户的唯一编号获取用户的详细信息
func (ur _UserRepository) FindUserDetailById(uid string) (*model.UserDetail, error) {
ur.log.Info("根据用户唯一编号查询指定用户的详细信息。", zap.String("user id", uid))
if cachedUser, _ := cache.RetrieveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户详细信息。")
return cachedUser, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -88,17 +72,12 @@ func (ur _UserRepository) FindUserDetailById(uid string) (*model.UserDetail, err
ur.log.Error("从数据库查询指定用户唯一编号的用户详细信息失败。", zap.String("user id", uid), zap.Error(err))
return nil, err
}
cache.CacheEntity(user, []string{"user", fmt.Sprintf("user:%s", uid)}, "user_detail", uid)
return &user, nil
}
// 使用用户唯一编号获取用户的综合详细信息
func (ur _UserRepository) FindUserInformation(uid string) (*model.UserWithDetail, error) {
ur.log.Info("根据用户唯一编号查询用户的综合详细信息", zap.String("user id", uid))
if cachedUser, _ := cache.RetrieveEntity[model.UserWithDetail]("user_information", uid); cachedUser != nil {
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户综合详细信息。")
return cachedUser, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -122,17 +101,12 @@ func (ur _UserRepository) FindUserInformation(uid string) (*model.UserWithDetail
ur.log.Error("从数据库查询指定用户唯一编号的用户详细信息失败。", zap.String("user id", uid), zap.Error(err))
return nil, err
}
cache.CacheEntity(user, []string{"user", fmt.Sprintf("user:%s", uid)}, "user_information", uid)
return &user, nil
}
// 检查指定用户唯一编号是否存在对应的用户
func (ur _UserRepository) IsUserExists(uid string) (bool, error) {
ur.log.Info("检查指定用户唯一编号是否存在对应的用户。", zap.String("user id", uid))
if exists, _ := cache.CheckExists("user", uid); exists {
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户基本信息。")
return exists, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -142,19 +116,12 @@ func (ur _UserRepository) IsUserExists(uid string) (bool, error) {
ur.log.Error("从数据库查询指定用户唯一编号的用户基本信息失败。", zap.String("user id", uid), zap.Error(err))
return false, err
}
if userCount > 0 {
cache.CacheExists([]string{"user", fmt.Sprintf("user:%s", uid)}, "user", uid)
}
return userCount > 0, nil
}
// 检查指定用户名在数据库中是否已经存在
func (ur _UserRepository) IsUsernameExists(username string) (bool, error) {
ur.log.Info("检查指定用户名在数据库中是否已经存在。", zap.String("username", username))
if exists, _ := cache.CheckExists("user", username); exists {
ur.log.Info("已经从缓存获取到了符合指定用户名的用户基本信息。")
return exists, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -164,9 +131,6 @@ func (ur _UserRepository) IsUsernameExists(username string) (bool, error) {
ur.log.Error("从数据库查询指定用户名的用户基本信息失败。", zap.String("username", username), zap.Error(err))
return false, err
}
if userCount > 0 {
cache.CacheExists([]string{"user", fmt.Sprintf("user:%s", username)}, "user", username)
}
return userCount > 0, nil
}
@ -222,8 +186,6 @@ func (ur _UserRepository) CreateUser(user model.User, detail model.UserDetail, o
ur.log.Error("提交数据库事务失败。", zap.Error(err))
tx.Rollback(ctx)
return false, err
} else {
cache.AbolishRelation("user")
}
return userResult.RowsAffected() > 0 && detailResult.RowsAffected() > 0, nil
}
@ -231,23 +193,6 @@ func (ur _UserRepository) CreateUser(user model.User, detail model.UserDetail, o
// 根据给定的条件检索用户
func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool, page uint) ([]*model.UserWithDetail, int64, error) {
ur.log.Info("根据给定的条件检索用户。", zap.Uint("page", page), zap.Stringp("keyword", keyword), zap.Int16("user type", userType), zap.Boolp("state", state))
cacheConditions := []string{
fmt.Sprintf("%d", page),
tools.CondFn(
func(v int16) bool {
return v != -1
},
userType,
fmt.Sprintf("%d", userType),
"UNDEF",
),
tools.DefaultStrTo("%s", state, "UNDEF"),
tools.DefaultTo(keyword, ""),
}
if users, total, err := cache.RetrievePagedSearch[[]*model.UserWithDetail]("user_with_detail", cacheConditions...); err == nil && users != nil && total != -1 {
return *users, total, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -312,13 +257,6 @@ func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool,
ur.log.Error("从数据库查询用户列表总数失败。", zap.Error(err))
return make([]*model.UserWithDetail, 0), 0, err
}
cache.CachePagedSearch(
userWithDetails,
userCount,
[]string{"user"},
"user_with_detail",
cacheConditions...,
)
return userWithDetails, userCount, nil
}
@ -349,7 +287,6 @@ func (ur _UserRepository) UpdateDetail(uid string, userDetail model.UserModifica
ur.log.Error("向数据库更新指定用户的详细信息失败。", zap.String("user id", uid), zap.Error(err))
return false, err
} else {
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return res.RowsAffected() > 0, nil
}
}
@ -372,7 +309,6 @@ func (ur _UserRepository) UpdatePassword(uid, newCredential string, needReset bo
ur.log.Error("向数据库更新指定用户的登录凭据失败。", zap.String("user id", uid), zap.Error(err))
return false, err
} else {
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return res.RowsAffected() > 0, nil
}
}
@ -395,7 +331,6 @@ func (ur _UserRepository) ChangeState(uid string, state bool) (bool, error) {
ur.log.Error("向数据库更新指定用户的可用性状态失败。", zap.String("user id", uid), zap.Error(err))
return false, err
} else {
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return res.RowsAffected() > 0, nil
}
}
@ -404,14 +339,6 @@ func (ur _UserRepository) ChangeState(uid string, state bool) (bool, error) {
func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string, limit uint) ([]*model.UserWithDetail, error) {
ur.log.Info("检索条目数量有限的用户详细信息。", zap.Int16p("user type", userType), zap.Uint("limit", limit), zap.Stringp("keyword", keyword))
actualUserType := tools.DefaultTo(userType, model.USER_TYPE_ENT)
cacheConditions := []string{
fmt.Sprintf("%d", actualUserType),
tools.DefaultTo(keyword, ""),
fmt.Sprintf("%d", limit),
}
if users, err := cache.RetrieveSearch[[]*model.UserWithDetail]("user_with_detail_limited", cacheConditions...); err == nil && users != nil {
return *users, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -446,7 +373,6 @@ func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string,
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
return nil, err
}
cache.CacheSearch(users, []string{"user"}, "user_with_detail_limited", cacheConditions...)
return users, nil
}
@ -466,7 +392,6 @@ func (ur _UserRepository) UpdateServiceExpiration(tx pgx.Tx, ctx context.Context
ur.log.Error("向数据库更新指定用户的服务有效期限失败。", zap.String("user id", uid), zap.Error(err))
return false, err
} else {
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return res.RowsAffected() > 0, nil
}
}
@ -477,12 +402,6 @@ func (ur _UserRepository) RetrieveUsersDetail(uids []string) ([]*model.UserDetai
if len(uids) == 0 {
return make([]*model.UserDetail, 0), nil
}
cacheConditions := []string{
strings.Join(uids, ","),
}
if users, err := cache.RetrieveSearch[[]*model.UserDetail]("user_detail", cacheConditions...); err == nil && users != nil {
return *users, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
@ -496,6 +415,5 @@ func (ur _UserRepository) RetrieveUsersDetail(uids []string) ([]*model.UserDetai
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
return make([]*model.UserDetail, 0), err
}
cache.CacheSearch(users, []string{"user", "user_detail"}, "user", cacheConditions...)
return users, nil
}

View File

@ -1,7 +1,6 @@
package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/repository"
@ -68,8 +67,6 @@ func (cs _ChargeService) RecordUserCharge(uid string, fee, discount, amount *flo
cs.log.Error("提交数据库事务失败。", zap.Error(err))
return false, err
}
cache.AbolishRelation("charge")
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return true, nil
}
@ -114,7 +111,5 @@ func (cs _ChargeService) CancelUserCharge(uid string, seq int64) (bool, error) {
cs.log.Error("提交数据库事务失败。", zap.Error(err))
return false, err
}
cache.AbolishRelation("charge")
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
return true, nil
}

View File

@ -1,13 +1,11 @@
package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/excel"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
"electricity_bill_calc/repository"
"electricity_bill_calc/tools"
"electricity_bill_calc/types"
"electricity_bill_calc/vo"
"fmt"
@ -69,8 +67,6 @@ func (ms _MeterService) CreateMeterRecord(pid string, form *vo.MeterCreationForm
tx.Rollback(ctx)
return err
}
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}
@ -104,8 +100,6 @@ func (ms _MeterService) UpdateMeterRecord(pid string, code string, form *vo.Mete
tx.Rollback(ctx)
return err
}
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}
@ -446,7 +440,6 @@ func (ms _MeterService) ReplaceMeter(
tx.Rollback(ctx)
return err
}
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}
@ -474,16 +467,6 @@ func (ms _MeterService) ListPooledMeterRelations(pid, masterMeter string) ([]*mo
// 列出指定园区中所有的公摊表计
func (ms _MeterService) SearchPooledMetersDetail(pid string, page uint, keyword *string) ([]*model.PooledMeterDetailCompound, int64, error) {
ms.log.Info("列出指定园区中所有的公摊表计", zap.String("park id", pid), zap.Uint("page", page), zap.String("keyword", *keyword))
cacheConditions := []string{
pid,
fmt.Sprintf("%d", page),
tools.DefaultTo(keyword, "UNDEFINED"),
}
if meters, total, err := cache.RetrievePagedSearch[[]*model.PooledMeterDetailCompound]("assemble_pooled_meters_detail", cacheConditions...); err == nil {
ms.log.Info("已经从缓存中获取到了指定园区中所有的公摊表计。", zap.Int("count", len(*meters)), zap.Int64("total", total))
return *meters, total, nil
}
poolingMeters, total, err := repository.MeterRepository.ListPoolingMeters(pid, page, keyword)
if err != nil {
ms.log.Error("无法列出指定园区中所有的公摊表计。", zap.Error(err))
@ -530,8 +513,6 @@ func (ms _MeterService) SearchPooledMetersDetail(pid string, page uint, keyword
})
}
cache.CachePagedSearch(assembled, total, []string{fmt.Sprintf("meter:%s", pid), fmt.Sprintf("meter_relation:%s", pid)}, "assemble_pooled_meter_detail", cacheConditions...)
return assembled, total, nil
}
@ -567,7 +548,6 @@ func (ms _MeterService) BindMeter(pid, masterMeter string, slaveMeters []string)
tx.Rollback(ctx)
return false, err
}
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return true, nil
}
@ -603,7 +583,6 @@ func (ms _MeterService) UnbindMeter(pid, masterMeter string, slaveMeters []strin
tx.Rollback(ctx)
return false, err
}
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return true, nil
}

View File

@ -1,7 +1,6 @@
package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
@ -62,7 +61,6 @@ func (ts _TenementService) CreateTenementRecord(pid string, creationForm *vo.Ten
tx.Rollback(ctx)
return fmt.Errorf("未能提交数据库事务,%w", err)
}
cache.AbolishRelation(fmt.Sprintf("tenement:%s", pid))
return nil
}
@ -108,8 +106,6 @@ func (ts _TenementService) BindMeter(pid, tid, meterCode string, reading *vo.Met
tx.Rollback(ctx)
return fmt.Errorf("未能提交数据库事务,%w", err)
}
cache.AbolishRelation(fmt.Sprintf("tenement:%s", pid))
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}
@ -155,8 +151,6 @@ func (ts _TenementService) UnbindMeter(pid, tid, meterCode string, reading *vo.M
tx.Rollback(ctx)
return fmt.Errorf("未能提交数据库事务,%w", err)
}
cache.AbolishRelation(fmt.Sprintf("tenement:%s", pid))
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}
@ -243,8 +237,5 @@ func (ts _TenementService) MoveOutTenement(pid, tid string, reading []*vo.MeterR
tx.Rollback(ctx)
return fmt.Errorf("未能提交数据库事务,%w", err)
}
cache.AbolishRelation(fmt.Sprintf("tenement:%s", pid))
cache.AbolishRelation(fmt.Sprintf("meter:%s", pid))
return nil
}