CategoryController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public function actionList()
  31. {
  32. $get = Yii::$app->request->get();
  33. $list = CategoryService::getCategoryList($this->mainId);
  34. util::success($list);
  35. }
  36. //查看详情 ssh 2019.12.8
  37. public function actionDetail()
  38. {
  39. $id = Yii::$app->request->get('categoryId', 0);
  40. $category = CategoryService::getById($id);
  41. CategoryService::valid($category, $this->mainId);
  42. $info = CategoryService::getInfo($id);
  43. util::success($info);
  44. }
  45. //添加分类 ssh 2019.12.8
  46. public function actionAdd()
  47. {
  48. $post = Yii::$app->request->post();
  49. $post['sjId'] = $this->sjId;
  50. $post['mainId'] = $this->mainId;
  51. $cat = CategoryService::addCategory($post);
  52. util::success(['id' => $cat['id']]);
  53. }
  54. //更新分类 ssh 20220404
  55. public function actionUpdate()
  56. {
  57. $post = Yii::$app->request->post();
  58. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : 0;
  59. $category = CategoryService::getById($categoryId);
  60. CategoryService::valid($category, $this->mainId);
  61. CategoryService::updateCategory($category, $post);
  62. util::complete('修改成功');
  63. }
  64. //分类启用停用 ssh 2019.12.8
  65. public function actionChangeStatus()
  66. {
  67. $get = Yii::$app->request->get();
  68. $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
  69. $status = isset($get['status']) ? $get['status'] : 0;
  70. $category = CategoryService::getById($categoryId);
  71. CategoryService::valid($category, $this->mainId);
  72. CategoryService::changeStatus($categoryId, $status);
  73. util::complete();
  74. }
  75. //删除分类 ssh 2019.12.28
  76. public function actionDelete()
  77. {
  78. $get = Yii::$app->request->get();
  79. $categoryId = isset($get['categoryId']) ? $get['categoryId'] : 0;
  80. $category = CategoryClass::getById($categoryId);
  81. util::validMainId($category, $this->mainId);
  82. if ($category['goodsNum'] > 0) {
  83. $goodsCateObjs = GoodsCategoryClass::getAllByCondition(['mainId' => $this->mainId, 'cId' => $categoryId]);
  84. //检查是否有默认分类,没有默认分类创建一个
  85. $defaultCategory = CategoryClass::getByCondition(['mainId' => $this->mainId, 'default' => 1]);
  86. if ($defaultCategory == null) {
  87. $data = [
  88. 'mainId' => $this->mainId,
  89. 'sjId' => $this->sjId,
  90. 'categoryName' => '默认分类',
  91. 'default' => 1
  92. ];
  93. $defaultCategory = CategoryClass::addCategory($data);
  94. }
  95. $defaultCategoryId = $defaultCategory['id'];
  96. foreach ($goodsCateObjs as $gcObj) {
  97. // 先判断商品是否也在其它分类下
  98. $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId' => $this->mainId, 'gId' => $gcObj['gId']]);
  99. if ($goodsOtherCateCount == 1) {
  100. // 如果商品仅在此分类下,则归到默认分类
  101. GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId, 'delStatus' => 0]);
  102. } else {
  103. // 如果商品还在其它分类下,则直接删除此商品的当前分类
  104. GoodsCategoryClass::delGoodsCategory($this->mainId, $categoryId, $gcObj['gId']);
  105. }
  106. }
  107. }
  108. $count = CategoryClass::deleteCategory($categoryId);
  109. if (!$count) {
  110. util::fail('删除失败');
  111. }
  112. util::complete('删除成功');
  113. }
  114. //取分类下商品 ssh 20220406
  115. public function actionGoodsList()
  116. {
  117. $get = Yii::$app->request->get();
  118. $shop = $this->shop;
  119. $custom = $this->custom;
  120. $data = GoodsCategoryClass::getGoodsList($get, $shop, $custom);
  121. util::success($data);
  122. }
  123. //分类排序
  124. public function actionSort()
  125. {
  126. $post = Yii::$app->request->post();
  127. $items = $post['items'];
  128. //验证商品
  129. // 提取所有商品ID
  130. $categoryIds = array_map(function ($item) {
  131. return intval($item['id']);
  132. }, $items);
  133. // 批量获取商品信息
  134. $goodsInfos = CategoryClass::getByIds($categoryIds);
  135. // 批量验证商品
  136. foreach ($goodsInfos as $info) {
  137. CategoryService::valid($info, $this->mainId);
  138. }
  139. // 更新商品排序
  140. foreach ($items as $item) {
  141. $id = intval($item['id']);
  142. $inTurn = intval($item['inTurn']);
  143. CategoryClass::updateById($id, ['inTurn' => $inTurn]);
  144. }
  145. util::complete('排序成功');
  146. }
  147. //获取分类 ssh 2019.12.2
  148. public function actionMallList()
  149. {
  150. $return = CategoryService::getEnableList($this->sjId);
  151. util::success($return);
  152. }
  153. }