重复车架号处理重写及导入文件改excel

master
zhaocheng 2 years ago
parent d806da099e
commit 143aff83cc
  1. 30
      app/controller/Index.php
  2. 27
      app/controller/Task.php
  3. 14
      app/view/index/index.html
  4. 20
      app/view/task/index.html
  5. 98
      extend/service/CarInfoHandle.php
  6. 8
      public/.htaccess

@ -218,8 +218,21 @@ class Index extends BaseController
public function delete($id) public function delete($id)
{ {
// //
$this->json_data['code'] = intval($this->model->destroy($id)); $car_frame_no = $this->model->where('id', $id)->value('car_frame_no');
Db::startTrans();
try {
$this->model->destroy($id);
if ($this->model->where('car_frame_no', $car_frame_no)->count() == 1)
RepeatFrameT::where('car_frame_no', $car_frame_no)->delete();
$this->json_data['code'] = 1;
$this->json_data['msg'] = $this->json_data['code'] ? '删除成功' : '删除失败'; $this->json_data['msg'] = $this->json_data['code'] ? '删除成功' : '删除失败';
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->json_data['code'] = 0;
$this->json_data['msg'] = '删除失败: '. $e->getMessage();
}
return json($this->json_data); return json($this->json_data);
} }
@ -242,21 +255,6 @@ class Index extends BaseController
return json($this->json_data); return json($this->json_data);
} }
/**
* 清除数据
* @return Json
*/
public function truncate(){
$ret = Db::execute("truncate table car_info_t");
if($ret === false){
$this->json_data['code'] = 0;
$this->json_data['msg'] = '删除失败';
}else {
$this->json_data['msg'] = '删除成功';
}
return json($this->json_data);
}
/** /**
* @param $id * @param $id
* @return Json * @return Json

@ -368,22 +368,6 @@ class Task extends BaseController
return json($this->json_data); return json($this->json_data);
} }
public function clearSubTable(){
Db::startTrans();
try {
Db::execute("truncate table jobs_t;");
Db::execute("truncate table peer_phone_t;");
Db::execute("truncate table repeat_frame_t;");
$this->json_data['msg'] = '删除成功';
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->json_data['code'] = 0;
$this->json_data['msg'] = '删除失败';
}
return json($this->json_data);
}
public function download($id) public function download($id)
{ {
$job_info = JobsT::find($id); $job_info = JobsT::find($id);
@ -630,11 +614,14 @@ class Task extends BaseController
private function checkUploadTpl($file, $type) private function checkUploadTpl($file, $type)
{ {
$path_info = pathinfo($file); $path_info = pathinfo($file);
if ($type == 1 && $path_info['extension'] == 'csv') { if ($type == 1 && $path_info['extension'] == 'xlsx') {
$title = \file\FileSystem::getFileBlockData($file,0,1); $spreadsheetReader = Excel::loadFile($file);
$tpl_title = ['税号', '初登日期', '车主', '证件号', '电话号码', '车架号', '车牌号', '发动机号', '车型', '新车购置价']; $sheet = $spreadsheetReader->getSheet(0);
$highestColumn = $sheet->getHighestColumn(); // e.g 'F'
$title = $sheet->rangeToArray("A1:{$highestColumn}1")[0];
$tpl_title = ['税号', '初登日期', '车主', '证件号', '电话号码', '车架号', '车牌号', '发动机号', '车型', '新车购置价', '商业止保日期', '交强止保日期'];
// $tpl_title = ['区域', '购车日期', '客户姓名', '证件号码', '联系方式', '车架号', '车牌号', '发动机号', '品牌型号', '新车购置价', '保险公司', '商业止保日期', '交强止保日期', '被保险人', '被保险人证件号']; // $tpl_title = ['区域', '购车日期', '客户姓名', '证件号码', '联系方式', '车架号', '车牌号', '发动机号', '品牌型号', '新车购置价', '保险公司', '商业止保日期', '交强止保日期', '被保险人', '被保险人证件号'];
$title = explode(',', trim($title[0]));
if (count(array_diff($tpl_title, $title)) > 0) { if (count(array_diff($tpl_title, $title)) > 0) {
return false; return false;
} }

@ -158,7 +158,6 @@
<button type="button" class="layui-btn" id="search-btn">搜索</button> <button type="button" class="layui-btn" id="search-btn">搜索</button>
<a href="{:url('index/index')}" class="layui-btn" id="reset-btn">重置条件</a> <a href="{:url('index/index')}" class="layui-btn" id="reset-btn">重置条件</a>
<button type="button" class="layui-btn layui-btn-danger" id="deletes-btn">批量删除</button> <button type="button" class="layui-btn layui-btn-danger" id="deletes-btn">批量删除</button>
<button type="button" class="layui-btn layui-btn-danger" id="clear-btn">清空数据</button>
</div> </div>
</div> </div>
</div> </div>
@ -358,19 +357,6 @@
}); });
}); });
}); });
$('#clear-btn').on('click', function () {
layer.confirm('清空数据将不能恢复,确认清空车辆信息吗?', function(index){
layer.close(index);
index = layer.load();
//向服务端发送删除指令
$.post('{:url("index/truncate/")}', function (ret) {
layer.close(index);
layer.msg(ret.msg, function () {
layui.table.reload('carInfo');
});
});
});
});
}); });
function task_create(type,id = 0){ function task_create(type,id = 0){

@ -26,7 +26,7 @@
<i class="layui-icon">&#xe67c;</i>上传CSV文件 <i class="layui-icon">&#xe67c;</i>上传CSV文件
</button> </button>
</div> </div>
<a href="/static/tpl.csv" download="车辆信息导入模板.csv" style="color: #009688;">[下载车辆信息导入模板]</a> <a href="/static/tpl.xlsx" download="车辆信息导入模板.xlsx" style="color: #009688;">[下载车辆信息导入模板]</a>
</div> </div>
<div class="task-param" id="task-export" style="display: none;"> <div class="task-param" id="task-export" style="display: none;">
<div class="layui-form-item"> <div class="layui-form-item">
@ -186,7 +186,6 @@
</div> </div>
<div class="layui-input-block layui-text-right"> <div class="layui-input-block layui-text-right">
<button type="button" class="layui-btn" onclick="task_add()">立即提交</button> <button type="button" class="layui-btn" onclick="task_add()">立即提交</button>
<button type="button" class="layui-btn layui-btn-danger" id="clear-btn">清空数据</button>
</div> </div>
</div> </div>
</div> </div>
@ -260,10 +259,10 @@
$('#task-export').addClass('active').show(); $('#task-export').addClass('active').show();
break; break;
case 3: case 3:
$('#task-peer-phone').addClass('active').show(); // $('#task-peer-phone').addClass('active').show();
break; break;
case 4: case 4:
$('#task-repeat_frame').addClass('active').show(); // $('#task-repeat_frame').addClass('active').show();
break; break;
case 5: case 5:
$('#task-import').addClass('active').show(); $('#task-import').addClass('active').show();
@ -379,19 +378,6 @@
elem: d, elem: d,
}); });
}); });
$('#clear-btn').on('click', function () {
layer.confirm('清空数据将不能恢复,确认清空附属表吗?', function(index){
layer.close(index);
index = layer.load();
//向服务端发送删除指令
$.post('{:url("task/clearSubTable/")}', function (ret) {
layer.close(index);
layer.msg(ret.msg, function () {
layui.table.reload('taskInfo');
});
});
});
});
}); });
function export_num(){ function export_num(){

@ -29,35 +29,7 @@ class CarInfoHandle
*/ */
public function import($data, $id) public function import($data, $id)
{ {
if(pathinfo($data['filename'])['extension'] != 'csv'){ return $this->importExcel($data, $id);
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 + 1999, 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);
} }
/** /**
@ -336,26 +308,64 @@ class CarInfoHandle
echo '总计:' . $count . PHP_EOL; echo '总计:' . $count . PHP_EOL;
$current = 0; $current = 0;
$faild_num = 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); cache('shell_process_' . $id, 0, 300);
$query->chunk(1000, function ($infos) use (&$current, &$faild_num, $count, $id, $map) { $query->chunk(1000, function ($infos) use (&$current, &$faild_num, $count, $id) {
foreach ($infos as $item) { foreach ($infos as $item) {
if ($current % 10 == 0) { if ($current % 10 == 0) {
cache('shell_process_' . $id, round($current / $count * 100, 3), 300); cache('shell_process_' . $id, round($current / $count * 100, 3), 300);
echo '当前进度:' . round($current / $count * 100, 3) . PHP_EOL; 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'); $sources = CarInfoT::where(['car_frame_no' => $item->car_frame_no])
array_shift($ids); ->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(); Db::startTrans();
try { try {
foreach ($sources as $index => $source) {
if (empty(trim($source['car_phone']))) {
$ids = array_column($sources, 'id');
CarInfoT::destroy($ids); CarInfoT::destroy($ids);
RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete(); RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete();
$sources = [];
break;
} else if ($source_a['car_phone'] == $source['car_phone']) {
foreach (['car_no', 'engine_no', 'insurer1_date'] as $key) {
if ($source_a[$key] != $source[$key]) {
if (trim($source_a[$key]) == '') {
list($source_a, $source) = [$source, $source_a];
CarInfoT::destroy($source['id']);
unset($sources[$index]);
break;
}
}
}
} else if (!preg_match('/^1[3-9][0-9]{9}/', $source['car_phone'])) {
CarInfoT::destroy($source['id']);
unset($sources[$index]);
} else if (CarInfoT::where('car_phone', $source['car_phone'])->count() >= 6) {
CarInfoT::destroy($source['id']);
CarInfoT::where('car_phone', $source['car_phone'])->update(['car_phone' => '']);
unset($sources[$index]);
}
}
if (empty($sources))
RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete();
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
$faild_num++; $faild_num++;
@ -487,16 +497,24 @@ class CarInfoHandle
'engine_no' => $worksheet->getCellByColumnAndRow(8, $row)->getValue(), 'engine_no' => $worksheet->getCellByColumnAndRow(8, $row)->getValue(),
'factory_model' => $worksheet->getCellByColumnAndRow(9, $row)->getValue(), 'factory_model' => $worksheet->getCellByColumnAndRow(9, $row)->getValue(),
'purchase_price' => $worksheet->getCellByColumnAndRow(10, $row)->getValue(), 'purchase_price' => $worksheet->getCellByColumnAndRow(10, $row)->getValue(),
'insurer1_date' => $worksheet->getCellByColumnAndRow(11, $row)->getValue(),
'insurer2_date' => $worksheet->getCellByColumnAndRow(12, $row)->getValue(),
'create_timestamp' => $now, 'create_timestamp' => $now,
'update_timestamp' => $now 'update_timestamp' => $now
]; ];
if ($row >= 1000) { if (count($row_array) > 1000) {
Db::name('car_info_t')->insertAll($row_array); Db::name('car_info_t')->insertAll($row_array);
cache('shell_process_' . $id, round(($row - 1) / $highestRow * 100, 3), 300); cache('shell_process_' . $id, round(($row - 1) / $highestRow * 100, 3), 300);
echo '当前进度:' . round(round(($row - 1) / $highestRow * 100, 3)) . PHP_EOL; echo '当前进度:' . round(round(($row - 1) / $highestRow * 100, 3)) . PHP_EOL;
$row_array = []; $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;'); Db::execute('ALTER TABLE car_info_t ENABLE KEYS;');
$t2 = microtime(true); $t2 = microtime(true);
echo '耗时' . round($t2 - $t1, 3) . '秒' . PHP_EOL; echo '耗时' . round($t2 - $t1, 3) . '秒' . PHP_EOL;

@ -1,8 +0,0 @@
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
Loading…
Cancel
Save