CategoryController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\classes\CategoryClass;
  4. use bizHd\goods\classes\GoodsCategoryClass;
  5. use bizHd\goods\services\CategoryService;
  6. use Yii;
  7. use common\components\util;
  8. class CategoryController extends BaseController
  9. {
  10. public function actionGetMenu()
  11. {
  12. $mainId = $this->mainId;
  13. $classInfo = CategoryClass::getMenuList($mainId);
  14. $index = [];
  15. if (!empty($classInfo)) {
  16. foreach ($classInfo as $class) {
  17. $id = $class['id'] ?? 0;
  18. $index[$id] = $class;
  19. }
  20. }
  21. util::success(['class' => $classInfo, 'classIndex' => $index]);
  22. }
  23. public function actionGetAllCategory()
  24. {
  25. $where = ['mainId' => $this->mainId, 'delStatus' => 0];
  26. $list = CategoryClass::getAllByCondition($where, 'inTurn desc', '*');
  27. util::success(['list' => $list]);
  28. }
  29. /**
  30. * 分类列表
  31. * onlyEnabled=1:仅启用且未删除(门店首页配置关联分类等场景)
  32. */
  33. public function actionList()
  34. {
  35. $get = Yii::$app->request->get();
  36. $onlyEnabled = isset($get['onlyEnabled']) && (int)$get['onlyEnabled'] === 1;
  37. $list = CategoryService::getCategoryList($this->mainId, $onlyEnabled);
  38. util::success($list);
  39. }
  40. //查看详情 ssh 2019.12.8
  41. public function actionDetail()
  42. {
  43. $id = Yii::$app->request->get('categoryId', 0);
  44. $category = CategoryService::getById($id);
  45. CategoryService::valid($category, $this->mainId);
  46. $info = CategoryService::getInfo($id);
  47. util::success($info);
  48. }
  49. //添加分类 ssh 2019.12.8
  50. public function actionAdd()
  51. {
  52. $post = Yii::$app->request->post();
  53. $post['sjId'] = $this->sjId;
  54. $post['mainId'] = $this->mainId;
  55. $cat = CategoryService::addCategory($post);
  56. util::success(['id' => $cat['id']]);
  57. }
  58. //更新分类 ssh 20220404
  59. public function actionUpdate()
  60. {
  61. $post = Yii::$app->request->post();
  62. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : 0;
  63. $category = CategoryService::getById($categoryId);
  64. CategoryService::valid($category, $this->mainId);
  65. CategoryService::updateCategory($category, $post);
  66. util::complete('修改成功');
  67. }
  68. //分类启用停用 ssh 2019.12.8
  69. public function actionChangeStatus()
  70. {
  71. $get = Yii::$app->request->get();
  72. $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
  73. $status = isset($get['status']) ? $get['status'] : 0;
  74. $category = CategoryService::getById($categoryId);
  75. CategoryService::valid($category, $this->mainId);
  76. CategoryService::changeStatus($categoryId, $status);
  77. util::complete();
  78. }
  79. //删除分类 ssh 2019.12.28
  80. public function actionDelete()
  81. {
  82. $get = Yii::$app->request->get();
  83. $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
  84. $category = CategoryClass::getById($categoryId);
  85. util::validMainId($category, $this->mainId);
  86. if ($category['goodsNum'] > 0) {
  87. $goodsCateObjs = GoodsCategoryClass::getAllByCondition(['mainId' => $this->mainId, 'cId' => $categoryId]);
  88. //检查是否有默认分类,没有默认分类创建一个
  89. $defaultCategory = CategoryClass::getByCondition(['mainId' => $this->mainId, 'default' => 1]);
  90. if ($defaultCategory == null) {
  91. $data = [
  92. 'mainId' => $this->mainId,
  93. 'sjId' => $this->sjId,
  94. 'categoryName' => '默认分类',
  95. 'default' => 1
  96. ];
  97. $defaultCategory = CategoryClass::addCategory($data);
  98. }
  99. $defaultCategoryId = $defaultCategory['id'];
  100. foreach ($goodsCateObjs as $gcObj) {
  101. // 先判断商品是否也在其它分类下
  102. $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId' => $this->mainId, 'gId' => $gcObj['gId']]);
  103. if ($goodsOtherCateCount == 1) {
  104. // 如果商品仅在此分类下,则归到默认分类
  105. GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId]);
  106. } else {
  107. // 如果商品还在其它分类下,则直接删除此商品的当前分类
  108. GoodsCategoryClass::deleteById($gcObj['id']);
  109. }
  110. }
  111. }
  112. $count = CategoryClass::deleteCategory($categoryId);
  113. // if (!$count) {
  114. // util::fail('删除失败');
  115. // }
  116. util::complete('删除成功');
  117. }
  118. //取分类下商品 ssh 20220406
  119. public function actionGoodsList()
  120. {
  121. $get = Yii::$app->request->get();
  122. $shop = $this->shop;
  123. $custom = $this->custom;
  124. $data = GoodsCategoryClass::getGoodsList($get, $shop, $custom);
  125. util::success($data);
  126. }
  127. //分类排序
  128. public function actionSort()
  129. {
  130. $post = Yii::$app->request->post();
  131. $items = $post['items'];
  132. //验证商品
  133. // 提取所有商品ID
  134. $categoryIds = array_map(function ($item) {
  135. return intval($item['id']);
  136. }, $items);
  137. // 批量获取商品信息
  138. $goodsInfos = CategoryClass::getByIds($categoryIds);
  139. // 批量验证商品
  140. foreach ($goodsInfos as $info) {
  141. CategoryService::valid($info, $this->mainId);
  142. }
  143. // 更新商品排序
  144. foreach ($items as $item) {
  145. $id = intval($item['id']);
  146. $inTurn = intval($item['inTurn']);
  147. CategoryClass::updateById($id, ['inTurn' => $inTurn]);
  148. }
  149. util::complete('排序成功');
  150. }
  151. //获取分类 ssh 2019.12.2
  152. public function actionMallList()
  153. {
  154. $return = CategoryService::getEnableList($this->sjId);
  155. util::success($return);
  156. }
  157. }