Browse Source

将逻辑工具类方法修改委静态方法

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
1b86300b92
  1. 24
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php

24
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -18,7 +18,7 @@ class GoodsManager
* @return array * @return array
* 保存新文件,删除不需要的文件操作 * 保存新文件,删除不需要的文件操作
*/ */
public function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1)
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1)
{ {
//判断类名是否存在 //判断类名是否存在
if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) { if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) {
@ -62,7 +62,7 @@ class GoodsManager
* @throws \Throwable * @throws \Throwable
* 创建修改商品操作 * 创建修改商品操作
*/ */
public function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
{ {
$attribute = $data['attribute']; $attribute = $data['attribute'];
$tra = Yii::$app->db->beginTransaction(); $tra = Yii::$app->db->beginTransaction();
@ -70,8 +70,8 @@ class GoodsManager
if (!$model->save()) { if (!$model->save()) {
throw new Exception(''); 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);
$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['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'];
@ -82,7 +82,7 @@ class GoodsManager
} else { } else {
throw new Exception('图片保存失败'); throw new Exception('图片保存失败');
} }
$this->addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
$tra->commit(); $tra->commit();
return ['status' => true]; return ['status' => true];
} catch (\yii\base\Exception $e) { } catch (\yii\base\Exception $e) {
@ -97,7 +97,7 @@ class GoodsManager
* @throws Exception * @throws Exception
* 创建修改商品属性操作 * 创建修改商品属性操作
*/ */
public function addAttributeOperating($data)
public static function addAttributeOperating($data)
{ {
if (!$data['attribute']) { if (!$data['attribute']) {
return true; return true;
@ -107,14 +107,14 @@ class GoodsManager
$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'])) { //如果传上来的是空数组,删除该商品下的全部属性
$this->delAttribute($goodsAttr);
self::delAttribute($goodsAttr);
return true; return true;
} }
foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组 foreach ($goodsAttr as $key => $value) { //把旧的商品属性保存到一个数组
$oldAttr[$value->id] = $value->attr_value; $oldAttr[$value->id] = $value->attr_value;
} }
} }
$newAttr = $this->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) {
return true; return true;
@ -135,7 +135,7 @@ class GoodsManager
* @throws Exception * @throws Exception
* 删除商品属性 * 删除商品属性
*/ */
public function delAttribute($goodsAttr)
public 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;
@ -152,7 +152,7 @@ class GoodsManager
* @throws Exception * @throws Exception
* 保存商品属性 * 保存商品属性
*/ */
public function addAttribute($attribute, $goodsId)
public static function addAttribute($attribute, $goodsId)
{ {
$newAttr = []; $newAttr = [];
if (!$attribute) { if (!$attribute) {
@ -183,7 +183,7 @@ class GoodsManager
* @return Attribute|array|null * @return Attribute|array|null
* 获取属性信息 * 获取属性信息
*/ */
public function getAttribute($id)
public static function getAttribute($id)
{ {
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all(); $goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filter = []; $filter = [];
@ -213,7 +213,7 @@ class GoodsManager
* @return array * @return array
* 获取sku信息 * 获取sku信息
*/ */
public function getSkuInfo($id)
public static function getSkuInfo($id)
{ {
$skus = GoodsSku::find()->where(['goods_id' => $id, 'is_delete' => GoodsSku::IS_DELETE_NO])->all(); $skus = GoodsSku::find()->where(['goods_id' => $id, 'is_delete' => GoodsSku::IS_DELETE_NO])->all();
$attrId = []; $attrId = [];

Loading…
Cancel
Save