[ 'id_list' => [], 'list' => [] ], 'waimai' => [ 'id_list' => [], 'list' => [] ], 'miaosha' => [ 'id_list' => [], 'list' => [] ], 'pintuan' => [ 'id_list' => [], 'list' => [] ], 'bargain' => [ 'id_list' => [], 'list' => [] ], 'book' => [ 'id_list' => [], 'list' => [] ], 'shop' => [ 'id_list' => [], 'list' => [] ], 'lottery' => [ 'id_list' => [], 'list' => [] ], 'mch' => [ 'id_list' => [], 'list' => [] ], 'integral' => [ 'id_list' => [], 'list' => [] ], 'topic' => [ 'id_list' => [], 'list' => [] ], ]; foreach ($template as &$value) { if ($value['type'] == 'search') { $value['param']['search_url'] = '/pages/search/search?mch_id=' . $mch_id; } if (array_key_exists($value['type'], $list) && $value['param']['list']) { $type = $value['type']; if($type == 'shop') { foreach ($value['param']['list'] as $item) { if (!in_array($item['id'], $list[$type]['id_list'])) { $list[$type]['id_list'][] = $item['id']; } } } else if ($type == 'mch') { foreach ($value['param']['list'] as $item) { if (!in_array($item['id'], $list[$type]['id_list'])) { $list[$type]['id_list'][] = $item['id']; } if ($item['goods_style'] == 2) { foreach ($item['goods_list'] as $v) { if (!in_array($v['id'], $list['goods']['id_list'])) { $list['goods']['id_list'][] = $v['id']; } } } } } else { foreach ($value['param']['list'] as $item) { if ($item['goods_style'] == 2) { foreach ($item['goods_list'] as $v) { if (!in_array($v['id'], $list[$type]['id_list'])) { $list[$type]['id_list'][] = $v['id']; } } } } } } } $ok = false; foreach ($list as $key => &$value) { if(!empty($value['id_list'])) { $res = MchDiyGoods::getGoods($store, $key, $value['id_list']); $list[$key]['list'] = $res['goods_list']; $ok = true; } else { $list[$key]['list'] = []; } } if ($ok) { foreach ($template as &$value) { if (array_key_exists($value['type'], $list) && $value['param']['list']) { foreach ($value['param']['list'] as &$item) { if($value['type'] == 'shop') { if(is_array($list[$value['type']]['list'])) { foreach ($list[$value['type']]['list'] as $goods) { if ($goods['id'] == $item['id']) { $item = $goods; break; } } } } else if($value['type'] == 'mch') { if(is_array($list[$value['type']]['list'])) { foreach ($list[$value['type']]['list'] as $mch) { if ($mch['id'] == $item['id']) { $item['name'] = $mch['name']; $item['pic_url'] = $mch['pic_url']; $item['logo'] = $mch['pic_url']; $item['goods_count'] = $mch['goods_count']; break; } } } if ($item['goods_style'] == 2) { foreach ($item['goods_list'] as &$v) { if(is_array($list['goods']['list'])){ foreach ($list['goods']['list'] as $goods) { if ($goods['id'] == $v['id']) { $v = $goods; break; } } } } unset($v); // $item['goods_list'] = $list['goods']['list']; } } else { if ($item['goods_style'] == 2) { $newList = []; foreach ($item['goods_list'] as $v) { if(is_array($list[$value['type']]['list'])){ foreach ($list[$value['type']]['list'] as $goods) { if ($goods['id'] == $v['id']) { $newList[] = $goods; break; } } } } $item['goods_list'] = $newList; } } } unset($item); } } unset($value); } } // 获取详情(商城、秒杀、拼团、砍价、积分商城、多商户等) public static function getGoods($store, $type, $idList = [] , $cat = 0, $limit = 8, $isPage = false, $page = 1, $mch = false) { $form = new MchDiyGoods(); $form->type = $type; $form->idList = $idList; $form->cat = $cat; $form->limit = $limit; $form->is_page = $isPage; $form->page = $page; $form->mch = $mch; $form->keyword = \Yii::$app->request->get('keyword'); $form->store = $store; $res = $form->getDetail(); return $res; } // 获取具体信息 public function getDetail() { $res = []; $type = $this->type; if (method_exists($this, $type)) { $res = $this->$type(); } return $res; } private function goods() { $query = Goods::find()->alias('g') ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.type' => 0, 'g.status' => 1]); if ($this->cat != 0) { $query->leftJoin(['gc' => MchGoodsCat::tableName()], 'gc.goods_id=g.id') ->andWhere(['gc.cat_id' => $this->cat]) ->groupBy('g.id') ->orderBy(['g.sort' => SORT_DESC, 'g.addtime' => SORT_DESC]); } if($this->mch) { $query->andWhere(['g.mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'g.name', $this->keyword]); } if($this->idList) { $query->andWhere(['g.id' => $this->idList]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC])->all(); $goodsList = []; /* @var Goods[] $list */ foreach ($list as $key => $item) { $attrGroupList = json_decode(json_encode($item->getAttrGroupList()), true); $price = round($item->price, 2); $res = CommonGoods::getMemberPrice([ 'attr' => $item['attr'], 'price' => $item['price'], 'mch_id' => $item['mch_id'], 'is_level' => $item['is_level'], ]); $goodsMemberPrice = $res['min_member_price'] ? $res['min_member_price'] : $item['price']; $goodsList[] = [ 'attr' => json_decode($item['attr'], true), 'attr_group_list' => $attrGroupList, 'id' => $item->id, 'goods_id' => $item->id, 'pic_url' => $item->cover_pic, 'cover_pic' => $item->cover_pic, 'price' => $price ? $price : 0, 'original_price' => $item->original_price, 'name' => $item->name, 'page_url' => '/goods/goods/goods?id=' . $item->id, 'use_attr' => $item->use_attr, 'is_negotiable' => $item->is_negotiable, 'price_content' => $item->is_negotiable == 0 ? '¥'.$price : '价格面议', 'goods_member_price' => $goodsMemberPrice ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function pintuan() { $query = PtGoods::find()->where([ 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1 ])->orderBy(['is_hot' => SORT_DESC, 'addtime' => SORT_DESC]); if($this->mch) { $query->andWhere(['mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'name', $this->keyword]); } if($this->idList) { $query->andWhere(['id' => $this->idList]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->all(); $goodsList = []; /* @var PtGoods[] $list */ foreach ($list as $key => $item) { $attr = json_decode($item['attr'], true); $goodsList[] = [ 'attr' => $attr, 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true), 'id' => $item->id, 'goods_id' => $item->id, 'pic_url' => $item->cover_pic, 'cover_pic' => $item->cover_pic, 'price' => round($item->price, 2) ? round($item->price, 2) : 0, 'original_price' => $item->original_price, 'name' => $item->name, 'page_url' => '/pages/pt/details/details?gid=' . $item->id, 'use_attr' => $item->use_attr, 'is_negotiable' => 0, 'price_content' => $item->group_num . '人团', 'original_price_content' => '单买价:', ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function miaosha() { $date = date('Y-m-d', time()); $time = date('H', time()); $query = MiaoshaGoods::find()->alias('mg')->where([ 'mg.store_id' => get_store_id(), 'mg.is_delete' => 0 ])->joinWith('msGoods g')->andWhere(['g.is_delete' => 0]); if($this->mch) { $query->andWhere(['mg.mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'g.name', $this->keyword]); } if($this->keyword) { $query->andWhere(['like', 'g.name', $this->keyword]); } if($this->idList) { $query->andWhere(['mg.id' => $this->idList]); } else { $query->andWhere([ 'or', [ 'and', ['mg.open_date' => $date], ['>=', 'mg.start_time', $time] ], [ 'and', ['>', 'mg.open_date', $date], ] ]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->all(); $goodsList = []; /* @var MiaoshaGoods[] $list */ foreach ($list as $key => $item) { /* @var MsGoods $msGoods */ $msGoods = $item->msGoods; $price = $msGoods->original_price; $attr = json_decode($item['attr'], true); foreach ($attr as &$value) { $value['num'] = $value['miaosh_num']; $value['price'] = $value['miaosh_price']; if ($price == 0 || ($price > 0 && round($price, 2) >= round($value['miaosha_price'], 2))) { $price = $value['miaosha_price']; } } unset($value); $goodsList[] = [ 'attr' => $attr, 'attr_group' => json_decode(json_encode($msGoods->getAttrGroupList()), true), 'id' => $item->id, 'goods_id' => $msGoods->id, 'pic_url' => $msGoods->cover_pic, 'cover_pic' => $msGoods->cover_pic, 'price' => round($price, 2) ? round($price, 2) : 0, 'original_price' => $msGoods->original_price, 'name' => $msGoods->name, 'page_url' => '/pages/miaosha/details/details?id=' . $item->id, 'use_attr' => $msGoods->use_attr, 'open_date' => $item->open_date, 'start_time' => $item->start_time, 'is_negotiable' => 0, 'price_content' => '秒杀价:', 'original_price_content' => '售价:', ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function book () { $query = YyGoods::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1]); if($this->mch) { $query->andWhere(['mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'name', $this->keyword]); } if($this->idList) { $query->andWhere(['id' => $this->idList]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->all(); $goodsList = []; /* @var YyGoods[] $list */ foreach ($list as $key => $item) { $price = round($item->price, 2); $goodsList[] = [ 'attr' => json_decode($item->attr, true), 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true), 'id' => $item->id, 'goods_id' => $item->id, 'pic_url' => $item->cover_pic, 'cover_pic' => $item->cover_pic, 'price' => $price ? $price : 0, 'original_price' => $item->original_price, 'name' => $item->name, 'page_url' => '/pages/book/details/details?id=' . $item->id, 'use_attr' => $item->use_attr, 'is_negotiable' => 0, 'price_content' => $price ? '¥'.$price : '免费' ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function bargain() { $query = Goods::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1,'type' => 2])->with('bargain'); if($this->mch) { $query->andWhere(['mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'name', $this->keyword]); } if($this->idList) { $query->andWhere(['id' => $this->idList]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])->all(); $goodsList = []; /* @var Goods[] $list */ foreach ($list as $key => $item) { /* @var BargainGoods $goods*/ $goods = $item->bargain; $goodsList[] = [ 'attr' => json_decode($item->attr, true), 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true), 'goods_id' => $item->id, 'pic_url' => $item->cover_pic, 'cover_pic' => $item->cover_pic, 'original_price' => $item->price, 'name' => $item->name, 'use_attr' => $item->use_attr, 'id' => $item->id, 'price' => round($goods->min_price, 2) ? round($goods->min_price, 2) : 0, 'page_url' => '/bargain/goods/goods?goods_id=' . $goods->goods_id, 'is_negotiable' => 0, 'begin_time' => $goods->begin_time, 'end_time' => $goods->end_time, 'begin_time_text' => $goods->getBeginTimeText(), 'end_time_text' => $goods->getEndTimeText(), 'price_content' => '最低价:', 'original_price_content' => '售价:', ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function waimai() { $query = Goods::find()->alias('g') ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.type' => 0, 'g.status' => 1]); if ($this->cat != 0) { $query->leftJoin(['gc' => MchGoodsCat::tableName()], 'gc.goods_id=g.id') ->andWhere(['gc.cat_id' => $this->cat]) ->groupBy('g.id') ->orderBy(['g.sort' => SORT_DESC, 'g.addtime' => SORT_DESC]); } if($this->mch) { $query->andWhere(['g.mch_id' => $this->mch]); } if($this->keyword) { $query->andWhere(['like', 'g.name', $this->keyword]); } if($this->idList) { $query->andWhere(['g.id' => $this->idList]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if($this->is_page) { $query->limit($pagination->limit)->offset($pagination->offset); } $list = $query->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC])->all(); $goodsList = []; /* @var Goods[] $list */ foreach ($list as $key => $item) { $attrGroupList = json_decode(json_encode($item->getAttrGroupList()), true); $price = round($item->price, 2); $monthly_sales = $this->monthly_sales($item->mch_id,$item->id); $goodsList[] = [ 'attr' => json_decode($item['attr'], true), 'attr_group_list' => $attrGroupList, 'id' => $item->id, 'goods_id' => $item->id, 'pic_url' => $item->cover_pic, 'cover_pic' => $item->cover_pic, 'price' => $price ? $price : 0, 'original_price' => $item->original_price, 'name' => $item->name, 'page_url' => '/goods/goods/goods?id=' . $item->id, 'use_attr' => $item->use_attr, 'is_negotiable' => $item->is_negotiable, 'price_content' => $item->is_negotiable == 0 ? '¥'.$price : '价格面议', 'monthly_sales' => $monthly_sales, 'num' => 0, ]; } return [ 'goods_list' => $goodsList, 'pagination' => $pagination ]; } private function monthly_sales($mch_id,$goods_id){ $beginThismonth = mktime(0,0,0,date('m'),1,date('Y')); $endThismonth = mktime(23,59,59,date('m'),date('t'),date('Y')); $monthly_sales = OrderDetail::find()->alias('od') ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id') ->where([ 'od.is_delete' => 0, 'o.mch_id' => $mch_id, 'od.goods_id' => $goods_id, 'o.is_pay' => 1, 'o.is_delete' => 0, ])->andWhere([ '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL ])->andWhere([ 'AND', ['>=', 'o.created_at', $beginThismonth], ['<=', 'o.created_at', $endThismonth], ])->sum('num'); $monthly_sales = $monthly_sales ? $monthly_sales : 0; return $monthly_sales; } // diy组件权限 public static function getDiyAuth() { return ['search', 'nav', 'banner', 'notice', 'link', 'rubik', 'video' ,'goods', 'waimai', 'time', 'line','pintuan','miaosha','book','bargain','coupon']; } }