Files

77 lines
2.8 KiB
Go

package pickup
import (
"context"
"encoding/json"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/processlog"
"gorm.io/datatypes"
"gorm.io/gorm"
)
const processBusinessPickup = "pickup"
type ProcessEventDTO struct {
ID uint64 `json:"id"`
Stage string `json:"stage"`
Action string `json:"action"`
ActorType string `json:"actor_type"`
ActorID uint64 `json:"actor_id"`
ActorName string `json:"actor_name"`
TargetType string `json:"target_type"`
TargetID *uint64 `json:"target_id,omitempty"`
TargetName string `json:"target_name"`
Content string `json:"content"`
Reason string `json:"reason"`
Payload datatypes.JSON `json:"payload"`
AttachmentURLs []string `json:"attachment_urls"`
StateBefore datatypes.JSON `json:"state_before"`
StateAfter datatypes.JSON `json:"state_after"`
CreatedAt string `json:"created_at"`
}
func pickupState(row model.AdminPickup) map[string]any {
return map[string]any{
"pickup_status": row.Status, "offline_settlement_status": row.OfflineSettlementStatus,
"settlement_mode": row.SettlementMode,
}
}
func appendPickupEvent(tx *gorm.DB, pickup model.AdminPickup, action string, adminID uint64, content, reason string, payload, before map[string]any) error {
return processlog.Append(tx, processlog.Entry{
BusinessType: processBusinessPickup, BusinessID: pickup.ID, Stage: "pickup", Action: action,
ActorType: processlog.ActorAdmin, ActorID: adminID, TargetType: processlog.ActorUser,
TargetID: &pickup.OwnerID, Content: content, Reason: reason, Payload: payload,
StateBefore: before, StateAfter: pickupState(pickup),
})
}
func (r *Repository) ProcessEvents(ctx context.Context, pickupID uint64) ([]ProcessEventDTO, error) {
var pickup model.AdminPickup
if err := r.db.WithContext(ctx).First(&pickup, pickupID).Error; err != nil {
return nil, err
}
rows, err := processlog.List(r.db.WithContext(ctx), processBusinessPickup, pickupID)
if err != nil {
return nil, err
}
items := make([]ProcessEventDTO, 0, len(rows))
for _, row := range rows {
items = append(items, ProcessEventDTO{
ID: row.ID, Stage: row.Stage, Action: row.Action, ActorType: row.ActorType, ActorID: row.ActorID,
ActorName: row.ActorName, TargetType: row.TargetType, TargetID: row.TargetID, TargetName: row.TargetName,
Content: row.Content, Reason: row.Reason, Payload: row.Payload, AttachmentURLs: decodeStringList(row.AttachmentURLs),
StateBefore: row.StateBefore, StateAfter: row.StateAfter, CreatedAt: row.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
})
}
return items, nil
}
func decodeStringList(raw datatypes.JSON) []string {
items := make([]string, 0)
_ = json.Unmarshal(raw, &items)
return items
}