| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace addons\Community\common\service;
- use addons\Community\common\models\CommunityGoods;
- use addons\Community\common\models\CommunityGoodsSetting;
- use addons\Community\common\models\CommunityHead;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\models\cart\Cart;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\goods\GoodsCateRelate;
- use yii\helpers\Json;
- class GoodsService
- {
- public function page(int $mall_id, array $params, int $user_id)
- {
- // 获取社区ID
- $head_id = CommunityHead::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['check_status' => StatusEnum::ENABLED])
- ->select('id')->scalar();
- if (!$head_id) return '社区不存在';
- $goods_ids = CommunityGoods::find()->where(['mall_id' => $mall_id])
- ->andWhere(['community_id' => $head_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('goods_id')
- ->column();
- if (!empty($goods_ids)) {
- $goods_ids = CommunityGoodsSetting::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['goods_id' => $goods_ids])
- ->select('goods_id')
- ->column();
- // 使用这个分页 - 这里有类目那边不一定有
- // $enabled_goods = CommunityGoodsSetting::listPage([
- // 'where' => [
- // 'mall_id' => $mall_id,
- // 'status' => StatusEnum::ENABLED,
- // 'goods_id' => $goods_ids,
- // ],
- // 'select'=> 'goods_id',
- // 'page' => $params['page'] ?? 1,
- // 'limit' => $params['limit'] ?? 5
- // ]);
- if (!empty($goods_ids)) {
- $where[] = ['g.mall_id' => $mall_id, 'g.is_on_sale' => 1, 'status' => StatusEnum::ENABLED];
- if (!empty($params['type']) && $params['type'] == 'all') {
- //查询三级的所有
- $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $mall_id, 'is_show' => 1]);
- $cate_id_arr = array_column($goods_cate_list, 'id');
- $cate_id_arr[] = $params['cate_id'];
- } else {
- if (empty($params['cate_id'])) {//顶级
- //$goods_cate_list = GoodsCate::findAll(['parent_id' => 0, 'mall_id' => $mall_id, 'is_show' => 1]);
- //查询所有分类的商品
- $goods_cate_list = GoodsCate::findAll([ 'mall_id' => $mall_id, 'is_show' => 1]);
- $cate_id_arr = array_column($goods_cate_list, 'id');
- } else {
- $cate_id_arr = [$params['cate_id']];
- }
- }
- $goods_cate_relate_list = GoodsCateRelate::findAll(['cate_id' => $cate_id_arr]);
- $goods_id_arr = array_column($goods_cate_relate_list, 'goods_id');
- // 取交集 - 仅限于当前团长的商品
- $goods_id_arr = array_intersect($goods_ids, $goods_id_arr);
- $where[] = ['g.id' => $goods_id_arr];
- $params['where'] = $where;
- $params['alias'] = 'g';
- $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';
- $params['order'] = 'sort asc';
- $params['with'] = ['attr','extra'];
- $goods_list = Goods::listPage($params);
- // $list = Cart::getCartList($this->user_id);
- $goods_num_arr = [];
- // foreach ($list as $v) {
- // $row = Json::decode($v);
- // if (isset($goods_num_arr[$row['goods_id']])) {
- // $goods_num_arr[$row['goods_id']] += $row['num'];
- // } else {
- // $goods_num_arr[$row['goods_id']] = $row['num'];
- // }
- // }
- if (!empty($goods_list['list'])) {
- foreach ($goods_list['list'] as &$item) {
- $item['member_buy_limit_setting'] = empty($item['extra']['member_buy_limit_setting'])?'':json_decode($item['extra']['member_buy_limit_setting'],true);
- unset($item['extra']);
- $item['commission'] = 0;//最高可赚钱的佣金,预留字段
- // $item['num'] = !empty($goods_num_arr[$item['id']]) ? $goods_num_arr[$item['id']] : 0;
- $item['num'] = 0;
- $item['status'] = empty($item['stock']) ? 0 : 1;
- // $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']]);
- // $item['smart_form_template_id'] = $smart_form_template_id ? $smart_form_template_id : 0;
- }
- }
- return $goods_list;
- }
- }
- return [];
- }
- }
|