Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
newShopBack
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
newshop
newShopBack
Commits
a227f1a0
Commit
a227f1a0
authored
Nov 28, 2019
by
孙磊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
门店服务类目上传
Signed-off-by:
sunlei
<
sunlei@romens.cn
>
parent
a3991ffa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
70 deletions
+48
-70
BranchServiceTypeController.php
...end/controllers/v1/branch/BranchServiceTypeController.php
+44
-70
BranchServiceType.php
backend/models/v1/branch/BranchServiceType.php
+4
-0
No files found.
backend/controllers/v1/branch/BranchServiceTypeController.php
View file @
a227f1a0
...
...
@@ -250,82 +250,56 @@ class BranchServiceTypeController extends BaseController
*/
public
function
actionBranchServiceImport
()
{
$upload_file
=
new
UploadFiles
([
'xlsx'
,
'xls'
]);
$upload_file
->
uploadFiles
=
UploadedFile
::
getInstance
(
$upload_file
,
'uploadFile'
);
$fileInfo
=
$upload_file
->
upload
();
if
(
$fileInfo
[
'CODE'
]
!=
'200'
){
throw
new
HttpException
(
422
,
json_encode
(
$fileInfo
));
}
$arr
=
Helper
::
newSpreadsheet
(
$fileInfo
[
'INFO'
][
0
])
->
getRows
(
false
,[
'rowOffset'
=>
2
,
'columns'
=>
4
]);
//$rows = Helper::newSpreadsheet($fileInfo['INFO'][0])->getRows();
//$arr=array_slice($rows,2);
$model
=
new
$this
->
modelClass
();
$create_data
=
[];
//待新建数组
$update_data
=
[];
//待更新数组
$invalidate_data
=
[];
//验证有误数组
if
(
is_array
(
$arr
)
&&
!
empty
(
$arr
)){
foreach
(
$arr
as
$value
){
$model
->
CODE
=
(
string
)
$value
[
0
];
$model
->
NAME
=
$value
[
1
];
$model
->
THEME_COLOR
=
$value
[
2
];
$model
->
DESCRIPTION
=
$value
[
3
];
if
(
!
$model
->
validate
()){
$value
[
'error'
]
=
$model
->
errors
;
$invalidate_data
[]
=
$value
;
continue
;
}
//若存在错误信息,即停止后续添加或更新,后续数据只做验证
if
(
empty
(
$invalidate_data
)){
//判断门店服务类目是否已存在,若存在放入待更新数组
$branch_service_info
=
BranchServiceType
::
findOne
([
'CODE'
=>
$value
[
0
]]);
if
(
$branch_service_info
){
$update_data
[]
=
$value
;
continue
;
}
//待新建
$value
[
4
]
=
Func
::
create_guid
();
$value
[
5
]
=
date
(
'Y-m-d H:i:s'
,
time
());
$value
[
6
]
=
Yii
::
$app
->
user
->
identity
->
ORG_GUID
;
$create_data
[]
=
$value
;
}
}
}
// return $data = ['create' => $create_data,'update' => $update_data,'invalidate_data' => $invalidate_data];
if
(
$invalidate_data
){
throw
new
HttpException
(
422
,
json_encode
(
$invalidate_data
));
}
$model
=
new
UploadFiles
();
if
(
Yii
::
$app
->
request
->
isPost
)
{
//多文件用getInstances
$model
->
uploadFiles
=
UploadedFile
::
getInstance
(
$model
,
'uploadFile'
);
if
(
$create_data
){
$attribute
=
[
'CODE'
,
'NAME'
,
'THEME_COLOR'
,
'DESCRIPTION'
,
'GUID'
,
'CREATE_TIME'
,
'ORG_GUID'
];
$result
=
$model
->
getDb
()
->
createCommand
()
->
batchInsert
(
'shop_branch_service_type'
,
$attribute
,
$create_data
)
->
execute
();
@
unlink
(
$fileInfo
[
'INFO'
][
0
]);
if
(
!
$result
){
throw
new
ServerErrorHttpException
(
'导入失败,请稍后重试'
);
}
}
$fileInfo
=
$model
->
upload
();
if
(
$fileInfo
[
'CODE'
]
==
200
)
{
// 文件上传成功
$rows
=
Helper
::
newSpreadsheet
(
$fileInfo
[
'INFO'
][
0
])
->
getRows
();
$tempArr
=
array_slice
(
$rows
,
2
);
$branchServiceModel
=
new
$this
->
modelClass
();
$branchServiceModel
->
scenario
=
'import'
;
$transaction
=
$branchServiceModel
->
getDb
()
->
beginTransaction
();
if
(
$update_data
){
foreach
(
$update_data
as
$key
=>
$value
){
$branch_service_type
=
BranchServiceType
::
findOne
([
'CODE'
=>
$value
[
0
]]);
$branch_service_type
->
CODE
=
(
string
)
$value
[
0
];
$branch_service_type
->
NAME
=
$value
[
1
];
$branch_service_type
->
THEME_COLOR
=
$value
[
2
];
$branch_service_type
->
DESCRIPTION
=
$value
[
3
];
$branch_service_type
->
CREATE_TIME
=
date
(
'Y-m-d H:i:s'
);
if
(
$branch_service_type
->
update
(
false
)
==
false
){
// return $branch_service_type->getErrors();
try
{
foreach
(
$tempArr
as
$key
=>
$value
)
{
$_servicModel
=
$branchServiceModel
::
findOne
([
'CODE'
=>
$value
[
0
]]);
if
(
$_servicModel
===
null
)
{
$_servicModel
=
clone
$branchServiceModel
;
$_servicModel
->
GUID
=
Func
::
create_guid
();
$_servicModel
->
ORG_GUID
=
Yii
::
$app
->
user
->
identity
->
ORG_GUID
;
$_servicModel
->
CREATE_TIME
=
date
(
'Y-m-d H:i:s'
,
time
());
}
$_servicModel
->
CODE
=
$value
[
0
];
$_servicModel
->
NAME
=
$value
[
1
];
$_servicModel
->
THEME_COLOR
=
$value
[
2
];
$_servicModel
->
DESCRIPTION
=
$value
[
3
];
if
(
!
$_servicModel
->
save
())
{
$transaction
->
rollBack
();
$returnInfo
=
[
(
$key
+
1
)
=>
$_servicModel
->
errors
];
return
$returnInfo
;
}
}
$transaction
->
commit
();
@
unlink
(
$fileInfo
[
'INFO'
][
0
]);
throw
new
HttpException
(
"200"
,
'导入成功'
);
}
catch
(
\Throwable
$e
)
{
$transaction
->
rollBack
();
@
unlink
(
$fileInfo
[
'INFO'
][
0
]);
throw
new
ServerErrorHttpException
(
'导入失败,请稍后重试'
)
;
throw
$e
;
}
}
else
{
return
$fileInfo
[
'INFO'
];
}
}
@
unlink
(
$fileInfo
[
'INFO'
][
0
]);
return
[
'code'
=>
200
,
'message'
=>
'导入成功!'
];
}
/**
* @OA\GET(
...
...
backend/models/v1/branch/BranchServiceType.php
View file @
a227f1a0
...
...
@@ -37,6 +37,10 @@ class BranchServiceType extends BaseModel
[[
'GUID'
,
'ORG_GUID'
,
'CODE'
,
'NAME'
],
'string'
,
'max'
=>
50
],
[[
'THEME_COLOR'
,
'DESCRIPTION'
],
'string'
,
'max'
=>
255
],
[[
'GUID'
],
'unique'
],
[[
'CODE'
,
'NAME'
],
'required'
,
'on'
=>
[
'import'
]
,
'message'
=>
'{attribute}不能为空'
],
[[
'DESCRIPTION'
],
'string'
,
'max'
=>
100
,
'on'
=>
[
'import'
],
'tooLong'
=>
'最大长度不能超过100'
],
[[
'NAME'
],
'string'
,
'max'
=>
50
,
'on'
=>
[
'import'
],
'tooLong'
=>
'{attribute}最大长度不能超过50'
],
];
}
...
...
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