shish пре 6 година
родитељ
комит
3b04fb2d5f

+ 10 - 9
app/business/controllers/GoodsController.php

@@ -12,10 +12,9 @@ use Yii;
 use common\components\util;
 use biz\goods\services\GoodsService;
 
-
 class GoodsController extends BaseController
 {
-
+	
 	//商品列表 shish 2019.12.7
 	public function actionList()
 	{
@@ -23,12 +22,14 @@ class GoodsController extends BaseController
 		$list = GoodsService::getGoodsList($where);
 		util::success($list);
 	}
-
-	//新建商品 shish 2019.12.7
+	
+	//添加商品 shish 2019.12.7
 	public function actionAdd()
 	{
-		$data = Yii::$app->request->get();
-		GoodsService::add($data);
+		$data = Yii::$app->request->post();
+		$data['merchantId'] = $this->merchantId;
+		$info = GoodsService::addGoods($data);
+		util::success($info);
 	}
 
 	//获取商品详情 shish 2019.12.7
@@ -38,7 +39,7 @@ class GoodsController extends BaseController
 		$goods = GoodsService::getGoodsInfo($id);
 		util::success($goods);
 	}
-
+	
 	//更新商品 shish 2019.12.7
 	public function actionUpdate()
 	{
@@ -47,7 +48,7 @@ class GoodsController extends BaseController
 		$re = GoodsService::updateById($id, $data);
 		util::success($re);
 	}
-
+	
 	//删除商品 shish 2019.12.7
 	public function actionDelete()
 	{
@@ -55,5 +56,5 @@ class GoodsController extends BaseController
 		GoodsService::deleteGoods($id, $this->admin);
 		util::success();
 	}
-
+	
 }

+ 11 - 0
biz/goods/classes/CategoryClass.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace biz\goods\classes;
+
 use Yii;
 use biz\base\classes\BaseClass;
 
@@ -8,5 +9,15 @@ class CategoryClass extends BaseClass
 {
 
 	public static $baseFile = '\biz\goods\models\Category';
+
+	//取出商家所有的分类 shish 2019.12.7
+	public static function getCategoryIdList($merchantId)
+	{
+		$category = self::getAllByCondition(['merchantId' => $merchantId], null, '*', 'id');
+		if (empty($category)) {
+			return [];
+		}
+		return array_keys($category);
+	}
 	
 }

+ 42 - 0
biz/goods/classes/GoodsClass.php

@@ -5,6 +5,7 @@ namespace biz\goods\classes;
 use biz\goods\services\GoodsCategoryService;
 use biz\goods\services\GoodsStyleService;
 use common\components\business;
+use common\components\stringUtil;
 use Yii;
 use biz\base\classes\BaseClass;
 
@@ -47,4 +48,45 @@ class GoodsClass extends BaseClass
 		return $list;
 	}
 	
+	public static function addGoods($data)
+	{
+		$categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
+		unset($data['categoryIdList']);
+		if (empty($categoryIdList)) {
+			util::fail('没有选择分类');
+		}
+		$goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
+		unset($data['goodsStyleList']);
+		
+		//用商户id生成商品的编号
+		$data['itemNumber'] = stringUtil::buildRandNo($merchantId);
+		$data['createTime'] = date("Y-m-d H:i:s");
+		$goodsInfo = self::add($data);
+		$id = $goodsInfo['id'];
+		
+		//处理分类
+		$merchantId = $data['merchantId'];
+		$hasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
+		$diff = array_diff($categoryIdList, $hasCategoryIdList);
+		if (!empty($diff)) {
+			util::fail('您使用了别人的分类');
+		}
+		$addCategory = [];
+		foreach ($categoryIdList as $categoryId) {
+			$addCategory[] = ['gId' => $id, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
+		}
+		GoodsCategoryClass::batchAdd($addCategory);
+		
+		//处理款式
+		if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
+			$goodsStyleList = [['id' => 0, 'styleName' => '红色', 'price' => 0.1, 'picture' => '']];
+			$addStyle = [];
+			foreach ($goodsStyleList as $style) {
+				$addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $id, 'picture' => $style['picture'], 'price' => $style['price']];
+			}
+			GoodsStyleClass::batchAdd($addStyle);
+		}
+		return $goodsInfo;
+	}
+
 }

+ 6 - 0
biz/goods/services/CategoryService.php

@@ -27,6 +27,12 @@ class CategoryService extends BaseService
 		}
 		return $cat;
 	}
+
+	//取出商家所有的分类 shish 2019.12.7
+	public static function getCategoryIdList($merchantId)
+	{
+		return CategoryClass::getCategoryIdList($merchantId);
+	}
 	
 	//取出可用的分类 shish 2019.12.2
 	public static function getEnableList()

+ 10 - 4
biz/goods/services/GoodsService.php

@@ -12,9 +12,9 @@ use linslin\yii2\curl;
 
 class GoodsService extends BaseService
 {
-
+	
 	public static $baseFile = '\biz\goods\classes\GoodsClass';
-
+	
 	//后台列表 shish 2019.12.7
 	public static function getGoodsList($where)
 	{
@@ -26,13 +26,13 @@ class GoodsService extends BaseService
 		$data['list'] = GoodsClass::groupGoodsBaseInfo($list);
 		return $data;
 	}
-
+	
 	//获取商品完整详情,包括分类 shish 2019.12.3
 	public static function getGoodsInfo($id)
 	{
 		return GoodsClass::getGoodsInfo($id);
 	}
-
+	
 	//删除商品 shish 2019.12.7
 	public static function deleteGoods($id, $admin)
 	{
@@ -47,4 +47,10 @@ class GoodsService extends BaseService
 		GoodsCategoryService::deleteByCondition(['gId' => $id]);
 	}
 	
+	//添加商品 shish 2019.12.7
+	public static function addGoods($data)
+	{
+		return GoodsClass::addGoods($data);
+	}
+
 }

+ 1 - 0
sql.txt

@@ -9,6 +9,7 @@ ALTER TABLE xhGoods MODIFY `status` TINYINT NOT NULL DEFAULT 1 COMMENT '商品
 ALTER TABLE xhGoods DROP COLUMN `delStatus`;
 ALTER TABLE xhGoodsCategory ADD INDEX `gId`(`gId`);
 ALTER TABLE xhGoods ADD autoRise TINYINT NOT NULL DEFAULT 1 COMMENT '自动涨价 0不需要 1需要' AFTER `goodsStyle`;
+ALTER TABLE `xhGoodsStyle` CHANGE `title` `styleName` VARCHAR(30) NOT NULL DEFAULT '' COMMENT '款式名称';
 
 2019.12.5 shish
 ALTER TABLE xhOrderGoods CHANGE `multipleTitle` `goodsStyleName` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '款式名称';