@@ -363,51 +363,71 @@ public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logCom
363363 });
364364 }
365365
366- public function getCount (int $ userId , string $ category , int $ businessType = 0 ): int
366+ public function getCount (string $ category = '' , int $ userId = 0 , int $ businessType = 0 ): int
367367 {
368368 if ($ category == BonusLogs::CATEGORY_COMMON ) {
369- $ query = BonusLogs::query ()->where ('uid ' , $ userId );
370- if ($ businessType > 0 ) {
371- $ query ->where ('business_type ' , $ businessType );
372- }
369+ $ query = $ this ->buildQuery ($ userId , $ businessType );
373370 return $ query ->count ();
374371 } else if ($ category == BonusLogs::CATEGORY_SEEDING ) {
375- $ whereStr = "uid = :uid " ;
376- $ binds = ["uid " => $ userId ];
377- if ($ businessType > 0 ) {
378- $ whereStr .= " AND business_type = :business_type " ;
379- $ binds ["business_type " ] = $ businessType ;
380- }
372+ list ($ whereStr , $ binds ) = $ this ->buildWhereStrAndBinds ($ userId , $ businessType );
381373 return ClickHouse::count ("bonus_logs " , $ whereStr , $ binds );
382374 }
383375 throw new \InvalidArgumentException ("Invalid category: $ category " );
384376 }
385377
386- public function getList (int $ userId , string $ category , int $ businessType = 0 , int $ page = 1 , int $ perPage = 50 )
378+ public function getList (string $ category = '' , int $ userId = 0 , int $ businessType = 0 , int $ page = 1 , int $ perPage = 50 )
387379 {
388380 if ($ category == BonusLogs::CATEGORY_COMMON ) {
389- $ query = BonusLogs::query ()->where ('uid ' , $ userId );
390- if ($ businessType > 0 ) {
391- $ query ->where ('business_type ' , $ businessType );
392- }
381+ $ query = $ this ->buildQuery ($ userId , $ businessType );
393382 return $ query ->orderBy ("id " , "desc " )->forPage ($ page , $ perPage )->get ();
394383 } else if ($ category == BonusLogs::CATEGORY_SEEDING ) {
395- $ sql = "select * from bonus_logs where uid = :uid " ;
396- $ binds = ["uid " => $ userId ];
397- if ($ businessType > 0 ) {
398- $ sql .= " AND business_type = :business_type " ;
399- $ binds ["business_type " ] = $ businessType ;
400- }
384+ list ($ whereStr , $ binds ) = $ this ->buildWhereStrAndBinds ($ userId , $ businessType );
401385 $ offset = ($ page - 1 ) * $ perPage ;
402- $ rows = ClickHouse::list ("$ sql order by created_at desc limit $ offset, $ perPage " , $ binds );
386+ $ rows = ClickHouse::list ("select * from bonus_logs $ whereStr order by created_at desc limit $ offset, $ perPage " , $ binds );
403387 $ result = [];
388+ $ id = 1 ;//fake id
404389 foreach ($ rows as $ row ) {
405- $ result [] = new BonusLogs ($ row );
390+ $ record = new BonusLogs ($ row );
391+ $ record ->id = $ id ;
392+ $ result [] = $ record ;
393+ $ id ++;
406394 }
407395 return $ result ;
408396 }
409397 throw new \InvalidArgumentException ("Invalid category: $ category " );
410398 }
411399
400+ private function buildWhereStrAndBinds (int $ userId = 0 , int $ businessType = 0 )
401+ {
402+ $ whereArr = [];
403+ $ binds = [];
404+ if ($ userId > 0 ) {
405+ $ whereArr [] = "uid = :uid " ;
406+ $ binds ['uid ' ] = $ userId ;
407+ }
408+ if ($ businessType > 0 ) {
409+ $ whereArr [] = "business_type = :business_type " ;
410+ $ binds ["business_type " ] = $ businessType ;
411+ }
412+ if (empty ($ whereArr )) {
413+ $ whereStr = "" ;
414+ } else {
415+ $ whereStr = sprintf ("where %s " , implode (' AND ' , $ whereArr ));
416+ }
417+ return [$ whereStr , $ binds ];
418+ }
419+
420+ private function buildQuery (int $ userId = 0 , int $ businessType = 0 ): Builder
421+ {
422+ $ query = BonusLogs::query ();
423+ if ($ userId > 0 ) {
424+ $ query ->where ('uid ' , $ userId );
425+ }
426+ if ($ businessType > 0 ) {
427+ $ query ->where ('business_type ' , $ businessType );
428+ }
429+ return $ query ;
430+ }
431+
412432
413433}
0 commit comments