From 143aff83ccd4aa2ad70dafc8abfe0dfef77853fb Mon Sep 17 00:00:00 2001
From: zhaocheng <578322713@qq.com>
Date: Mon, 29 Aug 2022 17:47:51 +0800
Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=A4=8D=E8=BD=A6=E6=9E=B6=E5=8F=B7?=
=?UTF-8?q?=E5=A4=84=E7=90=86=E9=87=8D=E5=86=99=E5=8F=8A=E5=AF=BC=E5=85=A5?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=B9excel?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/controller/Index.php | 32 +++++-----
app/controller/Task.php | 27 +++-----
app/view/index/index.html | 16 +----
app/view/task/index.html | 24 ++------
extend/service/CarInfoHandle.php | 102 ++++++++++++++++++-------------
public/.htaccess | 8 ---
6 files changed, 88 insertions(+), 121 deletions(-)
diff --git a/app/controller/Index.php b/app/controller/Index.php
index 008b0ea..89bc7e0 100644
--- a/app/controller/Index.php
+++ b/app/controller/Index.php
@@ -218,8 +218,21 @@ class Index extends BaseController
public function delete($id)
{
//
- $this->json_data['code'] = intval($this->model->destroy($id));
- $this->json_data['msg'] = $this->json_data['code'] ? '删除成功' : '删除失败';
+ $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'] ? '删除成功' : '删除失败';
+ Db::commit();
+ } catch (\Exception $e) {
+ Db::rollback();
+ $this->json_data['code'] = 0;
+ $this->json_data['msg'] = '删除失败: '. $e->getMessage();
+ }
+
return json($this->json_data);
}
@@ -242,21 +255,6 @@ class Index extends BaseController
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
* @return Json
diff --git a/app/controller/Task.php b/app/controller/Task.php
index 8ee1ed0..47d73e1 100644
--- a/app/controller/Task.php
+++ b/app/controller/Task.php
@@ -368,22 +368,6 @@ class Task extends BaseController
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)
{
$job_info = JobsT::find($id);
@@ -630,11 +614,14 @@ class Task extends BaseController
private function checkUploadTpl($file, $type)
{
$path_info = pathinfo($file);
- if ($type == 1 && $path_info['extension'] == 'csv') {
- $title = \file\FileSystem::getFileBlockData($file,0,1);
- $tpl_title = ['税号', '初登日期', '车主', '证件号', '电话号码', '车架号', '车牌号', '发动机号', '车型', '新车购置价'];
+ if ($type == 1 && $path_info['extension'] == 'xlsx') {
+ $spreadsheetReader = Excel::loadFile($file);
+ $sheet = $spreadsheetReader->getSheet(0);
+ $highestColumn = $sheet->getHighestColumn(); // e.g 'F'
+ $title = $sheet->rangeToArray("A1:{$highestColumn}1")[0];
+
+ $tpl_title = ['税号', '初登日期', '车主', '证件号', '电话号码', '车架号', '车牌号', '发动机号', '车型', '新车购置价', '商业止保日期', '交强止保日期'];
// $tpl_title = ['区域', '购车日期', '客户姓名', '证件号码', '联系方式', '车架号', '车牌号', '发动机号', '品牌型号', '新车购置价', '保险公司', '商业止保日期', '交强止保日期', '被保险人', '被保险人证件号'];
- $title = explode(',', trim($title[0]));
if (count(array_diff($tpl_title, $title)) > 0) {
return false;
}
diff --git a/app/view/index/index.html b/app/view/index/index.html
index 00510c1..27792d0 100644
--- a/app/view/index/index.html
+++ b/app/view/index/index.html
@@ -158,7 +158,6 @@
重置条件
-
@@ -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){
@@ -381,4 +367,4 @@
});
}
-{/block}
\ No newline at end of file
+{/block}
diff --git a/app/view/task/index.html b/app/view/task/index.html
index 811cdb4..89bb29b 100644
--- a/app/view/task/index.html
+++ b/app/view/task/index.html
@@ -26,7 +26,7 @@
上传CSV文件
- [下载车辆信息导入模板]
+ [下载车辆信息导入模板]
@@ -186,7 +186,6 @@
-
@@ -260,10 +259,10 @@
$('#task-export').addClass('active').show();
break;
case 3:
- $('#task-peer-phone').addClass('active').show();
+ // $('#task-peer-phone').addClass('active').show();
break;
case 4:
- $('#task-repeat_frame').addClass('active').show();
+ // $('#task-repeat_frame').addClass('active').show();
break;
case 5:
$('#task-import').addClass('active').show();
@@ -379,19 +378,6 @@
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(){
@@ -467,4 +453,4 @@
});
}
-{/block}
\ No newline at end of file
+{/block}
diff --git a/extend/service/CarInfoHandle.php b/extend/service/CarInfoHandle.php
index 4a05405..b7d68dd 100644
--- a/extend/service/CarInfoHandle.php
+++ b/extend/service/CarInfoHandle.php
@@ -29,35 +29,7 @@ class CarInfoHandle
*/
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 + 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);
+ return $this->importExcel($data, $id);
}
/**
@@ -336,26 +308,64 @@ class CarInfoHandle
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) {
+ $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_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);
+ $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 {
- CarInfoT::destroy($ids);
- RepeatFrameT::where('car_frame_no', $item->car_frame_no)->delete();
+ foreach ($sources as $index => $source) {
+ if (empty(trim($source['car_phone']))) {
+ $ids = array_column($sources, 'id');
+ CarInfoT::destroy($ids);
+ 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();
} catch (\Exception $e) {
$faild_num++;
@@ -487,16 +497,24 @@ class CarInfoHandle
'engine_no' => $worksheet->getCellByColumnAndRow(8, $row)->getValue(),
'factory_model' => $worksheet->getCellByColumnAndRow(9, $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,
'update_timestamp' => $now
];
- if ($row >= 1000) {
+ 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;
diff --git a/public/.htaccess b/public/.htaccess
index cbc7868..e69de29 100644
--- a/public/.htaccess
+++ b/public/.htaccess
@@ -1,8 +0,0 @@
-
- Options +FollowSymlinks -Multiviews
- RewriteEngine On
-
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
-