diff --git a/service/god_mode.go b/service/god_mode.go index 036a16a..7fc5007 100644 --- a/service/god_mode.go +++ b/service/god_mode.go @@ -1,34 +1,40 @@ package service import ( + "context" + "database/sql" "electricity_bill_calc/cache" "electricity_bill_calc/exceptions" "electricity_bill_calc/global" + "electricity_bill_calc/logger" "electricity_bill_calc/model" "fmt" "time" "github.com/samber/lo" "github.com/shopspring/decimal" - "xorm.io/builder" - "xorm.io/xorm" - "xorm.io/xorm/schemas" + "github.com/uptrace/bun" + "go.uber.org/zap" ) -type _GodModeService struct{} +type _GodModeService struct { + l *zap.Logger +} -var GodModeService _GodModeService +var GodModeService = _GodModeService{ + l: logger.Named("Service", "GodMode"), +} // 从此处开始为删除报表相关的部分 -func (_GodModeService) resetReportIndex(tx *xorm.Session, reportId string) (bool, error) { +func (_GodModeService) resetReportIndex(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { var report = new(model.Report) - has, err := tx.ID(reportId).NoAutoCondition().Get(report) + err := tx.NewSelect().Model(report).Where("id = ?", reportId).Scan(*ctx) if err != nil { tx.Rollback() return false, err } - if !has { + if report == nil { tx.Rollback() return false, exceptions.NewNotFoundError("指定报表索引未找到。") } @@ -44,9 +50,9 @@ func (_GodModeService) resetReportIndex(tx *xorm.Session, reportId string) (bool report.LastWithdrawAppliedAt = nil report.LastWithdrawAuditAt = nil - affected, err := tx. - ID(reportId). - MustCols( + res, err := tx.NewUpdate().Model(report). + WherePK(). + Column( "step_state", "published", "published_at", @@ -54,61 +60,66 @@ func (_GodModeService) resetReportIndex(tx *xorm.Session, reportId string) (bool "last_withdraw_applied_at", "last_withdraw_audit_at", ). - Update(report) - if err != nil { + Exec(*ctx) + + if affected, _ := res.RowsAffected(); err != nil || affected == 0 { tx.Rollback() return false, err } - return affected > 0, err + return true, err } -func (_GodModeService) resetReportSummary(tx *xorm.Session, reportId string) (bool, error) { +func (_GodModeService) resetReportSummary(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { var summary = &model.ReportSummary{ ReportId: reportId, } - _, err := tx.ID(reportId).AllCols().NoAutoCondition().Update(summary) + _, err := tx.NewUpdate().Model(summary).WherePK().Exec(*ctx) if err != nil { tx.Rollback() return false, err } var report = new(model.Report) - _, err = tx.ID(reportId).NoAutoCondition().Unscoped().Get(report) + err = tx.NewSelect().Model(report).WhereAllWithDeleted().Scan(*ctx) if err != nil { tx.Rollback() return false, err } report.StepState.Summary = false - rows, err := tx.ID(reportId).Cols("step_state").NoAutoCondition().Update(report) + res, err := tx.NewUpdate().Model(report).Column("step_state").Exec(*ctx) + rows, _ := res.RowsAffected() if err != nil { tx.Rollback() } return rows >= 0, err } -func (_GodModeService) flushReportMaintenances(tx *xorm.Session, reportId string) (bool, error) { - _, err := tx. - Where(builder.Eq{"report_id": reportId}). - NoAutoCondition(). - Delete(new(model.WillDilutedFee)) +func (_GodModeService) flushReportMaintenances(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { + _, err := tx.NewDelete().Model((*model.WillDilutedFee)(nil)). + Where("report_id = ?", reportId). + Exec(*ctx) if err != nil { tx.Rollback() return false, err } var report = new(model.Report) - _, err = tx.ID(reportId).NoAutoCondition().Unscoped().Get(report) + err = tx.NewSelect().Model(report).Where("id = ?", reportId).Scan(*ctx) if err != nil { tx.Rollback() return false, err } report.StepState.WillDiluted = false - rows, err := tx.ID(reportId).Cols("step_state").NoAutoCondition().Update(report) + res, err := tx.NewUpdate().Model(report). + WherePK(). + Column("step_state"). + Exec(*ctx) + rows, _ := res.RowsAffected() if err != nil { tx.Rollback() } return rows >= 0, err } -func (_GodModeService) resetSingleEndUserRecord(tx *xorm.Session, record *model.EndUserDetail, additionalColumns ...string) (bool, error) { +func (_GodModeService) resetSingleEndUserRecord(tx *bun.Tx, ctx *context.Context, record *model.EndUserDetail, additionalColumns ...string) (bool, error) { record.CurrentPeriodOverall = decimal.Zero record.CurrentPeriodCritical = decimal.Zero record.CurrentPeriodPeak = decimal.Zero @@ -172,10 +183,11 @@ func (_GodModeService) resetSingleEndUserRecord(tx *xorm.Session, record *model. } columns = append(columns, additionalColumns...) - affected, err := tx. - ID(schemas.PK{record.ReportId, record.ParkId, record.MeterId}). - MustCols(columns...). - Update(record) + res, err := tx.NewUpdate().Model(record). + WherePK(). + Column(columns...). + Exec(*ctx) + affected, _ := res.RowsAffected() if err != nil || affected == 0 { tx.Rollback() return false, err @@ -183,21 +195,29 @@ func (_GodModeService) resetSingleEndUserRecord(tx *xorm.Session, record *model. return true, nil } -func (g _GodModeService) resynchronizeEndUserArchives(tx *xorm.Session, reportId string) (bool, error) { +func (g _GodModeService) resynchronizeEndUserArchives(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { var currentRecords = make([]*model.EndUserDetail, 0) - err := tx.Where(builder.Eq{"report_id": reportId}).Find(¤tRecords) + err := tx.NewSelect().Model(¤tRecords). + Where("report_id = ?", reportId). + Scan(*ctx) if err != nil { tx.Rollback() return false, err } var report = new(model.Report) - has, err := tx.ID(reportId).NoAutoCondition().Unscoped().Get(report) - if err != nil || !has { + err = tx.NewSelect().Model(report). + Where("id = ?", reportId). + WhereAllWithDeleted(). + Scan(*ctx) + if err != nil || report == nil { tx.Rollback() return false, err } var latestArchives = make([]model.Meter04KV, 0) - err = tx.Table(new(model.Meter04KV)).Where(builder.Eq{"park_id": report.ParkId}).Find(&latestArchives) + err = tx.NewSelect().Model(&latestArchives). + Where("park_id = ?", report.ParkId). + Where("enabled = ?", true). + Scan(*ctx) if err != nil { tx.Rollback() return false, err @@ -217,7 +237,7 @@ func (g _GodModeService) resynchronizeEndUserArchives(tx *xorm.Session, reportId record.IsPublicMeter = meter.IsPublicMeter record.WillDilute = meter.WillDilute success, err := g.resetSingleEndUserRecord( - tx, record, + tx, ctx, record, "customer_name", "address", "ratio", @@ -249,7 +269,7 @@ func (g _GodModeService) resynchronizeEndUserArchives(tx *xorm.Session, reportId LastPeriodFlat: decimal.Zero, LastPeriodValley: decimal.Zero, } - _, err = tx.Insert(newEndUser) + _, err = tx.NewInsert().Model(&newEndUser).Exec(*ctx) if err != nil { tx.Rollback() return false, err @@ -258,34 +278,46 @@ func (g _GodModeService) resynchronizeEndUserArchives(tx *xorm.Session, reportId } report.StepState.Submeter = false - rows, err := tx.ID(reportId).Cols("step_state").NoAutoCondition().Update(report) + res, err := tx.NewUpdate().Model(report). + WherePK(). + Column("step_state"). + Exec(*ctx) + rows, _ := res.RowsAffected() if err != nil { tx.Rollback() } return rows >= 0, nil } -func (g _GodModeService) resetEndUserRecords(tx *xorm.Session, reportId string) (bool, error) { +func (g _GodModeService) resetEndUserRecords(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { var records = make([]*model.EndUserDetail, 0) - err := tx.Where(builder.Eq{"report_id": reportId}).Find(&records) + err := tx.NewSelect().Model(&records). + Where("report_id = ?", reportId). + Scan(*ctx) if err != nil { tx.Rollback() return false, err } for _, u := range records { - success, err := g.resetSingleEndUserRecord(tx, u) + success, err := g.resetSingleEndUserRecord(tx, ctx, u) if err != nil { return success, err } } var report = new(model.Report) - _, err = tx.ID(reportId).NoAutoCondition().Unscoped().Get(report) + err = tx.NewSelect().Model(report).WhereAllWithDeleted(). + Where("id = ?", reportId). + Scan(*ctx) if err != nil { tx.Rollback() return false, err } report.StepState.Submeter = false - rows, err := tx.ID(reportId).Cols("step_state").NoAutoCondition().Update(report) + res, err := tx.NewUpdate().Model(report). + WherePK(). + Column("step_state"). + Exec(*ctx) + rows, _ := res.RowsAffected() if err != nil { tx.Rollback() } @@ -297,46 +329,51 @@ type ReportPeriod struct { Period time.Time } -func (_GodModeService) isTheLatestReport(reportId string) (bool, error) { +func (_GodModeService) isTheLatestReport(ctx *context.Context, reportId string) (bool, error) { var report = new(model.Report) - has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(report) + err := global.DB.NewSelect().Model(report). + Where("id = ?", reportId). + WhereAllWithDeleted(). + Scan(*ctx) + if err != nil || report == nil { + return false, exceptions.NewNotFoundErrorFromError("指定报表索引未找到。", err) + } + var maxPeriod *time.Time + err = global.DB.NewSelect().Model((*model.Report)(nil)). + ColumnExpr("max(?)", bun.Ident("period")). + Where("park_id = ?", report.ParkId). + Scan(*ctx, maxPeriod) if err != nil { return false, err } - if !has { - return false, exceptions.NewNotFoundError("指定报表索引未找到。") - } - var reports = make([]ReportPeriod, 0) - err = global.DBConn. - Table(new(model.Report)). - Where(builder.Eq{"park_id": report.ParkId}). - Find(&reports) - if err != nil { - return false, err - } - maxReport := lo.MaxBy(reports, func(a, b ReportPeriod) bool { - return a.Period.After(b.Period) - }) - return maxReport.Id == reportId, nil + return maxPeriod.Equal(report.Period), nil } -func (_GodModeService) forceDeleteReport(tx *xorm.Session, reportId string) (bool, error) { - _, err := tx.Exec("delete from end_user_detail where report_id=?", reportId) +func (_GodModeService) forceDeleteReport(tx *bun.Tx, ctx *context.Context, reportId string) (bool, error) { + _, err := tx.NewDelete().Model((*model.EndUserDetail)(nil)). + Where("report_id = ?", reportId). + Exec(*ctx) if err != nil { tx.Rollback() return false, err } - _, err = tx.Exec("delete from will_diluted_fee where report_id=?", reportId) + _, err = tx.NewDelete().Model((*model.WillDilutedFee)(nil)). + Where("report_id = ?", reportId). + Exec(*ctx) if err != nil { tx.Rollback() return false, err } - _, err = tx.Exec("delete from report_summary where report_id=?", reportId) + _, err = tx.NewDelete().Model((*model.ReportSummary)(nil)). + Where("report_id = ?", reportId). + Exec(*ctx) if err != nil { tx.Rollback() return false, err } - _, err = tx.Exec("delete from report where id=?", reportId) + _, err = tx.NewDelete().Model((*model.Report)(nil)). + Where("id = ?", reportId). + Exec(*ctx) if err != nil { tx.Rollback() return false, err @@ -345,20 +382,22 @@ func (_GodModeService) forceDeleteReport(tx *xorm.Session, reportId string) (boo } func (g _GodModeService) ClearReportSummary(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能操作非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.resetReportSummary(tx, reportId) + result, err := g.resetReportSummary(&tx, &ctx, reportId) if err != nil { return false, err } @@ -368,25 +407,27 @@ func (g _GodModeService) ClearReportSummary(reportId string) (bool, error) { tx.Rollback() return false, err } - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } func (g _GodModeService) ClearReportMaintenances(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能操作非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.flushReportMaintenances(tx, reportId) + result, err := g.flushReportMaintenances(&tx, &ctx, reportId) if err != nil { return false, err } @@ -396,25 +437,27 @@ func (g _GodModeService) ClearReportMaintenances(reportId string) (bool, error) tx.Rollback() return false, err } - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } func (g _GodModeService) ResynchronizeEndUser(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能操作非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.resynchronizeEndUserArchives(tx, reportId) + result, err := g.resynchronizeEndUserArchives(&tx, &ctx, reportId) if err != nil { return false, err } @@ -425,25 +468,27 @@ func (g _GodModeService) ResynchronizeEndUser(reportId string) (bool, error) { return false, err } cache.AbolishRelation("end_user_detail") - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } func (g _GodModeService) ResetEndUserRegisterRecords(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能操作非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.resetEndUserRecords(tx, reportId) + result, err := g.resetEndUserRecords(&tx, &ctx, reportId) if err != nil { return false, err } @@ -454,40 +499,42 @@ func (g _GodModeService) ResetEndUserRegisterRecords(reportId string) (bool, err return false, err } cache.AbolishRelation("end_user_detail") - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } func (g _GodModeService) ResetReport(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能操作非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() var result = true - r, err := g.resetEndUserRecords(tx, reportId) + r, err := g.resetEndUserRecords(&tx, &ctx, reportId) if err != nil { return false, err } result = result && r - r, err = g.flushReportMaintenances(tx, reportId) + r, err = g.flushReportMaintenances(&tx, &ctx, reportId) if err != nil { return false, err } result = result && r - r, err = g.resetReportSummary(tx, reportId) + r, err = g.resetReportSummary(&tx, &ctx, reportId) if err != nil { return false, err } result = result && r - r, err = g.resetReportIndex(tx, reportId) + r, err = g.resetReportIndex(&tx, &ctx, reportId) if err != nil { return false, err } @@ -499,25 +546,27 @@ func (g _GodModeService) ResetReport(reportId string) (bool, error) { return false, err } cache.AbolishRelation("end_user_detail") - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } func (g _GodModeService) DeleteReport(reportId string) (bool, error) { - isLatest, err := g.isTheLatestReport(reportId) + ctx, cancel := global.TimeoutContext() + defer cancel() + + isLatest, err := g.isTheLatestReport(&ctx, reportId) if err != nil { return false, err } if !isLatest { return false, exceptions.NewImproperOperateError("不能删除非最新期数的报表。") } - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.forceDeleteReport(tx, reportId) + result, err := g.forceDeleteReport(&tx, &ctx, reportId) if err != nil { return false, err } @@ -528,15 +577,19 @@ func (g _GodModeService) DeleteReport(reportId string) (bool, error) { return false, err } cache.AbolishRelation("end_user_detail") - cache.AbolishRelation(fmt.Sprintf("report_%s", reportId)) + cache.AbolishRelation(fmt.Sprintf("report:%s", reportId)) return result, nil } // 从此处开始为删除园区相关的内容部分 -func (_GodModeService) deleteSpecificMaintenance(tx *xorm.Session, parkId, maintenanceId string) (bool, error) { - res, err := tx.Exec("delete from maintenance_fee where park_id=? and id=?", parkId, maintenanceId) +func (_GodModeService) deleteSpecificMaintenance(tx *bun.Tx, ctx *context.Context, parkId, maintenanceId string) (bool, error) { + res, err := tx.NewDelete().Model((*model.MaintenanceFee)(nil)). + Where("park_id = ?", parkId). + Where("id = ?", maintenanceId). + Exec(*ctx) + if err != nil { tx.Rollback() return false, nil @@ -549,8 +602,10 @@ func (_GodModeService) deleteSpecificMaintenance(tx *xorm.Session, parkId, maint } } -func (_GodModeService) deleteAllMaintenance(tx *xorm.Session, parkId string) (bool, error) { - res, err := tx.Exec("delete from maintenance_fee where park_id=?", parkId) +func (_GodModeService) deleteAllMaintenance(tx *bun.Tx, ctx *context.Context, parkId string) (bool, error) { + res, err := tx.NewDelete().Model((*model.MaintenanceFee)(nil)). + Where("park_id = ?", parkId). + Exec(*ctx) if err != nil { tx.Rollback() return false, nil @@ -563,8 +618,10 @@ func (_GodModeService) deleteAllMaintenance(tx *xorm.Session, parkId string) (bo } } -func (_GodModeService) deleteAllMeters(tx *xorm.Session, parkId string) (bool, error) { - res, err := tx.Exec("delete from meter_04kv where park_id=?", parkId) +func (_GodModeService) deleteAllMeters(tx *bun.Tx, ctx *context.Context, parkId string) (bool, error) { + res, err := tx.NewDelete().Model((*model.Meter04KV)(nil)). + Where("park_id = ?", parkId). + Exec(*ctx) if err != nil { tx.Rollback() return false, nil @@ -577,8 +634,10 @@ func (_GodModeService) deleteAllMeters(tx *xorm.Session, parkId string) (bool, e } } -func (_GodModeService) deletePark(tx *xorm.Session, parkId string) (bool, error) { - res, err := tx.Exec("delete from park where id=?", parkId) +func (_GodModeService) deletePark(tx *bun.Tx, ctx *context.Context, parkId string) (bool, error) { + res, err := tx.NewDelete().Model((*model.Park)(nil)). + Where("id = ?", parkId). + Exec(*ctx) if err != nil { tx.Rollback() return false, nil @@ -592,13 +651,15 @@ func (_GodModeService) deletePark(tx *xorm.Session, parkId string) (bool, error) } func (g _GodModeService) RemoveSpecificMaintenance(parkId, maintenanceId string) (bool, error) { - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.deleteSpecificMaintenance(tx, parkId, maintenanceId) + result, err := g.deleteSpecificMaintenance(&tx, &ctx, parkId, maintenanceId) if err != nil { return false, err } @@ -608,19 +669,20 @@ func (g _GodModeService) RemoveSpecificMaintenance(parkId, maintenanceId string) tx.Rollback() return false, err } - cache.AbolishRelation("maintenance_fee") - cache.AbolishRelation(fmt.Sprintf("maintenance_fee_%s", maintenanceId)) + cache.AbolishRelation(fmt.Sprintf("maintenance_fee:%s", maintenanceId)) return result, nil } func (g _GodModeService) RemoveAllMaintenance(parkId string) (bool, error) { - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.deleteAllMaintenance(tx, parkId) + result, err := g.deleteAllMaintenance(&tx, &ctx, parkId) if err != nil { return false, err } @@ -635,13 +697,15 @@ func (g _GodModeService) RemoveAllMaintenance(parkId string) (bool, error) { } func (g _GodModeService) RemoveAllMeters(parkId string) (bool, error) { - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.deleteAllMeters(tx, parkId) + result, err := g.deleteAllMeters(&tx, &ctx, parkId) if err != nil { return false, err } @@ -655,37 +719,35 @@ func (g _GodModeService) RemoveAllMeters(parkId string) (bool, error) { return result, nil } -func (g _GodModeService) erasePark(tx *xorm.Session, parkId string) (bool, error) { +func (g _GodModeService) erasePark(tx *bun.Tx, ctx *context.Context, parkId string) (bool, error) { var reportIds = make([]string, 0) - err := tx. - Table(new(model.Report)). - Where(builder.Eq{"park_id": parkId}). - NoAutoCondition(). - Select("id"). - Find(&reportIds) + err := tx.NewSelect().Model((*model.Report)(nil)). + Where("park_id = ?", parkId). + Column("id"). + Scan(*ctx, &reportIds) if err != nil { tx.Rollback() return false, err } var result = true for _, id := range reportIds { - r, err := g.forceDeleteReport(tx, id) + r, err := g.forceDeleteReport(tx, ctx, id) if err != nil { return false, err } result = result && r } - r, err := g.deleteAllMaintenance(tx, parkId) + r, err := g.deleteAllMaintenance(tx, ctx, parkId) if err != nil { return false, err } result = result && r - r, err = g.deleteAllMeters(tx, parkId) + r, err = g.deleteAllMeters(tx, ctx, parkId) if err != nil { return false, err } result = result && r - r, err = g.deletePark(tx, parkId) + r, err = g.deletePark(tx, ctx, parkId) if err != nil { return false, err } @@ -695,13 +757,15 @@ func (g _GodModeService) erasePark(tx *xorm.Session, parkId string) (bool, error } func (g _GodModeService) RemovePark(parkId string) (bool, error) { - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() - result, err := g.erasePark(tx, parkId) + result, err := g.erasePark(&tx, &ctx, parkId) if err != nil { return false, err } @@ -711,28 +775,27 @@ func (g _GodModeService) RemovePark(parkId string) (bool, error) { tx.Rollback() return false, err } - cache.AbolishRelation("park") - cache.AbolishRelation(fmt.Sprintf("park_%s", parkId)) + cache.AbolishRelation(fmt.Sprintf("park:%s", parkId)) return result, nil } // 从此处开始为删除用户相关的部分 func (g _GodModeService) DeleteUser(userId string) (bool, error) { - tx := global.DBConn.NewSession() - if err := tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { return false, err } - defer tx.Close() var parkIds = make([]string, 0) - err := tx. - Table(new(model.Park)). - Where(builder.Eq{"user_id": userId}). - NoAutoCondition(). - Unscoped(). - Select("id"). - Find(&parkIds) + err = tx.NewSelect().Model((*model.Park)(nil)). + Where("user_id = ?", userId). + WhereAllWithDeleted(). + Column("id"). + Scan(ctx, &parkIds) if err != nil { tx.Rollback() return false, err @@ -740,7 +803,7 @@ func (g _GodModeService) DeleteUser(userId string) (bool, error) { var result = true for _, p := range parkIds { - r, err := g.erasePark(tx, p) + r, err := g.erasePark(&tx, &ctx, p) if err != nil { return false, err } @@ -748,7 +811,9 @@ func (g _GodModeService) DeleteUser(userId string) (bool, error) { } // 删除用户服务计费数据。 - res, err := tx.Exec("delete from user_charge where user_id=?", userId) + res, err := tx.NewDelete().Model((*model.UserCharge)(nil)). + Where("user_id = ?", userId). + Exec(ctx) if err != nil { tx.Rollback() return false, err @@ -760,7 +825,9 @@ func (g _GodModeService) DeleteUser(userId string) (bool, error) { result = result && (rows >= 0) } // 删除用户详细信息数据 - res, err = tx.Exec("delete from user_detail where id=?", userId) + res, err = tx.NewDelete().Model((*model.UserDetail)(nil)). + Where("user_id = ?", userId). + Exec(ctx) if err != nil { tx.Rollback() return false, err @@ -772,7 +839,9 @@ func (g _GodModeService) DeleteUser(userId string) (bool, error) { result = result && (rows >= 0) } // 删除用户基本索引数据 - res, err = tx.Exec("delete from `user` where id=?", userId) + res, err = tx.NewDelete().Model((*model.User)(nil)). + Where("id = ?", userId). + Exec(ctx) if err != nil { tx.Rollback() return false, err @@ -789,7 +858,7 @@ func (g _GodModeService) DeleteUser(userId string) (bool, error) { tx.Rollback() return false, err } - cache.AbolishRelation(fmt.Sprintf("user_%s", userId)) + cache.AbolishRelation(fmt.Sprintf("user:%s", userId)) cache.AbolishRelation("user") cache.AbolishRelation("park") cache.AbolishRelation("report")