|
|
@@ -9,6 +9,7 @@ use bizHd\goods\classes\GoodsCategoryClass;
|
|
|
use bizHd\goods\classes\GoodsClass;
|
|
|
use bizHd\goods\classes\GoodsItemClass;
|
|
|
use bizHd\goods\models\GoodsCategory;
|
|
|
+use bizHd\goods\models\Goods;
|
|
|
use common\components\util;
|
|
|
use Yii;
|
|
|
use linslin\yii2\curl;
|
|
|
@@ -21,25 +22,21 @@ class GoodsService extends BaseService
|
|
|
//后台列表 ssh 2019.12.7
|
|
|
public static function getGoodsList($where)
|
|
|
{
|
|
|
+ $page = Yii::$app->request->get('page', 1);
|
|
|
+ $pageSize = Yii::$app->request->get('pageSize', 20);
|
|
|
+
|
|
|
if (isset($where['cId'])) {
|
|
|
- $data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC', 'goods');
|
|
|
- $list = [];
|
|
|
- if (isset($data['list']) && !empty($data['list'])) {
|
|
|
- foreach ($data['list'] as $val) {
|
|
|
- if (isset($val['goods']) && !empty($val['goods'])) {
|
|
|
- $list[] = $val['goods'];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // 使用新的分类商品查询方法
|
|
|
+ return self::getCategoryGoodsList($where, $page, $pageSize);
|
|
|
} else {
|
|
|
$data = GoodsClass::getList('*', $where, 'inTurn DESC,updateTime DESC');
|
|
|
$list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
|
|
|
- }
|
|
|
- if (empty($list)) {
|
|
|
+ if (empty($list)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
|
|
|
return $data;
|
|
|
}
|
|
|
- $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
|
|
|
- return $data;
|
|
|
}
|
|
|
|
|
|
//获取商品完整详情,包括分类 ssh 2019.12.3
|
|
|
@@ -116,4 +113,81 @@ class GoodsService extends BaseService
|
|
|
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(['goods.mainId' => $where['mainId']]);
|
|
|
+ }
|
|
|
+ if (isset($where['delStatus'])) {
|
|
|
+ $query->andWhere(['goods.delStatus' => $where['delStatus']]);
|
|
|
+ }
|
|
|
+ if (isset($where['status'])) {
|
|
|
+ $query->andWhere(['goods.status' => $where['status']]);
|
|
|
+ }
|
|
|
+ if (isset($where['name'])) {
|
|
|
+ $query->andWhere(['like', 'goods.name', $where['name']]);
|
|
|
+ }
|
|
|
+ // 如果需要筛选有库存的商品
|
|
|
+ if (isset($where['hasStock']) && $where['hasStock'] == 1) {
|
|
|
+ $query->andWhere(['>', 'goods.stock', 0]);
|
|
|
+ }
|
|
|
+ }]);
|
|
|
+
|
|
|
+ // 分类表的筛选条件
|
|
|
+ if (isset($where['cId'])) {
|
|
|
+ $query->andWhere(['goodsCategory.cId' => $where['cId']]);
|
|
|
+ }
|
|
|
+ if (isset($where['flower'])) {
|
|
|
+ $query->andWhere(['goodsCategory.flower' => $where['flower']]);
|
|
|
+ }
|
|
|
+ if (isset($where['flowerNum'])) {
|
|
|
+ $query->andWhere(['goodsCategory.flowerNum' => $where['flowerNum']]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算总数
|
|
|
+ $total = $query->count();
|
|
|
+
|
|
|
+ // 分页
|
|
|
+ $offset = ($page - 1) * $pageSize;
|
|
|
+ $query->offset($offset)->limit($pageSize);
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ $query->orderBy(['goodsCategory.inTurn' => 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'list' => $goodsList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'totalPage' => ceil($total / $pageSize)
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
}
|