|
|
@@ -4,6 +4,7 @@ namespace Tests\Unit\AppGame\Handler\Promotion;
|
|
|
|
|
|
use Tests\TestCase;
|
|
|
use App\Module\UrsPromotion\Models\UrsUserRelationCache;
|
|
|
+use App\Module\UrsPromotion\Models\UrsUserMapping;
|
|
|
use App\Module\UrsPromotion\Services\UrsUserMappingService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Carbon\Carbon;
|
|
|
@@ -16,52 +17,57 @@ use Carbon\Carbon;
|
|
|
class TodayStatsLogicTest extends TestCase
|
|
|
{
|
|
|
/**
|
|
|
- * 测试优化后的查询逻辑
|
|
|
+ * 测试优化后的查询逻辑(基于mapping_time)
|
|
|
*/
|
|
|
- public function test_optimized_query_logic()
|
|
|
+ public function test_optimized_query_logic_with_mapping_time()
|
|
|
{
|
|
|
// 模拟查询逻辑
|
|
|
$farmUserId = 20001;
|
|
|
-
|
|
|
- // 构建优化后的查询SQL
|
|
|
- $expectedSql = "select COUNT(CASE WHEN depth = 1 THEN 1 END) as direct_new_count, COUNT(CASE WHEN depth <= 3 THEN 1 END) as team_new_count from `kku_urs_promotion_user_relation_cache` where `related_user_id` = ? and date(`created_at`) = ?";
|
|
|
-
|
|
|
+
|
|
|
+ // 构建基于mapping_time的查询SQL
|
|
|
+ $expectedSql = "select COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth = 1 THEN 1 END) as direct_new_count, COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth <= 3 THEN 1 END) as team_new_count from `kku_urs_promotion_user_relation_cache` inner join `kku_urs_promotion_user_mappings` on `kku_kku_urs_promotion_user_relation_cache`.`urs_user_id` = `kku_urs_promotion_user_mappings`.`urs_user_id` where `related_user_id` = ? and `kku_urs_promotion_user_mappings`.`status` = ? and date(`kku_urs_promotion_user_mappings`.`mapping_time`) = ?";
|
|
|
+
|
|
|
// 验证查询构建逻辑
|
|
|
$query = UrsUserRelationCache::where('related_user_id', $farmUserId)
|
|
|
- ->whereDate('created_at', today())
|
|
|
+ ->join('urs_promotion_user_mappings', 'kku_urs_promotion_user_relation_cache.urs_user_id', '=', 'urs_promotion_user_mappings.urs_user_id')
|
|
|
+ ->where('urs_promotion_user_mappings.status', UrsUserMapping::STATUS_VALID)
|
|
|
+ ->whereDate('urs_promotion_user_mappings.mapping_time', today())
|
|
|
->selectRaw('
|
|
|
- COUNT(CASE WHEN depth = 1 THEN 1 END) as direct_new_count,
|
|
|
- COUNT(CASE WHEN depth <= 3 THEN 1 END) as team_new_count
|
|
|
+ COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth = 1 THEN 1 END) as direct_new_count,
|
|
|
+ COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth <= 3 THEN 1 END) as team_new_count
|
|
|
');
|
|
|
-
|
|
|
+
|
|
|
$actualSql = $query->toSql();
|
|
|
-
|
|
|
+
|
|
|
// 清理SQL中的多余空格和换行
|
|
|
$cleanExpectedSql = preg_replace('/\s+/', ' ', trim($expectedSql));
|
|
|
$cleanActualSql = preg_replace('/\s+/', ' ', trim($actualSql));
|
|
|
-
|
|
|
+
|
|
|
$this->assertEquals($cleanExpectedSql, $cleanActualSql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 测试查询参数绑定
|
|
|
+ * 测试查询参数绑定(基于mapping_time)
|
|
|
*/
|
|
|
- public function test_query_bindings()
|
|
|
+ public function test_query_bindings_with_mapping_time()
|
|
|
{
|
|
|
$farmUserId = 20001;
|
|
|
-
|
|
|
+
|
|
|
$query = UrsUserRelationCache::where('related_user_id', $farmUserId)
|
|
|
- ->whereDate('created_at', today())
|
|
|
+ ->join('urs_promotion_user_mappings', 'kku_urs_promotion_user_relation_cache.urs_user_id', '=', 'urs_promotion_user_mappings.urs_user_id')
|
|
|
+ ->where('urs_promotion_user_mappings.status', UrsUserMapping::STATUS_VALID)
|
|
|
+ ->whereDate('urs_promotion_user_mappings.mapping_time', today())
|
|
|
->selectRaw('
|
|
|
- COUNT(CASE WHEN depth = 1 THEN 1 END) as direct_new_count,
|
|
|
- COUNT(CASE WHEN depth <= 3 THEN 1 END) as team_new_count
|
|
|
+ COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth = 1 THEN 1 END) as direct_new_count,
|
|
|
+ COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth <= 3 THEN 1 END) as team_new_count
|
|
|
');
|
|
|
-
|
|
|
+
|
|
|
$bindings = $query->getBindings();
|
|
|
-
|
|
|
- // 验证绑定参数
|
|
|
+
|
|
|
+ // 验证绑定参数:related_user_id, status, mapping_time
|
|
|
$this->assertEquals($farmUserId, $bindings[0]);
|
|
|
- $this->assertEquals(today()->format('Y-m-d'), $bindings[1]);
|
|
|
+ $this->assertEquals(UrsUserMapping::STATUS_VALID, $bindings[1]);
|
|
|
+ $this->assertEquals(today()->format('Y-m-d'), $bindings[2]);
|
|
|
}
|
|
|
|
|
|
/**
|