Commit 19cc03d4 authored by 侯贺政's avatar 侯贺政

商品详情页模板,批量导入关联商品

Signed-off-by: 侯贺政's avatarhouhezheng <houhezheng@romens.cn>
parent ea1fd531
......@@ -423,6 +423,7 @@ class BranchScoreController extends BaseController
throw $th;
}
}
@unlink($fileInfo['INFO'][0]);
throw new BadRequestHttpException('导入失败!');
}
}
......@@ -18,7 +18,8 @@ class DownTemplateController extends BaseController
* 门店导入模板(import_branch.xls);
* 员工信息导入模板(employee_import.xls);
* 门店服务评价打分模板(shop_branch_score_import.xls);
* o2o配送价格导入模板(o2o_delivery_import.xls)",
* o2o配送价格导入模板(o2o_delivery_import.xls);
* 批量关联模板(associated_goods_import.xls);",
* @OA\Parameter(name="ENG_NAME",in="query",required=true,description="模板名称",@OA\Schema(type="string")),
* @OA\Parameter(name="CN_NAME",in="query",required=true,description="模板中文名称",@OA\Schema(type="string")),
* @OA\Parameter(name="SUFFIX",in="query",description="excel后缀,默认xlsx,首字母大写",@OA\Schema(type="string")),
......
......@@ -5,9 +5,12 @@ namespace backend\controllers\v1\shopgoods;
use app\models\v1\shopgoods\ShopGoods;
use Yii;
use backend\controllers\v1\BaseController;
use backend\helpers\UploadFiles;
use yidas\phpSpreadsheet\Helper;
use yii\web\BadRequestHttpException;
use yii\web\HttpException;
use yii\web\ServerErrorHttpException;
use yii\web\UploadedFile;
class GoodsDetailSettingController extends BaseController
{
......@@ -232,8 +235,8 @@ class GoodsDetailSettingController extends BaseController
* @OA\GET(
* path="/backend/web/v1/shopgoods/goods-detail-settings/associated-goods-list",
* tags={"商品管理"},
* summary="获取关联商品列表(侯贺政)",
* description="注:如果不传pageSize字段默认分页,每页数据条数为后台默认条数;如果传pageSize字段则不分页,输出所有数据。pageSize值任意,字段存在不传值亦可获取所有数据。",
* summary="商品详情页模板,获取已关联的商品列表(侯贺政)",
* description="注:如果不传pageSize字段默认分页,每页数据条数为后台默认条数如果传pageSize字段则不分页,输出所有数据。pageSize值任意,字段存在不传值亦可获取所有数据。",
* @OA\Parameter(name="page",in="query",description="分页页码",@OA\Schema(type="int")),
* @OA\Parameter(name="pageSize",in="query",description="每页条数(注意备注描述)",@OA\Schema(type="int")),
* @OA\Parameter(name="GUID",in="query",required=true,description="模板GUID",@OA\Schema(type="string")),
......@@ -259,8 +262,8 @@ class GoodsDetailSettingController extends BaseController
* @OA\POST(
* path="/backend/web/v1/shopgoods/goods-detail-settings/associated-goods-add",
* tags={"商品管理"},
* summary="添加关联商品(侯贺政)",
* description="添加关联商品",
* summary="商品详情页模板,添加关联商品(侯贺政)",
* description="添加模板关联商品",
* @OA\RequestBody(
* required=true,
* description="",
......@@ -295,7 +298,7 @@ class GoodsDetailSettingController extends BaseController
* @OA\DELETE(
* path="/backend/web/v1/shopgoods/goods-detail-settings/associated-goods-del",
* tags={"商品管理"},
* summary="删除单个关联商品(侯贺政)",
* summary="商品详情页模板,删除单个关联商品(侯贺政)",
* description="删除单个关联商品",
* @OA\Parameter(
* description="要删除的关联商品GUID",
......@@ -326,4 +329,82 @@ class GoodsDetailSettingController extends BaseController
}
throw new HttpException(200, '删除成功');
}
/**
* @OA\POST(
* path="/backend/web/v1/shopgoods/goods-detail-settings/associated-goods-import",
* tags={"商品管理"},
* summary="商品详情页模板,批量导入关联商品(侯贺政)",
* description="批量导入关联商品",
* @OA\RequestBody(
* required=true,
* description="批量导入关联商品",
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="uploadFile",
* type="string",
* format="binary"
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="导入成功",
* ),
* @OA\Response(
* response=422,
* description="导入失败,数据验证错误,返回错误数据行号及错误信息(数组)。",
* ),
* security={{"Authorization":{}}}
* )
*/
public function actionAssociatedGoodsImport()
{
$upload_file = new UploadFiles(['xlsx', 'xls']);
$upload_file->uploadFiles = UploadedFile::getInstance($upload_file, 'uploadFile');
$fileInfo = $upload_file->upload();
if ($fileInfo['CODE'] != '200') {
Yii::$app->response->statusCode = 422;
if ($fileInfo['INFO']['uploadFiles'][0] == "类型不对") {
$fileInfo['INFO']['uploadFiles'][0] = '文件类型要求97-2003工作簿 xls格式!';
}
return $fileInfo['INFO'];
}
$data = Helper::newSpreadsheet($fileInfo['INFO'][0])->getRows(true, ['rowOffset' => 2, 'columns' => 2]);
$model = new ShopGoods();
$transaction = $model->getDb()->beginTransaction();
if (!empty($data) && is_array($data)) {
try {
foreach ($data as $key => $value) {
$_model = $model::findOne(['CODE' => $value[0]]);
if ($_model !== null) {
$_model->DETAIL_SETTING_GUID = $value[1];
if (!$_model->save()) {
$transaction->rollBack();
$returnInfo = [
($key + 1) => $_model->errors
];
@unlink($fileInfo['INFO'][0]);
Yii::$app->response->statusCode = 422;
return $returnInfo;
}
}
}
$transaction->commit();
@unlink($fileInfo['INFO'][0]);
throw new HttpException("200", '导入成功');
} catch (\Throwable $th) {
$transaction->rollBack();
@unlink($fileInfo['INFO'][0]);
throw $th;
}
}
@unlink($fileInfo['INFO'][0]);
throw new BadRequestHttpException('导入失败!');
}
}
......@@ -200,6 +200,7 @@ class ShopEmployeeController extends BaseController
throw $th;
}
}
@unlink($fileInfo['INFO'][0]);
throw new BadRequestHttpException('导入失败!');
}
......
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