GoodsService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\models\CommunityGoods;
  4. use addons\Community\common\models\CommunityGoodsSetting;
  5. use addons\Community\common\models\CommunityHead;
  6. use common\enums\StatusEnum;
  7. use common\globals\GlobalInvoke;
  8. use common\models\cart\Cart;
  9. use common\models\goods\Goods;
  10. use common\models\goods\GoodsCate;
  11. use common\models\goods\GoodsCateRelate;
  12. use yii\helpers\Json;
  13. class GoodsService
  14. {
  15. public function page(int $mall_id, array $params, int $user_id)
  16. {
  17. // 获取社区ID
  18. $head_id = CommunityHead::find()->where(['mall_id' => $mall_id])
  19. ->andWhere(['user_id' => $user_id])
  20. ->andWhere(['status' => StatusEnum::ENABLED])
  21. ->andWhere(['check_status' => StatusEnum::ENABLED])
  22. ->select('id')->scalar();
  23. if (!$head_id) return '社区不存在';
  24. $goods_ids = CommunityGoods::find()->where(['mall_id' => $mall_id])
  25. ->andWhere(['community_id' => $head_id])
  26. ->andWhere(['status' => StatusEnum::ENABLED])
  27. ->select('goods_id')
  28. ->column();
  29. if (!empty($goods_ids)) {
  30. $goods_ids = CommunityGoodsSetting::find()
  31. ->where(['mall_id' => $mall_id])
  32. ->andWhere(['status' => StatusEnum::ENABLED])
  33. ->andWhere(['goods_id' => $goods_ids])
  34. ->select('goods_id')
  35. ->column();
  36. // 使用这个分页 - 这里有类目那边不一定有
  37. // $enabled_goods = CommunityGoodsSetting::listPage([
  38. // 'where' => [
  39. // 'mall_id' => $mall_id,
  40. // 'status' => StatusEnum::ENABLED,
  41. // 'goods_id' => $goods_ids,
  42. // ],
  43. // 'select'=> 'goods_id',
  44. // 'page' => $params['page'] ?? 1,
  45. // 'limit' => $params['limit'] ?? 5
  46. // ]);
  47. if (!empty($goods_ids)) {
  48. $where[] = ['g.mall_id' => $mall_id, 'g.is_on_sale' => 1, 'status' => StatusEnum::ENABLED];
  49. if (!empty($params['type']) && $params['type'] == 'all') {
  50. //查询三级的所有
  51. $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $mall_id, 'is_show' => 1]);
  52. $cate_id_arr = array_column($goods_cate_list, 'id');
  53. $cate_id_arr[] = $params['cate_id'];
  54. } else {
  55. if (empty($params['cate_id'])) {//顶级
  56. //$goods_cate_list = GoodsCate::findAll(['parent_id' => 0, 'mall_id' => $mall_id, 'is_show' => 1]);
  57. //查询所有分类的商品
  58. $goods_cate_list = GoodsCate::findAll([ 'mall_id' => $mall_id, 'is_show' => 1]);
  59. $cate_id_arr = array_column($goods_cate_list, 'id');
  60. } else {
  61. $cate_id_arr = [$params['cate_id']];
  62. }
  63. }
  64. $goods_cate_relate_list = GoodsCateRelate::findAll(['cate_id' => $cate_id_arr]);
  65. $goods_id_arr = array_column($goods_cate_relate_list, 'goods_id');
  66. // 取交集 - 仅限于当前团长的商品
  67. $goods_id_arr = array_intersect($goods_ids, $goods_id_arr);
  68. $where[] = ['g.id' => $goods_id_arr];
  69. $params['where'] = $where;
  70. $params['alias'] = 'g';
  71. $params['select'] = 'g.id,g.goods_name,g.cover_pic,g.price,g.stock,g.attr_groups,g.attr_groups_format,g.is_attr,g.is_show_stock,g.unit,g.goods_source,g.original_price';
  72. $params['order'] = 'sort asc';
  73. $params['with'] = ['attr','extra'];
  74. $goods_list = Goods::listPage($params);
  75. // $list = Cart::getCartList($this->user_id);
  76. $goods_num_arr = [];
  77. // foreach ($list as $v) {
  78. // $row = Json::decode($v);
  79. // if (isset($goods_num_arr[$row['goods_id']])) {
  80. // $goods_num_arr[$row['goods_id']] += $row['num'];
  81. // } else {
  82. // $goods_num_arr[$row['goods_id']] = $row['num'];
  83. // }
  84. // }
  85. if (!empty($goods_list['list'])) {
  86. foreach ($goods_list['list'] as &$item) {
  87. $item['member_buy_limit_setting'] = empty($item['extra']['member_buy_limit_setting'])?'':json_decode($item['extra']['member_buy_limit_setting'],true);
  88. unset($item['extra']);
  89. $item['commission'] = 0;//最高可赚钱的佣金,预留字段
  90. // $item['num'] = !empty($goods_num_arr[$item['id']]) ? $goods_num_arr[$item['id']] : 0;
  91. $item['num'] = 0;
  92. $item['status'] = empty($item['stock']) ? 0 : 1;
  93. // $smart_form_template_id = GlobalInvoke::exec(GlobalInvoke::GOODS_NEED_SMARTFORM, $mall_id, ['user_id' => $this->user_id,'mall_id' => $mall_id,'goods_id' => $item['id']]);
  94. // $item['smart_form_template_id'] = $smart_form_template_id ? $smart_form_template_id : 0;
  95. }
  96. }
  97. return $goods_list;
  98. }
  99. }
  100. return [];
  101. }
  102. }