|
|
@@ -3,6 +3,7 @@
|
|
|
namespace hd\controllers;
|
|
|
|
|
|
use bizHd\goods\classes\CategoryClass;
|
|
|
+use bizHd\goods\classes\GoodsCategoryClass;
|
|
|
use bizHd\goods\services\CategoryService;
|
|
|
use bizHd\goods\services\GoodsCategoryService;
|
|
|
use Yii;
|
|
|
@@ -75,12 +76,38 @@ class CategoryController extends BaseController
|
|
|
{
|
|
|
$get = Yii::$app->request->get();
|
|
|
$categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
|
|
|
- $category = CategoryService::getById($categoryId);
|
|
|
+ $category = CategoryClass::getById($categoryId);
|
|
|
CategoryService::valid($category, $this->mainId);
|
|
|
+
|
|
|
if ($category['goodsNum'] > 0) {
|
|
|
- util::fail('分类下还有商品');
|
|
|
+ $goodsCateObjs = GoodsCategoryClass::getAllByCondition(['mainId'=>$this->mainId, 'cId'=>$categoryId]);
|
|
|
+
|
|
|
+ //检查是否有默认分类,没有默认分类创建一个
|
|
|
+ $defaultCategory = CategoryClass::getByCondition(['mainId'=>$this->mainId, 'default'=>1]);
|
|
|
+ if ($defaultCategory == null){
|
|
|
+ $data = [
|
|
|
+ 'mainId' => $this->mainId,
|
|
|
+ 'sjId' => $this->sjId,
|
|
|
+ 'categoryName' => '默认分类',
|
|
|
+ 'default' => 1
|
|
|
+ ];
|
|
|
+ $defaultCategory = CategoryClass::addCategory($data);
|
|
|
+ }
|
|
|
+ $defaultCategoryId = $defaultCategory['id'];
|
|
|
+ foreach ($goodsCateObjs as $gcObj) {
|
|
|
+ // 先判断商品是否也在其它分类下
|
|
|
+ $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId'=>$this->mainId, 'gId'=>$gcObj['gId'], 'delStatus'=>0]);
|
|
|
+ if ($goodsOtherCateCount == 1) {
|
|
|
+ // 如果商品仅在此分类下,则归到默认分类
|
|
|
+ GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId, 'delStatus' => 0]);
|
|
|
+ } else {
|
|
|
+ // 如果商品还在其它分类下,则直接删除此商品的当前分类
|
|
|
+ GoodsCategoryClass::delGoodsCategory($this->mainId, $categoryId, $gcObj['gId']);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- $count = CategoryService::deleteCategory($categoryId);
|
|
|
+
|
|
|
+ $count = CategoryClass::deleteCategory($categoryId);
|
|
|
if (!$count) {
|
|
|
util::fail('删除失败');
|
|
|
}
|
|
|
@@ -101,11 +128,6 @@ class CategoryController extends BaseController
|
|
|
if (is_numeric($flower)) {
|
|
|
$where['flower'] = $flower;
|
|
|
}
|
|
|
- $where['delStatus'] = $get['delStatus'] ?? 0;
|
|
|
- $status = $get['status'] ?? '';
|
|
|
- if (is_numeric($status)) {
|
|
|
- $where['status'] = $status;
|
|
|
- }
|
|
|
$flowerNum = $get['flowerNum'] ?? 0;
|
|
|
if (!empty($flowerNum)) {
|
|
|
$where['flowerNum'] = $flowerNum;
|