project_task_link_statistic.go 818 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package db
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "youngee_b_api/model/gorm_model"
  8. )
  9. // CreateProjectTaskLinkStatistic 创建一条统计记录
  10. func CreateProjectTaskLinkStatistic(ctx context.Context, stat *gorm_model.ProjectTaskLinkStatistic) error {
  11. // 基本校验
  12. if stat == nil {
  13. return errors.New("nil statistic data")
  14. }
  15. // 获取写数据库连接
  16. db := GetWriteDB(ctx)
  17. if db == nil {
  18. return errors.New("database connection not available")
  19. }
  20. // 设置默认时间
  21. if stat.CreateTime == nil {
  22. var currentTime time.Time
  23. currentTime = time.Now()
  24. stat.CreateTime = &currentTime
  25. }
  26. // 执行创建
  27. if err := db.WithContext(ctx).Create(stat).Error; err != nil {
  28. // 可以在这里添加特定错误处理
  29. return fmt.Errorf("failed to create statistic: %w", err)
  30. }
  31. return nil
  32. }