StorageService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\StarChain\common\services;
  3. use addons\StarChain\common\models\Goods;
  4. use addons\StarChain\common\models\Storage;
  5. use yii\data\Pagination;
  6. use yii\helpers\Json;
  7. /**
  8. * 商品库服务
  9. */
  10. class StorageService extends BaseService
  11. {
  12. /**
  13. * 获取商品库列表
  14. *
  15. * @param array $params
  16. * @return array
  17. */
  18. public function getStorageList($params)
  19. {
  20. $limit = $params['limit'] ?? 10;
  21. $model = Storage::find();
  22. $model->with(['goods' => function ($query) {
  23. $query->with([
  24. 'category1', 'category2', 'category3',
  25. ])->select('id, name, spu_id, category_id_1, category_id_2, category_id_3, carousel_img_list, status');
  26. }]);
  27. $model->andWhere(['mall_id' => $this->mall_id, 'is_delete' => 0]);
  28. if (isset($params['category_ids'][0])) {
  29. $model->andWhere([
  30. 'spu_id' => Goods::find()->where(['category_id_1' => $params['category_ids'][0]])->select('spu_id')
  31. ]);
  32. }
  33. if (isset($params['category_ids'][1])) {
  34. $model->andWhere([
  35. 'spu_id' => Goods::find()->where(['category_id_2' => $params['category_ids'][1]])->select('spu_id')
  36. ]);
  37. }
  38. if (isset($params['category_ids'][2])) {
  39. $model->andWhere([
  40. 'spu_id' => Goods::find()->where(['category_id_3' => $params['category_ids'][2]])->select('spu_id')
  41. ]);
  42. }
  43. if (!empty($params['name'])) {
  44. $model->andWhere([
  45. 'spu_id' => Goods::find()->andWhere(['like', 'name', $params['name']])->select('spu_id')
  46. ]);
  47. }
  48. if (isset($params['import']) && $params['import'] !== '') {
  49. $model->andWhere([
  50. 'import' => $params['import']
  51. ]);
  52. }
  53. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  54. $model->limit($pagination->limit)->offset($pagination->offset);
  55. $list = $model->orderBy('id desc')
  56. ->asArray()
  57. ->all();
  58. foreach ($list as $k => $v) {
  59. $list[$k]['goods']['carousel_img_list'] = Json::decode($v['goods']['carousel_img_list']);
  60. }
  61. $data['list'] = $list;
  62. $data['page_count'] = $pagination->pageCount;
  63. return $data;
  64. }
  65. }