Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
liexin_supplier
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4987d99e
authored
Jul 27, 2022
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
导入csv问题
parent
b011c852
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
15 deletions
app/Http/Services/SupplierExaminationService.php
app/Http/function.php
app/Utils/CsvValueBinder.php
config/excel.php
public/data/supplier_examination_data.csv
resources/views/script/SupplierExaminationListScript.blade.php
app/Http/Services/SupplierExaminationService.php
View file @
4987d99e
...
@@ -9,6 +9,7 @@ use App\Http\Transformers\SupplierExaminationTransformer;
...
@@ -9,6 +9,7 @@ use App\Http\Transformers\SupplierExaminationTransformer;
use
App\Model\IntracodeModel
;
use
App\Model\IntracodeModel
;
use
App\Model\SupplierChannelModel
;
use
App\Model\SupplierChannelModel
;
use
App\Model\SupplierExaminationModel
;
use
App\Model\SupplierExaminationModel
;
use
App\Utils\CsvValueBinder
;
use
Maatwebsite\Excel\Facades\Excel
;
use
Maatwebsite\Excel\Facades\Excel
;
class
SupplierExaminationService
class
SupplierExaminationService
...
@@ -54,8 +55,21 @@ class SupplierExaminationService
...
@@ -54,8 +55,21 @@ class SupplierExaminationService
try
{
try
{
$content
=
file_get_contents
(
$filePath
);
$content
=
file_get_contents
(
$filePath
);
$fileType
=
mb_detect_encoding
(
$content
,
array
(
'UTF-8'
,
'GBK'
,
'LATIN1'
,
'BIG5'
));
$fileType
=
mb_detect_encoding
(
$content
,
array
(
'UTF-8'
,
'GBK'
,
'LATIN1'
,
'BIG5'
));
Excel
::
load
(
$filePath
,
function
(
$reader
)
{
Excel
::
load
(
$filePath
,
function
(
$reader
)
{
$data
=
$reader
->
getSheet
(
0
)
->
toArray
();
$result
=
$reader
->
getSheet
(
0
)
->
toArray
();
$header
=
$result
[
0
];
if
(
!
$this
->
checkImportCsvHeader
(
$header
))
{
throw
new
\Exception
(
'文件不是标准上传模板,请核对后再上传'
);
}
},
$fileType
);
$valueBinder
=
new
CsvValueBinder
();
$data
=
Excel
::
setValueBinder
(
$valueBinder
)
->
load
(
$filePath
,
$fileType
)
->
get
();
if
(
empty
(
$data
))
{
throw
new
\Exception
(
'上传数据不能为空'
);
}
$data
=
$data
->
toArray
();
$dataMd5
=
md5
(
json_encode
(
$data
));
$dataMd5
=
md5
(
json_encode
(
$data
));
if
(
SupplierExaminationModel
::
where
(
'data_md5'
,
$dataMd5
)
->
exists
())
{
if
(
SupplierExaminationModel
::
where
(
'data_md5'
,
$dataMd5
)
->
exists
())
{
throw
new
\Exception
(
'该文件已经上传过,请选择其它文件'
);
throw
new
\Exception
(
'该文件已经上传过,请选择其它文件'
);
...
@@ -67,14 +81,7 @@ class SupplierExaminationService
...
@@ -67,14 +81,7 @@ class SupplierExaminationService
//拼装数据插入校验,校验完成插入数据库
//拼装数据插入校验,校验完成插入数据库
$examineData
=
[];
$examineData
=
[];
foreach
(
$data
as
$index
=>
$item
)
{
foreach
(
$data
as
$index
=>
$item
)
{
$lineNo
=
$index
+
1
;
$lineNo
=
$index
;
//检测模板头部
if
(
$index
==
0
)
{
if
(
!
$this
->
checkImportCsvHeader
(
$item
))
{
throw
new
\Exception
(
'文件不是标准上传模板,请核对后再上传'
);
}
continue
;
}
$item
=
array_map
(
function
(
$value
)
{
$item
=
array_map
(
function
(
$value
)
{
return
trim
(
$value
);
return
trim
(
$value
);
},
$item
);
},
$item
);
...
@@ -177,16 +184,14 @@ class SupplierExaminationService
...
@@ -177,16 +184,14 @@ class SupplierExaminationService
foreach
(
$examineData
->
chunk
(
50
)
as
$chunk
)
{
foreach
(
$examineData
->
chunk
(
50
)
as
$chunk
)
{
SupplierExaminationModel
::
insert
(
$chunk
->
toArray
());
SupplierExaminationModel
::
insert
(
$chunk
->
toArray
());
}
}
return
true
;
return
true
;
},
$fileType
);
}
catch
(
\Exception
$exception
)
{
}
catch
(
\Exception
$exception
)
{
return
$exception
->
getMessage
();
return
$exception
->
getMessage
();
}
finally
{
}
finally
{
unlink
(
$filePath
);
unlink
(
$filePath
);
return
true
;
}
}
return
true
;
}
}
private
function
checkImportCsvHeader
(
$header
)
private
function
checkImportCsvHeader
(
$header
)
...
...
app/Http/function.php
View file @
4987d99e
...
@@ -249,3 +249,4 @@ function isDateTime($dateTime){
...
@@ -249,3 +249,4 @@ function isDateTime($dateTime){
$ret
=
strtotime
(
$dateTime
);
$ret
=
strtotime
(
$dateTime
);
return
$ret
!==
FALSE
&&
$ret
!=
-
1
;
return
$ret
!==
FALSE
&&
$ret
!=
-
1
;
}
}
app/Utils/CsvValueBinder.php
0 → 100644
View file @
4987d99e
<?php
namespace
App\Utils
;
use
PHPExcel_Cell
;
use
PHPExcel_Cell_DataType
;
use
PHPExcel_Cell_IValueBinder
;
use
PHPExcel_Cell_DefaultValueBinder
;
class
CsvValueBinder
extends
PHPExcel_Cell_DefaultValueBinder
implements
PHPExcel_Cell_IValueBinder
{
public
function
bindValue
(
PHPExcel_Cell
$cell
,
$value
=
null
)
{
if
(
is_numeric
(
$value
))
{
$cell
->
setValueExplicit
(
$value
,
PHPExcel_Cell_DataType
::
TYPE_STRING
);
return
true
;
}
return
parent
::
bindValue
(
$cell
,
$value
);
}
}
\ No newline at end of file
config/excel.php
View file @
4987d99e
...
@@ -363,7 +363,7 @@ return array(
...
@@ -363,7 +363,7 @@ return array(
|
|
*/
*/
'heading'
=>
'
fals
e'
,
'heading'
=>
'
tru
e'
,
/*
/*
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
...
...
public/data/supplier_examination_data.csv
View file @
4987d99e
销售订单号,采购订单号,*检货时间,销售,*采购员,A/B单,*供应商,*型号,*品牌,*数量,批次,产地,入库日期,来货单号,送货单,理货要求,验货要求,*不良数,异常等级,不良现象,*检验结果,备注
销售订单号,采购订单号,*检货时间,销售,*采购员,A/B单,*供应商,*型号,*品牌,*数量,批次,产地,入库日期,来货单号,送货单,理货要求,验货要求,*不良数,异常等级,不良现象,*检验结果,备注
120220606676311
2,1202206066763133
,2020/2/3,123,易敏,123,深圳市华之电科技有限公司,123,123,123,123,123,2020/2/3,123,123,123,123,123,1,特批入库,特批入库,123
120220606676311
4444,120220606676313555
,2020/2/3,123,易敏,123,深圳市华之电科技有限公司,123,123,123,123,123,2020/2/3,123,123,123,123,123,1,特批入库,特批入库,123
resources/views/script/SupplierExaminationListScript.blade.php
View file @
4987d99e
...
@@ -64,7 +64,7 @@
...
@@ -64,7 +64,7 @@
{
type
:
'checkbox'
},
{
type
:
'checkbox'
},
{
field
:
'id'
,
title
:
'ID'
,
align
:
'center'
,
width
:
80
},
{
field
:
'id'
,
title
:
'ID'
,
align
:
'center'
,
width
:
80
},
{
field
:
'order_sn'
,
title
:
'销售订单号'
,
align
:
'center'
,
width
:
150
},
{
field
:
'order_sn'
,
title
:
'销售订单号'
,
align
:
'center'
,
width
:
150
},
{
field
:
'purchase_sn'
,
title
:
'采购订单号'
,
align
:
'center'
,
width
:
1
5
0
},
{
field
:
'purchase_sn'
,
title
:
'采购订单号'
,
align
:
'center'
,
width
:
1
6
0
},
{
{
field
:
'examine_time'
,
title
:
'检货时间'
,
align
:
'center'
,
width
:
130
field
:
'examine_time'
,
title
:
'检货时间'
,
align
:
'center'
,
width
:
130
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment