GoodsCategoryClass.php 5.7 KB

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