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.
671 lines
34 KiB
671 lines
34 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\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use SpreadsheetReader;
|
|
use think\facade\Db;
|
|
use think\facade\Env;
|
|
|
|
class CarInfoHandle
|
|
{
|
|
/**
|
|
* @param $data
|
|
* @param $id
|
|
* @return bool
|
|
* @throws \Exception
|
|
*/
|
|
public function import($data, $id)
|
|
{
|
|
if(pathinfo($data['filename'])['extension'] != 'csv'){
|
|
return '操作失败:上传文件必须为csv文件';
|
|
}
|
|
$filename = public_path('public/storage') . $data['filename'];
|
|
$lines = FileSystem::getTxtFileLines($filename);
|
|
echo '总计:' . $lines . PHP_EOL;
|
|
cache('shell_process_' . $id, 0, 300);
|
|
Db::execute('ALTER TABLE car_info_t DISABLE KEYS;');
|
|
for ($i = 2; $i < $lines; $i += 2000) {
|
|
$insert_data = FileSystem::getFileBlockData($filename, $i, $i + 2000, function ($str) use ($id) {
|
|
$keys = ['duty_no', 'register_date', 'car_man', 'car_number', 'car_phone', 'car_frame_no', 'car_no', 'engine_no', 'factory_model', 'purchase_price', 'source'];
|
|
$keys_len = count($keys);
|
|
$arr = explode(',', $str);
|
|
array_walk($arr, function (&$value, $key) {
|
|
$value = trim(trim($value, '"'));
|
|
});
|
|
$values = array_slice($arr, 0, $keys_len);
|
|
$values[$keys_len - 1] = $id;
|
|
$data = array_combine($keys, $values);
|
|
$data['register_date'] = date('Y-m-d', strtotime($data['register_date']));
|
|
return $data;
|
|
});
|
|
|
|
Db::name('car_info_t')->insertAll($insert_data);
|
|
cache('shell_process_' . $id, round(($i + 2000) / $lines * 100, 3), 300);
|
|
echo '当前进度:' . round(round(($i + 2000) / $lines * 100, 3)) . PHP_EOL;
|
|
}
|
|
Db::execute('ALTER TABLE car_info_t ENABLE KEYS;');
|
|
return '导入成功:' . ($lines - 1);
|
|
}
|
|
|
|
/**
|
|
* @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];
|
|
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);
|
|
}
|
|
$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)$']
|
|
];
|
|
$map = [];
|
|
if (isset($data['export_date1']) && $data['export_date1'] != '') {
|
|
$map[] = ['register_date', '>=', $data['export_date1']];
|
|
}
|
|
if (isset($data['export_date2']) && $data['export_date2'] != '') {
|
|
$map[] = ['register_date', '<=', $data['export_date2']];
|
|
}
|
|
$query = CarInfoT::field('car_phone,count(*) as count')
|
|
->where($where)
|
|
->where($map)
|
|
->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, $map) {
|
|
$insert_data = [];
|
|
foreach ($infos as $item) {
|
|
if ($item->count >= $peer_phone_number) {
|
|
$card_ids = CarInfoT::where('car_phone', $item->car_phone)->where($map)->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;
|
|
$map = [];
|
|
if (isset($data['export_date1']) && $data['export_date1'] != '') {
|
|
$map[] = ['register_date', '>=', $data['export_date1']];
|
|
}
|
|
if (isset($data['export_date2']) && $data['export_date2'] != '') {
|
|
$map[] = ['register_date', '<=', $data['export_date2']];
|
|
}
|
|
cache('shell_process_' . $id, 0, 300);
|
|
$query->chunk(1000, function ($infos) use (&$current, &$faild_num, $count, $id, $map) {
|
|
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)->where($map)->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;
|
|
$faild_num = 0;
|
|
$map = [];
|
|
if (isset($data['export_date1']) && $data['export_date1'] != '') {
|
|
$map[] = ['register_date', '>=', $data['export_date1']];
|
|
}
|
|
if (isset($data['export_date2']) && $data['export_date2'] != '') {
|
|
$map[] = ['register_date', '<=', $data['export_date2']];
|
|
}
|
|
cache('shell_process_' . $id, 0, 300);
|
|
$query->chunk(1000, function ($infos) use (&$current, &$faild_num, $count, $id, $map) {
|
|
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_frame_no' => $item->car_frame_no])->where($map)->order('car_phone desc, insurer1_date desc, car_no desc, id desc')->column('id');
|
|
array_shift($ids);
|
|
Db::startTrans();
|
|
try {
|
|
CarInfoT::destroy($ids);
|
|
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 . ', 失败总数:' . $faild_num;
|
|
}
|
|
|
|
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()?:'');
|
|
|
|
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;
|
|
$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 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']}条,
|
|
更新{$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 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++) {
|
|
$row_array[] = [
|
|
'duty_no' => $worksheet->getCellByColumnAndRow(1, $row)->getValue(),
|
|
'register_date' => date('Y-m-d', strtotime($worksheet->getCellByColumnAndRow(2, $row)->getValue())),
|
|
'car_man' => $worksheet->getCellByColumnAndRow(3, $row)->getValue(),
|
|
'car_number' => $worksheet->getCellByColumnAndRow(4, $row)->getValue(),
|
|
'car_phone' => $worksheet->getCellByColumnAndRow(5, $row)->getValue(),
|
|
'car_frame_no' => $worksheet->getCellByColumnAndRow(6, $row)->getValue(),
|
|
'car_no' => $worksheet->getCellByColumnAndRow(7, $row)->getValue(),
|
|
'engine_no' => $worksheet->getCellByColumnAndRow(8, $row)->getValue(),
|
|
'factory_model' => $worksheet->getCellByColumnAndRow(9, $row)->getValue(),
|
|
'purchase_price' => $worksheet->getCellByColumnAndRow(10, $row)->getValue(),
|
|
'create_timestamp' => $now,
|
|
'update_timestamp' => $now
|
|
];
|
|
|
|
|
|
if ($row >= 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 = [];
|
|
}
|
|
}
|
|
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) {
|
|
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) {
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
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'];
|
|
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']) ? $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();
|
|
}
|
|
}
|
|
|