CategoryController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $goodsCond = [];
  110. $where['mainId'] = $this->mainId;
  111. if (!empty($catId)) {
  112. $where['cId'] = $catId;
  113. }
  114. $flower = $get['flower'] ?? '';
  115. if (is_numeric($flower)) {
  116. $where['flower'] = $flower;
  117. }
  118. // flowerNum 是针对 xhGoods 表的条件查询
  119. $flowerNum = $get['flowerNum'] ?? 0;
  120. if ($flowerNum > 0) {
  121. $goodsCond['flowerNum'] = $flowerNum;
  122. }
  123. if (isset($get['lookPrice'])) { // 1:有价格 2:无价格 -- 根据前端的传值
  124. $lookPrice = intval($get['lookPrice']);
  125. if ($lookPrice == 1) {
  126. $goodsCond['priceType'] = 1;
  127. }
  128. if ($lookPrice == 2) {
  129. $goodsCond['priceType'] = 0;
  130. }
  131. }
  132. if (isset($get['lookStock'])) { // 1:有库存 2:无库存 -- 根据前端的传值
  133. $lookStock = intval($get['lookStock']);
  134. if ($lookStock == 1) {
  135. $goodsCond['stock>'] = 0;
  136. }
  137. if ($lookStock == 2) {
  138. $goodsCond['stock'] = 0;
  139. }
  140. }
  141. $searchText = $get['searchText'] ?? '';
  142. if (!empty($searchText)) {
  143. $where['name'] = ['like', $searchText];
  144. }
  145. $data = GoodsCategoryService::getGoodsList($where, $goodsCond);
  146. util::success($data);
  147. }
  148. //分类排序
  149. public function actionSort()
  150. {
  151. $post = Yii::$app->request->post();
  152. $items = $post['items'];
  153. //验证商品
  154. // 提取所有商品ID
  155. $categoryIds = array_map(function($item) {
  156. return intval($item['id']);
  157. }, $items);
  158. // 批量获取商品信息
  159. $goodsInfos = CategoryClass::getByIds($categoryIds);
  160. // 批量验证商品
  161. foreach($goodsInfos as $info) {
  162. CategoryService::valid($info, $this->mainId);
  163. }
  164. // 更新商品排序
  165. foreach($items as $item) {
  166. $id = intval($item['id']);
  167. $inTurn = intval($item['inTurn']);
  168. CategoryClass::updateById($id, ['inTurn' => $inTurn]);
  169. }
  170. util::complete('排序成功');
  171. }
  172. //获取分类 ssh 2019.12.2
  173. public function actionMallList()
  174. {
  175. $return = CategoryService::getEnableList($this->sjId);
  176. util::success($return);
  177. }
  178. }