GoodsCategoryClass.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace bizHd\goods\classes;
  3. use bizHd\goods\models\GoodsCategory;
  4. use common\components\business;
  5. use common\components\util;
  6. use Yii;
  7. use bizHd\base\classes\BaseClass;
  8. class GoodsCategoryClass extends BaseClass
  9. {
  10. public static $baseFile = '\bizHd\goods\models\GoodsCategory';
  11. //$params 必须预留字段,为后面做准备
  12. public static function getGoodsList($askInfo, $shop, $custom, $params = [])
  13. {
  14. $requestType = $askInfo['requestType'] ?? '';
  15. $page = $askInfo['page'] ?? 1;
  16. $pageSize = $askInfo['pageSize'] ?? 20;
  17. if (empty($requestType)) {
  18. util::fail('没有请求类型');
  19. }
  20. /** GoodsCategory 版块查询 **/
  21. $mainId = $shop->mainId;
  22. $query = GoodsCategory::find();
  23. $query->where(['xhGoodsCategory.mainId' => $mainId]);
  24. //早期前期写法没规范,cId catId categoryId都有可能,需要兼容
  25. if (!empty($askInfo['cId']) || !empty($askInfo['categoryId']) || !empty($askInfo['catId'])) {
  26. $cId = $askInfo['cId'] ?? 0;
  27. if (empty($cId)) {
  28. $cId = $askInfo['categoryId'] ?? 0;
  29. }
  30. if (empty($cId)) {
  31. $cId = $askInfo['catId'] ?? 0;
  32. }
  33. $query->andWhere(['xhGoodsCategory.cId' => $cId]);
  34. }
  35. if (!empty($askInfo['searchText'])) {
  36. $query->andWhere(['like', 'xhGoodsCategory.name', $askInfo['searchText']]);
  37. }
  38. $query->joinWith(['goods' => function ($query) use ($askInfo, $requestType) {
  39. if ($requestType == 'kd') {
  40. //花束开单
  41. $query->andWhere(['xhGoods.delStatus' => 0]);
  42. $query->andWhere(['>', 'xhGoods.stock', 0]);
  43. } elseif ($requestType == 'goodsList') {
  44. //花束管理列表
  45. $query->andWhere(['xhGoods.delStatus' => 0]);
  46. $priceStatus = isset($askInfo['priceStatus']) && is_numeric($askInfo['priceStatus']) ? $askInfo['priceStatus'] : -1;
  47. if ($priceStatus > -1) {
  48. //1 有价格,2 无价格
  49. if ($priceStatus == 1) {
  50. $query->andWhere(['xhGoods.priceType' => 1]);
  51. }
  52. if ($priceStatus == 2) {
  53. $query->andWhere(['xhGoods.priceType' => 0]);
  54. }
  55. }
  56. $stockStatus = isset($askInfo['stockStatus']) && is_numeric($askInfo['stockStatus']) ? $askInfo['stockStatus'] : -1;
  57. if ($stockStatus > -1) {
  58. //1 有库存,2 无库存
  59. if ($stockStatus == 1) {
  60. $query->andWhere(['>', 'xhGoods.stock', 0]);
  61. }
  62. if ($stockStatus == 2) {
  63. $query->andWhere(['xhGoods.stock' => 0]);
  64. }
  65. }
  66. $flowerNum = isset($askInfo['flowerNum']) && is_numeric($askInfo['flowerNum']) ? $askInfo['flowerNum'] : 0;
  67. if ($flowerNum > 0) {
  68. $query->andWhere(['xhGoods.flowerNum' => $flowerNum]);
  69. }
  70. } elseif ($requestType == 'mall') {
  71. //花束商城
  72. $query->andWhere(['xhGoods.delStatus' => 0]);
  73. $query->andWhere(['xhGoods.status' => 1]);
  74. $query->andWhere(['xhGoods.priceType' => 1]);
  75. } elseif ($requestType == 'album') {
  76. //相册
  77. $query->andWhere(['xhGoods.delStatus' => 0]);
  78. $query->andWhere(['xhGoods.status' => 1]);
  79. $query->andWhere(['xhGoods.priceType' => 0]);
  80. }
  81. }]);
  82. $query->orderBy(['inTurn' => SORT_DESC]);
  83. // 获取总数
  84. $total = $query->count();
  85. // 分页查询
  86. $goodsCategoryInfoList = $query->offset(($page - 1) * $pageSize)
  87. ->limit($pageSize)
  88. ->all();
  89. // 整理结果,只要有库存的商品
  90. $list = [];
  91. foreach ($goodsCategoryInfoList as $info) {
  92. if ($info->goods) { // 只要有关联且有库存的商品
  93. $list[] = $info->goods->attributes;
  94. }
  95. }
  96. $list = GoodsClass::groupGoodsBaseInfo($list, $shop, $custom, $params);
  97. $totalPage = ceil($total / $pageSize);
  98. $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据
  99. return [
  100. 'list' => $list,
  101. 'total' => $total,
  102. 'page' => $page,
  103. 'pageSize' => $pageSize,
  104. 'moreData' => $moreData
  105. ];
  106. }
  107. //删除商品的分类
  108. public static function delGoodsCategory($mainId, $categoryId, $goodsId)
  109. {
  110. self::updateByCondition(['mainId' => $mainId, 'cId' => $categoryId, 'gId' => $goodsId], ['delStatus' => 1]);
  111. }
  112. //批量获取多个商品的分类ID ssh 2023
  113. public static function getGoodsHasCategoryIdsBatch($goodsIds)
  114. {
  115. if (empty($goodsIds)) {
  116. return [];
  117. }
  118. // 一次性查询所有商品的分类关系
  119. $categoryList = self::getAllByCondition(['gId' => ['in', $goodsIds]], null, 'gId,cId');
  120. // 按商品ID分组整理结果
  121. $result = [];
  122. foreach ($categoryList as $item) {
  123. $goodsId = $item['gId'];
  124. $categoryId = $item['cId'];
  125. if (!isset($result[$goodsId])) {
  126. $result[$goodsId] = [];
  127. }
  128. $result[$goodsId][] = $categoryId;
  129. }
  130. return $result;
  131. }
  132. }