Commit a227f1a0 authored by 孙磊's avatar 孙磊

门店服务类目上传

Signed-off-by: 孙磊's avatarsunlei <sunlei@romens.cn>
parent a3991ffa
......@@ -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();
$model = new UploadFiles();
if (Yii::$app->request->isPost) {
//多文件用getInstances
$model->uploadFiles = UploadedFile::getInstance($model,'uploadFile');
if($fileInfo['CODE'] != '200'){
throw new HttpException(422,json_encode($fileInfo));
}
$fileInfo = $model->upload();
$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 ($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($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));
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());
}
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('导入失败,请稍后重试');
$_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;
}
}
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();
$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(
......
......@@ -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'],
];
}
......
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