| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace bizHd\goods\classes;
- use bizHd\goods\models\GoodsCategory;
- use common\components\business;
- use common\components\util;
- use Yii;
- 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都有可能,需要兼容
- 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;
- }
- }
|