diff --git a/backend/modules/file/logic/file/FileManager.php b/backend/modules/file/logic/file/FileManager.php index ba555a7..35edc15 100755 --- a/backend/modules/file/logic/file/FileManager.php +++ b/backend/modules/file/logic/file/FileManager.php @@ -5,10 +5,13 @@ namespace backend\modules\file\logic\file; use backend\modules\file\models\ars\File; use backend\modules\file\models\ars\TemFile; +use yii\web\HttpException; +use yii; class FileManager { //数据表ats_file和ats_tem_file的类型字段type + const TYPE_NONE = 0;//不存在 const TYPE_IMAGE = 1;//图片 const TYPE_VIDEO = 2;//影视 const TYPE_EXCEL = 3;//excel表单 @@ -24,10 +27,9 @@ class FileManager ]; /** - * @param $array * @param $keyword - * @return array - * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回false + * @return int|string + * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回self::TYPE_NONE */ public function searchType($keyword) { @@ -36,40 +38,45 @@ class FileManager return $key; } } - return 0; + return self::TYPE_NONE; } /** - * @param $temFIleIdArr + * @param $temFileIdArr * @param $ownId * @param $ownType - * @return array + * @return array|bool + * @throws \Exception * 根据临时文件id将临时文件保存在文件中 */ - public function saveTemFileToFile($temFIleIdArr, $ownId, $ownType) + public function saveTemFileToFile($temFileIdArr, $ownId, $ownType) { - if(!$temFIleIdArr || !$ownId) { - return ['status' => false, 'info' => '参数错误']; + if(empty($temFileIdArr) || !$ownId) { + return false; } - $firstFileId = 0; - foreach ($temFIleIdArr as $key => $value) { - $temFile = TemFile::findOne($value); + $tra = Yii::$app->db->beginTransaction(); + try { + $firstFileId = 0; + foreach ($temFileIdArr as $key => $value) { + $temFile = TemFile::findOne($value); - if(!$temFile) { - return ['status' => false, 'info' => '存在查找不到的文件']; - } + if (!$temFile) { + throw new \Exception('存在查找不到的文件'); + } - $res = self::saveNewFile($temFile, $ownId, $ownType); - if(!$res['status']) { - return ['status' => false, 'info' => '存在文件保存失败']; - } - if($key == 0) { - $firstFileId = $res['file_id']; + $res = self::saveNewFile($temFile, $ownId, $ownType); + if ($key == 0) { + $firstFileId = $res['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()); + } } /** @@ -77,6 +84,7 @@ class FileManager * @param $ownId * @param $ownType * @return array + * @throws HttpException * 创建新的文件 */ private function saveNewFile($temFile, $ownId, $ownType) @@ -91,13 +99,14 @@ class FileManager if($newFile->save()) { return ['status' => true, 'info' => '操作成功', 'file_id' => $newFile->id]; } else { - return ['status' => false, 'info' => '操作失败']; + throw new HttpException('500', 'File保存失败'); } } /** * @param $fileIdArr - * @return array + * @return bool + * @throws HttpException * 删除file表中的文件 */ public function deleteFile($fileIdArr) @@ -107,12 +116,12 @@ class FileManager $fileModel = File::findOne($value); if($fileModel){ $fileModel->is_delete = File::IS_DELETE_YES; - if($fileModel->save()){ - return ['status' => false, 'info' => '操作失败']; + if(!$fileModel->save()){ + throw new HttpException('500', '文件删除失败'); } } } } - return ['status' => true, 'info' => '操作成功']; + return true; } } \ No newline at end of file diff --git a/backend/modules/goods/controllers/GoodsController.php b/backend/modules/goods/controllers/GoodsController.php index 26b63ec..9284c30 100755 --- a/backend/modules/goods/controllers/GoodsController.php +++ b/backend/modules/goods/controllers/GoodsController.php @@ -11,6 +11,7 @@ use Yii; use backend\modules\goods\models\ars\Goods; use backend\modules\goods\models\searchs\GoodsSearch; use yii\web\Controller; +use yii\web\HttpException; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use backend\modules\goods\logic\goods\GoodsManager; @@ -100,7 +101,9 @@ class GoodsController extends Controller $model->is_express = Goods::IS_EXPRESS_YES; $model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLAGE; if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $model->uniform_postage *= 100; + if ($model->uniform_postage) { + $model->uniform_postage *= 100; + } //商品封面图和商品详情图上传保存处理 $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model); if ($res['status']) { @@ -130,7 +133,9 @@ class GoodsController extends Controller $cover_image_old_id_str = $model->image; $detail_image_old_id_str = $model->detailImageId; if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $model->uniform_postage *= 100; + if ($model->uniform_postage) { + $model->uniform_postage *= 100; + } //商品封面图和商品详情图上传保存处理 $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str); if ($res['status']) { diff --git a/backend/modules/goods/logic/goods/GoodsManager.php b/backend/modules/goods/logic/goods/GoodsManager.php index ce534c8..eb0de2f 100755 --- a/backend/modules/goods/logic/goods/GoodsManager.php +++ b/backend/modules/goods/logic/goods/GoodsManager.php @@ -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) {