|
|
@ -10,6 +10,7 @@ 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; |
|
|
|
|
|
|
|
class GoodsManager |
|
|
|
{ |
|
|
@ -19,36 +20,44 @@ class GoodsManager |
|
|
|
* @param array $oldFileIdArr |
|
|
|
* @param int $fileType |
|
|
|
* @return array |
|
|
|
* @throws \Exception |
|
|
|
* 保存新文件,删除不需要的文件操作 |
|
|
|
*/ |
|
|
|
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1) |
|
|
|
{ |
|
|
|
//需要新建的文件id
|
|
|
|
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr); |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
//需要新建的文件id
|
|
|
|
$createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr); |
|
|
|
|
|
|
|
//创建文件
|
|
|
|
$class = new \backend\modules\file\logic\file\FileManager(); |
|
|
|
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType); |
|
|
|
//创建文件
|
|
|
|
$class = new \backend\modules\file\logic\file\FileManager(); |
|
|
|
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType); |
|
|
|
|
|
|
|
//需要删除的文件id
|
|
|
|
$delFileIdArr = array_diff($oldFileIdArr, $newFileIdArr); |
|
|
|
//需要删除的文件id
|
|
|
|
$delFileIdArr = array_diff($oldFileIdArr, $newFileIdArr); |
|
|
|
|
|
|
|
//删除文件
|
|
|
|
$class->deleteFile($delFileIdArr); |
|
|
|
//删除文件
|
|
|
|
$class->deleteFile($delFileIdArr); |
|
|
|
|
|
|
|
//记录第一张图片id
|
|
|
|
$firstFileId = 0; |
|
|
|
//记录第一张图片id
|
|
|
|
$firstFileId = 0; |
|
|
|
|
|
|
|
//查看修改数组是否为空
|
|
|
|
if (!$newFileIdArr[0]) { |
|
|
|
$firstFileId = null; |
|
|
|
}else { |
|
|
|
if ($createFileRes['status']) { |
|
|
|
$firstFileId = $createFileRes['first_file_id']; |
|
|
|
//查看修改数组是否为空
|
|
|
|
if (!$newFileIdArr[0]) { |
|
|
|
$firstFileId = null; |
|
|
|
} else { |
|
|
|
if ($createFileRes['status']) { |
|
|
|
$firstFileId = $createFileRes['first_file_id']; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId]; |
|
|
|
$tra->commit(); |
|
|
|
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 $detailImageOldIdStr |
|
|
|
* @return array |
|
|
|
* @throws \Throwable |
|
|
|
* @throws \Exception |
|
|
|
* 创建修改商品操作 |
|
|
|
*/ |
|
|
|
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null) |
|
|
@ -67,19 +76,15 @@ class GoodsManager |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
if (!$model->save()) { |
|
|
|
throw new Exception(''); |
|
|
|
throw new \Exception('商品保存失败'); |
|
|
|
} |
|
|
|
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr)); |
|
|
|
$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) { |
|
|
|
$model->image = $saveCoverImageRes['first_file_id']; |
|
|
|
if (!$model->save()) { |
|
|
|
throw new Exception('图片保存失败'); |
|
|
|
} |
|
|
|
if($saveCoverImageRes['first_file_id'] !== 0) { |
|
|
|
$model->image = $saveCoverImageRes['first_file_id']; |
|
|
|
if (!$model->save()) { |
|
|
|
throw new \Exception('图片保存失败'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
throw new Exception('图片保存失败'); |
|
|
|
} |
|
|
|
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]); |
|
|
|
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]); |
|
|
@ -87,60 +92,71 @@ class GoodsManager |
|
|
|
return ['status' => true]; |
|
|
|
} catch (\yii\base\Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
return ['status' => false, 'info' => $e->getMessage()]; |
|
|
|
throw new \Exception($e->getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $data |
|
|
|
* @return bool |
|
|
|
* @throws Exception |
|
|
|
* @throws \Exception |
|
|
|
* 创建修改商品属性操作 |
|
|
|
*/ |
|
|
|
public static function addAttributeOperating($data) |
|
|
|
private static 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'])) { //如果传上来的是空数组,删除该商品下的全部属性
|
|
|
|
self::delAttribute($goodsAttr); |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
if (!$data['attribute']) { |
|
|
|
$tra->commit(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
|
|
|
|
$oldAttr[$value->id] = $value->attr_value; |
|
|
|
$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'])) { //如果传上来的是空数组,删除该商品下的全部属性
|
|
|
|
self::delAttribute($goodsAttr); |
|
|
|
$tra->commit(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
|
|
|
|
$oldAttr[$value->id] = $value->attr_value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$newAttr = self::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'); |
|
|
|
$newAttr = self::addAttribute($data['attribute'], $data['id']); //添加新的商品属性
|
|
|
|
$delAttr = array_diff(array_keys($oldAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
|
|
|
|
if (!$delAttr) { |
|
|
|
$tra->commit(); |
|
|
|
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'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$tra->commit(); |
|
|
|
return true; |
|
|
|
} catch (\Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
throw new \Exception($e->getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $goodsAttr |
|
|
|
* @throws Exception |
|
|
|
* @throws \Exception |
|
|
|
* 删除商品属性 |
|
|
|
*/ |
|
|
|
public static function delAttribute($goodsAttr) |
|
|
|
private static 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'); |
|
|
|
throw new \Exception('goods attribute delete false'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -149,10 +165,10 @@ class GoodsManager |
|
|
|
* @param $attribute |
|
|
|
* @param $goodsId |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* @throws \Exception |
|
|
|
* 保存商品属性 |
|
|
|
*/ |
|
|
|
public static function addAttribute($attribute, $goodsId) |
|
|
|
private static function addAttribute($attribute, $goodsId) |
|
|
|
{ |
|
|
|
$newAttr = []; |
|
|
|
if (!$attribute) { |
|
|
@ -169,7 +185,7 @@ class GoodsManager |
|
|
|
$goodsAttrModel->goods_id = $goodsId; |
|
|
|
$goodsAttrModel->attr_value = $v; |
|
|
|
if (!$goodsAttrModel->save()) { |
|
|
|
throw new Exception('goodsAttribute save false'); |
|
|
|
throw new \Exception('goodsAttribute save false'); |
|
|
|
} |
|
|
|
$newAttr[$goodsAttrModel->id] = $goodsAttrModel->attr_value; //新增的数据
|
|
|
|
} |
|
|
@ -306,29 +322,18 @@ class GoodsManager |
|
|
|
if ($attribute && $attribute->type == Attribute::TYPE_ATTR) { |
|
|
|
$ret['name'] = $attribute->name; |
|
|
|
$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; |
|
|
|
} |
|
|
|
} |
|
|
|
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 $goodsId |
|
|
@ -356,45 +361,53 @@ class GoodsManager |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $sku |
|
|
|
* @throws \yii\db\Exception |
|
|
|
* @param $type |
|
|
|
* @param $goodsId |
|
|
|
* @throws \Exception |
|
|
|
* 添加或更新sku数据 |
|
|
|
*/ |
|
|
|
public static function AddOrUpdateData($sku, $type, $goodsId) |
|
|
|
{ |
|
|
|
$goodsModel = Goods::findOne($goodsId); |
|
|
|
if ($sku['id'] > 0) { |
|
|
|
$goodsSku = GoodsSku::findOne($sku['id']); |
|
|
|
$attrId = array_filter(explode(',', $goodsSku->goods_attr)); |
|
|
|
$attr = GoodsAttr::findOne($attrId[0]); |
|
|
|
} else { |
|
|
|
$goodsSku = new GoodsSku(); |
|
|
|
$attr = new GoodsAttr(); |
|
|
|
} |
|
|
|
if (!$attr || !$goodsSku || !$goodsModel) { |
|
|
|
throw new \yii\db\Exception('系统异常'); |
|
|
|
} |
|
|
|
if ($type == Goods::SKU_MODE_MANUAL) { |
|
|
|
$attr->attr_value = $sku['value']; |
|
|
|
if (!$attr->save()) { |
|
|
|
throw new \yii\db\Exception('手动属性修改失败'); |
|
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
|
try { |
|
|
|
$goodsModel = Goods::findOne($goodsId); |
|
|
|
if ($sku['id'] > 0) { |
|
|
|
$goodsSku = GoodsSku::findOne($sku['id']); |
|
|
|
$attrId = array_filter(explode(',', $goodsSku->goods_attr)); |
|
|
|
$attr = GoodsAttr::findOne($attrId[0]); |
|
|
|
} else { |
|
|
|
$goodsSku = new GoodsSku(); |
|
|
|
$attr = new GoodsAttr(); |
|
|
|
} |
|
|
|
$goodsSku->goods_attr = (string)$attr->id; |
|
|
|
$goodsSku->is_manaul = 1; |
|
|
|
} else { |
|
|
|
$goodsSku->goods_attr = implode(',', array_filter($sku['value'])); |
|
|
|
} |
|
|
|
$goodsSku->goods_id = $goodsId; |
|
|
|
$goodsSku->price = $sku['price']; |
|
|
|
$goodsSku->stock = $sku['stock']; |
|
|
|
$goodsSku->weight = $sku['weight']; |
|
|
|
$goodsSku->goods_sn = $goodsModel->sn; |
|
|
|
if (!$goodsSku->save()) { |
|
|
|
throw new \yii\db\Exception('保存失败,请检查是否有重复规格'); |
|
|
|
} |
|
|
|
$goods = Goods::findOne($goodsId); |
|
|
|
$goods->sku_mode = $type; |
|
|
|
if (!$goods->save()) { |
|
|
|
throw new \yii\db\Exception('商品sku类型修改失败'); |
|
|
|
if (!$attr || !$goodsSku || !$goodsModel) { |
|
|
|
throw new \Exception('参数错误'); |
|
|
|
} |
|
|
|
if ($type == Goods::SKU_MODE_MANUAL) { |
|
|
|
$attr->attr_value = $sku['value']; |
|
|
|
if (!$attr->save()) { |
|
|
|
throw new \Exception('手动属性修改失败'); |
|
|
|
} |
|
|
|
$goodsSku->goods_attr = (string)$attr->id; |
|
|
|
$goodsSku->is_manaul = 1; |
|
|
|
} else { |
|
|
|
$goodsSku->goods_attr = implode(',', array_filter($sku['value'])); |
|
|
|
} |
|
|
|
$goodsSku->goods_id = $goodsId; |
|
|
|
$goodsSku->price = $sku['price']; |
|
|
|
$goodsSku->stock = $sku['stock']; |
|
|
|
$goodsSku->weight = $sku['weight']; |
|
|
|
$goodsSku->goods_sn = $goodsModel->sn; |
|
|
|
if (!$goodsSku->save()) { |
|
|
|
throw new \Exception('保存失败,请检查是否有重复规格'); |
|
|
|
} |
|
|
|
$goods = Goods::findOne($goodsId); |
|
|
|
$goods->sku_mode = $type; |
|
|
|
if (!$goods->save()) { |
|
|
|
throw new \Exception('商品sku类型修改失败'); |
|
|
|
} |
|
|
|
} catch (\Exception $e) { |
|
|
|
$tra->rollBack(); |
|
|
|
throw new \Exception($e->getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -433,9 +446,10 @@ class GoodsManager |
|
|
|
* @param $data |
|
|
|
* @return bool |
|
|
|
* @throws Exception |
|
|
|
* @throws HttpException |
|
|
|
* 创建修改商品筛选属性操作 |
|
|
|
*/ |
|
|
|
public static function addFilterAttributeOperating($data) |
|
|
|
private static function addFilterAttributeOperating($data) |
|
|
|
{ |
|
|
|
if (!$data['filterAttribute']) { |
|
|
|
return true; |
|
|
@ -462,7 +476,7 @@ class GoodsManager |
|
|
|
if ($model) { |
|
|
|
$model->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
|
if (!$model->save()) { |
|
|
|
throw new Exception('goodsAttribute delete false'); |
|
|
|
throw new \Exception('goodsAttribute delete false'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -470,15 +484,15 @@ class GoodsManager |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $goodsFilterAttr |
|
|
|
* @throws Exception |
|
|
|
* @throws HttpException |
|
|
|
* 删除商品筛选属性 |
|
|
|
*/ |
|
|
|
public static function delFilterAttribute($goodsFilterAttr) |
|
|
|
private static function delFilterAttribute($goodsFilterAttr) |
|
|
|
{ |
|
|
|
foreach ($goodsFilterAttr as $key => $value) { |
|
|
|
$value->is_delete = FilterAttr::IS_DELETE_YES; |
|
|
|
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 $goodsId |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* @throws \Exception |
|
|
|
* 保存商品筛选属性 |
|
|
|
*/ |
|
|
|
public static function addFilterAttribute($attribute, $goodsId) |
|
|
|
private static function addFilterAttribute($attribute, $goodsId) |
|
|
|
{ |
|
|
|
$newAttr = []; |
|
|
|
if (!$attribute) { |
|
|
@ -507,7 +521,7 @@ class GoodsManager |
|
|
|
$goodsFilterAttrModel->goods_id = $goodsId; |
|
|
|
$goodsFilterAttrModel->attr_value = $v; |
|
|
|
if (!$goodsFilterAttrModel->save()) { |
|
|
|
throw new Exception('goodsAttribute save false'); |
|
|
|
throw new \Exception('goodsAttribute save false'); |
|
|
|
} |
|
|
|
$newAttr[$goodsFilterAttrModel->id] = $goodsFilterAttrModel->attr_value; //新增的数据
|
|
|
|
} |
|
|
@ -549,7 +563,7 @@ class GoodsManager |
|
|
|
/** |
|
|
|
* @param $goodsModel |
|
|
|
* @return bool |
|
|
|
* |
|
|
|
* 判断该商品的sku是否存在已选属性,存在则返回true,表示不得删除 |
|
|
|
*/ |
|
|
|
public static function judgeGoodsCategory($goodsModel) |
|
|
|
{ |
|
|
|