feat: 卡实名时间以上游 archiveTime 为准并记录实名时间历史,新增二次实名批次
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
This commit is contained in:
96
internal/application/cardobservation/realname_time.go
Normal file
96
internal/application/cardobservation/realname_time.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package cardobservation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
domain "github.com/break/junhong_cmp_fiber/internal/domain/cardobservation"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// loadRealnameTime 在调用方事务中读取卡当前实名记录;调用方已锁定卡行,同一张卡的写入因此串行。
|
||||
func loadRealnameTime(ctx context.Context, tx *gorm.DB, cardID uint) (*model.IotCardRealname, domain.RealnameTimeSnapshot, error) {
|
||||
var record model.IotCardRealname
|
||||
err := tx.WithContext(ctx).Where("card_id = ?", cardID).Take(&record).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, domain.RealnameTimeSnapshot{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, domain.RealnameTimeSnapshot{}, errors.Wrap(errors.CodeDatabaseError, err, "查询卡当前实名记录失败")
|
||||
}
|
||||
return &record, realnameTimeSnapshot(&record), nil
|
||||
}
|
||||
|
||||
func realnameTimeSnapshot(record *model.IotCardRealname) domain.RealnameTimeSnapshot {
|
||||
if record == nil {
|
||||
return domain.RealnameTimeSnapshot{}
|
||||
}
|
||||
return domain.RealnameTimeSnapshot{
|
||||
Exists: true, RealnameAt: record.RealnameAt, RealnameAtSource: record.RealnameAtSource,
|
||||
UpstreamRealnameAt: record.UpstreamRealnameAt, FirstObservedAt: record.FirstObservedAt,
|
||||
ResetStatus: record.ResetStatus, ResetBaselineAt: record.ResetBaselineAt,
|
||||
}
|
||||
}
|
||||
|
||||
// applyRealnameTime 在卡已被采纳为已实名后更新当前实名记录;只有实名时间变化才写历史。
|
||||
func applyRealnameTime(ctx context.Context, tx *gorm.DB, card *model.IotCard, record *model.IotCardRealname,
|
||||
observation domain.RealnameObservation, gate domain.RealnameResetGate) error {
|
||||
decision := domain.DecideRealnameTime(realnameTimeSnapshot(record), observation, gate)
|
||||
if !decision.Changed {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
var resetBatchID *uint
|
||||
if record != nil {
|
||||
resetBatchID = record.ResetBatchID
|
||||
}
|
||||
if record == nil {
|
||||
created := model.IotCardRealname{
|
||||
CardID: card.ID, RealnameAt: decision.RealnameAt, RealnameAtSource: decision.Source,
|
||||
UpstreamRealnameAt: decision.UpstreamRealnameAt, FirstObservedAt: decision.FirstObservedAt,
|
||||
ResetStatus: constants.RealnameResetStatusNone, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := tx.WithContext(ctx).Create(&created).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建卡当前实名记录失败")
|
||||
}
|
||||
} else {
|
||||
updates := map[string]any{
|
||||
"realname_at": decision.RealnameAt, "realname_at_source": decision.Source,
|
||||
"upstream_realname_at": decision.UpstreamRealnameAt, "first_observed_at": decision.FirstObservedAt,
|
||||
"updated_at": now,
|
||||
}
|
||||
if decision.ClearReset {
|
||||
updates["reset_status"] = constants.RealnameResetStatusNone
|
||||
updates["reset_at"] = nil
|
||||
updates["reset_baseline_at"] = nil
|
||||
updates["reset_batch_id"] = nil
|
||||
}
|
||||
if err := tx.WithContext(ctx).Model(&model.IotCardRealname{}).Where("id = ?", record.ID).Updates(updates).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新卡当前实名记录失败")
|
||||
}
|
||||
}
|
||||
if decision.ClearReset && resetBatchID != nil {
|
||||
if err := tx.WithContext(ctx).Model(&model.IotCardRealnameResetBatch{}).Where("id = ?", *resetBatchID).
|
||||
Updates(map[string]any{"completed_count": gorm.Expr("completed_count + 1"), "updated_at": now}).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新二次实名批次完成数失败")
|
||||
}
|
||||
}
|
||||
if !decision.WriteHistory {
|
||||
return nil
|
||||
}
|
||||
history := model.IotCardRealnameHistory{
|
||||
CardID: card.ID, ICCID: card.ICCID, BeforeAt: decision.BeforeAt, AfterAt: decision.RealnameAt,
|
||||
AfterSource: decision.Source, ChangeType: decision.ChangeType, ObservationSource: observation.Metadata.Source,
|
||||
ObservedAt: observation.Metadata.ObservedAt, CreatedAt: now,
|
||||
}
|
||||
if decision.ClearReset {
|
||||
history.ResetBatchID = resetBatchID
|
||||
}
|
||||
if err := tx.WithContext(ctx).Create(&history).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "写入卡实名时间历史失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user