|
@ -10,6 +10,7 @@ use backend\modules\goods\models\ars\GoodsSku; |
|
|
use backend\modules\goods\models\ars\Goods; |
|
|
use backend\modules\goods\models\ars\Goods; |
|
|
use backend\modules\goods\models\ars\FilterAttr; |
|
|
use backend\modules\goods\models\ars\FilterAttr; |
|
|
use backend\modules\goods\models\ars\Category; |
|
|
use backend\modules\goods\models\ars\Category; |
|
|
|
|
|
use yii\web\HttpException; |
|
|
|
|
|
|
|
|
class GoodsManager |
|
|
class GoodsManager |
|
|
{ |
|
|
{ |
|
@ -19,10 +20,13 @@ class GoodsManager |
|
|
* @param array $oldFileIdArr |
|
|
* @param array $oldFileIdArr |
|
|
* @param int $fileType |
|
|
* @param int $fileType |
|
|
* @return array |
|
|
* @return array |
|
|
|
|
|
* @throws \Exception |
|
|
* 保存新文件,删除不需要的文件操作 |
|
|
* 保存新文件,删除不需要的文件操作 |
|
|
*/ |
|
|
*/ |
|
|
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1) |
|
|
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1) |
|
|
{ |
|
|
{ |
|
|
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
|
|
try { |
|
|
//需要新建的文件id
|
|
|
//需要新建的文件id
|
|
|
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr); |
|
|
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr); |
|
|
|
|
|
|
|
@ -48,7 +52,12 @@ class GoodsManager |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$tra->commit(); |
|
|
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId]; |
|
|
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId]; |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
$tra->rollBack(); |
|
|
|
|
|
throw new \Exception($e->getMessage()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -57,7 +66,7 @@ class GoodsManager |
|
|
* @param null $coverImageOldIdStr |
|
|
* @param null $coverImageOldIdStr |
|
|
* @param null $detailImageOldIdStr |
|
|
* @param null $detailImageOldIdStr |
|
|
* @return array |
|
|
* @return array |
|
|
* @throws \Throwable |
|
|
|
|
|
|
|
|
* @throws \Exception |
|
|
* 创建修改商品操作 |
|
|
* 创建修改商品操作 |
|
|
*/ |
|
|
*/ |
|
|
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null) |
|
|
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null) |
|
@ -67,19 +76,15 @@ class GoodsManager |
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
try { |
|
|
try { |
|
|
if (!$model->save()) { |
|
|
if (!$model->save()) { |
|
|
throw new Exception(''); |
|
|
|
|
|
|
|
|
throw new \Exception('商品保存失败'); |
|
|
} |
|
|
} |
|
|
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr)); |
|
|
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr)); |
|
|
$saveDetailImageRes = self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS); |
|
|
$saveDetailImageRes = self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS); |
|
|
if ($saveCoverImageRes['status'] && $saveDetailImageRes['status']) { |
|
|
|
|
|
if($saveCoverImageRes['first_file_id'] !== 0) { |
|
|
if($saveCoverImageRes['first_file_id'] !== 0) { |
|
|
$model->image = $saveCoverImageRes['first_file_id']; |
|
|
$model->image = $saveCoverImageRes['first_file_id']; |
|
|
if (!$model->save()) { |
|
|
if (!$model->save()) { |
|
|
throw new Exception('图片保存失败'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
throw new \Exception('图片保存失败'); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
|
|
|
throw new Exception('图片保存失败'); |
|
|
|
|
|
} |
|
|
} |
|
|
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]); |
|
|
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]); |
|
|
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]); |
|
|
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]); |
|
@ -87,19 +92,22 @@ class GoodsManager |
|
|
return ['status' => true]; |
|
|
return ['status' => true]; |
|
|
} catch (\yii\base\Exception $e) { |
|
|
} catch (\yii\base\Exception $e) { |
|
|
$tra->rollBack(); |
|
|
$tra->rollBack(); |
|
|
return ['status' => false, 'info' => $e->getMessage()]; |
|
|
|
|
|
|
|
|
throw new \Exception($e->getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $data |
|
|
* @param $data |
|
|
* @return bool |
|
|
* @return bool |
|
|
* @throws Exception |
|
|
|
|
|
|
|
|
* @throws \Exception |
|
|
* 创建修改商品属性操作 |
|
|
* 创建修改商品属性操作 |
|
|
*/ |
|
|
*/ |
|
|
public static function addAttributeOperating($data) |
|
|
|
|
|
|
|
|
private static function addAttributeOperating($data) |
|
|
{ |
|
|
{ |
|
|
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
|
|
try { |
|
|
if (!$data['attribute']) { |
|
|
if (!$data['attribute']) { |
|
|
|
|
|
$tra->commit(); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
$data['attribute'] = json_decode($data['attribute'], true); |
|
|
$data['attribute'] = json_decode($data['attribute'], true); |
|
@ -108,6 +116,7 @@ class GoodsManager |
|
|
if ($goodsAttr) { //如果商品有旧的属性
|
|
|
if ($goodsAttr) { //如果商品有旧的属性
|
|
|
if (count($data['attribute']) == 0 && is_array($data['attribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
|
|
|
if (count($data['attribute']) == 0 && is_array($data['attribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
|
|
|
self::delAttribute($goodsAttr); |
|
|
self::delAttribute($goodsAttr); |
|
|
|
|
|
$tra->commit(); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
|
|
|
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
|
|
@ -117,6 +126,7 @@ class GoodsManager |
|
|
$newAttr = self::addAttribute($data['attribute'], $data['id']); //添加新的商品属性
|
|
|
$newAttr = self::addAttribute($data['attribute'], $data['id']); //添加新的商品属性
|
|
|
$delAttr = array_diff(array_keys($oldAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
|
|
|
$delAttr = array_diff(array_keys($oldAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
|
|
|
if (!$delAttr) { |
|
|
if (!$delAttr) { |
|
|
|
|
|
$tra->commit(); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
foreach ($delAttr as $value) { |
|
|
foreach ($delAttr as $value) { |
|
@ -124,23 +134,29 @@ class GoodsManager |
|
|
if ($model) { |
|
|
if ($model) { |
|
|
$model->is_delete = GoodsAttr::IS_DELETE_YES; |
|
|
$model->is_delete = GoodsAttr::IS_DELETE_YES; |
|
|
if (!$model->save()) { |
|
|
if (!$model->save()) { |
|
|
throw new Exception('goodsAttribute delete false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goodsAttribute delete false'); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
$tra->commit(); |
|
|
|
|
|
return true; |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
$tra->rollBack(); |
|
|
|
|
|
throw new \Exception($e->getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $goodsAttr |
|
|
* @param $goodsAttr |
|
|
* @throws Exception |
|
|
|
|
|
|
|
|
* @throws \Exception |
|
|
* 删除商品属性 |
|
|
* 删除商品属性 |
|
|
*/ |
|
|
*/ |
|
|
public static function delAttribute($goodsAttr) |
|
|
|
|
|
|
|
|
private static function delAttribute($goodsAttr) |
|
|
{ |
|
|
{ |
|
|
foreach ($goodsAttr as $key => $value) { |
|
|
foreach ($goodsAttr as $key => $value) { |
|
|
$value->is_delete = GoodsAttr::IS_DELETE_YES; |
|
|
$value->is_delete = GoodsAttr::IS_DELETE_YES; |
|
|
if (!$value->save()) { |
|
|
if (!$value->save()) { |
|
|
throw new Exception('goods attribute delete false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goods attribute delete false'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -149,10 +165,10 @@ class GoodsManager |
|
|
* @param $attribute |
|
|
* @param $attribute |
|
|
* @param $goodsId |
|
|
* @param $goodsId |
|
|
* @return array |
|
|
* @return array |
|
|
* @throws Exception |
|
|
|
|
|
|
|
|
* @throws \Exception |
|
|
* 保存商品属性 |
|
|
* 保存商品属性 |
|
|
*/ |
|
|
*/ |
|
|
public static function addAttribute($attribute, $goodsId) |
|
|
|
|
|
|
|
|
private static function addAttribute($attribute, $goodsId) |
|
|
{ |
|
|
{ |
|
|
$newAttr = []; |
|
|
$newAttr = []; |
|
|
if (!$attribute) { |
|
|
if (!$attribute) { |
|
@ -169,7 +185,7 @@ class GoodsManager |
|
|
$goodsAttrModel->goods_id = $goodsId; |
|
|
$goodsAttrModel->goods_id = $goodsId; |
|
|
$goodsAttrModel->attr_value = $v; |
|
|
$goodsAttrModel->attr_value = $v; |
|
|
if (!$goodsAttrModel->save()) { |
|
|
if (!$goodsAttrModel->save()) { |
|
|
throw new Exception('goodsAttribute save false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goodsAttribute save false'); |
|
|
} |
|
|
} |
|
|
$newAttr[$goodsAttrModel->id] = $goodsAttrModel->attr_value; //新增的数据
|
|
|
$newAttr[$goodsAttrModel->id] = $goodsAttrModel->attr_value; //新增的数据
|
|
|
} |
|
|
} |
|
@ -306,29 +322,18 @@ class GoodsManager |
|
|
if ($attribute && $attribute->type == Attribute::TYPE_ATTR) { |
|
|
if ($attribute && $attribute->type == Attribute::TYPE_ATTR) { |
|
|
$ret['name'] = $attribute->name; |
|
|
$ret['name'] = $attribute->name; |
|
|
$ret['id'] = $attribute->id; |
|
|
$ret['id'] = $attribute->id; |
|
|
$ret['attrValue'] = self::getAttrValue($attribute->id, $id); |
|
|
|
|
|
|
|
|
$ret['attrValue'] = GoodsAttr::find() |
|
|
|
|
|
->select(['id', 'attr_value']) |
|
|
|
|
|
->where(['goods_id' => $id]) |
|
|
|
|
|
->andWhere(['attr_id' => $attribute->id]) |
|
|
|
|
|
->asArray() |
|
|
|
|
|
->all(); |
|
|
$attributes[] = $ret; |
|
|
$attributes[] = $ret; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return $attributes; |
|
|
return $attributes; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param $attrId |
|
|
|
|
|
* @param $goodsId |
|
|
|
|
|
* @return GoodsAttr[]|GoodsSku[]|array|File[]|\backend\modules\file\models\ars\TemFile[]|\yii\db\ActiveRecord[] |
|
|
|
|
|
* 获取属性值 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static function getAttrValue($attrId, $goodsId) |
|
|
|
|
|
{ |
|
|
|
|
|
return GoodsAttr::find() |
|
|
|
|
|
->select(['id', 'attr_value']) |
|
|
|
|
|
->where(['goods_id' => $goodsId]) |
|
|
|
|
|
->andWhere(['attr_id' => $attrId]) |
|
|
|
|
|
->asArray() |
|
|
|
|
|
->all(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $type |
|
|
* @param $type |
|
|
* @param $goodsId |
|
|
* @param $goodsId |
|
@ -356,11 +361,15 @@ class GoodsManager |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $sku |
|
|
* @param $sku |
|
|
* @throws \yii\db\Exception |
|
|
|
|
|
|
|
|
* @param $type |
|
|
|
|
|
* @param $goodsId |
|
|
|
|
|
* @throws \Exception |
|
|
* 添加或更新sku数据 |
|
|
* 添加或更新sku数据 |
|
|
*/ |
|
|
*/ |
|
|
public static function AddOrUpdateData($sku, $type, $goodsId) |
|
|
public static function AddOrUpdateData($sku, $type, $goodsId) |
|
|
{ |
|
|
{ |
|
|
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
|
|
try { |
|
|
$goodsModel = Goods::findOne($goodsId); |
|
|
$goodsModel = Goods::findOne($goodsId); |
|
|
if ($sku['id'] > 0) { |
|
|
if ($sku['id'] > 0) { |
|
|
$goodsSku = GoodsSku::findOne($sku['id']); |
|
|
$goodsSku = GoodsSku::findOne($sku['id']); |
|
@ -371,12 +380,12 @@ class GoodsManager |
|
|
$attr = new GoodsAttr(); |
|
|
$attr = new GoodsAttr(); |
|
|
} |
|
|
} |
|
|
if (!$attr || !$goodsSku || !$goodsModel) { |
|
|
if (!$attr || !$goodsSku || !$goodsModel) { |
|
|
throw new \yii\db\Exception('系统异常'); |
|
|
|
|
|
|
|
|
throw new \Exception('参数错误'); |
|
|
} |
|
|
} |
|
|
if ($type == Goods::SKU_MODE_MANUAL) { |
|
|
if ($type == Goods::SKU_MODE_MANUAL) { |
|
|
$attr->attr_value = $sku['value']; |
|
|
$attr->attr_value = $sku['value']; |
|
|
if (!$attr->save()) { |
|
|
if (!$attr->save()) { |
|
|
throw new \yii\db\Exception('手动属性修改失败'); |
|
|
|
|
|
|
|
|
throw new \Exception('手动属性修改失败'); |
|
|
} |
|
|
} |
|
|
$goodsSku->goods_attr = (string)$attr->id; |
|
|
$goodsSku->goods_attr = (string)$attr->id; |
|
|
$goodsSku->is_manaul = 1; |
|
|
$goodsSku->is_manaul = 1; |
|
@ -389,12 +398,16 @@ class GoodsManager |
|
|
$goodsSku->weight = $sku['weight']; |
|
|
$goodsSku->weight = $sku['weight']; |
|
|
$goodsSku->goods_sn = $goodsModel->sn; |
|
|
$goodsSku->goods_sn = $goodsModel->sn; |
|
|
if (!$goodsSku->save()) { |
|
|
if (!$goodsSku->save()) { |
|
|
throw new \yii\db\Exception('保存失败,请检查是否有重复规格'); |
|
|
|
|
|
|
|
|
throw new \Exception('保存失败,请检查是否有重复规格'); |
|
|
} |
|
|
} |
|
|
$goods = Goods::findOne($goodsId); |
|
|
$goods = Goods::findOne($goodsId); |
|
|
$goods->sku_mode = $type; |
|
|
$goods->sku_mode = $type; |
|
|
if (!$goods->save()) { |
|
|
if (!$goods->save()) { |
|
|
throw new \yii\db\Exception('商品sku类型修改失败'); |
|
|
|
|
|
|
|
|
throw new \Exception('商品sku类型修改失败'); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
$tra->rollBack(); |
|
|
|
|
|
throw new \Exception($e->getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -433,9 +446,10 @@ class GoodsManager |
|
|
* @param $data |
|
|
* @param $data |
|
|
* @return bool |
|
|
* @return bool |
|
|
* @throws Exception |
|
|
* @throws Exception |
|
|
|
|
|
* @throws HttpException |
|
|
* 创建修改商品筛选属性操作 |
|
|
* 创建修改商品筛选属性操作 |
|
|
*/ |
|
|
*/ |
|
|
public static function addFilterAttributeOperating($data) |
|
|
|
|
|
|
|
|
private static function addFilterAttributeOperating($data) |
|
|
{ |
|
|
{ |
|
|
if (!$data['filterAttribute']) { |
|
|
if (!$data['filterAttribute']) { |
|
|
return true; |
|
|
return true; |
|
@ -462,7 +476,7 @@ class GoodsManager |
|
|
if ($model) { |
|
|
if ($model) { |
|
|
$model->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
$model->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
if (!$model->save()) { |
|
|
if (!$model->save()) { |
|
|
throw new Exception('goodsAttribute delete false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goodsAttribute delete false'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -470,15 +484,15 @@ class GoodsManager |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $goodsFilterAttr |
|
|
* @param $goodsFilterAttr |
|
|
* @throws Exception |
|
|
|
|
|
|
|
|
* @throws HttpException |
|
|
* 删除商品筛选属性 |
|
|
* 删除商品筛选属性 |
|
|
*/ |
|
|
*/ |
|
|
public static function delFilterAttribute($goodsFilterAttr) |
|
|
|
|
|
|
|
|
private static function delFilterAttribute($goodsFilterAttr) |
|
|
{ |
|
|
{ |
|
|
foreach ($goodsFilterAttr as $key => $value) { |
|
|
foreach ($goodsFilterAttr as $key => $value) { |
|
|
$value->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
$value->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
if (!$value->save()) { |
|
|
if (!$value->save()) { |
|
|
throw new Exception('goods attribute delete false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goods attribute delete false'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -487,10 +501,10 @@ class GoodsManager |
|
|
* @param $attribute |
|
|
* @param $attribute |
|
|
* @param $goodsId |
|
|
* @param $goodsId |
|
|
* @return array |
|
|
* @return array |
|
|
* @throws Exception |
|
|
|
|
|
|
|
|
* @throws \Exception |
|
|
* 保存商品筛选属性 |
|
|
* 保存商品筛选属性 |
|
|
*/ |
|
|
*/ |
|
|
public static function addFilterAttribute($attribute, $goodsId) |
|
|
|
|
|
|
|
|
private static function addFilterAttribute($attribute, $goodsId) |
|
|
{ |
|
|
{ |
|
|
$newAttr = []; |
|
|
$newAttr = []; |
|
|
if (!$attribute) { |
|
|
if (!$attribute) { |
|
@ -507,7 +521,7 @@ class GoodsManager |
|
|
$goodsFilterAttrModel->goods_id = $goodsId; |
|
|
$goodsFilterAttrModel->goods_id = $goodsId; |
|
|
$goodsFilterAttrModel->attr_value = $v; |
|
|
$goodsFilterAttrModel->attr_value = $v; |
|
|
if (!$goodsFilterAttrModel->save()) { |
|
|
if (!$goodsFilterAttrModel->save()) { |
|
|
throw new Exception('goodsAttribute save false'); |
|
|
|
|
|
|
|
|
throw new \Exception('goodsAttribute save false'); |
|
|
} |
|
|
} |
|
|
$newAttr[$goodsFilterAttrModel->id] = $goodsFilterAttrModel->attr_value; //新增的数据
|
|
|
$newAttr[$goodsFilterAttrModel->id] = $goodsFilterAttrModel->attr_value; //新增的数据
|
|
|
} |
|
|
} |
|
@ -549,7 +563,7 @@ class GoodsManager |
|
|
/** |
|
|
/** |
|
|
* @param $goodsModel |
|
|
* @param $goodsModel |
|
|
* @return bool |
|
|
* @return bool |
|
|
* |
|
|
|
|
|
|
|
|
* 判断该商品的sku是否存在已选属性,存在则返回true,表示不得删除 |
|
|
*/ |
|
|
*/ |
|
|
public static function judgeGoodsCategory($goodsModel) |
|
|
public static function judgeGoodsCategory($goodsModel) |
|
|
{ |
|
|
{ |
|
|