CategoryController.php 5.8 KB

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