GoodsService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace bizHd\goods\services;
  3. use bizHd\base\services\BaseService;
  4. use bizHd\goods\classes\GoodsCategoryClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. use bizHd\goods\classes\GoodsItemClass;
  7. use bizHd\goods\models\GoodsCategory;
  8. use common\components\util;
  9. use Yii;
  10. class GoodsService extends BaseService
  11. {
  12. public static $baseFile = '\bizHd\goods\classes\GoodsClass';
  13. //商品列表
  14. public static function getGoodsList($where, $shop)
  15. {
  16. $page = Yii::$app->request->get('page', 1);
  17. $pageSize = Yii::$app->request->get('pageSize', 20);
  18. if (isset($where['cId'])) {
  19. // 使用新的分类商品查询方法
  20. return self::getCategoryGoodsList($where, $page, $pageSize);
  21. } else {
  22. $data = GoodsClass::getList('*', $where, 'updateTime DESC');
  23. $list = !empty($data['list']) ? $data['list'] : [];
  24. if (empty($list)) {
  25. return $data;
  26. }
  27. $custom = [];
  28. $params = [];
  29. $data['list'] = GoodsClass::groupGoodsBaseInfo($list, $shop, $custom, $params);
  30. return $data;
  31. }
  32. }
  33. //获取商品完整详情,包括分类 ssh 2019.12.3
  34. public static function getGoodsInfo($id)
  35. {
  36. return GoodsClass::getGoodsInfo($id);
  37. }
  38. //删除商品 ssh 2019.12.7
  39. public static function deleteGoods($goods)
  40. {
  41. GoodsClass::deleteGoods($goods['id']);
  42. }
  43. //验证对此商品的操作权限 ssh 2019.12.7
  44. public static function valid($info, $mainId)
  45. {
  46. if (empty($info)) {
  47. util::fail('没有找到商品');
  48. }
  49. if ($info['mainId'] != $mainId) {
  50. util::fail('您无法操作');
  51. }
  52. }
  53. //上下架 ssh 2019.12.8
  54. public static function changeStatus($id, $status)
  55. {
  56. GoodsClass::changeStatus($id, $status);
  57. }
  58. //保存花材组合
  59. public static function saveItemGroup($order, $items, $classId, $name, $adminId)
  60. {
  61. $connection = Yii::$app->db;
  62. $transaction = $connection->beginTransaction();
  63. $now = date('Y-m-d H:i:s');
  64. try {
  65. //新增goods 还有很多字段待确定
  66. $goodsData = [
  67. 'name' => $name,
  68. 'shopImg' => '',//待确定
  69. 'price' => $order['actPrice'],//实际支付价格
  70. 'adminId' => $adminId,
  71. 'sjId' => $order['sjId'],
  72. 'status' => 0,
  73. 'content' => '',
  74. 'createTime' => $now,
  75. 'updateTime' => $now,
  76. ];
  77. $goodsInfo = GoodsClass::add($goodsData);
  78. $gId = $goodsInfo['id'];
  79. //新增goodsCategory
  80. $cateData = [
  81. 'gId' => $gId,
  82. 'name' => $name,
  83. 'cId' => $classId,
  84. 'sjId' => $order['sjId'],
  85. ];
  86. GoodsCategoryClass::add($cateData);
  87. //新增goodsItem
  88. foreach ($items as $v) {
  89. $itemData = [
  90. 'goodsId' => $gId,
  91. 'productId' => $v['productId'],
  92. 'itemId' => $v['itemId'],
  93. ];
  94. GoodsItemClass::addData($itemData);
  95. }
  96. } catch (\Exception $exception) {
  97. $transaction->rollBack();
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * 获取分类下的商品列表,支持分页和库存筛选
  104. * @param array $where 查询条件
  105. * @param int $page 页码
  106. * @param int $pageSize 每页数量
  107. * @return array
  108. */
  109. public static function getCategoryGoodsList($where = [], $page = 1, $pageSize = 20)
  110. {
  111. // 构建基础查询
  112. $query = GoodsCategory::find()
  113. ->joinWith(['goods' => function ($query) use ($where) {
  114. // 商品表的筛选条件
  115. if (isset($where['mainId'])) {
  116. $query->andWhere(['xhGoods.mainId' => $where['mainId']]);
  117. }
  118. if (isset($where['delStatus'])) {
  119. $query->andWhere(['xhGoods.delStatus' => $where['delStatus']]);
  120. }
  121. if (isset($where['status'])) {
  122. $query->andWhere(['xhGoods.status' => $where['status']]);
  123. }
  124. if (isset($where['name'])) {
  125. $query->andWhere(['like', 'xhGoods.name', $where['name']]);
  126. }
  127. // 如果需要筛选有库存的商品
  128. if (isset($where['hasStock']) && $where['hasStock'] == 1) {
  129. $query->andWhere(['>', 'xhGoods.stock', 0]);
  130. }
  131. }]);
  132. // 分类表的筛选条件
  133. if (isset($where['cId'])) {
  134. $query->andWhere(['cId' => $where['cId']]);
  135. }
  136. if (isset($where['flower'])) {
  137. $query->andWhere(['xhGoodsCategory.flower' => $where['flower']]);
  138. }
  139. if (isset($where['flowerNum'])) {
  140. $query->andWhere(['xhGoodsCategory.flowerNum' => $where['flowerNum']]);
  141. }
  142. // 计算总数
  143. $total = $query->count();
  144. // 分页
  145. $offset = ($page - 1) * $pageSize;
  146. $query->offset($offset)->limit($pageSize);
  147. // 排序
  148. $query->orderBy(['xhGoodsCategory.inTurn' => SORT_DESC, 'xhGoods.sold' => SORT_DESC]);
  149. // 获取数据
  150. $list = $query->all();
  151. // 整理数据 -- 旧的实现
  152. // $goodsList = [];
  153. // foreach ($list as $goodsCategory) {
  154. // if ($goodsCategory->goods) {
  155. // $goodsData = $goodsCategory->goods->toArray();
  156. // // 使用现有的方法处理商品信息
  157. // $processedGoods = GoodsClass::groupGoodsBaseInfo([$goodsData]);
  158. // if (!empty($processedGoods)) {
  159. // $goodsList[] = current($processedGoods);
  160. // }
  161. // }
  162. // }
  163. // 整理数据
  164. $goodsToProcess = [];
  165. foreach ($list as $goodsCategory) {
  166. if ($goodsCategory->goods) {
  167. $goodsToProcess[] = $goodsCategory->goods->toArray();
  168. }
  169. }
  170. $goodsList = [];
  171. if (!empty($goodsToProcess)) {
  172. $goodsList = GoodsClass::groupGoodsBaseInfo($goodsToProcess);
  173. }
  174. $totalPage = ceil($total / $pageSize);
  175. $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据
  176. return [
  177. 'list' => $goodsList,
  178. 'total' => $total,
  179. 'page' => $page,
  180. 'pageSize' => $pageSize,
  181. 'moreData' => $moreData,
  182. 'totalPage' => ceil($total / $pageSize)
  183. ];
  184. }
  185. }