request->get('page', 1); $pageSize = Yii::$app->request->get('pageSize', 20); if (isset($where['cId'])) { // 使用新的分类商品查询方法 return self::getCategoryGoodsList($where, $page, $pageSize); } else { $data = GoodsClass::getList('*', $where, 'updateTime DESC'); $list = !empty($data['list']) ? $data['list'] : []; if (empty($list)) { return $data; } $custom = []; $params = []; $data['list'] = GoodsClass::groupGoodsBaseInfo($list, $shop, $custom, $params); return $data; } } //获取商品完整详情,包括分类 ssh 2019.12.3 public static function getGoodsInfo($id) { return GoodsClass::getGoodsInfo($id); } //删除商品 ssh 2019.12.7 public static function deleteGoods($goods) { GoodsClass::deleteGoods($goods['id']); } //验证对此商品的操作权限 ssh 2019.12.7 public static function valid($info, $mainId) { if (empty($info)) { util::fail('没有找到商品'); } if ($info['mainId'] != $mainId) { util::fail('您无法操作'); } } //上下架 ssh 2019.12.8 public static function changeStatus($id, $status) { GoodsClass::changeStatus($id, $status); } //保存花材组合 public static function saveItemGroup($order, $items, $classId, $name, $adminId) { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); $now = date('Y-m-d H:i:s'); try { //新增goods 还有很多字段待确定 $goodsData = [ 'name' => $name, 'shopImg' => '',//待确定 'price' => $order['actPrice'],//实际支付价格 'adminId' => $adminId, 'sjId' => $order['sjId'], 'status' => 0, 'content' => '', 'createTime' => $now, 'updateTime' => $now, ]; $goodsInfo = GoodsClass::add($goodsData); $gId = $goodsInfo['id']; //新增goodsCategory $cateData = [ 'gId' => $gId, 'name' => $name, 'cId' => $classId, 'sjId' => $order['sjId'], ]; GoodsCategoryClass::add($cateData); //新增goodsItem foreach ($items as $v) { $itemData = [ 'goodsId' => $gId, 'productId' => $v['productId'], 'itemId' => $v['itemId'], ]; GoodsItemClass::addData($itemData); } } catch (\Exception $exception) { $transaction->rollBack(); return false; } return true; } /** * 获取分类下的商品列表,支持分页和库存筛选 * @param array $where 查询条件 * @param int $page 页码 * @param int $pageSize 每页数量 * @return array */ public static function getCategoryGoodsList($where = [], $page = 1, $pageSize = 20) { // 构建基础查询 $query = GoodsCategory::find() ->joinWith(['goods' => function ($query) use ($where) { // 商品表的筛选条件 if (isset($where['mainId'])) { $query->andWhere(['xhGoods.mainId' => $where['mainId']]); } if (isset($where['delStatus'])) { $query->andWhere(['xhGoods.delStatus' => $where['delStatus']]); } if (isset($where['status'])) { $query->andWhere(['xhGoods.status' => $where['status']]); } if (isset($where['name'])) { $query->andWhere(['like', 'xhGoods.name', $where['name']]); } // 如果需要筛选有库存的商品 if (isset($where['hasStock']) && $where['hasStock'] == 1) { $query->andWhere(['>', 'xhGoods.stock', 0]); } }]); // 分类表的筛选条件 if (isset($where['cId'])) { $query->andWhere(['cId' => $where['cId']]); } if (isset($where['flower'])) { $query->andWhere(['xhGoodsCategory.flower' => $where['flower']]); } if (isset($where['flowerNum'])) { $query->andWhere(['xhGoodsCategory.flowerNum' => $where['flowerNum']]); } // 计算总数 $total = $query->count(); // 分页 $offset = ($page - 1) * $pageSize; $query->offset($offset)->limit($pageSize); // 排序 $query->orderBy(['xhGoodsCategory.inTurn' => SORT_DESC, 'xhGoods.sold' => SORT_DESC]); // 获取数据 $list = $query->all(); // 整理数据 -- 旧的实现 // $goodsList = []; // foreach ($list as $goodsCategory) { // if ($goodsCategory->goods) { // $goodsData = $goodsCategory->goods->toArray(); // // 使用现有的方法处理商品信息 // $processedGoods = GoodsClass::groupGoodsBaseInfo([$goodsData]); // if (!empty($processedGoods)) { // $goodsList[] = current($processedGoods); // } // } // } // 整理数据 $goodsToProcess = []; foreach ($list as $goodsCategory) { if ($goodsCategory->goods) { $goodsToProcess[] = $goodsCategory->goods->toArray(); } } $goodsList = []; if (!empty($goodsToProcess)) { $goodsList = GoodsClass::groupGoodsBaseInfo($goodsToProcess); } $totalPage = ceil($total / $pageSize); $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据 return [ 'list' => $goodsList, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize, 'moreData' => $moreData, 'totalPage' => ceil($total / $pageSize) ]; } }