Browse Source

优化商品模块获取属性信息方法代码

antshop
linyaostalker 5 years ago
parent
commit
bfdfd4304c
  1. 10
      backend/modules/goods/controllers/AttributeController.php
  2. 3
      backend/modules/goods/controllers/BrandController.php
  3. 5
      backend/modules/goods/controllers/CategoryController.php
  4. 7
      backend/modules/goods/controllers/GoodsController.php
  5. 70
      backend/modules/goods/logic/goods/GoodsManager.php
  6. 3
      composer.json

10
backend/modules/goods/controllers/AttributeController.php

@ -8,6 +8,7 @@ use backend\modules\goods\models\searchs\AttributeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use iron\widget\Excel;
/**
* AttributeController implements the CRUD actions for Attribute model.
@ -70,10 +71,11 @@ class AttributeController extends Controller
$model->cat_id = 0;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->value = str_replace(',', ',', $model->value);
$value = $model->value;
$model->value = str_replace(',', ',', $value);
$array = explode(',', $model->value);
if (count($array) != count(array_unique($array))) {
\Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
return $this->render('create', ['model' => $model]);
}
if ($model->save()) {
@ -101,7 +103,7 @@ class AttributeController extends Controller
$model->value = str_replace(',', ',', $model->value);
$array = explode(',', $model->value);
if (count($array) != count(array_unique($array))) {
\Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
return $this->render('create', ['model' => $model]);
}
if ($model->save()) {
@ -159,7 +161,7 @@ class AttributeController extends Controller
} else {
$dataProvider = $searchModel->search($params);
}
\iron\widget\Excel::export([
Excel::export([
'models' => $dataProvider->getModels(),
'format' => 'Xlsx',
'asAttachment' => true,

3
backend/modules/goods/controllers/BrandController.php

@ -8,6 +8,7 @@ use backend\modules\goods\models\searchs\BrandSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use iron\widget\Excel;
/**
* BrandController implements the CRUD actions for Brand model.
@ -140,7 +141,7 @@ class BrandController extends Controller
} else {
$dataProvider = $searchModel->search($params);
}
\iron\widget\Excel::export([
Excel::export([
'models' => $dataProvider->getModels(),
'format' => 'Xlsx',
'asAttachment' => true,

5
backend/modules/goods/controllers/CategoryController.php

@ -72,9 +72,8 @@ class CategoryController extends Controller
}
/**
* Creates a new Category model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
* @return string|yii\web\Response
* @throws Exception
*/
public function actionCreate()
{

7
backend/modules/goods/controllers/GoodsController.php

@ -2,6 +2,7 @@
namespace backend\modules\goods\controllers;
use backend\modules\goods\models\ars\FilterAttr;
use backend\modules\goods\models\ars\GoodsAttr;
use backend\modules\goods\models\ars\GoodsSku;
use backend\models\ars\OrderGoods;
@ -139,9 +140,11 @@ class GoodsController extends Controller
$model->uniform_postage /= 100;
$model->market_price /= 100;
$model->price /= 100;
$attributeModel = GoodsManager::getAttribute($id);
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$attributeModel = GoodsManager::getAttribute($goodsAttributes);
$checkAttr = GoodsManager::getSkuInfo($id);
$filterAttributeModel = GoodsManager::getFilterAttribute($id);
$goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filterAttributeModel = GoodsManager::getAttribute($goodsFilterAttributes);
$judgeGoodsCategory = GoodsManager::judgeGoodsCategory($model);
return $this->render('update', [
'model' => $model,

70
backend/modules/goods/logic/goods/GoodsManager.php

@ -3,14 +3,15 @@ namespace backend\modules\goods\logic\goods;
use backend\modules\file\models\ars\File;
use Yii;
use yii\base\Exception;
use yii\db\Exception;
use backend\modules\goods\models\ars\GoodsAttr;
use backend\modules\goods\models\ars\Attribute;
use backend\modules\goods\models\ars\GoodsSku;
use backend\modules\goods\models\ars\Goods;
use backend\modules\goods\models\ars\FilterAttr;
use backend\modules\goods\models\ars\Category;
use yii\web\HttpException;
use backend\modules\file\logic\file\FileManager;
use yii\web\ServerErrorHttpException;
class GoodsManager
{
@ -31,7 +32,7 @@ class GoodsManager
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr);
//创建文件
$class = new \backend\modules\file\logic\file\FileManager();
$class = new FileManager();
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType);
//需要删除的文件id
@ -54,15 +55,15 @@ class GoodsManager
$tra->commit();
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId];
} catch (\Exception $e) {
} catch (Exception $e) {
$tra->rollBack();
throw new \Exception($e->getMessage());
throw new ServerErrorHttpException($e->getMessage());
}
}
/**
* @param $data
* @param $model
* @param Goods|$model
* @param null $coverImageOldIdStr
* @param null $detailImageOldIdStr
* @return array
@ -82,7 +83,7 @@ class GoodsManager
throw new \Exception('商品保存失败');
}
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr), File::OWN_TYPE_GOODS_INDEX);
$saveDetailImageRes = self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS);
self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS);
if($saveCoverImageRes['first_file_id'] !== 0) {
$model->image = $saveCoverImageRes['first_file_id'];
if (!$model->save()) {
@ -198,33 +199,32 @@ class GoodsManager
}
/**
* @param $id
* @return Attribute|array|null
* @param GoodsAttr|FilterAttr|$attributes
* @return array
* 获取属性信息
*/
public static function getAttribute($id)
public static function getAttribute($attributes)
{
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = [];
$goodsAttributeModel = [];
if (!$goodsAttributes) {
return $goodsAttributeModel;
$filter = array();
$attributeModel = array();
if (!$attributes) {
return $attributeModel;
}
foreach ($goodsAttributes as $key => $value) {
foreach ($attributes as $key => $value) {
$attribute = Attribute::findOne($value->attr_id);
if (!in_array($attribute->name, $filter)) {
$filter[] = $attribute->name;
$attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]];
$goodsAttributeModel[] = $attribute;
$attributeModel[] = $attribute;
} else {
foreach ($goodsAttributeModel as $k => $v) {
foreach ($attributeModel as $k => $v) {
if ($v['name'] == $attribute->name) {
$goodsAttributeModel[$k]['value'][] = $value->attr_value;
$attributeModel[$k]['value'][] = $value->attr_value;
}
}
}
}
return $goodsAttributeModel;
return $attributeModel;
}
/**
@ -534,36 +534,6 @@ class GoodsManager
return $newAttr;
}
/**
* @param $id
* @return Attribute|array|null
* 获取筛选属性信息
*/
public static function getFilterAttribute($id)
{
$goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = [];
$goodsFilterAttributeModel = [];
if (!$goodsFilterAttributes) {
return $goodsFilterAttributeModel;
}
foreach ($goodsFilterAttributes as $key => $value) {
$attribute = Attribute::findOne($value->attr_id);
if (!in_array($attribute->name, $filter)) {
$filter[] = $attribute->name;
$attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]];
$goodsFilterAttributeModel[] = $attribute;
} else {
foreach ($goodsFilterAttributeModel as $k => $v) {
if ($v['name'] == $attribute->name) {
$goodsFilterAttributeModel[$k]['value'][] = $value->attr_value;
}
}
}
}
return $goodsFilterAttributeModel;
}
/**
* @param $goodsModel
* @return bool

3
composer.json

@ -20,7 +20,8 @@
"kartik-v/yii2-editable": "^1.7@dev",
"kartik-v/yii2-widget-depdrop": "dev-master",
"antkaz/yii2-vue": "dev-master",
"xj/yii2-babel": "dev-master"
"xj/yii2-babel": "dev-master",
"ext-json": "*"
},
"repositories": {
"asset_packagist": {

Loading…
Cancel
Save