소스 검색

数据格式更新

shish 6 년 전
부모
커밋
779ff58653
3개의 변경된 파일58개의 추가작업 그리고 32개의 파일을 삭제
  1. 0 1
      app/business/controllers/GoodsController.php
  2. 4 28
      biz/goods/classes/GoodsClass.php
  3. 54 3
      biz/goods/classes/GoodsStyleClass.php

+ 0 - 1
app/business/controllers/GoodsController.php

@@ -65,7 +65,6 @@ class GoodsController extends BaseController
 		$id = isset($data['id']) ? $data['id'] : 0;
 		$info = GoodsService::getById($id);
 		GoodsService::valid($info, $this->merchantId);
-		echo 'ok';die;
 		$data['merchantId'] = $this->merchantId;
 		GoodsService::updateGoods($id, $data);
 		util::ok();

+ 4 - 28
biz/goods/classes/GoodsClass.php

@@ -105,7 +105,7 @@ class GoodsClass extends BaseClass
 		}
 		$goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
 		unset($data['goodsStyleList']);
-		
+
 		//处理分类
 		$merchantId = $data['merchantId'];
 		$merchantHasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
@@ -113,43 +113,19 @@ class GoodsClass extends BaseClass
 		if (!empty($diff)) {
 			util::fail('您使用了别人的分类');
 		}
-		//先删除分类
+		//先删除再添加分类
 		GoodsCategoryClass::delGoodsCategory($id);
 		$addCategory = [];
 		foreach ($categoryIdList as $categoryId) {
 			$addCategory[] = ['gId' => $id, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
 		}
-		//再添加分类
 		GoodsCategoryClass::batchAdd($addCategory);
 		
 		//处理款式
 		if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
-			$goodsStyleList = [
-				'add' => [['styleName' => '红色', 'price' => 0.1, 'picture' => ''], ['styleName' => '蓝色', 'price' => 0.1, 'picture' => '']],
-				'delete' => [1, 2, 3],
-				'update' => [['id' => 4, 'styleName' => '红色', 'price' => 0.1, 'picture' => ''], ['id' => 5, 'styleName' => '蓝色', 'price' => 0.1, 'picture' => '']],
-			];
-			if (isset($goodsStyleList['add']) && !empty($goodsStyleList['add'])) {
-				$addStyleList = $goodsStyleList['add'];
-				$addStyle = [];
-				foreach ($addStyleList as $style) {
-					$addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $id, 'picture' => $style['picture'], 'price' => $style['price']];
-				}
-				GoodsStyleClass::batchAdd($addStyle);
-			}
-			if (isset($goodsStyleList['delete']) && !empty($goodsStyleList['delete'])) {
-				GoodsStyleClass::deleteByIds($goodsStyleList['delete']);
-			}
-			if (isset($goodsStyleList['update']) && !empty($goodsStyleList['update'])) {
-				$update = $goodsStyleList['update'];
-				foreach ($update as $up) {
-					$currentId = $up['id'];
-					unset($up['id']);
-					GoodsStyleClass::updateById($currentId, $up);
-				}
-			}
+			GoodsStyleClass::updateGoodsStyle($goodsStyleList,$id);
 		}
-		
+
 		$data['shopImg'] = json_encode($data['shopImg']);
 		self::updateById($id, $data);
 	}

+ 54 - 3
biz/goods/classes/GoodsStyleClass.php

@@ -1,13 +1,14 @@
 <?php
 
 namespace biz\goods\classes;
+
 use common\components\business;
 use Yii;
 use biz\base\classes\BaseClass;
 
 class GoodsStyleClass extends BaseClass
 {
-
+	
 	public static $baseFile = '\biz\goods\models\GoodsStyle';
 	
 	
@@ -21,16 +22,66 @@ class GoodsStyleClass extends BaseClass
 		}
 		return $list;
 	}
-
+	
 	//取商品的款式和价格
 	public static function getStyleList($id)
 	{
-		$list = self::getAllByCondition(['goodsId' => $id], 'inTurn DESC', '*','id');
+		$list = self::getAllByCondition(['goodsId' => $id], 'inTurn DESC', '*', 'id');
 		if (empty($list)) {
 			return $list;
 		}
 		$new = self::groupStyleInfo($list);
 		return $new;
 	}
+	
+	/**
+	更新商品的款式 shish 2020.2.27
+	输入的数据格式 $goodsStyleList = [
+	['styleName' => '红色1', 'price' => 0.1, 'picture' => '', 'action' => 'add', 'id' => 0],
+	['styleName' => '蓝色', 'price' => 0.1, 'picture' => '', 'action' => 'update', 'id' => 1],
+	['styleName' => '黑色2', 'price' => 0.1, 'picture' => '', 'action' => 'delete', 'id' => 2]
+	];
+	 */
+	public function updateGoodsStyle($goodsStyleList, $goodsId)
+	{
+		$add = [];
+		$update = [];
+		$delete = [];
+		foreach ($goodsStyleList as $goodsStyle) {
+			$action = $goodsStyle['action'];
+			$id = $goodsStyle['id'];
+			switch ($action) {
+				case 'add':
+					unset($goodsStyle['id']);
+					$add[] = $goodsStyle;
+					break;
+				case 'update':
+					$update[] = $goodsStyle;
+					break;
+				case 'delete':
+					$delete[] = $id;
+					break;
+				default:
+					$add[] = $goodsStyle;
+			}
+		}
+		if (!empty($add)) {
+			$addStyle = [];
+			foreach ($add as $style) {
+				$addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $goodsId, 'picture' => $style['picture'], 'price' => $style['price']];
+			}
+			GoodsStyleClass::batchAdd($addStyle);
+		}
+		if (!empty($delete)) {
+			GoodsStyleClass::deleteByIds($delete);
+		}
+		if (!empty($update)) {
+			foreach ($update as $up) {
+				$currentId = $up['id'];
+				unset($up['id']);
+				self::updateById($currentId, $up);
+			}
+		}
+	}
 
 }