mainId; $query = GoodsCategory::find(); $query->where(['xhGoodsCategory.mainId' => $mainId]); //早期前期写法没规范,cId catId categoryId都有可能,需要兼容 if (!empty($askInfo['cId']) || !empty($askInfo['categoryId']) || !empty($askInfo['catId'])) { $cId = $askInfo['cId'] ?? 0; if (empty($cId)) { $cId = $askInfo['categoryId'] ?? 0; } if (empty($cId)) { $cId = $askInfo['catId'] ?? 0; } $query->andWhere(['xhGoodsCategory.cId' => $cId]); } if (!empty($askInfo['searchText'])) { $query->andWhere(['like', 'xhGoodsCategory.name', $askInfo['searchText']]); } $query->joinWith(['goods' => function ($query) use ($askInfo, $requestType) { if ($requestType == 'kd') { //花束开单 $query->andWhere(['xhGoods.delStatus' => 0]); $query->andWhere(['>', 'xhGoods.stock', 0]); } elseif ($requestType == 'goodsList') { //花束管理列表 $query->andWhere(['xhGoods.delStatus' => 0]); $priceStatus = isset($askInfo['priceStatus']) && is_numeric($askInfo['priceStatus']) ? $askInfo['priceStatus'] : -1; if ($priceStatus > -1) { //1 有价格,2 无价格 if ($priceStatus == 1) { $query->andWhere(['xhGoods.priceType' => 1]); } if ($priceStatus == 2) { $query->andWhere(['xhGoods.priceType' => 0]); } } $stockStatus = isset($askInfo['stockStatus']) && is_numeric($askInfo['stockStatus']) ? $askInfo['stockStatus'] : -1; if ($stockStatus > -1) { //1 有库存,2 无库存 if ($stockStatus == 1) { $query->andWhere(['>', 'xhGoods.stock', 0]); } if ($stockStatus == 2) { $query->andWhere(['xhGoods.stock' => 0]); } } $flowerNum = isset($askInfo['flowerNum']) && is_numeric($askInfo['flowerNum']) ? $askInfo['flowerNum'] : 0; if ($flowerNum > 0) { $query->andWhere(['xhGoods.flowerNum' => $flowerNum]); } } elseif ($requestType == 'mall') { //花束商城 $query->andWhere(['xhGoods.delStatus' => 0]); $query->andWhere(['xhGoods.status' => 1]); $query->andWhere(['xhGoods.priceType' => 1]); } elseif ($requestType == 'album') { //相册 $query->andWhere(['xhGoods.delStatus' => 0]); $query->andWhere(['xhGoods.status' => 1]); $query->andWhere(['xhGoods.priceType' => 0]); } }]); $query->orderBy(['inTurn' => SORT_DESC]); // 获取总数 $total = $query->count(); // 分页查询 $goodsCategoryInfoList = $query->offset(($page - 1) * $pageSize) ->limit($pageSize) ->all(); // 整理结果,只要有库存的商品 $list = []; foreach ($goodsCategoryInfoList as $info) { if ($info->goods) { // 只要有关联且有库存的商品 $list[] = $info->goods->attributes; } } $list = GoodsClass::groupGoodsBaseInfo($list, $shop, $custom, $params); $totalPage = ceil($total / $pageSize); $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据 return [ 'list' => $list, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize, 'moreData' => $moreData ]; } //删除商品的分类 public static function delGoodsCategory($mainId, $categoryId, $goodsId) { self::updateByCondition(['mainId' => $mainId, 'cId' => $categoryId, 'gId' => $goodsId], ['delStatus' => 1]); } //批量获取多个商品的分类ID ssh 2023 public static function getGoodsHasCategoryIdsBatch($goodsIds) { if (empty($goodsIds)) { return []; } // 一次性查询所有商品的分类关系 $categoryList = self::getAllByCondition(['gId' => ['in', $goodsIds]], null, 'gId,cId'); // 按商品ID分组整理结果 $result = []; foreach ($categoryList as $item) { $goodsId = $item['gId']; $categoryId = $item['cId']; if (!isset($result[$goodsId])) { $result[$goodsId] = []; } $result[$goodsId][] = $categoryId; } return $result; } }