| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace bizHd\goods\classes;
- use bizHd\goods\models\GoodsCategory;
- use common\components\util;
- use bizHd\base\classes\BaseClass;
- class GoodsCategoryClass extends BaseClass
- {
- public static $baseFile = '\bizHd\goods\models\GoodsCategory';
- //$params 必须预留字段,为后面做准备
- public static function getGoodsList($askInfo, $shop, $custom, $params = [])
- {
- $requestType = $askInfo['requestType'] ?? '';
- $page = $askInfo['page'] ?? 1;
- $pageSize = $askInfo['pageSize'] ?? 20;
- if (empty($requestType)) {
- util::fail('没有请求类型');
- }
- /** GoodsCategory 版块查询 **/
- $mainId = $shop->mainId;
- $query = GoodsCategory::find();
- $query->where(['xhGoodsCategory.mainId' => $mainId]);
- //早期前期写法没规范,cId catId categoryId classId都有可能,需要兼容
- if (!empty($askInfo['cId']) || !empty($askInfo['categoryId']) || !empty($askInfo['catId']) || !empty($askInfo['classId'])) {
- $cId = $askInfo['cId'] ?? 0;
- if (empty($cId)) {
- $cId = $askInfo['categoryId'] ?? 0;
- }
- if (empty($cId)) {
- $cId = $askInfo['catId'] ?? 0;
- }
- if (empty($cId)) {
- $cId = $askInfo['classId'] ?? 0;
- }
- $query->andWhere(['xhGoodsCategory.cId' => $cId]);
- }
- if (!empty($askInfo['searchText'])) {
- if($askInfo['searchType'] == 1){
- $query->andWhere(['xhGoods.sn' => $askInfo['searchText']]);
- }else{
- $query->andWhere(['like', 'xhGoods.name', $askInfo['searchText']]);
- }
- }
- if (!empty($askInfo['search'])) {
- $query->andWhere(['like', 'xhGoods.name', $askInfo['search']]);
- }
- $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]);
- $priceType = isset($askInfo['priceType']) && is_numeric($askInfo['priceType']) ? $askInfo['priceType'] : -1;
- if ($priceType > -1) {
- //0 无价格,1 有价格
- $query->andWhere(['xhGoods.priceType' => $priceType]);
- }
- $hasStock = isset($askInfo['hasStock']) && is_numeric($askInfo['hasStock']) ? $askInfo['hasStock'] : -1;
- if ($hasStock > -1) {
- //$hasStock 0 没库存 1有库存
- if ($hasStock == 0) {
- $query->andWhere(['xhGoods.stock' => 0]);
- }
- if ($hasStock == 1) {
- $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]);
- } elseif ($requestType == 'album') {
- util::fail('相册已经不存在');
- }
- }]);
- $query->orderBy(['inTurn' => SORT_DESC]);
- // 获取总数
- $total = $query->count();
- // 分页查询
- $goodsCategoryInfoList = $query->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->all();
- // 整理结果,只要有库存的商品,并去重
- $list = [];
- $addedGoodsIds = []; // 用于记录已添加的商品ID,避免重复,一个商品在多个分类下就会出现重复,搜索的情况下
- foreach ($goodsCategoryInfoList as $info) {
- if ($info->goods && !in_array($info->goods->id, $addedGoodsIds)) {
- // 只要有关联且有库存的商品,且未添加过
- $list[] = $info->goods->attributes;
- $addedGoodsIds[] = $info->goods->id; // 记录已添加的商品ID
- }
- }
- $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;
- }
- }
|