importExcel($data, $id); } /** * @param $data * @param $id * @return string */ public function export($data, $id) { $where = []; $map_or1 = []; $map_or2 = []; if (isset($data['export_date1']) && $data['export_date1'] != '') { $where[] = ['car_info_t.register_date', '>=', $data['export_date1']]; } if (isset($data['export_date2']) && $data['export_date2'] != '') { $where[] = ['car_info_t.register_date', '<=', $data['export_date2']]; } if (isset($data['price1']) && $data['price1'] != '') { $where[] = ['car_info_t.purchase_price', '>=', $data['price1'] * 10000]; } if (isset($data['price2']) && $data['price2'] != '') { $where[] = ['car_info_t.purchase_price', '<=', $data['price2'] * 10000]; } if (isset($data['tag']) && $data['tag'] != '') { $where[] = ['car_info_t.tag', '=', $data['tag']]; } if (isset($data['empty_phone_check']) && $data['empty_phone_check'] == 'yes') { $where[] = ['car_phone', '<>', '']; } if (isset($data['export_field'])) { foreach ($data['export_field'] as $item) { $where[] = [$item, '<>', '']; } } if (isset($data['export_type']) && $data['export_type'] != '' && $data['export_limit'] != 0) { if($data['export_type'] == 'bhx'){ $where[] = ['is_export_bhx', '=', 0]; $where[] = ['is_update_bhx', '=', 0]; $where[] = ['is_export_bmc', '=', 0]; $where[] = ['is_export_failed', '=', 0]; $where[] = ['is_export_failed_bmc', '=', 0]; } if($data['export_type'] == 'bmc'){ $where[] = ['is_export_bhx', '>', 0]; $where[] = ['is_update_bhx', '>', 0]; $where[] = ['is_export_bmc', '=', 0]; if($data['update_no'] > 0){ $where[] = ['is_update_bhx', '=', $data['update_no']]; } } if ($data['export_type'] == 'failed') { $where[] = ['is_export_bhx', '>', 0]; $where[] = ['is_update_bhx', '<', 0]; $where[] = ['is_export_bmc', '=', 0]; $where[] = ['is_export_failed', '=', 0]; } if ($data['export_type'] == 'failed_bmc') { $where[] = ['is_export_bhx', '>', 0]; $where[] = ['is_update_bhx', '<', 0]; $where[] = ['is_export_bmc', '=', 0]; $where[] = ['is_export_failed_bmc', '=', 0]; } if ($data['export_type'] == 'none_bmc') { $where[] = ['is_export_bhx', '=', 0]; $where[] = ['is_update_bhx', '=', 0]; $where[] = ['is_export_bmc', '=', 0]; $where[] = ['is_export_failed', '=', 0]; $where[] = ['is_export_failed_bmc', '=', 0]; $where[] = ['is_export_none_bmc', '=', 0]; } if ($data['export_type'] == 'success_bmc') { $where[] = ['is_export_bhx', '>', 0]; $where[] = ['is_update_bhx', '>', 0]; $where[] = ['is_export_bmc', '>', 0]; $insurer_date1 = $data['insurer_date1']??''; $insurer_date2 = $data['insurer_date2']??''; if($insurer_date1!=''){ $map_or1[] = ['car_info_t.insurer1_date', '>=', $insurer_date1]; $map_or2[] = ['car_info_t.insurer2_date', '>=', $insurer_date1]; } if($insurer_date2!=''){ $map_or1[] = ['car_info_t.insurer1_date', '<=', $insurer_date2]; $map_or2[] = ['car_info_t.insurer2_date', '<=', $insurer_date2]; } } } $insurer_month1 = $data['insurer_month1']??''; $insurer_day1 = $data['insurer_day1']??''; $insurer_month2 = $data['insurer_month2']??''; $insurer_day2 = $data['insurer_day2']??''; if($insurer_month1 != '' && $insurer_day1 == '') { $map_or1[] = [Db::raw('month(car_info_t.insurer1_date)'), '>=', $insurer_month1]; $map_or2[] = [Db::raw('month(car_info_t.insurer2_date)'), '>=', $insurer_month1]; } else if ($insurer_month1 == '' && $insurer_day1 != '') { $map_or1[] = [Db::raw('day(car_info_t.insurer1_date)'), '>=', $insurer_day1]; $map_or2[] = [Db::raw('day(car_info_t.insurer2_date)'), '>=', $insurer_day1]; } else if ($insurer_month1 != '' && $insurer_day1 != '') { $map_or1[] = [Db::raw('DATE_FORMAT(car_info_t.insurer1_date,"%m-%d")'), '>=', substr('0' . $insurer_month1, -2) . '-' . substr('0' . $insurer_day1, -2)]; $map_or2[] = [Db::raw('DATE_FORMAT(car_info_t.insurer2_date,"%m-%d")'), '>=', substr('0' . $insurer_month1, -2) . '-' . substr('0' . $insurer_day1, -2)]; } if($insurer_month2 != '' && $insurer_day2 == '') { $map_or1[] = [Db::raw('month(car_info_t.insurer1_date)'), '<=', $insurer_month2]; $map_or2[] = [Db::raw('month(car_info_t.insurer2_date)'), '<=', $insurer_month2]; } else if ($insurer_month2 == '' && $insurer_day2 != '') { $map_or1[] = [Db::raw('day(car_info_t.insurer1_date)'), '<=', $insurer_day2]; $map_or2[] = [Db::raw('day(car_info_t.insurer2_date)'), '<=', $insurer_day2]; } else if ($insurer_month2 != '' && $insurer_day2 != '') { $map_or1[] = [Db::raw('DATE_FORMAT(car_info_t.insurer1_date,"%m-%d")'), '<=', substr('0' . $insurer_month2, -2) . '-' . substr('0' . $insurer_day2, -2)]; $map_or2[] = [Db::raw('DATE_FORMAT(car_info_t.insurer2_date,"%m-%d")'), '<=', substr('0' . $insurer_month2, -2) . '-' . substr('0' . $insurer_day2, -2)]; } if (count($map_or1) > 0) { $map_or1 = array_merge([['car_info_t.insurer1_date', '<>', '']], $map_or1); } if (count($map_or2) > 0) { $map_or2 = array_merge([['car_info_t.insurer1_date', '=', ''], ['car_info_t.insurer2_date', '<>', '']], $map_or2); } if ($data['export_type'] == 'repeat_delete') $query = RepeatDataT::alias('car_info_t')->where(empty($where) ? 1:$where) ->where(function ($query) use ($map_or1, $map_or2) { if (count($map_or1) > 0 && count($map_or2) > 0) { $query->whereOr([$map_or1, $map_or2]); } }); else $query = CarInfoT::where(empty($where) ? 1:$where)->where(function ($query) use ($map_or1, $map_or2) { if (count($map_or1) > 0 && count($map_or2) > 0) { $query->whereOr([$map_or1, $map_or2]); } }); if ($data['export_type'] == 'repeat') { if ($data['export_table'] == 'repeat_frame_t') { $query = RepeatFrameT::leftjoin('car_info_t', 'repeat_frame_t.car_frame_no = car_info_t.car_frame_no') ->field('car_info_t.*')->order('car_info_t.id asc') ->where('repeat_frame_t.source', $data['source_id']); } else if ($data['export_table'] == 'peer_phone_t') { $query = $query->rightJoin('peer_phone_t', 'peer_phone_t.phone = car_info_t.car_phone') ->where('peer_phone_t.source', $data['source_id']) ->where('car_info_t.car_number', 'REGEXP', '^[1-9][[:digit:]]{7}((0[[:digit:]])|(1[0-2]))(([0|1|2][[:digit:]])|3[0-1])[[:digit:]]{3}$|^[1-9][[:digit:]]{5}[1-9][[:digit:]]{3}((0[[:digit:]])|(1[0-2]))(([0|1|2][[:digit:]])|3[0-1])[[:digit:]]{3}([0-9]|X)$') ->field('car_info_t.*')->order('car_info_t.id asc'); } }elseif ($data['export_type'] != 'repeat_delete') { $query = $query->order('car_info_t.id asc'); } $count = $query->count(); $count = $count <= $data['export_limit'] || $data['export_limit'] == 0 ? $count : $data['export_limit']; echo '总计:' . $count . PHP_EOL; cache('shell_process_' . $id, 0, 300); $data['export_field'] = $data['export_field'] ?? []; list($titles, $fields) = $this->getTitleAndField($data['export_type'], $data['export_field']); if (isset($data['is_excel']) && $data['is_excel'] == 'yes') { $basename = '/export/' . date('YmdHis') . '.xlsx'; $this->exportExcel($data['export_type'], $data['export_limit'], $titles, $fields, root_path() . 'public' . $basename, $query, $count, $id); } else { $basename = '/export/' . date('YmdHis') . '.csv'; $this->exportCsv($data['export_type'], $data['export_limit'], $titles, $fields, root_path() . 'public' . $basename, $query, $count, $id); } $this->setProcess($count, $count, $id); $this->addExportLog($count, $data['export_type'],$id); return $basename; } // 电话去重按不同证件号电话重复次数算 public function peerPhones($data, $id) { $where = [ ['car_phone', '<>', ''], ['car_number', 'REGEXP', '^[1-9][[:digit:]]{7}((0[[:digit:]])|(1[0-2]))(([0|1|2][[:digit:]])|3[0-1])[[:digit:]]{3}$|^[1-9][[:digit:]]{5}[1-9][[:digit:]]{3}((0[[:digit:]])|(1[0-2]))(([0|1|2][[:digit:]])|3[0-1])[[:digit:]]{3}([0-9]|X)$'] ]; $query = CarInfoT::field('car_phone,count(*) as count') ->where($where) ->group('car_phone'); $count = $query->count(); echo '总计:' . $count . PHP_EOL; $current = 0; $peer_phone_number = $data['peer_phone_number']; $peer_phone_count = $repeat_count = 0; cache('shell_process_' . $id, 0, 300); $query->chunk(3000, function ($infos) use (&$current, &$peer_phone_count, &$repeat_count, $count, $id, $peer_phone_number) { $insert_data = []; foreach ($infos as $item) { if ($item->count >= $peer_phone_number) { $card_ids = CarInfoT::where('car_phone', $item->car_phone)->column('car_number'); if (count(array_flip(array_flip($card_ids))) <= 1) { continue; } $insert_data[] = [ 'phone' => $item->car_phone, 'source' => $id, ]; $peer_phone_count++; $repeat_count += $item->count; } $current++; } PeerPhoneT::duplicate(Db::raw('phone = values(phone), source = values(source)'))->insertAll($insert_data); cache('shell_process_' . $id, round($current / $count * 100, 3), 300); echo '当前进度:' . round($current / $count * 100, 3) . PHP_EOL; }, 'car_phone'); return '重复总数:' . $repeat_count . ', 重复电话数:' . $peer_phone_count; } public function carFrameNo($data, $id) { $where = [ ['car_frame_no', '<>', ''] ]; if (isset($data['export_date1']) && $data['export_date1'] != '') { $where[] = ['register_date', '>=', $data['export_date1']]; } if (isset($data['export_date2']) && $data['export_date2'] != '') { $where[] = ['register_date', '<=', $data['export_date2']]; } $query = CarInfoT::field('car_frame_no,count(*) as count') ->where($where) ->group('car_frame_no') ->having('count>=2'); $current = $repeat_frame_count = 0; $count = $query->count(); echo '总计:' . $count . PHP_EOL; cache('shell_process_' . $id, 0, 300); $query->chunk(3000, function ($infos) use (&$current, &$repeat_frame_count, $count, $id) { $insert_data = []; foreach ($infos as $item) { $insert_data[] = [ 'car_frame_no' => $item->car_frame_no, 'source' => $id, ]; $current++; $repeat_frame_count += $item->count; } RepeatFrameT::duplicate(Db::raw('source = values(source)'))->insertAll($insert_data); cache('shell_process_' . $id, round($current / $count * 100, 3), 300); echo '当前进度:' . round($current / $count * 100, 3) . PHP_EOL; }, 'car_frame_no'); return '重复总数:' . $repeat_frame_count . ', 重复车架数:' . $count; } public function deletePeerPhones($data, $id) { $query = PeerPhoneT::where(['source' => $data['source_id']]); $count = $query->count(); echo '总计:' . $count . PHP_EOL; $current = 0; $faild_num = 0; cache('shell_process_' . $id, 0, 300); $query->chunk(1000, function ($infos) use (&$current, &$faild_num, $count, $id) { foreach ($infos as $item) { if ($current % 10 == 0) { cache('shell_process_' . $id, round($current / $count * 100, 3), 300); echo '当前进度:' . round($current / $count * 100, 3) . PHP_EOL; } $ids = CarInfoT::where('car_phone', $item->phone)->column('id'); Db::startTrans(); try { CarInfoT::where('id', 'in', $ids) ->update(['car_phone' => '']);; PeerPhoneT::where('phone', $item->phone)->delete(); Db::commit(); } catch (\Exception $e) { $faild_num++; Db::rollback(); } $current++; } }, 'phone'); return '处理总数:' . $count . ', 失败总数:' . $faild_num; } public function deleteCarFrameNo($data, $id) { $query = RepeatFrameT::where(['source' => $data['source_id']]); $count = $query->count(); echo '总计:' . $count . PHP_EOL; $current = 0; $delete_num = 0; $faild_num = 0; cache('shell_process_' . $id, 0, 300); $query->chunk(1000, function ($infos) use (&$current, &$delete_num, &$faild_num, $count, $id) { foreach ($infos as $item) { if ($current % 10 == 0) { cache('shell_process_' . $id, round($current / $count * 100, 3), 300); echo '当前进度:' . round($current / $count * 100, 3) . PHP_EOL; } $sources = CarInfoT::where(['car_frame_no' => $item->car_frame_no]) ->order('car_phone desc, id desc')->select(); if ($sources->isEmpty()) continue; $sources = $sources->toArray(); $source_a = null; foreach ($sources as $index => $source) { $sources[$index]['car_phone'] = trim($source['car_phone']); if (preg_match('/^1[3-9][0-9]{9}/', $source['car_phone'])) { $source_a = $source; unset($sources[$index]); break; } } if (empty($source_a)) { $source_a = $sources[0]; array_shift($sources); } Db::startTrans(); try { $car_phone_a_valid = preg_match('/^1[3-9][0-9]{9}/', $source_a['car_phone']); foreach ($sources as $index => $source) { $car_phone_valid = preg_match('/^1[3-9][0-9]{9}/', $source['car_phone']); // 当不是标准有效对比无效的情况下,要进行数据对比替换 if (!($car_phone_a_valid && !$car_phone_valid)) { if (!$car_phone_a_valid && $car_phone_valid) { // 标准无效,对比有效 list($source_a, $source) = [$source, $source_a]; } else if (trim($source_a['insurer1_date']) != trim($source['insurer1_date']) && (trim($source_a['insurer1_date']) == '' || $source_a['insurer1_date'] < $source['insurer1_date'])) { list($source_a, $source) = [$source, $source_a]; } else if (trim($source_a['insurer2_date']) != trim($source['insurer2_date']) && (trim($source_a['insurer2_date']) == '' || $source_a['insurer2_date'] < $source['insurer2_date'])) { list($source_a, $source) = [$source, $source_a]; } else if ($source_a['is_export_bmc'] == 0 && $source['is_export_bmc'] > 0) { list($source_a, $source) = [$source, $source_a]; } else if ($source_a['is_update_bhx'] == 0 && $source['is_update_bhx'] > 0) { list($source_a, $source) = [$source, $source_a]; } else if ($source_a['is_export_bhx'] == 0 && $source['is_export_bhx'] > 0) { list($source_a, $source) = [$source, $source_a]; } else if ($source_a['car_no'] != $source['car_no'] && (trim($source_a['car_no']) == '' || !in_array(mb_strlen($source_a['car_no']), [7,8]))) { list($source_a, $source) = [$source, $source_a]; } else if ($source_a['register_date'] != $source['register_date'] && (trim($source_a['register_date']) == '' || $source_a['register_date'] < $source['register_date'])) { list($source_a, $source) = [$source, $source_a]; } if ($car_phone_a_valid && $car_phone_valid) { // 都有效更新电话 $phones = [$source['car_phone']]; $phones_a = [$source_a['car_phone']]; foreach (['id_phone', 'insured_phone', 'link_phone_1', 'link_phone_2', 'link_phone_3'] as $key) { if (preg_match('/^1[3-9][0-9]{9}/', $source_a[$key]) && !in_array($source_a[$key], $phones_a)) $phones_a[] = $source_a[$key]; if (preg_match('/^1[3-9][0-9]{9}/', $source[$key]) && !in_array($source[$key], $phones)) $phones[] = $source[$key]; } $phones = array_unique(array_merge($phones_a, $phones)); // 可用电话大于6个时跳过 if (count($phones) <= 6) { array_shift($phones); foreach (['id_phone', 'insured_phone', 'link_phone_1', 'link_phone_2', 'link_phone_3'] as $key) { if (in_array($source_a[$key], $phones)) array_shift($phones); else $source_a[$key] = array_shift($phones); if (empty($source_a[$key]) || empty($phones)) break; } CarInfoT::where('id', $source_a['id'])->update($source_a); } } } $this->repeatDataHandle($source, $id, 'frame'); $delete_num ++; unset($sources[$index]); } if (empty($sources)) RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete(); Db::commit(); } catch (\Exception $e) { $faild_num++; Db::rollback(); Log::record('PG12138' . $e->getMessage(), 'error'); Log::record($e->getTraceAsString(), 'error'); exit(); } $current++; } }, 'car_frame_no'); return '处理总数:' . $count . ', 删除总数:' . $delete_num . ', 失败总数:' . $faild_num; } public function exportRepeatData($data, $id) { $query = RepeatDataT::alias('car_info_t')->where('repeat_id', $data['source_id']); $count = $query->count(); $count = $count <= $data['export_limit'] || $data['export_limit'] == 0 ? $count : $data['export_limit']; echo '总计:' . $count . PHP_EOL; cache('shell_process_' . $id, 0, 300); $data['export_field'] = $data['export_field'] ?? []; list($titles, $fields) = $this->getTitleAndField($data['export_type'], $data['export_field']); if (isset($data['is_excel']) && $data['is_excel'] == 'yes') { $basename = '/export/' . date('YmdHis') . '.xlsx'; $this->exportExcel($data['export_type'], $data['export_limit'], $titles, $fields, root_path() . 'public' . $basename, $query, $count, $id); } else { $basename = '/export/' . date('YmdHis') . '.csv'; $this->exportCsv($data['export_type'], $data['export_limit'], $titles, $fields, root_path() . 'public' . $basename, $query, $count, $id); } $this->setProcess($count, $count, $id); $this->addExportLog($count, $data['export_type'],$id); return $basename; } public function carInfoUpdate($data, $id) { $filename = public_path('public/storage') . $data['filename']; if (empty($filename) OR !file_exists($filename)) { return '文件不存在!'; } $export_id = 0; if($data['queue'] != '') $export_id = JobsT::where('type', 2) ->where('payload','like','%'. str_replace(['\\','"'],['_',''],json_encode($data['queue'])) .'%') ->value('id'); $spreadRead = Excel::loadFile($filename); $worksheet = $spreadRead->getSheet(0); $highestRow = $worksheet->getHighestRow(); // e.g. 10 $total = $highestRow - 1; echo '总计:' . $total . PHP_EOL; $statistical_information = [ 'total_all' => $total, 'total_skip' => 0, 'total_insert' => 0, 'total_update' => 0, 'total_error' => 0, 'total_error_arr' => [], 'total_skip_arr' => [], ]; $is_update_failed = 0; for ($row = 2; $row <= $highestRow; $row++) { $car_no = trim($worksheet->getCellByColumnAndRow(1, $row)->getValue()?:''); $car_number = trim($worksheet->getCellByColumnAndRow(4, $row)->getValue()?:''); $car_man = trim($worksheet->getCellByColumnAndRow(5, $row)->getValue()?:''); $factory_model = trim($worksheet->getCellByColumnAndRow(10, $row)->getValue()?:''); $engine_no = trim($worksheet->getCellByColumnAndRow(3, $row)->getValue()?:''); $car_frame_no = trim($worksheet->getCellByColumnAndRow(2, $row)->getValue()?:''); $register_date = substr(trim($worksheet->getCellByColumnAndRow(11, $row)->getValue()?:''), 0, 10); $company = trim($worksheet->getCellByColumnAndRow(12, $row)->getValue()?:''); $insurer1_date = substr(trim($worksheet->getCellByColumnAndRow(14, $row)->getValue()?:''), 0, 10); $insurer2_date = substr(trim($worksheet->getCellByColumnAndRow(13, $row)->getValue()?:''), 0, 10); $id_man = trim($worksheet->getCellByColumnAndRow(7, $row)->getValue()?:''); $id_number = trim($worksheet->getCellByColumnAndRow(6, $row)->getValue()?:''); $insured_man = trim($worksheet->getCellByColumnAndRow(9, $row)->getValue()?:''); $insured_number = trim($worksheet->getCellByColumnAndRow(8, $row)->getValue()?:''); if ($car_frame_no == '') { continue; } $car_info = null; if ($export_id) $car_info = CarInfoT::where('car_frame_no', $car_frame_no)->where('is_export_bhx', $export_id)->find(); if (!$car_info) $car_info = CarInfoT::where('car_frame_no', $car_frame_no)->where('is_export_bhx', '>', 0)->find(); if (!$car_info) $car_info = CarInfoT::where('car_frame_no', $car_frame_no)->find(); if (isset($car_info)) { if ($is_update_failed == 0 && $car_info->is_export_failed > 0) { $is_update_failed = $car_info->is_export_failed; } $car_info->car_no = $car_no != '' ? $car_no : $car_info->car_no; $car_info->car_man = $car_info->car_man != '' ? $car_info->car_man : $car_man; $car_info->car_number = $car_info->car_number != '' ? $car_info->car_number : $car_number; $car_info->register_date = $register_date; $car_info->factory_model = $factory_model; //对比原信息是否包含待更新信息(删除星号) $car_info->engine_no = $car_info->engine_no != '' && startsWith($car_info->engine_no, rtrim($engine_no, '*')) ? $car_info->engine_no : $engine_no; $insurer1_flag = strtotime(date('Y', strtotime($insurer1_date))) > strtotime(date('Y', strtotime($car_info->insurer1_date))); if ($insurer1_flag) { $car_info->insurer1_date = $insurer1_date; } $insurer2_flag = strtotime(date('Y', strtotime($insurer2_date))) > strtotime(date('Y', strtotime($car_info->insurer2_date))); if ($insurer2_flag) { $car_info->insurer2_date = $insurer2_date; } if (($car_info->insurer1_date != '' && $insurer1_flag) || ($car_info->insurer2_date != '' && $insurer2_flag) || $car_info->insurer1_date == '' || $car_info->insurer2_date == '') { $car_info->company = $company != '' ? $company : $car_info->company; $car_info->id_man = $car_info->id_man != '' && startsWith($car_info->id_man, rtrim($id_man, '*')) ? $car_info->id_man : $id_man; $car_info->id_number = $car_info->id_number != '' && startsWith($car_info->id_number, rtrim($id_number, '*')) ? $car_info->id_number : $id_number; $car_info->insured_man = $car_info->insured_man != '' && startsWith($car_info->insured_man, rtrim($insured_man, '*')) ? $car_info->insured_man : $insured_man; $car_info->insured_number = $car_info->insured_number != '' && startsWith($car_info->insured_number, rtrim($insured_number, '*')) ? $car_info->insured_number : $insured_number; } $car_info->is_update_bhx = $id; $car_info->is_export_failed = 0; if (!$car_info->save()) { $statistical_information['total_error']++; $statistical_information['total_error_arr'][] = $row; } else { $statistical_information['total_update']++; } } else { $statistical_information['total_skip_arr'][] = $row; $statistical_information['total_skip']++; } cache('shell_process_' . $id, round($row / $total * 100, 3), 300); echo '当前进度:' . round($row / $total * 100, 3) . PHP_EOL; } if ($is_update_failed > 0) { CarInfoT::where('is_export_failed', $is_update_failed) ->update(['is_export_failed' => 0, 'is_update_bhx' => Db::raw('is_update_bhx - 1')]); } else { $export_id = $export_id ?: $car_info->is_export_bhx; CarInfoT::where('is_update_bhx', 0) ->where('is_export_bhx', $export_id) ->update(['is_update_bhx' => -1]); } return '操作成功' . "数据总计{$statistical_information['total_all']}条, 更新{$statistical_information['total_update']}, 失败{$statistical_information['total_error']}, 失败数据列:" . implode(',', $statistical_information['total_error_arr']) . ", 跳过{$statistical_information['total_skip']}, 跳过数据列:" . implode(',', $statistical_information['total_skip_arr']); } private function repeatDataHandle($data, $id, $type) { if ($type == 'frame') { CarInfoT::destroy($data['id']); } $data['repeat_id'] = $id; Db::table('repeat_data_t')->insert($data); } private function importExcel($data, $id) { $filename = public_path('public/storage') . $data['filename']; $t1 = microtime(true); $spreadRead = Excel::loadFile($filename); $worksheet = $spreadRead->getSheet(0); $highestRow = $worksheet->getHighestRow(); // e.g. 10 echo '总计:' . $highestRow . PHP_EOL; $now = date('Y-m-d H:i:s'); $update_keys = ['register_date', 'car_no', 'car_frame_no', 'engine_no', 'factory_model', 'car_man', 'car_number', 'purchase_price', 'id_man', 'id_number', 'insured_man', 'insured_number', 'company', 'insurer1_date', 'insurer2_date']; $link_phone_keys = [ 'car_phone' => 'car_man', 'id_phone' => 'id_man', 'insured_phone' => 'insured_man', 'link_phone_1' => 'link_man_1', 'link_phone_2' => 'link_man_2', 'link_phone_3' => 'link_man_3' ]; Db::execute('ALTER TABLE car_info_t DISABLE KEYS;'); for ($row = 2; $row <= $highestRow; $row++) { $register_date = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); if (is_numeric($register_date) && $register_date < 99999) $register_date = date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($register_date)); else $register_date = date('Y-m-d', strtotime(trim(trim($register_date, '"')))); $frame_no = trim(trim($worksheet->getCellByColumnAndRow(6, $row)->getValue(), '"')); $row_array = [ 'duty_no' => trim(trim($worksheet->getCellByColumnAndRow(1, $row)->getValue(), '"')), 'register_date' => $register_date, 'car_man' => trim(trim($worksheet->getCellByColumnAndRow(3, $row)->getValue(), '"')), 'car_number' => trim(trim($worksheet->getCellByColumnAndRow(4, $row)->getValue(), '"')), 'car_phone' => trim(trim($worksheet->getCellByColumnAndRow(5, $row)->getValue(), '"')), 'car_frame_no' => $frame_no, 'car_no' => str_replace('-', '', trim(trim($worksheet->getCellByColumnAndRow(7, $row)->getValue(), '"'))), 'engine_no' => trim(trim($worksheet->getCellByColumnAndRow(8, $row)->getValue(), '"')), 'factory_model' => trim(trim($worksheet->getCellByColumnAndRow(9, $row)->getValue(), '"')), 'purchase_price' => trim(trim($worksheet->getCellByColumnAndRow(10, $row)->getValue(), '"')), 'insurer2_date' => trim(trim($worksheet->getCellByColumnAndRow(11, $row)->getValue(), '"')), 'insurer1_date' => trim(trim($worksheet->getCellByColumnAndRow(12, $row)->getValue(), '"')), 'id_man' => trim(trim($worksheet->getCellByColumnAndRow(13, $row)->getValue(), '"')), 'id_number' => trim(trim($worksheet->getCellByColumnAndRow(14, $row)->getValue(), '"')), 'id_phone' => trim(trim($worksheet->getCellByColumnAndRow(15, $row)->getValue(), '"')), 'link_man_1' => trim(trim($worksheet->getCellByColumnAndRow(16, $row)->getValue(), '"')), 'link_phone_1' => trim(trim($worksheet->getCellByColumnAndRow(17, $row)->getValue(), '"')), 'link_man_2' => trim(trim($worksheet->getCellByColumnAndRow(18, $row)->getValue(), '"')), 'link_phone_2' => trim(trim($worksheet->getCellByColumnAndRow(19, $row)->getValue(), '"')), 'tag' => trim(trim($worksheet->getCellByColumnAndRow(20, $row)->getValue(), '"')), 'source' => $id, 'create_timestamp' => $now, 'update_timestamp' => $now ]; // 有效车架号且存在更新并清除状态 if (strlen($frame_no) == 17 && ($old = Db::name('car_info_t')->where('car_frame_no', $frame_no)->find())) { foreach ($update_keys as $key) { if (empty($row_array[$key])) $row_array[$key] = $old[$key]; } $phones = []; $phones_empty_keys = []; // 新数据去重,并获得空电话位 foreach ($link_phone_keys as $phone_key => $man_key) { $row_array[$phone_key] = $row_array[$phone_key] ?? ''; if (in_array($row_array[$phone_key], $phones)) $row_array[$phone_key] = ''; if (!empty($row_array[$phone_key])) $phones[] = $row_array[$phone_key]; else $phones_empty_keys[] = $phone_key; } // 旧数据查重并依次补位 foreach ($link_phone_keys as $phone_key => $man_key) { if (empty($phones_empty_keys)) break; if (in_array($old[$phone_key], $phones)) continue; if (!empty($old[$phone_key])) { $key = array_shift($phones_empty_keys); $row_array[$key] = $old[$phone_key]; $row_array[$link_phone_keys[$key]] = $old[$man_key]; } } $row_array['is_export_bhx'] = 0; $row_array['is_update_bhx'] = 0; $row_array['is_export_bmc'] = 0; $row_array['is_export_failed'] = 0; $row_array['is_export_none_bmc'] = 0; $row_array['is_export_failed_bmc'] = 0; Db::name('car_info_t')->where('id', $old['id'])->save($row_array); } else { Db::name('car_info_t')->insert($row_array); } $process = round($row / $highestRow * 100, 3); $process = $process > 100 ? 100: $process; cache('shell_process_' . $id, $process, 300); echo '当前进度:' . $process . PHP_EOL; } Db::execute('ALTER TABLE car_info_t ENABLE KEYS;'); $t2 = microtime(true); echo '耗时' . round($t2 - $t1, 3) . '秒' . PHP_EOL; echo 'Now memory_get_usage: ' . memory_get_usage() . PHP_EOL; return '导入成功:' . ($highestRow - 1); } private function exportCsv($export_type, $export_limit, $titles, $fields, $filename, $query, $count, $id) { $head = ''; foreach ($titles as $val) { if ($val == 'line') { $head .= "\r\n"; continue; } $head .= "\"{$val}\t\","; } $head = substr($head, 0, -1) . "\r\n"; if (file_exists($filename)) { unlink($filename); } if (is_dir(pathinfo($filename, PATHINFO_DIRNAME))) { file_put_contents($filename, $head); } else { mkdir(pathinfo($filename, PATHINFO_DIRNAME), 0777); file_put_contents($filename, $head); } $current = 0; if ($count <= 3000) { $ids = []; $content = ''; foreach ($query->limit($count)->select() as $item) { $ids[] = $item->id; foreach ($fields as $field) { $content .= "\"" . ($item->$field ?: '') . "\"\t,"; } $content = substr($content, 0, -1) . "\r\n"; $this->setProcess(++$current, $count, $id); } FileSystem::appendWriteFile($filename, $content, false); if($export_limit != 0 && $export_type == 'success_bmc'){ CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]); } else if($export_limit != 0 && $export_type != 'repeat_delete') { CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]); } } else { $info = $query->limit(3000)->select(); $limit = $count; while ($limit > 0) { $ids = []; $content = ''; foreach ($info as $item) { $ids[] = $item->id; foreach ($fields as $field) { $content .= "\"" . ($item->$field ?: '') . "\"\t,"; } $content = substr($content, 0, -1) . "\r\n"; $this->setProcess(++$current, $count, $id); } FileSystem::appendWriteFile($filename, $content, false); if($export_limit != 0 && $export_type == 'success_bmc'){ CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]); } else if($export_limit != 0 && $export_type != 'repeat_delete') { CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]); } $limit -= 3000; $end = $info->pop(); $lastRegisterDate = is_array($end) ? $end['id'] : $end->getData('id'); $query_1 = clone $query; $limit > 0 && ($info = $query_1->where('car_info_t.id', '>=', $lastRegisterDate)->limit($limit >= 3000 ? 3000 : $limit)->select()); } } } private function exportExcel($export_type, $export_limit, $titles, $fields, $filename, $query, $count, $id) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet()->setTitle('sheet1'); $row = 0; $i = 0; foreach ($titles as $col => $val) { if ($val == 'line') { $row += 1; $i += 1; continue; } $sheet->setCellValueByColumnAndRow($col + 1 - $i, $row + 1, $val); } if ($count <= 3000) { $ids = []; foreach ($query->limit($count)->select() as $item) { $ids[] = $item->id; $row += 1; foreach ($fields as $index => $field) { $sheet->setCellValueExplicitByColumnAndRow($index + 1, $row + 1, ($item->$field ?: ''), \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); } $this->setProcess($row, $count * 1.2, $id); } if($export_limit != 0 && $export_type == 'success_bmc') { CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]); } else if($export_limit != 0 && $export_type != 'repeat_delete') { CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]); } } else { $info = $query->limit(3000)->select(); $limit = $count; while ($limit > 0) { $ids = []; foreach ($info as $item) { $ids[] = $item->id; $row += 1; foreach ($fields as $index => $field) { $sheet->setCellValueExplicitByColumnAndRow($index + 1, $row + 1, ($item->$field ?: ''), \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); } $this->setProcess($row, $count * 1.2, $id); } if($export_limit != 0 && $export_type == 'success_bmc'){ CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]); } else if($export_limit != 0 && $export_type != 'repeat_delete') { CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]); } $limit -= 3000; $end = $info->pop(); $lastRegisterDate = is_array($end) ? $end['id'] : $end->getData('id'); $query_1 = clone $query; $limit > 0 && ($info = $query_1->where('car_info_t.id', '>=', $lastRegisterDate)->limit($limit >= 3000 ? 3000 : $limit)->select()); } } $writer = new Xlsx($spreadsheet); $writer->save($filename); } private function setProcess($cur, $count, $id) { $process = round($cur / $count * 100, 3); cache('shell_process_' . $id, $process, 300); echo '当前进度:' . $process . PHP_EOL; } /** * @param $export_type * @param $export_field * @return array */ private function getTitleAndField($export_type, $export_field) { $orgTitles = ['区域', '购车日期', '客户姓名', '证件号码', '联系方式', '车架号', '车牌号', '发动机号', '品牌型号', '新车购置价', '保险公司', '商业止保日期', '交强止保日期', '被保险人', '被保险人证件号']; $bhxTitles = ['line', 'line', 'line', '车牌号', '车架号', '发动机号', '车主证件号码', '车主姓名', '被保人证件号', '被保人姓名', '投保人证件号', '投保人姓名', '品牌型号', '注册日期', '去年投保公司', '交强险到期时间', '商业险到期时间', '客户姓名', '客户电话1', '客户电话2', '客户类别','地址信息', '客户备注1', '客户备注2', '用户姓名', '用户账号']; $bmcTitles = ['注册日期', '车牌号', '车架号', '发动机号', '品牌型号', '年款', '排量', '车主', '车主证件号', '联系电话', '新车购置价', '被保险人', '被保险人证件号', '联系电话', '投保人姓名', '投保人证件号', '联系电话', '上年承保公司', '商业止保日期', '交强止保日期', 'insurer_type', '联系人', '联系电话', '联系人', '联系电话', '联系人', '联系电话']; $orgFields = ['duty_no', 'register_date', 'car_man', 'car_number', 'car_phone', 'car_frame_no', 'car_no', 'engine_no', 'factory_model', 'purchase_price', 'company', 'insurer1_date', 'insurer2_date', 'id_man', 'id_number']; if (in_array($export_type, ['bhx', 'failed','success_bmc']) && count($export_field) == 1) { $bhxFields = $export_field[0] == 'car_no' ? ['car_no'] : ['car_empty_item', 'car_frame_no']; } else { $bhxFields = ['car_no', 'car_frame_no']; } $bmcFields = ['register_date', 'car_no', 'car_frame_no', 'engine_no', 'factory_model', 'car_year', 'displacement', 'car_man', 'car_number', 'car_phone', 'purchase_price', 'id_man', 'id_number', 'id_phone', 'insured_man', 'insured_number', 'insured_phone', 'company', 'insurer1_date', 'insurer2_date', 'insurer_type', 'linkman', 'linkphone', 'linkman', 'linkphone', 'linkman', 'linkphone']; $titles = in_array($export_type, ['bmc','failed_bmc','none_bmc', 'repeat_delete']) ? $bmcTitles : ($export_type == 'repeat' ? $orgTitles : $bhxTitles); $fields = in_array($export_type, ['bmc','failed_bmc','none_bmc', 'repeat_delete']) ? $bmcFields : ($export_type == 'repeat' ? $orgFields : $bhxFields); return [$titles, $fields]; } private function addExportLog($total, $export_type, $id) { $queue_name = JobsT::where('id',$id)->value('queue'); $export_log = new ExportLogT; $export_log->type = $export_type; $export_log->name = $queue_name; $export_log->total = $total; $export_log->create_timestamp = date('Y-m-d H:i:s'); $export_log->save(); } }