Browse Source

修改含义不明的字段,优化代码结构

antshop
linyaostalker 5 years ago
parent
commit
3024d7ee72
  1. 30
      backend/modules/goods/logic/goods/GoodsManager.php

30
backend/modules/goods/logic/goods/GoodsManager.php

@ -13,19 +13,20 @@ use backend\modules\goods\models\ars\FilterAttr;
use yii\db\StaleObjectException;
use backend\modules\file\logic\file\FileManager;
use yii\web\ServerErrorHttpException;
use backend\modules\shop\logic\ShopManager;
class GoodsManager
{
/**
* @param $newFileIdArr
* @param $goodsModel
* @param $hostObject
* @param array $oldFileIdArr
* @param int $fileType
* @return array
* @throws \Exception
* 保存新文件,删除不需要的文件操作
*/
public static function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $fileType = 1)
public static function saveFile($newFileIdArr, $hostObject, $oldFileIdArr = [], $fileType = File::OWN_TYPE_GOODS_INDEX)
{
$tra = Yii::$app->db->beginTransaction();
try {
@ -34,7 +35,7 @@ class GoodsManager
//创建文件
$class = new FileManager();
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType);
$createFileRes = $class->saveTemFileToFile($createFileIdArr, $hostObject->id, $fileType);
//需要删除的文件id
$delFileIdArr = array_diff($oldFileIdArr, $newFileIdArr);
@ -63,21 +64,20 @@ class GoodsManager
}
/**
* @param $data
* @param $formData
* @param Goods|$model
* @param null $coverImageOldIdStr
* @param null $detailImageOldIdStr
* @return array
* @throws \Exception
* 创建修改商品操作
* @return bool
* @throws \Exception 创建修改商品操作
*/
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
public static function updateGoods($formData, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
{
$attribute = $data['attribute'];
$filterAttribute = $data['filterattribute'];
$model->uniform_postage *= 100;
$model->market_price *= 100;
$model->price *= 100;
$attribute = $formData['attribute'];
$filterAttribute = $formData['filterattribute'];
$model->uniform_postage *= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$model->market_price *= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$model->price *= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$tra = Yii::$app->db->beginTransaction();
try {
if (!$model->save()) {
@ -85,7 +85,7 @@ class GoodsManager
}
$saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr), File::OWN_TYPE_GOODS_INDEX);
self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS);
if($saveCoverImageRes['first_file_id'] !== 0) {
if($saveCoverImageRes['first_file_id'] !== 0) { //保存商品首页
$model->image = $saveCoverImageRes['first_file_id'];
if (!$model->save()) {
throw new \Exception('图片保存失败');
@ -94,7 +94,7 @@ class GoodsManager
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]);
$tra->commit();
return ['status' => true];
return true;
} catch (\yii\base\Exception $e) {
$tra->rollBack();
throw new \Exception($e->getMessage());

Loading…
Cancel
Save