177 lines
5.4 KiB
PHP
177 lines
5.4 KiB
PHP
<?php
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
require_once realpath(dirname(__FILE__) . '/TestCase.php');
|
|
|
|
set_time_limit(-1);
|
|
ini_set('memory_limit', '4096m');
|
|
|
|
$file = __DIR__ . '/db.csv';
|
|
|
|
$array = [];
|
|
|
|
if (($handle = fopen($file, 'r')) !== false) {
|
|
while (($data = fgetcsv($handle)) !== false) {
|
|
$sim = trim(str_replace('\t', '', $data[0]));
|
|
|
|
if ($sim === 'sim' || empty($sim)) {
|
|
continue;
|
|
}
|
|
|
|
$content = trim($data[1]);
|
|
|
|
$array[] = [
|
|
'sim' => $sim,
|
|
'content' => $content,
|
|
];
|
|
}
|
|
fclose($handle);
|
|
}
|
|
|
|
$packages = DB::connection('vd_old')->table('ckb_package')->select(['package_sn', 'cycle_value'])->get()->pluck('cycle_value', 'package_sn');
|
|
|
|
$addValuePackages = DB::connection('vd_old')->table('ckb_add_value_package')->select(['package_sn', 'service_cycle'])->get()->pluck('service_cycle', 'package_sn');
|
|
|
|
echo 'total - ' . count($array) . PHP_EOL;
|
|
|
|
foreach (array_chunk($array, 5000) as $k => $data) {
|
|
$updates = [];
|
|
|
|
echo 'k - ' . $k . PHP_EOL;
|
|
|
|
$t = array_pluck($data, 'content', 'sim');
|
|
$customs = DB::connection('vd_old')->table('ckb_custom')->select(['custom_no', 'card_number'])
|
|
->whereIn('card_number', array_pluck($data, 'sim'))->get()->keyBy('custom_no')->toArray();
|
|
|
|
foreach ($customs as &$custom) {
|
|
$custom = (array)$custom;
|
|
}
|
|
|
|
$rows = DB::connection('vd_old')->table('ckb_custom_handle_log')->select(['id', 'type', 'content', 'custom_no', 'valid_start_time', 'valid_end_time'])
|
|
->whereIn('custom_no', array_keys($customs))->orderBy('valid_start_time', 'asc')->get()->groupBy('custom_no');
|
|
|
|
$renews = [];
|
|
|
|
foreach ($rows as $custom_no => $items) {
|
|
$sim = $customs[$custom_no]['card_number'];
|
|
|
|
$count = 0;
|
|
foreach ($items as $row) {
|
|
echo $row->id . '-' . $custom_no . PHP_EOL;
|
|
|
|
if ($row->type === 13 || $row->type === 10) {
|
|
$valid_end_time = Carbon::createFromTimestamp($row->valid_start_time)->addMonths($packages[$row->content] - 1)->endOfMonth()->timestamp;
|
|
$updates[] = [
|
|
'id' => $row->id,
|
|
'valid_start_time' => $row->valid_start_time,
|
|
'content' => $row->content,
|
|
'valid_end_time' => $valid_end_time,
|
|
];
|
|
$valid_start_time = $valid_end_time + 1;
|
|
continue;
|
|
}
|
|
|
|
if ($row->type == 14) {
|
|
$count++;
|
|
if ($count > 1) {
|
|
echo $row->id . PHP_EOL;
|
|
DB::connection('vd_old')->table('ckb_custom_handle_log')->where('id', $row->id)->delete();
|
|
continue;
|
|
}
|
|
|
|
$vet = Carbon::createFromTimestamp($valid_start_time)->addMonths($addValuePackages[$t[$sim]] - 1)->endOfMonth()->timestamp;
|
|
|
|
$updates[] = [
|
|
'id' => $row->id,
|
|
'valid_start_time' => $valid_start_time,
|
|
'content' =>$t[$sim],
|
|
'valid_end_time' => $vet,
|
|
];
|
|
|
|
$renews[$sim] = [
|
|
'valid_start_time' => $valid_start_time,
|
|
'content' =>$t[$sim],
|
|
'valid_end_time' => $vet,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
$rows = DB::connection('vd_old')->table('ckb_user_order')->select(['id', 'package_id', 'new_package_id', 'sim', 'valid_start_time', 'valid_end_time'])
|
|
->whereIn('sim', array_pluck($customs, 'card_number'))->orderBy('valid_start_time', 'asc')->get()->groupBy('sim');
|
|
|
|
$userUpdates = [];
|
|
|
|
foreach ($rows as $sim => $items) {
|
|
echo $sim . PHP_EOL;
|
|
$count = 0;
|
|
foreach ($items as $row) {
|
|
$count++;
|
|
|
|
if ($count > 1) {
|
|
DB::connection('vd_old')->table('ckb_user_order')->where('id', $row->id)->delete();
|
|
continue;
|
|
}
|
|
|
|
if (!$renews[$sim]) {
|
|
throw new \Exception('未找到卡 #:'. $item);
|
|
}
|
|
|
|
$userUpdates[] = [
|
|
'id' => $row->id,
|
|
'valid_start_time' => $renews[$sim]['valid_start_time'],
|
|
'package_id' => $renews[$sim]['content'],
|
|
'new_package_id' => $renews[$sim]['content'],
|
|
'valid_end_time' =>$renews[$sim]['valid_end_time'],
|
|
];
|
|
}
|
|
}
|
|
|
|
DB::connection('vd_old')->table('ckb_custom_handle_log')->upsert($updates, 'id');
|
|
DB::connection('vd_old')->table('ckb_user_order')->upsert($userUpdates, 'id');
|
|
}
|
|
|
|
dd(1);
|
|
|
|
$file = __DIR__ . '/test.csv';
|
|
|
|
$out = __DIR__ . '/test_out.csv';
|
|
|
|
$fp = fopen($out, 'w');
|
|
|
|
fputcsv($fp, ['sim', 'iccid', 'imsi']);
|
|
|
|
$array = [];
|
|
|
|
if (($handle = fopen($file, 'r')) !== false) {
|
|
while (($data = fgetcsv($handle)) !== false) {
|
|
$sim = trim($data[0]);
|
|
|
|
if ($sim === 'sim' || empty($sim)) {
|
|
continue;
|
|
}
|
|
|
|
$array[] = $sim;
|
|
}
|
|
fclose($handle);
|
|
}
|
|
|
|
foreach (array_chunk($array, 10000) as $values) {
|
|
$rows = DB::connection('vd_old')->table('ckb_custom')->select(['card_number as sim', 'iccid', 'imsi'])
|
|
->whereIn('card_number', $values)->get()->keyBy('sim');
|
|
|
|
foreach ($values as $value) {
|
|
$row = $rows[$value];
|
|
|
|
if (!$row) {
|
|
throw new \Exception('未找到卡 #:' . $value);
|
|
}
|
|
|
|
fputcsv($fp, [$row->sim."\t", $row->iccid."\t", $row->imsi."\t"]);
|
|
}
|
|
}
|
|
|
|
fclose($fp);
|