MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / CreateLotteryDraw

Method CreateLotteryDraw

internal/service/lotteryDraw.go:456–503  ·  view source on GitHub ↗

CreateLotteryDraw 创建新的抽奖活动

(ctx context.Context, input domain.LotteryDraw)

Source from the content-addressed store, hash-verified

454
455// CreateLotteryDraw 创建新的抽奖活动
456func (s *lotteryDrawService) CreateLotteryDraw(ctx context.Context, input domain.LotteryDraw) error {
457 // 验证输入
458 if err := validateLotteryDrawInput(input); err != nil {
459 s.l.Error("invalid lottery draw input", zap.Error(err))
460 return err
461 }
462
463 // 检查名称唯一性
464 exists, err := s.repo.ExistsLotteryDrawByName(ctx, input.Name)
465 if err != nil {
466 s.l.Error("failed to check lottery draw name uniqueness", zap.String("name", input.Name), zap.Error(err))
467 return err
468 }
469
470 if exists {
471 return errors.New("同名的抽奖活动已存在")
472 }
473
474 // 设置状态
475 currentTime := time.Now().Unix()
476 var status string
477 switch {
478 case input.EndTime <= currentTime:
479 status = domain.LotteryStatusCompleted
480 case input.StartTime <= currentTime:
481 status = domain.LotteryStatusActive
482 default:
483 status = domain.LotteryStatusPending
484 }
485
486 // 创建抽奖活动
487 lotteryDraw := domain.LotteryDraw{
488 Name: input.Name,
489 Description: input.Description,
490 StartTime: input.StartTime,
491 EndTime: input.EndTime,
492 Status: status,
493 }
494
495 if err := s.repo.CreateLotteryDraw(ctx, lotteryDraw); err != nil {
496 s.l.Error("failed to create lottery draw", zap.String("name", input.Name), zap.Error(err))
497 return err
498 }
499
500 s.l.Info("lottery draw created", zap.String("name", input.Name))
501
502 return nil
503}
504
505// GetLotteryDrawByID 根据ID获取抽奖活动
506func (s *lotteryDrawService) GetLotteryDrawByID(ctx context.Context, id int) (domain.LotteryDraw, error) {

Callers

nothing calls this directly

Calls 3

validateLotteryDrawInputFunction · 0.85
CreateLotteryDrawMethod · 0.65

Tested by

no test coverage detected