diff --git a/app/Domains/Virtual/Services/FlowPoolService.php b/app/Domains/Virtual/Services/FlowPoolService.php index 20e4d21b..cf57ab3e 100644 --- a/app/Domains/Virtual/Services/FlowPoolService.php +++ b/app/Domains/Virtual/Services/FlowPoolService.php @@ -428,7 +428,7 @@ class FlowPoolService extends Service 'unit_price' => 0 ]; - $cards = app(OrderCardPartitionRepository::class)->selectRaw('distinct sim as sim, package_id')->withConditions($cardConditions)->get(); + $cards = app(OrderCardPartitionRepository::class)->selectRaw('distinct sim as sim, package_id')->withConditions($cardConditions)->skipCache()->get(); $allTotal = $cards->count(); diff --git a/database/migrations/create_virtual_order_cards_func.pgsql b/database/migrations/create_virtual_order_cards_func.pgsql index 75d00787..539233b2 100644 --- a/database/migrations/create_virtual_order_cards_func.pgsql +++ b/database/migrations/create_virtual_order_cards_func.pgsql @@ -28,10 +28,8 @@ DECLARE activate_cards JSONB := '{}'; packages JSONB := '{}'; orders JSONB[] := '{}'; - query TEXT; package_row JSONB; order_row JSONB; - row RECORD; temp_service_start_at TIMESTAMP; temp_service_end_at TIMESTAMP; activated_at TIMESTAMP; @@ -39,19 +37,6 @@ BEGIN SELECT jsonb_object_agg(cards.sim, virtual_activated_at) INTO activate_cards FROM vd.cards WHERE cards.sim = ANY ($1); IF activate_cards IS NOT NULL THEN - SELECT jsonb_object_agg(t.id, row_to_json(t)) INTO packages FROM ( - SELECT virtual_packages.id, - virtual_packages.service_months, - virtual_packages.effect_months, - virtual_packages.delay_months - FROM vd.virtual_packages - ) t; - - query := 'SELECT id, type, sim, package_id, created_at, counts, service_start_at, service_end_at - FROM vd.virtual_order_cards_partition - WHERE sim = ANY($1) - ORDER BY sim ASC, created_at ASC'; - SELECT array_agg(row_to_json(t)) INTO orders FROM (SELECT virtual_order_cards_partition.id, virtual_order_cards_partition.type, @@ -65,50 +50,59 @@ BEGIN WHERE virtual_order_cards_partition.sim = ANY ($1) ORDER BY virtual_order_cards_partition.sim ASC, virtual_order_cards_partition.created_at ASC) t; - FOR i IN 1..array_length(orders, 1) - LOOP - order_row := orders [ i]; - activated_at := activate_cards -> (order_row->>'sim')->>'virtual_activated_at'; - package_row := packages -> (order_row->>'package_id'); - -- 服务时间 - CASE (order_row->>'type') - WHEN '0' THEN - temp_service_start_at := TO_CHAR(activated_at, 'YYYY-MM-01 00:00:00'); - temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL + - (package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL; - WHEN '1', '2' THEN - IF (temp_service_end_at > (order_row->>'created_at')::TIMESTAMP) THEN - temp_service_start_at := TO_CHAR(temp_service_end_at + '1 month'::INTERVAL, 'YYYY-MM-01 00:00:00'); - ELSE - temp_service_start_at := - TO_CHAR( - (order_row->>'created_at')::TIMESTAMP + (package_row->>'effect_months' || ' month')::INTERVAL, - 'YYYY-MM-01 00:00:00'); - END IF; - temp_service_end_at := - temp_service_start_at + (order_row->>'counts')::INT * (package_row->>'service_months' || ' month')::INTERVAL + - (package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL; - ELSE - -- 先购买了加油包后再激活的 - IF ((order_row->>'created_at')::TIMESTAMP < activated_at) THEN - IF ((order_row->>'created_at')::TIMESTAMP <= - TO_CHAR(activated_at - '1 month'::INTERVAL, 'YYYY-MM-01 00:00:00')::TIMESTAMP) THEN - -- 购买时间小于一个月的,直接生效 + IF orders IS NOT NULL THEN + SELECT jsonb_object_agg(t.id, row_to_json(t)) INTO packages FROM ( + SELECT virtual_packages.id, + virtual_packages.service_months, + virtual_packages.effect_months, + virtual_packages.delay_months + FROM vd.virtual_packages + ) t; + + FOR i IN 1..array_length(orders, 1) + LOOP + order_row := orders [ i]; + activated_at := activate_cards -> (order_row->>'sim')->>'virtual_activated_at'; + package_row := packages -> (order_row->>'package_id'); + -- 服务时间 + CASE (order_row->>'type') + WHEN '0' THEN temp_service_start_at := TO_CHAR(activated_at, 'YYYY-MM-01 00:00:00'); + temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL + + (package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL; + WHEN '1', '2' THEN + IF (temp_service_end_at > (order_row->>'created_at')::TIMESTAMP) THEN + temp_service_start_at := TO_CHAR(temp_service_end_at + '1 month'::INTERVAL, 'YYYY-MM-01 00:00:00'); ELSE - -- 延时生效 - temp_service_start_at := - TO_CHAR(activated_at + (package_row->>'effect_months' || ' month')::INTERVAL, + temp_service_start_at := + TO_CHAR( + (order_row->>'created_at')::TIMESTAMP + (package_row->>'effect_months' || ' month')::INTERVAL, 'YYYY-MM-01 00:00:00'); END IF; - ELSE - -- 延时生效 - temp_service_start_at := - TO_CHAR( - (order_row->>'created_at')::TIMESTAMP + (package_row->>'effect_months' || ' month')::INTERVAL, - 'YYYY-MM-01 00:00:00'); - END IF; - temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL + + temp_service_end_at := + temp_service_start_at + (order_row->>'counts')::INT * (package_row->>'service_months' || ' month')::INTERVAL + + (package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL; + ELSE + -- 先购买了加油包后再激活的 + IF ((order_row->>'created_at')::TIMESTAMP < activated_at) THEN + IF ((order_row->>'created_at')::TIMESTAMP <= + TO_CHAR(activated_at - '1 month'::INTERVAL, 'YYYY-MM-01 00:00:00')::TIMESTAMP) THEN + -- 购买时间小于一个月的,直接生效 + temp_service_start_at := TO_CHAR(activated_at, 'YYYY-MM-01 00:00:00'); + ELSE + -- 延时生效 + temp_service_start_at := + TO_CHAR(activated_at + (package_row->>'effect_months' || ' month')::INTERVAL, + 'YYYY-MM-01 00:00:00'); + END IF; + ELSE + -- 延时生效 + temp_service_start_at := + TO_CHAR( + (order_row->>'created_at')::TIMESTAMP + (package_row->>'effect_months' || ' month')::INTERVAL, + 'YYYY-MM-01 00:00:00'); + END IF; + temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL + (package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL; END CASE; @@ -122,17 +116,21 @@ BEGIN orders [ i] = order_row; END LOOP; - FOR i IN 1..array_length(orders, 1) - LOOP - order_row := orders[i]; - id := (order_row->>'id')::INT; - sim := (order_row->>'sim')::INT8; - "type" := (order_row->>'type')::INT2; - package_id := (order_row->>'package_id')::INT; - service_start_at := (order_row->>'service_start_at')::TIMESTAMP; - service_end_at := (order_row->>'service_end_at')::TIMESTAMP; - RETURN NEXT; - END LOOP; + FOR i IN 1..array_length(orders, 1) + LOOP + order_row := orders[i]; + id := (order_row->>'id')::INT; + sim := (order_row->>'sim')::INT8; + "type" := (order_row->>'type')::INT2; + package_id := (order_row->>'package_id')::INT; + service_start_at := (order_row->>'service_start_at')::TIMESTAMP; + service_end_at := (order_row->>'service_end_at')::TIMESTAMP; + RETURN NEXT; + END LOOP; + + END IF; + + END IF; END; diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index fc757996..abcb4d33 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -10,3 +10,6 @@ use Illuminate\Database\Schema\Blueprint; use App\Domains\Virtual\Services\OrderService; require_once realpath(dirname(__FILE__) . '/TestCase.php'); + + + diff --git a/tests/MongoTest.php b/tests/MongoTest.php index b03a2089..17d03683 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -5,500 +5,13 @@ use App\Domains\Card\Services\CardService; require_once realpath(dirname(__FILE__) . '/TestCase.php'); $simArray = [ - 1064716544044, - 1064716544045, - 1064716544055, - 1064716544069, - 1064716544071, - 1064716544075, - 1064716544077, - 1064716544093, - 1064716544094, - 1064716544180, - 1064716544197, - 1064716544198, - 1064716544202, - 1064716544203, - 1064716544204, - 1064716544205, - 1064716544206, - 1064716544207, - 1064716544208, - 1064716544210, - 1064716544211, - 1064716544212, - 1064716544213, - 1064716544214, - 1064716544215, - 1064716544216, - 1064716544217, - 1064716544218, - 1064716544219, - 1064716544220, - 1064716544221, - 1064716544242, - 1064716544243, - 1064716544244, - 1440048068104, - 1440048068231, - 1440048068232, - 1440048068290, - 1440048068396, - 1440048068981, - 1440048071709, - 1440048071710, - 1440048071727, - 1440048071728, - 1440048071729, - 1440048074462, - 1440048074464, - 1440048074521, - 1440048074583, - 1440048074584, - 1440048074593, - 1440048075122, - 1440048076342, - 1440048076344, - 1440048076357, - 1440048076358, - 1440048076359, - 1440048076360, - 1440048076361, - 1440048076362, - 1440048076363, - 1440048076364, - 1440048076365, - 1440048076366, - 1440048076367, - 1440048076443, - 1440048076445, - 1440048076446, - 1440048076447, - 1440048076448, - 1440048076449, - 1440048133256, - 1440048133258, - 1440048133259, - 1440048133262, - 1440048133264, - 1440048133266, - 1440048133267, - 1440048133310, - 1440048133322, - 1440048133323, - 1440048133549, - 1440048133550, - 1440048170313, - 1440048170348, - 1440048170475, - 1440048170476, - 1440048170478, - 1440048170479, - 1440048170494, - 1440048170495, - 1440048179332, - 1440048179385, - 1440048179390, - 1440048179393, - 1440048179394, - 1440048184484, - 1440048254602, - 1440048254604, - 1440048254606, - 1440048254607, - 1440048254608, - 1440048254609, - 1440048254621, - 1440048254625, - 1440048254626, - 1440048254628, - 1440048254630, - 1440048254631, - 1440048254632, - 1440048254633, - 1440048254634, - 1440048254635, - 1440048254636, - 1440048254637, - 1440048254638, - 1440048254639, - 1440048254640, - 1440048254641, - 1440048254642, - 1440048254643, - 1440048254644, - 1440048254646, - 1440048254647, - 1440048254648, - 1440048254649, - 1440048254650, - 1440048254651, - 1440048254652, - 1440048254653, - 1440048254654, - 1440048254655, - 1440048254656, - 1440048254657, - 1440048254658, - 1440048254659, - 1440048254660, - 1440048254661, - 1440048254662, - 1440048254663, - 1440048254664, - 1440048254665, - 1440048254666, - 1440048254667, - 1440048254668, - 1440048254669, - 1440048254670, - 1440048254671, - 1440048254672, - 1440048254673, - 1440048254674, - 1440048254682, - 1440048254696, - 1440048254698, - 1440048254706, - 1440048254707, - 1440048254709, - 1440048254711, - 1440048254712, - 1440048254717, - 1440048254723, - 1440048254726, - 1440048254760, - 1440048254782, - 1440048254821, - 1440048254826, - 1440048254833, - 1440048254834, - 1440048254850, - 1440048254851, - 1440048254853, - 1440048254854, - 1440048254855, - 1440048254857, - 1440048254858, - 1440048254859, - 1440048254860, - 1440048254861, - 1440048254862, - 1440048254863, - 1440048254864, - 1440048254866, - 1440048254867, - 1440048254868, - 1440048254869, - 1440048254871, - 1440048254873, - 1440048254874, - 1440048254880, - 1440048254881, - 1440048254883, - 1440048254884, - 1440048254885, - 1440048254886, - 1440048254888, - 1440048254889, - 1440048254890, - 1440048254892, - 1440048254893, - 1440048254894, - 1440048254895, - 1440048254896, - 1440048254897, - 1440048254898, - 1440048254899, - 1440048254908, - 1440048254917, - 1440048254925, - 1440048254927, - 1440048254928, - 1440048254929, - 1440048254930, - 1440048254931, - 1440048254932, - 1440048254933, - 1440048254934, - 1440048254935, - 1440048254936, - 1440048254937, - 1440048254938, - 1440048254939, - 1440048254940, - 1440048254941, - 1440048254942, - 1440048254943, - 1440048254944, - 1440048254946, - 1440048254947, - 1440048254948, - 1440048254949, - 1440048254950, - 1440048254951, - 1440048254952, - 1440048254953, - 1440048254954, - 1440048254955, - 1440048254956, - 1440048254957, - 1440048254958, - 1440048254959, - 1440048254960, - 1440048254961, - 1440048254962, - 1440048254964, - 1440048254965, - 1440048254966, - 1440048254967, - 1440048254968, - 1440048254969, - 1440048254970, - 1440048254971, - 1440048254972, - 1440048254973, - 1440048254974, - 1440048254991, - 1440048254993, - 1440048254995, - 1440048254996, - 1440048480852, - 1440048480859, - 1440048481144, - 1440048482023, - 1440048482027, - 1440048482028, - 1440048482029, - 1440048484154, - 1440048484155, - 1440048484159, - 1440048484160, - 1440048484161, - 1440048484162, - 1440048484163, - 1440048484165, - 1440048484170, - 1440048484217, - 1440048484231, - 1440048484234, - 1440048484331, - 1440048484497, - 1440048484501, - 1440048484640, - 1440048484916, - 1440048484952, - 1440048484966, - 1440048484970, - 1440048495570, - 1440048495571, - 1440048495700, - 1440048495703, - 1440048495704, - 1440048495713, - 1440048495720, - 1440048495721, - 1440048495733, - 1440048495736, - 1440048495740, - 1440048495751, - 1440048495752, - 1440048495754, - 1440048495768, - 1440048495923, - 1440048495924, - 1440048495925, - 1440048495926, - 1440048495930, - 1440409747001, - 1440409747007, - 1440409747010, - 1440409747017, - 1440409747041, - 1440409747042, - 1440409747043, - 1440409747044, - 1440409747052, - 1440409747053, - 1440409747062, - 1440409747063, - 1440409747064, - 1440409747065, - 1440409747079, - 1440409747185, - 1440409747190, - 1440409747208, - 1440409747209, - 1440409747210, - 1440409747211, - 1440409747243, - 1440409747244, - 1440409747245, - 1440409747247, - 1440409747248, - 1440409747249, - 1440415646391, - 1440415646400, - 1440415646401, - 1440415646402, - 1440415646403, - 1440415646405, - 1440415646406, - 1440415646414, - 1440415646418, - 1440415646421, - 1440415647525, - 1440415647584, - 1440415647589, - 1440415647592, - 1440415647593, - 1440415647596, - 1440415647640, - 1440415647641, - 1440415647648, - 1440415647727, - 1440415647728, - 1440415647740, - 1440415647741, - 1440415647742, - 1440415647743, - 1440415647745, - 1440415647775, - 1440415647780, - 1440415647781, - 1440415647782, - 1440415647792, - 1440415647794, - 1440415647795, - 1440415647796, - 1440415647799, - 1440415647826, - 1440415647828, - 1440415647832, - 1440415647833, - 1440415647834, - 1440415647835, - 1440415647840, - 1440415647841, - 1440415647842, - 1440415647843, - 1440415647845, - 1440415647848, - 1440415647875, - 1440415647876, - 1440415647877, - 1440415647878, - 1440415647879, - 1440415647883, - 1440415647884, - 1440415647885, - 1440415647900, - 1440415647902, - 1440415647903, - 1440415647905, - 1440415647908, - 1440415647909, - 1440415647917, - 1440415647920, - 1440415647924, - 1440415647975, - 1440415647997, - 1440415647998, - 1440415647999, - 1440415697860, - 1440415697861, - 1440415697876, - 1440415697893, - 1440415697899, - 1440415697904, - 1440415697931, - 1440415873625, - 1440415873627, - 1440415873654, - 1440416453598, - 1440416453614, - 1440416453617, - 1440416453642, - 1440416453672, - 1440416453704, - 1440416453725, - 1440416453731, - 1440416453808, - 1440416453828, - 1440416453829, - 1440416453830, - 1440416453834, - 1440416453835, - 1440416453839, - 1440416453840, - 1440416453847, - 1440416453848, - 1440416453849, - 1440416453851, - 1440416453852, - 1440416453858, - 1440416453859, - 1440416453861, - 1440416453862, - 1440416453863, - 1440416453864, - 1440416453865, - 1440416453867, - 1440416453868, - 1440416453869, - 1440416453870, - 1440416453871, - 1440416453890, - 1440416453904, - 1440416453905, - 1440416455788, - 1440416456530, - 1440416456531, - 1440416456532, - 1440416456533, - 1440416456534, - 1440416456535, - 1440416456615, - 1440416456616, - 1440416456617, - 1440416456618, - 1440416456619, - 1440416456643, - 1440416456646, - 1440416456647, - 1440416456649, - 1440416456650, - 1440416456655, - 1440416456656, - 1440416456657, - 1440416456658, - 1440416456660, - 1440416456720, - 1440416456721, - 1440416456722, - 1440416457545, - 1440416457546, - 1440416460095, - 1440416460097, - 1440416460107, - 1440416460743, - 1440416460840, - 1440416460853, - 1440416460929, - 1440416460930, - 1440416460933, - 1440416460938, - 1440416460950, - 1440416460951, - 1440416460952, - 1440416460956, - 1440416460963, - 1440416460971, - 1440416460981, - 1440416460986, - 1440416460998, - 1440416460999, - 1440416490702, + 1440048320107, +1440048320150, +1440048320151, +1440048320272, +1440048320274, +1440048320277, + ]; $res = CardService::getMongoCardsInfo($simArray);