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