project_task_link_statistic.go 793 B

12345678910111213141516171819202122232425262728293031323334353637
  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.IsZero() {
  22. var currentTime time.Time
  23. stat.CreateTime = &currentTime
  24. }
  25. // 执行创建
  26. if err := db.WithContext(ctx).Create(stat).Error; err != nil {
  27. // 可以在这里添加特定错误处理
  28. return fmt.Errorf("failed to create statistic: %w", err)
  29. }
  30. return nil
  31. }