Browse Source

修改优化代码

antshop
linyaostalker 5 years ago
parent
commit
a29c58273e
  1. 51
      backend/modules/file/logic/file/FileManager.php
  2. 5
      backend/modules/goods/controllers/GoodsController.php
  3. 114
      backend/modules/goods/logic/goods/GoodsManager.php

51
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\File;
use backend\modules\file\models\ars\TemFile; use backend\modules\file\models\ars\TemFile;
use yii\web\HttpException;
use yii;
class FileManager class FileManager
{ {
//数据表ats_file和ats_tem_file的类型字段type //数据表ats_file和ats_tem_file的类型字段type
const TYPE_NONE = 0;//不存在
const TYPE_IMAGE = 1;//图片 const TYPE_IMAGE = 1;//图片
const TYPE_VIDEO = 2;//影视 const TYPE_VIDEO = 2;//影视
const TYPE_EXCEL = 3;//excel表单 const TYPE_EXCEL = 3;//excel表单
@ -24,10 +27,9 @@ class FileManager
]; ];
/** /**
* @param $array
* @param $keyword * @param $keyword
* @return array
* 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回false
* @return int|string
* 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回self::TYPE_NONE
*/ */
public function searchType($keyword) public function searchType($keyword)
{ {
@ -36,40 +38,45 @@ class FileManager
return $key; return $key;
} }
} }
return 0;
return self::TYPE_NONE;
} }
/** /**
* @param $temFIleIdArr
* @param $temFileIdArr
* @param $ownId * @param $ownId
* @param $ownType * @param $ownType
* @return array
* @return array|bool
* @throws \Exception
* 根据临时文件id将临时文件保存在文件中 * 根据临时文件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;
} }
$tra = Yii::$app->db->beginTransaction();
try {
$firstFileId = 0; $firstFileId = 0;
foreach ($temFIleIdArr as $key => $value) {
foreach ($temFileIdArr as $key => $value) {
$temFile = TemFile::findOne($value); $temFile = TemFile::findOne($value);
if(!$temFile) {
return ['status' => false, 'info' => '存在查找不到的文件'];
if (!$temFile) {
throw new \Exception('存在查找不到的文件');
} }
$res = self::saveNewFile($temFile, $ownId, $ownType); $res = self::saveNewFile($temFile, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($key == 0) {
if ($key == 0) {
$firstFileId = $res['file_id']; $firstFileId = $res['file_id'];
} }
} }
$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());
}
} }
/** /**
@ -77,6 +84,7 @@ class FileManager
* @param $ownId * @param $ownId
* @param $ownType * @param $ownType
* @return array * @return array
* @throws HttpException
* 创建新的文件 * 创建新的文件
*/ */
private function saveNewFile($temFile, $ownId, $ownType) private function saveNewFile($temFile, $ownId, $ownType)
@ -91,13 +99,14 @@ class FileManager
if($newFile->save()) { if($newFile->save()) {
return ['status' => true, 'info' => '操作成功', 'file_id' => $newFile->id]; return ['status' => true, 'info' => '操作成功', 'file_id' => $newFile->id];
} else { } else {
return ['status' => false, 'info' => '操作失败'];
throw new HttpException('500', 'File保存失败');
} }
} }
/** /**
* @param $fileIdArr * @param $fileIdArr
* @return array
* @return bool
* @throws HttpException
* 删除file表中的文件 * 删除file表中的文件
*/ */
public function deleteFile($fileIdArr) public function deleteFile($fileIdArr)
@ -107,12 +116,12 @@ class FileManager
$fileModel = File::findOne($value); $fileModel = File::findOne($value);
if($fileModel){ if($fileModel){
$fileModel->is_delete = File::IS_DELETE_YES; $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;
} }
} }

5
backend/modules/goods/controllers/GoodsController.php

@ -11,6 +11,7 @@ use Yii;
use backend\modules\goods\models\ars\Goods; use backend\modules\goods\models\ars\Goods;
use backend\modules\goods\models\searchs\GoodsSearch; use backend\modules\goods\models\searchs\GoodsSearch;
use yii\web\Controller; use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\goods\logic\goods\GoodsManager;
@ -100,7 +101,9 @@ class GoodsController extends Controller
$model->is_express = Goods::IS_EXPRESS_YES; $model->is_express = Goods::IS_EXPRESS_YES;
$model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLAGE; $model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLAGE;
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->uniform_postage) {
$model->uniform_postage *= 100; $model->uniform_postage *= 100;
}
//商品封面图和商品详情图上传保存处理 //商品封面图和商品详情图上传保存处理
$res = GoodsManager::updateGoods(Yii::$app->request->post(), $model); $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
if ($res['status']) { if ($res['status']) {
@ -130,7 +133,9 @@ class GoodsController extends Controller
$cover_image_old_id_str = $model->image; $cover_image_old_id_str = $model->image;
$detail_image_old_id_str = $model->detailImageId; $detail_image_old_id_str = $model->detailImageId;
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->uniform_postage) {
$model->uniform_postage *= 100; $model->uniform_postage *= 100;
}
//商品封面图和商品详情图上传保存处理 //商品封面图和商品详情图上传保存处理
$res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str); $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
if ($res['status']) { if ($res['status']) {

114
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\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);
@ -42,13 +46,18 @@ class GoodsManager
//查看修改数组是否为空 //查看修改数组是否为空
if (!$newFileIdArr[0]) { if (!$newFileIdArr[0]) {
$firstFileId = null; $firstFileId = null;
}else {
} else {
if ($createFileRes['status']) { if ($createFileRes['status']) {
$firstFileId = $createFileRes['first_file_id']; $firstFileId = $createFileRes['first_file_id'];
} }
} }
$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,27 +92,31 @@ 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);
$oldAttr = []; $oldAttr = [];
$goodsAttr = GoodsAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => GoodsAttr::IS_DELETE_NO])->all(); $goodsAttr = GoodsAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => GoodsAttr::IS_DELETE_NO])->all();
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)
{ {

Loading…
Cancel
Save