| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace bizHd\goods\services;
- use bizHd\base\services\BaseService;
- use bizHd\goods\classes\GoodsCategoryClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\goods\classes\GoodsItemClass;
- use bizHd\goods\models\GoodsCategory;
- use common\components\util;
- use Yii;
- class GoodsService extends BaseService
- {
- public static $baseFile = '\bizHd\goods\classes\GoodsClass';
- //商品列表
- public static function getGoodsList($where, $shop)
- {
- $page = Yii::$app->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)
- ];
- }
- }
|