vd/tests/OldTimeTest.php
2019-04-25 15:45:11 +08:00

76 lines
2.6 KiB
PHP

<?php
require_once realpath(dirname(__FILE__) . '/TestCase.php');
use Illuminate\Http\UploadedFile;
use App\Domains\Export\Services\ImportService;
use Illuminate\Support\Facades\DB;
echo 'started.' . PHP_EOL;
$file = new UploadedFile(__DIR__ . '/服务时间有重叠的卡.xls', '服务时间有重叠的卡.xls');
$data = ImportService::load($file);
echo 'loaded.' . PHP_EOL;
$packages = DB::table('virtual_packages')->get()->collect()->keyBy('sn')->toArray();
foreach ($data as $values) {
$values = array_values($values);
echo $values[0] . PHP_EOL;
$rows = DB::connection('vd_old')->table('ckb_custom_handle_log')->whereNotIn('type', [10, 15])
->where('valid_start_time', '>=', 1546272000)
->where('custom_no', $values[0])->orderBy('create_time')->get()->collect()->toArray();
foreach ($rows as $key => $item) {
if ($key === 0) {
$endTime = $item['valid_end_time'];
} else {
echo '.' . PHP_EOL;
$startTime = strtotime("+{$package['effect_months']} month", $endTime + 1);
$startTime = $item['create_time'] > $startTime ? strtotime(date('Y-m-01 00:00:00', $item['create_time'])) : $startTime;
$package = $packages[$item['content']];
$month = $package['service_months'] + $package['delay_months'];
$endTime = strtotime("+{$month} month", $startTime);
$item['valid_start_time'] = $startTime;
$item['valid_end_time'] = $endTime;
DB::connection('vd_old')->table('ckb_custom_handle_log')->where('id', $item['id'])->update([
'valid_start_time' => $item['valid_start_time'],
'valid_end_time' => $item['valid_end_time'],
]);
}
}
}
$subQuery = DB::raw('(
SELECT
custom_no AS cno,
MIN(valid_start_time) AS valid_start_time,
MAX(valid_end_time) AS valid_end_time
FROM ckb_custom_handle_log
GROUP BY custom_no
) t');
$custom_nos = array_pluck($data, '客户编号')
$res = DB::connection('vd_old')->table('ckb_custom')->join($subQuery, function($join) {
$join->on('t.cno', '=', 'ckb_custom.custom_no');
})->whereIn('custom_no', $custom_nos)->update([
'card_cycle_start' => DB::raw('t.valid_start_time'),
'valid_end_time' => DB::raw('t.valid_end_time'),
]);
$res = DB::connection('vd_old')->table('ckb_user_package')->join($subQuery, function($join) {
$join->on('t.cno', '=', 'ckb_user_package.custom_no');
})->whereIn('custom_no', $custom_nos)->update([
'service_start_time' => DB::raw('t.valid_start_time'),
'service_end_time' => DB::raw('t.valid_end_time'),
]);