You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
excel_handle/extend/service/CarInfoHandle.php

718 lines
38 KiB

<?php
namespace service;
use app\model\CarInfoT;
use app\model\ExportLogT;
use app\model\JobsT;
use app\model\PeerPhoneT;
use app\model\RepeatFrameT;
use excel\Excel;
use file\FileSystem;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\Db;
use think\facade\Log;
class CarInfoHandle
{
/**
* @param $data
* @param $id
* @return bool
* @throws \Exception
*/
public function import($data, $id)
{
return $this->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['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];
4 years ago
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];
}
4 years ago
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);
}
$query = CarInfoT::where($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.register_date 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')
->field('car_info_t.*')->order('car_info_t.register_date asc')
->where([['peer_phone_t.source', '=', $data['source_id']],['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)$']]);
}
}else {
$query = $query->order('register_date 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, insurer1_date desc, car_no desc, id desc')->select();
if ($sources->isEmpty())
continue;
$sources = $sources->toArray();
$source_a = null;
foreach ($sources as $index => &$source) {
$source['car_phone'] = trim($source['car_phone']);
if (preg_match('/^1[3-9][0-9]{9}/', $source['car_phone'])) {
$source_a = $source;
array_splice($sources, $index, 1);
break;
}
}
if (empty($source_a)) {
$source_a = $sources[0];
array_shift($sources);
}
Db::startTrans();
try {
foreach ($sources as $index => $source) {
if (empty(trim($source['car_phone']))) {
foreach ($sources as $source_data) {
$this->repeatDataHandle($source_data, $id, 'frame');
$delete_num ++;
}
RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete();
$sources = [];
break;
} else if ($source_a['car_phone'] == $source['car_phone']) {
if ($source_a['insurer1_date'] != $source['insurer1_date'] && (trim($source_a['insurer1_date']) == '' || $source_a['insurer1_date'] < $source['insurer1_date'])) {
list($source_a, $source) = [$source, $source_a];
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
unset($sources[$index]);
} else if ($source_a['insurer2_date'] != $source['insurer2_date'] && (trim($source_a['insurer2_date']) == '' || $source_a['insurer2_date'] < $source['insurer2_date'])) {
list($source_a, $source) = [$source, $source_a];
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
unset($sources[$index]);
} else if ($source_a['car_no'] != $source['car_no'] && (trim($source_a['car_no']) == '' || !in_array(strlen($source_a['car_no']), [7,8]))) {
list($source_a, $source) = [$source, $source_a];
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
unset($sources[$index]);
}
if (isset($sources[$index])) {
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
unset($sources[$index]);
}
} else if (!preg_match('/^1[3-9][0-9]{9}/', $source['car_phone'])) {
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
unset($sources[$index]);
} else {
$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))
$source_a[$key] = array_shift($phones);
if (empty($source_a[$key]))
break;
}
CarInfoT::where('id', $source_a['id'])->update($source_a);
$this->repeatDataHandle($source, $id, 'frame');
$delete_num ++;
CarInfoT::destroy($source['id']);
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();
}
$current++;
}
}, 'car_frame_no');
return '处理总数:' . $count . ', 删除总数:' . $delete_num . ', 失败总数:' . $faild_num;
}
4 years ago
public function carInfoUpdate($data, $id)
{
$filename = public_path('public/storage') . $data['filename'];
if (empty($filename) OR !file_exists($filename)) {
return '文件不存在!';
}
$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()?:'');
4 years ago
if ($car_frame_no == '') {
continue;
}
$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;
4 years ago
$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;
}
4 years ago
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')]);
4 years ago
} else if ($car_info && $car_info->is_update_bhx <= 0) {
CarInfoT::where('is_update_bhx', 0)
->where('is_export_bhx', $car_info->is_export_bhx)
->update(['is_update_bhx' => -1]);
}
return '操作成功' . "数据总计{$statistical_information['total_all']}条,
4 years ago
更新{$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');
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 = date('Y-m-d', \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($register_date));
else
$register_date = date('Y-m-d', strtotime(trim(trim($register_date, '"'))));
$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' => trim(trim($worksheet->getCellByColumnAndRow(6, $row)->getValue(), '"')),
'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(), '"')),
'source' => $id,
'create_timestamp' => $now,
'update_timestamp' => $now
];
if (count($row_array) > 1000) {
Db::name('car_info_t')->insertAll($row_array);
cache('shell_process_' . $id, round(($row - 1) / $highestRow * 100, 3), 300);
echo '当前进度:' . round(round(($row - 1) / $highestRow * 100, 3)) . PHP_EOL;
$row_array = [];
}
}
if (count($row_array) > 0) {
Db::name('car_info_t')->insertAll($row_array);
cache('shell_process_' . $id, round(($row - 1) / $highestRow * 100, 3), 300);
echo '当前进度:' . round(round(($row - 1) / $highestRow * 100, 3)) . PHP_EOL;
$row_array = [];
}
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);
4 years ago
if($export_limit != 0 && $export_type == 'success_bmc'){
CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]);
} else if($export_limit != 0) {
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);
4 years ago
if($export_limit != 0 && $export_type == 'success_bmc'){
CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]);
} else if($export_limit != 0) {
CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]);
}
$limit -= 3000;
$end = $info->pop();
$lastRegisterDate = is_array($end) ? $end['register_date'] : $end->getData('register_date');
$limit > 0 && ($info = $query->where('register_date', '>=', $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);
}
4 years ago
if($export_limit != 0 && $export_type == 'success_bmc'){
CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]);
} else if($export_limit != 0) {
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);
}
4 years ago
if($export_limit != 0 && $export_type == 'success_bmc'){
CarInfoT::where('id', 'in', $ids)->update(['is_export_bmc' => 0]);
} else if($export_limit != 0) {
CarInfoT::where('id', 'in', $ids)->update(['is_export_' . $export_type => $id]);
}
$limit -= 3000;
$end = $info->pop();
$lastRegisterDate = is_array($end) ? $end['register_date'] : $end->getData('register_date');
$limit > 0 && ($info = $query->where('register_date', '>=', $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', 'compay', 'insurer1_date', 'insurer2_date', 'id_man', 'id_number'];
4 years ago
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 {
4 years ago
$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']) ? $bmcTitles : ($export_type == 'repeat' ? $orgTitles : $bhxTitles);
$fields = in_array($export_type, ['bmc','failed_bmc','none_bmc']) ? $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();
}
}