|
@ -1,8 +1,12 @@ |
|
|
<?php |
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace antgoods\goods\logic\goods; |
|
|
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 |
|
|
class GoodsManager |
|
|
{ |
|
|
{ |
|
@ -48,4 +52,191 @@ class GoodsManager |
|
|
|
|
|
|
|
|
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId]; |
|
|
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; |
|
|
|
|
|
} |
|
|
} |
|
|
} |