| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\diy;
- use app\models\common\CommonGoods;
- use app\models\Goods;
- use app\models\MchGoodsCat;
- use app\models\Order;
- use app\models\OrderDetail;
- use yii\data\Pagination;
- use yii\db\ActiveQuery;
- use yii\helpers\ArrayHelper;
- /**
- * @property Store $store
- */
- class MchDiyGoods
- {
- public $store;
- public $type;
- public $cat;
- public $page;
- public $limit = 8;
- public $idList;
- public $is_page = false;
- public $mch;
- public $keyword;
- // 初始化模板信息
- public static function getTemplate(&$template, $mch_id, $store)
- {
- // 获取需要查找的id
- $list = [
- 'goods' => [
- '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'];
- }
- }
|