mainId; $classInfo = CategoryClass::getMenuList($mainId); $index = []; if (!empty($classInfo)) { foreach ($classInfo as $class) { $id = $class['id'] ?? 0; $index[$id] = $class; } } util::success(['class' => $classInfo, 'classIndex' => $index]); } public function actionGetAllCategory() { $where = ['mainId' => $this->mainId, 'delStatus' => 0]; $list = CategoryClass::getAllByCondition($where, 'inTurn desc', '*'); util::success(['list' => $list]); } //分类列表 public function actionList() { $get = Yii::$app->request->get(); $list = CategoryService::getCategoryList($this->mainId); util::success($list); } //查看详情 ssh 2019.12.8 public function actionDetail() { $id = Yii::$app->request->get('categoryId', 0); $category = CategoryService::getById($id); CategoryService::valid($category, $this->mainId); $info = CategoryService::getInfo($id); util::success($info); } //添加分类 ssh 2019.12.8 public function actionAdd() { $post = Yii::$app->request->post(); $post['sjId'] = $this->sjId; $post['mainId'] = $this->mainId; $cat = CategoryService::addCategory($post); util::success(['id' => $cat['id']]); } //更新分类 ssh 20220404 public function actionUpdate() { $post = Yii::$app->request->post(); $categoryId = isset($post['categoryId']) ? $post['categoryId'] : 0; $category = CategoryService::getById($categoryId); CategoryService::valid($category, $this->mainId); CategoryService::updateCategory($category, $post); util::complete('修改成功'); } //分类启用停用 ssh 2019.12.8 public function actionChangeStatus() { $get = Yii::$app->request->get(); $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0; $status = isset($get['status']) ? $get['status'] : 0; $category = CategoryService::getById($categoryId); CategoryService::valid($category, $this->mainId); CategoryService::changeStatus($categoryId, $status); util::complete(); } //删除分类 ssh 2019.12.28 public function actionDelete() { $get = Yii::$app->request->get(); $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0; $category = CategoryClass::getById($categoryId); CategoryService::valid($category, $this->mainId); if ($category['goodsNum'] > 0) { $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 = CategoryClass::deleteCategory($categoryId); if (!$count) { util::fail('删除失败'); } util::complete('删除成功'); } //取分类下商品 ssh 20220406 public function actionGoodsList() { $get = Yii::$app->request->get(); $shop = $this->shop; $custom = $this->custom; $data = GoodsCategoryClass::getGoodsList($get, $shop, $custom); util::success($data); } //分类排序 public function actionSort() { $post = Yii::$app->request->post(); $items = $post['items']; //验证商品 // 提取所有商品ID $categoryIds = array_map(function ($item) { return intval($item['id']); }, $items); // 批量获取商品信息 $goodsInfos = CategoryClass::getByIds($categoryIds); // 批量验证商品 foreach ($goodsInfos as $info) { CategoryService::valid($info, $this->mainId); } // 更新商品排序 foreach ($items as $item) { $id = intval($item['id']); $inTurn = intval($item['inTurn']); CategoryClass::updateById($id, ['inTurn' => $inTurn]); } util::complete('排序成功'); } //获取分类 ssh 2019.12.2 public function actionMallList() { $return = CategoryService::getEnableList($this->sjId); util::success($return); } }