@@ -256,6 +256,23 @@ func (a *TaskUsecase) Continue(ctx context.Context, user *domain.User, id uuid.U
256256 return nil
257257}
258258
259+ // IncrUserInputCount 记录用户输入次数到 Redis Hash,并按天计数
260+ func (a * TaskUsecase ) IncrUserInputCount (ctx context.Context , userID , taskID uuid.UUID ) error {
261+ // 按 task 维度计数(总量,不设过期)
262+ key := fmt .Sprintf ("mcai:user:%s:input_count" , userID .String ())
263+ if err := a .redis .HIncrBy (ctx , key , taskID .String (), 1 ).Err (); err != nil {
264+ return err
265+ }
266+
267+ // 按天计数(用于时间范围统计,90 天过期)
268+ dailyKey := fmt .Sprintf ("mcai:user:%s:input_daily:%s" , userID .String (), time .Now ().Format ("2006-01-02" ))
269+ pipe := a .redis .Pipeline ()
270+ pipe .Incr (ctx , dailyKey )
271+ pipe .Expire (ctx , dailyKey , 90 * 24 * time .Hour )
272+ _ , err := pipe .Exec (ctx )
273+ return err
274+ }
275+
259276// Create implements domain.TaskUsecase.
260277func (a * TaskUsecase ) Create (ctx context.Context , user * domain.User , req domain.CreateTaskReq ) (* domain.ProjectTask , error ) {
261278 r , err := a .taskflow .Host ().IsOnline (ctx , & taskflow.IsOnlineReq [string ]{
@@ -455,6 +472,10 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
455472 }
456473 a .logger .With ("req" , req ).InfoContext (ctx , "task created" )
457474
475+ if err := a .IncrUserInputCount (ctx , user .ID , pt .Edges .Task .ID ); err != nil {
476+ a .logger .WarnContext (ctx , "failed to incr user input count on create" , "error" , err )
477+ }
478+
458479 result := cvt .From (pt , & domain.ProjectTask {})
459480
460481 // 通知 TaskHook(如内部项目的 git task 创建等)
0 commit comments