| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Clerk;
- use app\models\DeliveryRules;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsBrowse;
- use app\models\GoodsCat;
- use app\models\Mch;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\MdGroupActivities;
- use app\models\MdGroupActivitiesGoods;
- use app\models\Order;
- use app\models\OrderComment;
- use app\models\Shop;
- use app\models\User;
- use app\modules\client\models\ApiModel;
- use app\modules\client\models\v1\common\CommonGoods;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use app\models\Cat;
- use app\models\Cart;
- class DiyForm extends ApiModel
- {
- /**
- * 获取商品列表
- */
- public static function getGoods($reqeruy)
- {
- try {
- $goods_id = Json::decode($reqeruy['goods_id']);
- } catch (\Exception $e) {
- $goods_id = explode(',', $reqeruy['goods_id']);
- }
- if ($goods_id === null) {
- $goods_id = [-1];
- }
- $goods_id = (array)$goods_id;
- $userLevel = get_user()->level;
- $query = Goods::find()->alias('g')
- ->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')
- ->where([
- 'g.is_delete' => 0,
- 'g.store_id' => get_store_id(),
- 'g.status' => 1
- ])->andWhere(['not like', 'g.name', '当面付']);
- // 这里判断商品浏览权限
- if(empty($userLevel)){
- $query->andWhere(['gb.goods_id' => null]);
- }else{
- $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
- }
- if (isset($reqeruy['product_type']) && $reqeruy['product_type'] != null) {
- $query->andWhere(['g.product_type' => $reqeruy['product_type']]);
- } else {
- $query->andWhere(['<>', 'g.product_type', Goods::GOODS_TYPE_INTEGRAL]);
- }
- if (get_md_id()) {
- $md = Md::findOne(get_md_id());
- if ($md->is_single) {
- $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id');
- //兼容独立运营商品可以搜索到平台的预约商品
- $query->andWhere(['OR', [
- 'g.product_type' => 0,
- 'mdg.status' => 1,
- 'mdg.md_id' => get_md_id()
- ], [
- 'g.product_type' => [
- 1, 2
- ]
- ]]);
- }
- }
- if ($goods_id) {
- $query->andWhere([
- 'in', 'g.id', $goods_id
- ]);
- $query->orderBy([new \yii\db\Expression('FIELD(g.id, '. implode(",", $goods_id) .')')]);
- // $query->addParams([':ids' => implode(',', array_map(function($id) {
- // return (int)$id;
- // }, $goods_id))]);
- } else {
- if ($reqeruy['cat_id']) {
- if ($reqeruy['product_type'] == 2) {
- $query->andWhere(['g.id' => \app\models\BookingGoodsExt::find()->where(['cat_id' => $reqeruy['cat_id']])->select('goods_id')]);
- }else{
- $cat_arr = Cat::find()->where(['parent_id' => $reqeruy['cat_id'], 'is_delete' => 0, 'is_show' => 1])->select('id')->column();
- $cat_arr = array_merge($cat_arr, [$reqeruy['cat_id']]);
- if (!empty($cat_arr)) {
- $cat_arr2 = Cat::find()->where(['parent_id' => $cat_arr, 'is_delete' => 0, 'is_show' => 1])->select('id')->column();
- $cat_arr = array_merge($cat_arr, $cat_arr2);
- }
- $goods_cat_arr = GoodsCat::find()->where(['cat_id' => $cat_arr, 'is_delete' => 0])->select('goods_id')->column();
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id')
- ->andWhere(['OR', [
- 'gc.cat_id' => Cat::getCatId($reqeruy['cat_id'])
- ], ['gc.goods_id' => $goods_cat_arr]]);
- }
- }
- // 团购活动
- if($reqeruy['activities_id'] > 0){
- $goodsIds = MdGroupActivitiesGoods::find()->where(['activities_id' => $reqeruy['activities_id']])->select('goods_id')->column();
- $query->andWhere(['in', 'g.id', $goodsIds]);
- }
- $count = $query->groupBy('g.id')->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $reqeruy['limit'], 'page' => $reqeruy['page'] - 1]);
- $query->offset($pagination->offset)->limit($pagination->limit)
- ->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
- }
- $is_show_alliance_coupon = \app\models\Option::get('is_show_alliance_coupon', get_store_id(), 'store', 0)['value'];
- $select = ['g.goods_num', 'g.order_min_count', 'g.status', 'g.product_type', 'g.virtual_sales', 'g.name', 'g.id', 'g.service', 'g.attr', 'g.cover_pic', 'g.attr', 'g.use_attr', 'g.price', 'g.original_price', 'g.delivery_rules_id', 'g.is_level', 'g.is_negotiable'];
- if ($is_show_alliance_coupon) {
- array_push($select, 'g.goods_take_price', 'g.goods_send_profit');
- }
- if (get_md_id()) {
- if ($md->is_single) {
- array_push($select, 'mdg.virtual_sales as md_virtual_sales', 'mdg.attr as mdg_attr', 'mdg.goods_num as mdg_goods_num', 'mdg.price as mdg_price', 'mdg.status as mdg_status' );
- }
- }
- $goods_list = $query->select($select)->groupBy('g.id')
- ->asArray()->all();
- if (get_md_id()) {
- if ($md->is_single) {
- foreach ($goods_list as $key => $value) {
- //独立运营商品可以搜索到平台的预约商品
- if (!in_array($value['product_type'], [1, 2])) {
- $goods_list[$key]['virtual_sales'] = $value['md_virtual_sales'];
- $goods_list[$key]['attr'] = $value['mdg_attr'];
- $goods_list[$key]['goods_num'] = $value['mdg_goods_num'];
- $goods_list[$key]['price'] = $value['mdg_price'];
- $goods_list[$key]['status'] = $value['mdg_status'];
- }
- }
- }
- }
- foreach ($goods_list as $key => $value) {
- $goods = Goods::findOne($value['id']);
- $goods_list[$key]['virtual_sales'] = $goods->getSalesVolume() + $value['virtual_sales'];
- if ($value['product_type'] == 1) {
- $goods_list[$key]['date_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->date_book);
- }
- if ($value['product_type'] == 2) {
- $goods_list[$key]['service_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->service_book);
- }
- $goods_list[$key]['delivery_rules'] = DeliveryRules::find()->where(['id' => $value['delivery_rules_id'], 'is_delete' => 0, 'status' => 1])
- ->select('type, times, days, name')->asArray()->one();
- if ($goods_list[$key]['delivery_rules']) {
- $days = "下单" . $goods_list[$key]['delivery_rules']['days'] . "天后";
- $goods_list[$key]['delivery_rules']['times'] =
- (int)$goods_list[$key]['delivery_rules']['type'] === 1 ? $days : date("m月d日 H:i:s", $goods_list[$key]['delivery_rules']['times']);
- $goods_list[$key]['delivery_rules']['type'] .= '';
- }
- $res = CommonGoods::getMemberPrice([
- 'attr' => $value['attr'],
- 'price' => $value['price'],
- 'mch_id' => 0,
- 'is_level' => $value['is_level'],
- 'use_attr' => isset($value['use_attr']) ? $value['use_attr'] : 0,
- ]);
- $goodsMemberPrice = $res['min_member_price'] ?: $value['price'];
- $goods_list[$key]['price'] = sprintf("%.2f", $goodsMemberPrice);
- if (intval($value['is_negotiable']) === 1) {
- $goods_list[$key]['price'] = '价格面议';
- }
- }
- // 检测商品是不是有团购活动 因为一个商品只能添加在一个团购活动中
- // 团购活动不参与会员折扣!!
- foreach($goods_list as $key => $value){
- $activityModel = MdGroupActivitiesGoods::find()->alias('mgag')->leftJoin(['mga'=>MdGroupActivities::tableName()],'mga.id=mgag.activities_id')->where(['goods_id'=>$value['id'],'mgag.is_delete'=>0])->andWhere(['>=','mga.end_time',time()])->andWhere(['mga.is_delete'=>0])->asArray()->select('mgag.*,mga.start_time,mga.end_time')->one();
- if($activityModel){
- if($activityModel['start_time'] > time()){
- $goods_list[$key]['status'] = ['status'=>0,'time'=>$activityModel['start_time'] - time()];
- }else{
- $goods_list[$key]['status'] = ['status'=>1,'time'=>$activityModel['end_time'] - time()];
- }
- $goods_list[$key]['price'] = sprintf("%.2f", $activityModel['price']);
- // 计算购物车的商品数量
- $goods_list[$key]['cart_info']['cart_num'] = get_user_id() > 0 ? Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'goods_id'=>$value['id'],'is_delete'=>0,'user_id'=>get_user_id()])->andWhere(['>','num',0])->sum('num') : 0;
- $goods_list[$key]['cart_info']['cart_id'] = get_user_id() > 0 ? Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'goods_id'=>$value['id'],'is_delete'=>0,'user_id'=>get_user_id()])->andWhere(['>','num',0])->select('id')->one()->id : 0;
- }
- $goods_list[$key]['take_price'] = 0;
- $goods_list[$key]['send_price'] = 0;
- if ($is_show_alliance_coupon) {
- if ($value['goods_take_price'] > 0) {
- $goods_list[$key]['take_price'] = \round($goods_list[$key]['price'] * $value['goods_take_price'] / 100, 2);
- }
- if ($value['goods_send_profit'] > 0) {
- $goods_list[$key]['send_price'] = \round($goods_list[$key]['price'] * $value['goods_send_profit'] / 100, 2);
- }
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'row_count' => isset($count) ? $count: 0,
- 'page_count' => isset($pagination) ? $pagination->pageCount : 0,
- 'list' => $goods_list
- ]
- ];
- }
- /**
- * 获取门店列表
- */
- public static function getShop($requery)
- {
- $shop_id = Json::decode($requery['shop_id']);
- $query = Shop::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- ])->andWhere([
- '>',
- 'user_id',
- 0
- ]);
- if ($shop_id) {
- $query->andWhere([
- 'id' => $shop_id
- ]);
- } else {
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page']]);
- $query->offset($pagination->offset)->limit($pagination->limit)
- ->orderBy(['id' => SORT_DESC]);
- }
- $shop_list = $query->select(['*'])
- ->asArray()->all();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $shop_list
- ]
- ];
- }
- /**
- * 获取门店列表
- */
- public static function getNearbyShop($requery)
- {
- $query = Shop::find()->alias('s')
- ->leftJoin(['c' => Clerk::tableName()], 's.id=c.shop_id')
- ->where([
- 's.store_id' => get_store_id(),
- 's.is_delete' => 0
- ])->andWhere(['>','c.shop_id',0]);
- $count = $query->count();
- $p = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page'] - 1]);
- $shop_table_name = Shop::tableName();
- $user_table_name = Clerk::tableName();
- $start = ($requery['page'] - 1) * $requery['limit'];
- $store_id = get_store_id();
- $sql = "SELECT `s`.*, acos(cos({$requery['latitude']}*pi()/180 )*cos(s.latitude*pi()/180)*cos({$requery['longitude']}*pi()/180 -s.longitude*pi()/180)+sin({$requery['latitude']}*pi()/180 )*sin(s.latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} `s` LEFT JOIN {$user_table_name} `c` ON s.id=u.shop_id WHERE ((`s`.`store_id`={$store_id}) AND (`s`.`is_delete`=0)) AND (`c`.`shop_id` > 0) ";
- $sql .= "ORDER BY `distance` LIMIT {$start},{$requery['limit']}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $list
- ]
- ];
- }
- /**
- * 获取门店列表
- */
- public static function getMch($requery)
- {
- $shop_id = Json::decode($requery['shop_id']);
- $query = Mch::find()->alias('m')
- ->where([
- 'm.is_delete' => 0,
- 'm.store_id' => get_store_id()
- ])->andWhere([
- '>',
- 'u.shop_id',
- 0
- ]);
- if ($shop_id) {
- $query->andWhere([
- 's.id' => $shop_id
- ]);
- } else {
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page']]);
- $query->offset($pagination->offset)->limit($pagination->limit)
- ->orderBy(['s.id' => SORT_DESC]);
- }
- $shop_list = $query->select(['s.*'])
- ->asArray()->all();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $shop_list
- ]
- ];
- }
- /**
- * 获取门店列表
- * @param $requery
- * @return array
- */
- public static function getNearbyMch($requery)
- {
- $mch_table = Mch::tableName();
- $start = ($requery['page'] - 1) * $requery['limit'];
- $store_id = get_store_id();
- $sql = "SELECT `id`,`name`,`header_bg`,`longitude`,`latitude`,`logo`, acos(cos({$requery['latitude']}*pi()/180 )*cos(latitude*pi()/180)*cos({$requery['longitude']}*pi()/180 -longitude*pi()/180)+sin({$requery['latitude']}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as distance FROM {$mch_table} WHERE `store_id`={$store_id} AND `is_delete`=0 AND `is_lock`=0 AND `review_status`=1 AND `is_open`=1 ";
- $sql .= "ORDER BY `distance` LIMIT {$start},{$requery['limit']}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach($list as &$val) {
- $val['goods_list'] = Goods::find()->where([
- 'mch_id' => $val['id'],
- 'status' => 1,
- 'is_delete' => 0
- ])->select(['cover_pic', 'id', 'name', 'original_price', 'price'])->limit(10)->all();
- // 获取评分
- $where = [
- '>',
- 'created_at',
- time() - 30 * 24 * 60 * 60
- ];
- $query = OrderComment::find()->where([
- 'mch_id' => $val['id'],
- ])->andWhere($where);
- $count = $query->count();
- if ($count > 0) {
- $commit_sum = $query->sum('score');
- $val['score'] = round($commit_sum/$count,1);
- } else {
- $val['score'] = round(5,1);
- }
- // 统计销量
- $order_num = Order::find()->where([
- 'mch_id' => $val['id'],
- ])->andWhere($where)->count();
- $val['order_num'] = self::setNum($order_num);
- }
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $list
- ]
- ];
- }
- private static function setNum($num){
- if ($num > 10000) {
- return round($num/10000).'万+';
- } elseif($num > 1000) {
- return round($num/1000).'千+';
- } else {
- return $num;
- }
- }
- }
|