get()->collect()->keyBy('sn')->toArray(); foreach ($data as $values) { $values = array_values($values); $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 { $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'), ]);