Commit a227f1a0 authored by 孙磊's avatar 孙磊

门店服务类目上传

Signed-off-by: 孙磊's avatarsunlei <sunlei@romens.cn>
parent a3991ffa
...@@ -250,82 +250,56 @@ class BranchServiceTypeController extends BaseController ...@@ -250,82 +250,56 @@ class BranchServiceTypeController extends BaseController
*/ */
public function actionBranchServiceImport() public function actionBranchServiceImport()
{ {
$upload_file = new UploadFiles(['xlsx','xls']); $model = new UploadFiles();
$upload_file->uploadFiles = UploadedFile::getInstance($upload_file,'uploadFile'); if (Yii::$app->request->isPost) {
$fileInfo = $upload_file->upload(); //多文件用getInstances
$model->uploadFiles = UploadedFile::getInstance($model,'uploadFile');
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));
}
if($create_data){ $fileInfo = $model->upload();
$attribute = ['CODE','NAME','THEME_COLOR','DESCRIPTION','GUID','CREATE_TIME','ORG_GUID'];
$result = $model->getDb()->createCommand()->batchInsert('shop_branch_service_type',$attribute,$create_data)->execute(); if ($fileInfo['CODE']==200) {
@unlink($fileInfo['INFO'][0]); // 文件上传成功
if(!$result){ $rows = Helper::newSpreadsheet($fileInfo['INFO'][0])->getRows();
throw new ServerErrorHttpException('导入失败,请稍后重试'); $tempArr=array_slice($rows,2);
} $branchServiceModel = new $this->modelClass();
} $branchServiceModel->scenario = 'import';
$transaction=$branchServiceModel->getDb()->beginTransaction();
if($update_data){ try {
foreach($update_data as $key => $value){ foreach ($tempArr as $key => $value) {
$branch_service_type = BranchServiceType::findOne(['CODE' => $value[0]]); $_servicModel = $branchServiceModel::findOne(['CODE' => $value[0]]);
if ($_servicModel===null) {
$branch_service_type->CODE = (string)$value[0]; $_servicModel = clone $branchServiceModel;
$branch_service_type->NAME = $value[1]; $_servicModel->GUID=Func::create_guid();
$branch_service_type->THEME_COLOR = $value[2]; $_servicModel->ORG_GUID=Yii::$app->user->identity->ORG_GUID;
$branch_service_type->DESCRIPTION = $value[3]; $_servicModel->CREATE_TIME=date('Y-m-d H:i:s',time());
$branch_service_type->CREATE_TIME = date('Y-m-d H:i:s'); }
if($branch_service_type->update(false) == false){ $_servicModel->CODE=$value[0];
// return $branch_service_type->getErrors(); $_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]); @unlink($fileInfo['INFO'][0]);
throw new ServerErrorHttpException('导入失败,请稍后重试'); throw $e;
} }
}else{
return $fileInfo['INFO'];
} }
} }
@unlink($fileInfo['INFO'][0]);
return ['code' => 200, 'message' => '导入成功!'];
} }
/** /**
* @OA\GET( * @OA\GET(
......
...@@ -37,6 +37,10 @@ class BranchServiceType extends BaseModel ...@@ -37,6 +37,10 @@ class BranchServiceType extends BaseModel
[['GUID', 'ORG_GUID', 'CODE', 'NAME'], 'string', 'max' => 50], [['GUID', 'ORG_GUID', 'CODE', 'NAME'], 'string', 'max' => 50],
[['THEME_COLOR', 'DESCRIPTION'], 'string', 'max' => 255], [['THEME_COLOR', 'DESCRIPTION'], 'string', 'max' => 255],
[['GUID'], 'unique'], [['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'],
]; ];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment