Browse Source

开发创建修改商品属性操作方法,开发删除商品属性方法,开发保存商品属性,开发获取属性信息,开发获取sku信息方法,完成商品创建修改方法的代码整合,完成开发创建商品属性功能

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
79406345b4
  1. 52
      vendor/antgoods/goods/src/controllers/GoodsController.php
  2. 195
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php
  3. 1
      vendor/antgoods/goods/src/models/ars/Goods.php
  4. 3
      vendor/antgoods/goods/src/models/ars/GoodsAttr.php
  5. 3
      vendor/antgoods/goods/src/models/ars/GoodsSku.php

52
vendor/antgoods/goods/src/controllers/GoodsController.php

@ -81,24 +81,16 @@ class GoodsController extends Controller
public function actionCreate()
{
$model = new Goods();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//商品封面图和商品详情图上传保存处理
$cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_cover_image_res = $goods_manager->saveFile(explode(',', $cover_image_id_str), $model);
$save_detail_image_res = $goods_manager->saveFile(explode(',', $detail_image_id_str), $model, [], File::OWN_TYPE_GOODS_DETAILS);
if($save_cover_image_res['status'] && $save_detail_image_res['status']){
$model->image = $save_cover_image_res['first_file_id'];
$model->save();
$res = $goods_manager->updateGoods(Yii::$app->request->post(), $model);
if ($res['status']) {
return $this->redirect('index');
}
return $this->redirect('index');
} else {
$model->ruleVerify = 1;
}
return $this->render('create', [
'model' => $model,
]);
@ -116,30 +108,23 @@ class GoodsController extends Controller
$model = $this->findModel($id);
$model->coverImageId = $model->image;
$model->detailImageId = implode(',', File::find()->select('id')->where(['is_delete' => File::IS_DELETE_NO, 'own_id' => $model->id, 'own_type' => File::OWN_TYPE_GOODS_DETAILS])->column());
$goods_manager = new GoodsManager();
//记录已保存的商品图片id,用于修改
$cover_image_old_id_arr = $model->image;
$detail_image_old_id_arr = $model->detailImageId;
$cover_image_old_id_str = $model->image;
$detail_image_old_id_str = $model->detailImageId;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//商品封面图和商品详情图上传保存处理
$cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_cover_image_res = $goods_manager->saveFile(explode(',', $cover_image_id_str), $model, explode(',', $cover_image_old_id_arr));
$save_detail_image_res = $goods_manager->saveFile(explode(',', $detail_image_id_str), $model, explode(',', $detail_image_old_id_arr), File::OWN_TYPE_GOODS_DETAILS);
if($save_cover_image_res['status'] && $save_detail_image_res['status'] && $save_cover_image_res['first_file_id'] !== 0){
$model->image = $save_cover_image_res['first_file_id'];
$model->save();
$res = $goods_manager->updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
if ($res['status']) {
return $this->redirect('index');
}
return $this->redirect('index');
}
$attributeModel = $goods_manager->getAttribute($id);
$checkAttr = $goods_manager->getSkuInfo($id);
return $this->render('update', [
'model' => $model,
'attributeModel' => $attributeModel,
'attrValue' => $checkAttr,
]);
}
@ -264,9 +249,14 @@ class GoodsController extends Controller
return false;
}
$rule_verify = Yii::$app->request->get('ruleverify');
$file_id_str = Yii::$app->request->get('fileidstr');
$file_id_arr = explode(',', $file_id_str);
$data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all();
if ($rule_verify == 1) {
$data = \common\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all();
} else {
$data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all();
}
$res = array();
if($data) {
$i = 0;

195
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -1,8 +1,12 @@
<?php
namespace antgoods\goods\logic\goods;
use common\models\ars\File;
use Yii;
use yii\base\Exception;
use antgoods\goods\models\ars\GoodsAttr;
use antgoods\goods\models\ars\Attribute;
use antgoods\goods\models\ars\GoodsSku;
class GoodsManager
{
@ -48,4 +52,191 @@ class GoodsManager
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId];
}
/**
* @param $data
* @param $model
* @param null $coverImageOldIdStr
* @param null $detailImageOldIdStr
* @return array
* @throws \Throwable
* 创建修改商品操作
*/
public function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
{
$attribute = $data['attribute'];
$tra = Yii::$app->db->beginTransaction();
try {
if (!$model->save()) {
throw new Exception('');
}
$saveCoverImageRes = $this->saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr));
$saveDetailImageRes = $this->saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS);
if ($saveCoverImageRes['status'] && $saveDetailImageRes['status']) {
if($saveCoverImageRes['first_file_id'] !== 0) {
$model->image = $saveCoverImageRes['first_file_id'];
if (!$model->save()) {
throw new Exception('图片保存失败');
}
}
} else {
throw new Exception('图片保存失败');
}
$this->addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
$tra->commit();
return ['status' => true];
} catch (\yii\base\Exception $e) {
$tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
/**
* @param $data
* @return bool
* @throws Exception
* 创建修改商品属性操作
*/
public function addAttributeOperating($data)
{
if (!$data['attribute']) {
return true;
}
$data['attribute'] = json_decode($data['attribute'], true);
$oldAttr = [];
$goodsAttr = GoodsAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => GoodsAttr::IS_DELETE_NO])->all();
if ($goodsAttr) { //如果商品有旧的属性
if(count($data['attribute']) == 0 && is_array($data['attribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
$this->delAttribute($goodsAttr);
return true;
}
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
$oldAttr[$value->id] = $value->attr_value;
}
}
$newAttr = $this->addAttribute($data['attribute'], $data['id']); //添加新的商品属性
$delAttr = array_diff(array_keys($oldAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
if (!$delAttr) {
return true;
}
foreach ($delAttr as $value) {
$model = GoodsAttr::find()->where(['id' => $value, 'is_delete' => GoodsAttr::IS_DELETE_NO])->One();
if ($model) {
$model->is_delete = GoodsAttr::IS_DELETE_YES;
if (!$model->save()) {
throw new Exception('goodsAttribute delete false');
}
}
}
}
/**
* @param $goodsAttr
* @throws Exception
* 删除商品属性
*/
public function delAttribute($goodsAttr)
{
foreach ($goodsAttr as $key => $value) {
$value->is_delete = GoodsAttr::IS_DELETE_YES;
if (!$value->save()) {
throw new Exception('goods attribute delete false');
}
}
}
/**
* @param $attribute
* @param $goodsId
* @return array
* @throws Exception
* 保存商品属性
*/
public function addAttribute($attribute, $goodsId)
{
$newAttr = [];
if (!$attribute) {
return [];
}
foreach ($attribute as $value) {
foreach ($value['value'] as $k => $v) {
$goodsAttrModel = new GoodsAttr();
$goodsAttrModel->attr_id = $value['id'];
$attr = GoodsAttr::find()->where(['goods_id' => $goodsId, 'attr_id' => $value['id'], 'attr_value' => $v, 'is_delete' => GoodsAttr::IS_DELETE_NO])->one();
if ($attr) {
$newAttr[$attr->id] = $attr->attr_value; //原有的数据
} else {
$goodsAttrModel->goods_id = $goodsId;
$goodsAttrModel->attr_value = $v;
if (!$goodsAttrModel->save()) {
throw new Exception('goodsAttribute save false');
}
$newAttr[$goodsAttrModel->id] = $goodsAttrModel->attr_value; //新增的数据
}
}
}
return $newAttr;
}
/**
* @param $id
* @return Attribute|array|null
* 获取属性信息
*/
public function getAttribute($id)
{
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = [];
$goodsAttributeModel = [];
if (!$goodsAttributes) {
return $goodsAttributeModel;
}
foreach ($goodsAttributes 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;
} else {
foreach ($goodsAttributeModel as $k => $v) {
if ($v['name'] == $attribute->name) {
$goodsAttributeModel[$k]['value'][] = $value->attr_value;
}
}
}
}
return $goodsAttributeModel;
}
/**
* @param $id
* @return array
* 获取sku信息
*/
public function getSkuInfo($id)
{
$skus = GoodsSku::find()->where(['goods_id' => $id, 'is_delete' => GoodsSku::IS_DELETE_NO])->all();
$attrId = [];
foreach ($skus as $sku) {
$attrId = array_merge(explode(',', $sku->goods_attr), $attrId);
}
$attrs = GoodsAttr::find()->where(['id' => $attrId, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$checkAttr = [];
$filterAttr = [];
foreach ($attrs as $value) {
$attr = Attribute::findOne(['id' => $value->attr_id, 'is_delete' => Attribute::IS_DELETE_NO]);
if (!in_array($attr->name, $filterAttr)) {
$filterAttr[] = $attr->name;
$newAttr = ['id' => $attr->id, 'name' => $attr->name, 'value' => [$value->attr_value]];
$checkAttr[] = $newAttr;
} else {
foreach ($checkAttr as $key => $item) {
if ($item['name'] == $attr->name) { //如果attr_id与$filter的attr_id相符,入栈
$checkAttr[$key]['value'][] = $value->attr_value;
}
}
}
}
return $checkAttr;
}
}

1
vendor/antgoods/goods/src/models/ars/Goods.php

@ -60,6 +60,7 @@ class Goods extends \yii\db\ActiveRecord
self::IS_SALE_NO => '否',
self::IS_SALE_YES => '是'
];
public $ruleVerify = 0;//规则验证是否通过
/**
* {@inheritdoc}
*/

3
vendor/antgoods/goods/src/models/ars/GoodsAttr.php

@ -18,6 +18,9 @@ use yii\behaviors\TimestampBehavior;
*/
class GoodsAttr extends \yii\db\ActiveRecord
{
//是否删除is_delete
const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除
/**
* {@inheritdoc}
*/

3
vendor/antgoods/goods/src/models/ars/GoodsSku.php

@ -31,6 +31,9 @@ use yii\behaviors\TimestampBehavior;
*/
class GoodsSku extends \yii\db\ActiveRecord
{
//是否删除is_delete
const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除
/**
* {@inheritdoc}
*/

Loading…
Cancel
Save