activity_cut_price_order_id) { $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id); $this->store_list = json_decode($activityOrder['order_mch_list'], true); if ($activityOrder) { $this->store_id = $activityOrder->store_id; } } } public function rules() { $rules = [ ['store_list', 'required'], ['address_id', 'integer'], ['verify_card_id', 'integer'], ['order_type', 'integer'], [['order_type',], 'default', 'value' => 0], ['store_list', function ($attr, $params) { if(!is_array($this->store_list)){ $data = Json::decode($this->store_list); if (!$data) { $this->addError($attr, "{$attr}数据格式错误。"); } $this->store_list = $data; } }], ['store_list', function ($attr, $params) { foreach ($this->store_list as $i => &$store) { if (!is_array($store['goods_list'])) { $this->addError($attr, "{$attr}[{$i}]['goods_list']必须是一个数组。"); return; } } }], [['longitude', 'latitude', '_from'], 'trim'], [['_from'], 'in', 'range' => ['app', 'mini']], [['activity_cut_price_order_id', 'is_seckill', 'order_no'], 'safe'], ]; return $rules; } public function afterValidate() { //$this->user = User::findOne($this->user_id); $saas_user = get_saas_user(); $this->user = User::findOne(['binding'=>$saas_user->mobile,'store_id'=>get_store_id(),'is_delete'=>0]); //$this->level = $this->getLevelData(); $this->address = $this->getAddressData(); $this->integral = [ 'forehead' => 0, 'forehead_integral' => 0, 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'store')['value'])['value'] ]; parent::afterValidate(); } protected function getStoreListData($submit = false) { $submit || $this->reInitMchList(); //处理多店铺订单,前端实现比较困难先注释 start // $goods_list = $this->getGoodsList($this->store_list[0]['goods_list'], $submit, 1); // // $store_list = []; // foreach ($goods_list as $goods_index => $goods_item) { // $store_list[$goods_index] = ['mch_id' => 0, 'store_id' => $goods_item['store_id']]; // foreach ($goods_item['goods_list'] as $goods_item_) { // $store_list[$goods_index]['goods_list'][] = ['goods_id' => $goods_item_['goods_id'], 'attr' => $goods_item_['attr_list'], 'num' => $goods_item_['num']]; // } // } // $this->store_list = $store_list; //End foreach ($this->store_list as $i => &$mch) { $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit); if (isset($mch['goods_list']['code'])) { throw new \Exception($mch['goods_list']['msg'], 1); } $shop_count_result = $this->checkCatShopCount($mch['goods_list']); if ($shop_count_result['code'] !== 0) { return $shop_count_result; } if ($this->store_list[0]['store_id']) { $storeInfo = Store::find()->where(['id'=>$this->store_list[0]['store_id'],'is_delete'=>0])->asArray()->one(); if(!$storeInfo){ throw new \Exception('商城信息有误', 1); } } else { $storeInfo['name'] = '平台'; } $mch['name'] = $storeInfo['name']; $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value']; $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type); // $send_type = $send_type ? Json::decode($send_type['value']) : []; $send_type = !empty($send_type['value']) ? Json::decode($send_type['value']) : ["express" => ["text" => "快递", "value" => 1]]; $send_type_arr = []; if (isset($send_type['express']['value']) && $send_type['express']['value']) { $send_type_arr[] = 'express'; } //商盟没有 店铺配送和同城配送 if (isset($send_type['shop']['value']) && $send_type['shop']['value']) { $send_type_arr[] = 'shop'; } if ((isset($send_type['delivery']['value']) && $send_type['delivery']['value']) && $submit == false) { $send_type_arr[] = 'delivery'; } //商盟没有 店铺配送和同城配送 if (in_array($mch['product_type'], [Goods::GOODS_TYPE_DATE, Goods::GOODS_TYPE_TIME])) { $send_type_arr = ['shop']; } //Yii::error($send_type_arr); $mch['send_type'] = $send_type_arr; if (in_array('shop', $send_type_arr)) { $shopArr = $this->getShopList(); $mch['is_shop'] = $shopArr['shop']; $mch['shop_list'] = $shopArr['list']; } else { $mch['shop_list'] = []; $mch['is_shop'] = ''; } $total_price = 0; $level_price = 0; $integral = [ 'forehead' => 0, 'forehead_integral' => 0 ]; $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0; $open = false; if ($this->is_seckill) { foreach ($mch['goods_list'] as &$mch_goods) { $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $mch_goods['goods_id']]) ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id') ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => get_store_id()]]) ->select('sg.is_use_coupon')->asArray()->one(); if ($seckill_activity_goods && (int)$seckill_activity_goods['is_use_coupon'] === 0) { $open = true; } } } $mch['express_price'] = 0; foreach ($mch['goods_list'] as &$_goods) { // if (in_array($_goods['product_type'], [1, 2]) && $submit == true) { // $res = $this->bookCheckGoodsNum($_goods, $_goods['product_type']); // if ($res['code'] != 0) { // throw new \Exception($res['msg'], 1); // } // } if ($submit == false) { $_goods['form'] = $this->getNewFormData($_goods); } else { $_goods['form'] = $this->getForm($_goods['form']); } $total_price += doubleval($_goods['price']); $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']); $_goods['integral'] = [ 'forehead' => doubleval($_goods['resIntegral']['forehead']), 'forehead_integral' => doubleval($_goods['resIntegral']['forehead_integral']) ]; $integral['forehead'] += doubleval($_goods['resIntegral']['forehead']); $integral['forehead_integral'] += doubleval($_goods['resIntegral']['forehead_integral']); unset($_goods['is_form']); unset($_goods['form_name']); //如果是酒店就检测商品数量 $goods_ = Goods::findOne($_goods['goods_id']); if ($goods_->product_type == Goods::GOODS_TYPE_DATE) { //判断是否有库存 $num = Goods::getGoodsNum($goods_)['data']; if (isset($mch['list'])) { foreach ($mch['list'] as $iattr) { $idate = strtotime($iattr['date']); foreach ($num as $inum) { $indate = strtotime($inum['date']); if(($idate == $indate) && ($inum['num'] < $_goods['num'])){ return ['code' => 1, 'msg' => $iattr['date'].'库存不足']; } } } } } } $mch['total_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $total_price); $mch['level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $level_price); $mch['diy_send_type'] = [ [ 'key' => 'express', 'name' => Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送')['value'] ], [ 'key' => 'shop', 'name' => Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送')['value'] ], [ 'key' => 'delivery', 'name' => Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送')['value'] ] ]; $integral['forehead'] = round($integral['forehead'], 2); if ($integral['forehead'] > $mch['level_price']) { $integral['forehead'] = 0; $integral['forehead_integral'] = 0; } $mch['integral'] = !empty($this->verify_card_id) ? [] : $integral; $this->getCouponList($mch); $mch['express_price'] = $this->getExpressPrice($mch); if($open){ //积分兑换商品不能使用优惠券 或 秒杀活动中不允许使用优惠券则所有商品不允许使用优惠券 $mch['coupon_list'] = []; }else{ $this->getCouponList($mch); } $mch['offer_rule'] = $this->getOfferRule($mch); $mch['is_area'] = $this->getTerritorialLimitation($mch); // 如果是点餐的商品,结构需要变一下 if ( (!empty(input_params('flag_id')) || !empty(input_params('table_num'))) && !$submit) { $saas_list = []; $saas_ids = array_unique(array_column($mch['goods_list'], 'saas_id')); foreach ($saas_ids as $saas_id) { $goods_list = []; foreach ($mch['goods_list'] as $item) { if ($item['saas_id'] == $saas_id) { $goods_list[] = $item; } } //$saas_user = SaasUser::findOne(['mobile' => User::findOne($user_id)->binding]); $saas_user = SaasUser::findOne(['id'=>$saas_id]); $saas_list[] = [ 'name' => $saas_user->name, 'avatar' => $saas_user->avatar, 'user_id' => 0, 'saas_id' => $saas_id, 'goods_list' => $goods_list ]; } $mch['goods_list'] = $saas_list; } } return $this->store_list; } protected function getGoodsList($goods_list, $submit, $type = 0) { // 获取卡券绑定的运费模板id $VerifyCardSale = VerifyCardSale::findOne(['id' => $this->verify_card_id]); $VerifyCard = null; if ($VerifyCardSale) { $VerifyCard = VerifyCard::findOne(['id' => $VerifyCardSale['verify_card_id']]); } $goodsIds = []; $cards = []; $levelDiscount = 10; $saasInfo = get_saas_user(); $time = time(); // //如果是会员获取会员商品折扣 // $levelInfo = BusinessMemberOrder::find()->where(['is_pay'=>1,'is_delete'=>0,'saas_id'=>$saasInfo['id']])->andWhere([">=","begin_time",$time])->andWhere(["<","end_time",$time])->asArray()->all(); // if(count($levelInfo) > 0){ // $memberIds = $levelType = []; // foreach($levelInfo as $val){ // if(!in_array($val['member_id'], $memberIds)){ // $memberIds[] = $val['member_id']; // } // $levelType[$val['member_id'].'_'.$val['member_type']] = 1000; // } // $memberInfo = BusinessMember::find()->where(['id'=>$memberIds,'is_delete'=>0,'status'=>1])->asArray()->all(); // if(count($memberInfo)>0){ // foreach($memberInfo as $val){ // if($val['sale_info']){ // $saleInfo = json_decode($val['sale_info'],true); // foreach($saleInfo as $v){ // if($levelType[$val['id'].'_'.$v['type']] == 1000){ // $levelType[$val['id'].'_'.$v['type']] = $v['discount']; // } // } // } // } // foreach($levelType as $v){ // if( $v < $levelDiscount) $levelDiscount = $v; // } // } // } $take_price = 0; $send_price = 0; foreach ($goods_list as $i => &$item) { if (isset($item['cart_id'])) { $cart = BusinessCart::findOne([ // 'store_id' => get_store_id(), 'id' => $item['cart_id'], 'is_delete' => 0 ]); if (!$cart) { unset($goods_list[$i]); continue; } $item['num'] = $cart->num; $attr_id_list = (array)Json::decode($cart->attr); $goods = Goods::findOne($cart->goods_id); } elseif (isset($item['food_cart_id'])) { $cart = FoodCart::findOne([ 'id' => $item['food_cart_id'], 'status' => 1, 'is_delete' => 0 ]); if (!$cart) { unset($goods_list[$i]); continue; } $item['num'] = $cart->num; $attr_id_list = (array)Json::decode($cart->attr); $goods = Goods::findOne($cart->goods_id); $user_id = $cart->user_id; $saas_id = $cart->saas_id; } elseif (isset($item['goods_id'])) { $attr_id_list = []; foreach ($item['attr'] as $_a) { if ($_a['attr_id'] > 0) { array_push($attr_id_list, $_a['attr_id']); } } // $store_id = [0, -1]; // if (get_store_id() > 0) { // $store_id = get_store_id(); // } $goods = Goods::findOne([ // 'store_id' => $store_id, 'id' => $item['goods_id'], ]); } else { unset($goods_list[$i]); continue; } if (!$goods) { unset($goods_list[$i]); continue; } // $cat_id_arr = GoodsCat::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('cat_id')->column(); // $shop_count = Cat::find()->where(['id' => $cat_id_arr])->andWhere(['>', 'shop_count', 0])->select('shop_count')->min('shop_count') ?: 0; // if ($shop_count > 0 && $shop_count > $item['num']) { // return [ // 'code' => 1, // 'msg' => $goods->name . "起购数量为" . $shop_count, // ]; // } //商品限购数量 if (($goods->confine_count && $goods->confine_count > 0)) { $goodsNum = Goods::getSaasBuyNum($saasInfo, $goods->id); if ($goodsNum) { } else { $goodsNum = 0; } $goodsTotalNum = intval($goodsNum + $item['num']); if ($goodsTotalNum > $goods->confine_count) { throw new \Exception('商品:' . $goods->name . ' 只允许购买' . $goods->confine_count . $goods->unit, 1); } } //起订数量 if (($goods['order_min_count'] && $goods['order_min_count'] > 0)) { if ($item['num'] < $goods['order_min_count']) { if($submit){ throw new \Exception('商品:' . $goods->name . ' 起订数量' . $goods['order_min_count'], 1); }else{ $item['errors']['order_min_count'] = $goods['order_min_count']; } } } $attr_info = $goods->getAttrInfo($attr_id_list); // 排除当面付检测库存 if ((in_array($goods->type, [2, 6]) == false && !in_array($goods->product_type, [1, 2]) && $item['num'] > $attr_info['num']) || $item['num'] <= 0) { //库存不足 unset($goods_list[$i]); continue; } $cards = []; if (!empty($goods->verify_card_id)) { $ids = explode(',', $goods->verify_card_id); $cards = Tools::getVerifyList($ids); } else { $cards = []; } $store_id = $goods->store_id; if ($store_id <= 0) { $store_id = [0, -1]; } $attr_list = Attr::find()->alias('a') ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name') ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id') ->where(['a.id' => $attr_id_list, 'ag.store_id' => $store_id]) ->asArray()->all(); $item['cost_price'] = $attr_info['cost_price']; $item['user_id'] = !empty($user_id) ? $user_id : 0; $item['saas_id'] = !empty($saas_id) ? $saas_id : 0; $item['attr_list'] = $attr_list; $item['goods_id'] = $goods->id; $item['store_id'] = $goods->store_id; $item['mch_id'] = $goods->mch_id; $item['rate'] = $goods->rate; $item['rate_type'] = $goods->rate_type; $item['chain_rate_type'] = $goods->chain_rate_type; $item['chain_rate'] = $goods->chain_rate; $item['goods_name'] = $goods->name; $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic; //如果没有使用规格,则使用商品售价 否则使用规格售价 $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num'])); //$item['level_price'] = sprintf('%.2f', ( $item['price'] * ($levelDiscount / 10) ) ); $item['level_price'] = $item['price']; //商盟的会员价 全部通过返利的方式发放 $item['single_price'] = !empty($this->verify_card_id) ? 0 : sprintf('%.2f', $attr_info['price']); $item['weight'] = $goods->weight; if (!empty($this->verify_card_id)) { $item['integral'] = 0; } else { $item['integral'] = $goods->integral ? $goods->integral : 0; } $item['integral_price'] = $goods->integral_price; $item['freight'] = !empty($VerifyCard['freight_id']) ? $VerifyCard['freight_id'] : $goods->freight; $item['full_cut'] = $goods->full_cut; $item['goods_cat_id'] = $goods->cat_id; $item['id'] = $goods->id; $item['verify_card_id'] = $cards; $item['type'] = $goods->type; $item['delivery_type'] = Json::decode($goods->delivery_type); if (in_array($goods->product_type, [Goods::GOODS_TYPE_DATE, Goods::GOODS_TYPE_TIME])) { $item['delivery_type'] = ['shop']; } // // 当前选择的规格 $attrIdArr = []; foreach ($item['attr_list'] as $attrListItem) { $attrIdArr[] = $attrListItem['attr_id']; } $GoodsFullMinus = GoodsFullMinus::find() ->select('*') ->where(['goods_id' => $goods->id, 'is_delete' => 0]) ->orderBy('full_minus_num ASC')->asArray()->all(); // // 门店逻辑 $attr = $goods->attr; $price = $goods->price; // if (get_md_id() && empty(input_params('flag_id'))) { // $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]); // if ($md_goods) { // $attr = $md_goods->attr; // $price = $md_goods->price; // } // } $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([ 'attr' => $attr, 'price' => $price, 'is_level' => $goods->is_level, 'mch_id' => $goods->mch_id, 'full_minus' => $GoodsFullMinus, 'unit' => $goods->unit, 'use_attr' => $goods->use_attr ], $attrIdArr, [], $item['num']); // $item['batch_price_tips'] = $res['batch_price_tips']; // $item['current_batch_price_tips'] = $res['current_batch_price_tips']; $item['price'] = sprintf('%.2f', ($res['price'] * $item['num'])); $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num'])); $item['is_level'] = $res['is_level']; if(isset($this->user->id) && $this->user->id > 0){ $integralArr = $this->getIntegral((object)$item, Option::get(OptionSetting::STORE_INTEGRAL, $goods->store_id, 'gift', Option::get(OptionSetting::STORE_INTEGRAL, $goods->store_id, 'store')['value'])['value'], $goodsIds); $item['give'] = $integralArr['give']; $item['resIntegral'] = $integralArr['resIntegral']; }else{ $item['give'] = 0; $item['resIntegral'] = ['forehead'=>0,'forehead_integral'=>0]; } $goodsIds[] = $goods->id; $item['goods_card_list'] = Goods::getGoodsCard($goods->id); $item['is_form'] = $goods->is_form; $item['form_name'] = $goods->form_name; $item['product_type'] = $goods->product_type; $item['send_price'] = $item['take_price'] = 0; if ($store_id > 0) { $store = Store::findOne($store_id); $item['send_price'] = bcmul($goods->goods_send_profit * 0.01, $item['level_price'], 2); $item['take_price'] = bcmul($goods->goods_take_price * 0.01, $item['level_price'], 2); $saas_user = SaasUser::findOne($saasInfo->id); $user_league_price = 0; if ($saas_user) { $user_league_price = $saas_user->league_price; } if(bcadd($take_price, $item['take_price'], 2) > $user_league_price){ $item['take_price'] = 0; } if (bcadd($send_price, $item['send_price'], 2) > $store->league_price) { $item['send_price'] = 0; } $send_price += $item['send_price']; $take_price += $item['take_price']; } $res_acnu = $this->getActivityNewUserGoodsPrice($res['price'], $goods, $attr_id_list, $item['num']); if ($res_acnu['code'] === 0) { $item['is_level'] = 0; if ($res_acnu['price'] > 0) { $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_acnu['price'])); $item['price_str'] = $res_acnu['price_str']; } } else { return $res_acnu; } if($this->activity_cut_price_order_id){ $res_accp = $this->getActivityCutPriceGoodsPrice($goods, $attr_id_list, $item['num']); // var_dump($res_accp);die; if ($res_accp['code'] === 0) { $item['is_level'] = 0; if ($res_accp['price'] > 0) { $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_accp['price'])); $item['price_str'] = $res_accp['price_str']; } } else { return $res_accp; } } if ($this->is_seckill) { $res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']); if ($res['code'] === 0) { $item['is_level'] = 0; if ($res['data'] > 0) { $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num'])); } } else { return $res; } } } // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题 if ($type) { $goods_store_id = array_column($goods_list, 'store_id'); $goods_store_id = array_unique($goods_store_id); $new_mch = []; foreach ($goods_store_id as $store_id_item) { foreach ($goods_list as $goods_item) { if ($goods_item['store_id'] == $store_id_item) { $new_mch[$store_id_item]['store_id'] = $store_id_item; $new_mch[$store_id_item]['goods_list'][] = $goods_item; } } } return array_values($new_mch); } return array_merge($goods_list, []); } //砍价商品处理 public function getActivityCutPriceGoodsPrice($goods, $attr, $num) { try { $price_str = []; $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id); if(!$activityOrder){ throw new \Exception('砍价活动订单信息不存在'); } $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $activityOrder->activity_id, 'goods_id' => $goods->id, 'is_delete' => 0]); if(!$goods_ext){ throw new \Exception('砍价商品信息不存在'); } $ext_attrs = json_decode($goods_ext['attr'], true); foreach ($ext_attrs as $ext_attr) { $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id'); if (empty(array_diff($ext_attr_id, $attr))) { if($ext_attr['num'] < $num){ throw new \Exception('砍价商品库存不足'); } } } //全部享受优惠 $price = $activityOrder->pay_price; $price_str[] = '砍价优惠价¥' . sprintf('%.2f', $price); return [ 'code' => 0, 'msg' => '获取成功', 'price' => $price, 'price_str' => implode('+', $price_str), 'id' => $activityOrder->id, ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //新人专享商品处理 public function getActivityNewUserGoodsPrice($level_price, $goods, $attr, $num) { try { $price = 0; $price_str = []; $activity = ActivityNewUser::activityAt($goods->store_id); $open = false; if($activity){ $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $goods->id, 'is_delete' => 0]); if($goods_ext){ $ext_attrs = json_decode($goods_ext['attr'], true); foreach ($ext_attrs as $ext_attr) { $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id'); //与购物车添加的规格id数组比较 if (empty(array_diff($ext_attr_id, $attr))) { $ext_price = $ext_attr['price']; $id = $goods_ext['id']; $open = true; } } if ($open) { $buy_limit = $activity->buy_limit; if (($buy_limit && $buy_limit > 0)) { // $goodsNum = 0; //部分享受优惠 $user = User::findOne(['binding' => get_saas_user()->mobile, 'store_id' => $goods->store_id, 'is_delete' => 0]); $goodsNum = intval(Goods::getBuyNum($user, $goods->id)); $diff_num = $buy_limit - $goodsNum;//1 - 1 if ($diff_num > 0) { $buy_num = $num - $diff_num; if ($buy_num > 0) { $price += ($diff_num * $ext_price) + ($buy_num * $level_price); $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $diff_num; } else { $price += $num * $ext_price; $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num; } } }else{ //全部享受优惠 $price = $ext_price * $num; $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num; } } } } return [ 'code' => 0, 'msg' => '获取成功', 'price' => $price, 'price_str' => implode('+', $price_str), 'id' => $id ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //秒杀商品处理 public function getSceKillGoodsPrice($goods, $attr, $submit, $num) { $t = \Yii::$app->db->beginTransaction(); try { //秒杀订单 $sql = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id]) ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id') ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => $goods->store_id]]) ->select('sag.id sag_id, sg.id, sg.end_time, sag.attr, sag.use_attr, sag.seckill_num, sag.seckill_price, sag.sale_num, sg.order_limit_num, sg.self_limit_num')->createCommand()->getRawSql(); $sql = $sql . ' FOR UPDATE'; $seckill_activity_goods = Yii::$app->db->createCommand($sql)->queryOne(); $price = 0; $id = 0; if (!empty($seckill_activity_goods)) { $user = User::findOne(['binding' => get_saas_user()->mobile, 'store_id' => $goods->store_id, 'is_delete' => 0]); $sum = SeckillActivityOrderLog::find() ->where(['activity_goods_id' => $seckill_activity_goods['sag_id'], 'store_id' => $goods->store_id, 'user_id' => $user->id, 'is_delete' => 0]) ->count('num'); if ($seckill_activity_goods['self_limit_num'] > 0 && ($sum * 1 + $num) > $seckill_activity_goods['self_limit_num']) { throw new \Exception("购买数量超出限购数量"); } if ($seckill_activity_goods['order_limit_num'] > 0 && $num > $seckill_activity_goods['order_limit_num']) { throw new \Exception("购买数量超出订单限购数量"); } //如果商品使用规格 且 已经查找到活动 // if ((int)$goods->use_attr === 1 && !empty($seckill_activity_goods['attr'])) { //解码 $seckill_activity_goods['attr'] = $sag_attrs = json_decode($seckill_activity_goods['attr'], true); //获取设置的商品秒杀规格id数组与购物车添加的规格id数组比较 // $sag_attrs = array_column($sag_attrs, 'attr_list'); //假设没有对应的规格信息 $open = false; foreach ($sag_attrs as $sag_attr) { $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id'); // $sag_attr['seckill_num'] -= $num; //与购物车添加的规格id数组比较 if (empty(array_diff($sag_attr_id, $attr))) { if ((int)$sag_attr['seckill_num'] >= $num) { $price = $sag_attr['seckill_price']; $id = $seckill_activity_goods['sag_id']; } else { throw new \Exception("当前秒杀商品库存不足"); } } } // } else { // if ($seckill_activity_goods['seckill_num'] >= ($seckill_activity_goods['sale_num'] + $num)) { // $price = $seckill_activity_goods['seckill_price']; // $id = $seckill_activity_goods['sag_id']; // } else { // throw new \Exception("当前秒杀商品库存不足"); // } // } if ($submit && $id > 0) { $activity_goods = SeckillActivityGoods::findOne($id); $activity_goods_attr = json_decode($activity_goods->attr, true); foreach ($activity_goods_attr as &$sag_attr) { $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id'); // $sag_attr['seckill_num'] -= $num; //与购物车添加的规格id数组比较 if (empty(array_diff($sag_attr_id, $attr))) { $sag_attr['seckill_num'] = $sag_attr['seckill_num'] - $num; } } $activity_goods->attr = json_encode($activity_goods_attr); $activity_goods->sale_num = ($activity_goods->sale_num * 1 + $num); $activity_goods->seckill_num = ($activity_goods->seckill_num - $num); if (!$activity_goods->save()) { throw new \Exception(json_encode($activity_goods->errors)); } } } $t->commit(); return [ 'code' => 0, 'msg' => '获取成功', 'data' => $price, 'id' => $id ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //自定义表单 protected function getFormData() { $new_list = []; $new_list['is_form'] = Option::get('is_form', $this->store_id, 'store', 0)['value']; $form_list = []; if ($new_list['is_form'] == 1) { $new_list['name'] = Option::get('form_name', $this->store_id, 'store', '表单信息')['value']; $form_list = Form::find()->where([ 'store_id' => $this->store_id, 'is_delete' => 0, ])->orderBy(['sort' => SORT_ASC])->asArray()->all(); foreach ($form_list as $index => $value) { if (in_array($value['type'], ['radio', 'checkbox'])) { $default = str_replace(",", ",", $value['default']); $list = explode(',', $default); $default_list = []; foreach ($list as $k => $v) { $default_list[$k]['name'] = $v; if ($k == 0) { if ($value['type'] == 'radio') { $form_list[$index]['default'] = $v; } else { $form_list[$index]['default'] = [$v]; } $default_list[$k]['checked'] = true; } else { $default_list[$k]['checked'] = false; } } $form_list[$index]['default_list'] = $default_list; } } } $new_list['list'] = $form_list; return $new_list; } //自定义表单 protected function getNewFormData($goods) { $new_list = []; $new_list['is_form'] = $goods['is_form']; $form_list = []; $store_id = $this->store_id; if ($this->store_id <= 0) { $store_id = [0, -1]; } if ($new_list['is_form'] == 1) { $new_list['name'] = $goods['form_name']; $form_list = Form::find()->where([ 'store_id' => $store_id, 'is_delete' => 0, 'goods_id' => $goods['id'] ])->orderBy(['sort' => SORT_ASC])->asArray()->all(); foreach ($form_list as $index => $value) { if (in_array($value['type'], ['radio', 'checkbox'])) { $default = str_replace(",", ",", $value['default']); $list = explode(',', $default); $default_list = []; foreach ($list as $k => $v) { $default_list[$k]['name'] = $v; if ($k == 0) { if ($value['type'] == 'radio') { $form_list[$index]['default'] = $v; } else { $form_list[$index]['default'] = [$v]; } $default_list[$k]['checked'] = true; } else { $default_list[$k]['checked'] = false; } } $form_list[$index]['default_list'] = $default_list; } } } $new_list['list'] = $form_list; return $new_list; } protected function getAddress() { if (!$this->address) { if ($this->address_id) { $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]); } else { $this->address = Address::find()->where([ 'user_id' => get_saas_user_id(), 'is_default' => 1, 'is_delete' => 0, ])->limit(1)->one(); } } return (object)$this->address; } //获取收货地址,有address_id优先获取,没有则获取默认地址 protected function getAddressData() { $address = $this->getAddress(); if (isset($address->id)) { return [ 'id' => $address->id, 'name' => $address->name, 'mobile' => $address->mobile, 'province_id' => $address->province_id, 'province' => $address->province, 'city_id' => $address->city_id, 'city' => $address->city, 'district_id' => $address->district_id, 'district' => $address->district, 'detail' => $address->detail, 'is_default' => $address->is_default, 'latitude' => $address->latitude, 'longitude' => $address->longitude, ]; } else { return null; } } //获取支付方式 public function getPayTypeList() { $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']); $pay_type_list = Json::decode($pay_type_list_json['value']); $new_list = []; $ok = true; $is_virtual = false; // foreach ($this->store_list as $mch) { // if ($mch['goods_list'][0]['product_type'] == 3) { // $is_virtual = true; // } // if (isset($mch['mch_id']) && $mch['mch_id'] == 0) { // continue; // } else { // $ok = false; // break; // } // } if (is_wechat_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } //如果是微信且商城已配置通联支付进件数据 if (is_wechat_platform()) { $store = Store::findOne(get_store_id()); if (!empty($store->cusid)) { $new_list[] = [ 'name' => '通联支付', 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } } if (is_alipay_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png' ]; } foreach ($new_list as $index => $item) { foreach ($pay_type_list as $pay_type_index => $pay_type_item) { if ($item['payment'] === 1 && $pay_type_index === 'wechat') { if (intval($pay_type_item['value']) === 0) { unset($new_list[$index]); } } if ($item['payment'] === 4 && $pay_type_index === 'alipay') { if (intval($pay_type_item['value']) === 0) { unset($new_list[$index]); } } if ($item['payment'] === Order::PAY_TYPE_YUNST_WECHAT_PAY && $pay_type_index === Order::PAY_TYPE_KEY_YUNST_WECHAT_PAY) { if (intval($pay_type_item['value']) === 0) { unset($new_list[$index]); } } } } // if (is_toutiao_platform()) { // $new_list[] = [ // 'name' => '线上支付', // 'payment' => 5, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' // ]; // } // foreach ($pay_type_list as $index => $value) { // if ($index == 'huodao' && $value['value'] == 1 && $ok && !$is_virtual) { // $new_list[] = [ // 'name' => '货到付款', // 'payment' => 2, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png' // ]; // } // if ($index == 'friend' && $value['value'] == 1) { // $new_list[] = [ // 'name' => '朋友代付', // 'payment' => 7, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png' // ]; // } // } return array_values($new_list);; } /** * 支付方式 * @param array $is_payment //支付方式 * @param array $ignore //忽略的支付方式 * @return array */ public static function getPayType($is_payment = array(), $ignore = array()) { if (!$is_payment || empty($is_payment)) { $default = '{"wechat":{"value":1}}'; if (is_alipay_platform()) { $default = '{"alipay":{"value":1}}'; } if (is_toutiao_platform()) { $default = '{"toutiao":{"value":1}}'; } $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['value']); $is_payment = Json::decode($pay_str['value']); } $pay_type_list = []; foreach ($is_payment as $index => $value) { if (in_array($index, $ignore)) { continue; } if ($index == 'wechat' && $value['value'] == 1 && is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' ]; } // if ($index == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) { // $pay_type_list[] = [ // 'name' => '线上支付', // 'payment' => 5, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' // ]; // } // if ($index == 'huodao' && $value['value'] == 1) { // $pay_type_list[] = [ // 'name' => '货到付款', // 'payment' => 2, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png' // ]; // } if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP || is_alipay_platform()) { if ($index == 'alipay' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png' ]; } } // if ($index == 'balance' && $value['value'] == 1) { // $pay_type_list[] = [ // 'name' => '账户余额支付', // 'payment' => 3, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png' // ]; // } } if (!$pay_type_list) { if (is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } if (is_toutiao_platform()) { $pay_type_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } } return $pay_type_list; } protected function getCouponList(&$mch) { if (get_md_id()) { $mch['coupon_list'] = []; return; } if (!isset($mch['mch_id'])) { $mch['mch_id'] = 0; } $goods_total_price = $mch['total_price']; $cat_ids = $this->getCatIdList($mch['goods_list'],$mch['mch_id']); $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']); $list = SaasCoupon::find()->alias('uc') ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id') //->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id') ->where([ 'AND', ['uc.is_delete' => 0], ['uc.is_use' => 0], ['c.mch_id' => $mch['mch_id']], ['uc.is_expire' => 0], ['uc.saas_id' => get_saas_user_id()], ['<=', 'c.min_price', $goods_total_price], ['c.store_id'=>get_store_id()], ]) ->andWhere($mch['plugin_type'] == 6 ? ['c.appoint_type' => 3] : [ 'OR', ['<>', 'c.appoint_type', 3], 'c.appoint_type IS NULL' ]) //cas.event, 暂时去掉 ->select('c.name, c.expire_day, c.id, c.expire_type, c.discount_type, c.discount, c.discount_max_price, uc.id user_coupon_id,c.mch_id,c.rule,c.sub_price,c.min_price,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list') ->asArray()->all(); // $events = [ // 0 => '平台发放', // 1 => '分享红包', // 2 => '购物返券', // 3 => '领券中心', // ]; $max_price = 0; if ($mch['plugin_type'] == 6) { $max_price = $mch['total_price']; } else { foreach ($mch['goods_list'] as $v) { $max_price += $v['price']; } } $new_list = []; foreach ($list as $i => $item) { if ($item['expire_type'] == 1) { if ($item['end_time'] > 0) { if (time() > $item['end_time']) { continue; } } } if ($item['expire_type'] == 2) { if (time() > $item['end_time'] || time() < $item['begin_time']) { continue; } } $list[$i]['status'] = 0; if (isset($item['is_use']) && $item['is_use']) { $list[$i]['status'] = 1; } if (isset($item['is_expire']) && $item['is_expire']) { $list[$i]['status'] = 2; } $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用'; $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']); $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']); $list[$i]['discount_type'] = $item['discount_type']; $list[$i]['discount'] = $item['discount']; // if (!$item['event']) { // if ($item['type'] == 2) { // $list[$i]['event'] = $item['event'] = 3; // } else { // $list[$i]['event'] = $item['event'] = 0; // } // } // $list[$i]['event_desc'] = $events[$item['event']] ?: '领券中心'; $list[$i]['min_price'] = doubleval($item['min_price']); if ($list[$i]['discount_type'] == 1) { $list[$i]['sub_price'] = round($mch['total_price'] - doubleval($mch['total_price'] * $list[$i]['discount'] / 10), 2); if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){ $list[$i]['sub_price'] = $item['discount_max_price']; } } else { $list[$i]['sub_price'] = doubleval($item['sub_price']); } if ($list[$i]['appoint_type'] == 1) { $list[$i]['cat_id_list'] = json_decode($list[$i]['cat_id_list']); if ($list[$i]['cat_id_list'] != null) { $current = array_intersect($list[$i]['cat_id_list'], $cat_ids); if ($current) { $goodsAdd = []; $price = 0; foreach ($current as $v) { foreach ($mch['goods_list'] as $v2) { if (in_array($v, $v2['cat_id']) && !in_array($v2['goods_id'], $goodsAdd)) { $price += $v2['price']; array_push($goodsAdd, $v2['goods_id']); } }; } if ($price < $list[$i]['min_price']) { unset($list[$i]); continue; } } else { unset($list[$i]); continue; } } } elseif ($list[$i]['appoint_type'] == 2) { $list[$i]['goods_id_list'] = json_decode($list[$i]['goods_id_list']); if ($list[$i]['goods_id_list'] != null) { $current = array_intersect($list[$i]['goods_id_list'], $coupon_goods_id); if ($current) { $goodsAdd = []; $price = 0; foreach ($current as $v) { foreach ($mch['goods_list'] as $v2) { if ($v == $v2['goods_id'] && !in_array($v2['goods_id'], $goodsAdd)) { $price += $v2['price']; } } } if ($price < $list[$i]['min_price']) { unset($list[$i]); continue; } } else { unset($list[$i]); continue; } } } else { if ($max_price < $list[$i]['min_price']) { unset($list[$i]); continue; } } $new_list[] = $list[$i]; } if (!empty($this->verify_card_id)) { $mch['coupon_list'] = []; } else { $mch['coupon_list'] = $new_list; } } protected function getCatIdList(&$goods_list,$mch_id = 0) { $cat_id_list = []; foreach ($goods_list as &$goods) { if ($goods['goods_cat_id'] == 0) { // if ($mch_id > 0) { // $goods_cat_list = MchGoodsCat::find() // ->select('cat_id')->where([ // 'goods_id' => $goods['goods_id'], // ])->all(); // }else { $goods_cat_list = GoodsCat::find() ->select('cat_id')->where([ 'goods_id' => $goods['goods_id'], 'is_delete' => 0, ])->all(); //} foreach ($goods_cat_list as $goods_cat) { $cat_id_list[] = $goods_cat->cat_id; $goods['cat_id'][] = $goods_cat->cat_id; } } else { $cat_id_list[] = $goods['goods_cat_id']; $goods['cat_id'][] = $goods['goods_cat_id']; } $cat_parent_list = Cat::find()->select('parent_id') ->andWhere(['id' => $goods['cat_id'], 'is_delete' => 0])->andWhere(['>', 'parent_id', 0]) ->column(); $cat_id_list = array_merge($cat_parent_list, $cat_id_list); $goods['cat_id'] = array_merge($cat_parent_list, is_array($goods['cat_id']) ? $goods['cat_id'] : []); } unset($goods); return array_unique($cat_id_list); } protected function getGoodsIdList($goods_list) { $goods_id_list = []; foreach ($goods_list as $goods) { $goods_id_list[] = $goods['goods_id']; } return $goods_id_list; } protected function getLevelData() { $level = Level::find()->select([ 'name', 'level', 'discount', ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0]) ->asArray()->one(); return $level; } //积分计算 /** * @param $goods_item object 重新编写的goods_item * @param $store_integral int 商城设置的积分规则 * @param $goods_id array 已设置积分的商品id数组 * @return array */ protected function getIntegral($goods_item, $store_integral, $goods_id = array()) { $integral = json_decode($goods_item->integral, true); $resIntegral = [ 'forehead' => 0, 'forehead_integral' => 0, ]; if ($integral) { // 赠送积分计算 $give = $integral['give']; if (strpos($give, '%') !== false) { // 百分比 $give = trim($give, '%'); $goods_item->give = (int)($goods_item->price * ($give / 100)); } else { // 固定积分 $goods_item->give = (int)($give * $goods_item->num); } // 抵扣积分计算 $forehead = $integral['forehead']; if ($forehead) { if (strpos($forehead, '%') !== false) {//百分比积分抵扣计算 if ($forehead >= 100) { $forehead = 100; } if ($integral['more'] == '1') {//多件累计计算 $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->price * $store_integral); } else { if (!in_array($goods_item->id, $goods_id)) { //不允许多件累计 同id商品值计算一次积分抵扣 $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->single_price * $store_integral); } } } else { // if ($integral['more'] == '1') { // $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price); // if ($goods_item->price > ($forehead * $goods_item->num)) { // $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral); // } // } else { // if (!in_array($goods_item->id, $goods_id)) { // $goodsPrice = $goods_item->single_price; // $resIntegral['forehead_integral'] = (int)($store_integral * $goodsPrice); // if ($goodsPrice > $forehead) { // if ($forehead > 0) { // $resIntegral['forehead_integral'] = (int)($forehead * $store_integral); // } else { // $resIntegral['forehead_integral'] = 0; // } // } // } // } // 累计 $resIntegral['forehead_integral'] = bcmul($store_integral, $goods_item->price); if ($goods_item->price > ($forehead * $goods_item->num)) { $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral); } } } if ($this->integral['forehead_integral'] < $this->user->integral) { $resetIntegral = $this->user->integral - $this->integral['forehead_integral']; $resIntegral['forehead_integral'] = $resIntegral['forehead_integral'] >= $resetIntegral ? $resetIntegral : $resIntegral['forehead_integral']; $resIntegral['forehead'] = $store_integral ? sprintf("%.2f", ($resIntegral['forehead_integral'] / $store_integral)) : 0; $this->integral['forehead_integral'] += $resIntegral['forehead_integral']; $this->integral['forehead'] += $resIntegral['forehead']; } else { $resIntegral['forehead_integral'] = 0; $resIntegral['forehead'] = 0; } } return [ 'resIntegral' => $resIntegral, 'give' => ($goods_item->give)?$goods_item->give:0 ]; } public function getExpressPrice($mch) { $expressPrice = 0; if ($this->address) { $address = $this->address; //先计算单品满件包邮和满额包邮 $resGoodsList = Goods::cutFull($mch['goods_list']); //再通过运费规则计算运费 $store_id = $mch['store_id'] ?: $this->store_id; if ($store_id <= 0) { $store_id = [0, -1]; } $expressPrice = PostageRules::getExpressPriceMore($store_id, $address['city_id'], $resGoodsList, $address['province_id']); } $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice); return $expressPrice >= 0 ? $expressPrice : 0; } /** * 计算打包费 * @param $mch * @return int */ protected function getDabaoPrice($mch) { $goods_list = $mch['goods_list']; $dabao_price = 0; foreach ($goods_list as $value) { $dabao_price += $value['dabao_price']; } return $dabao_price >= 0 ? $dabao_price : 0; } // 获取门店列表 protected function getShopList() { $start = 0; $shop_table_name = Shop::tableName(); $latitude = 0; $longitude = 0; if ($this->address && isset($this->address['id'])) { $address = Address::findOne($this->address['id']); if ($address) { $latitude = $address->latitude ? $address->latitude : 0; $longitude = $address->longitude ? $address->longitude : 0; } } $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} WHERE ((`store_id`={$this->store_id}) AND (`is_delete`=0)) "; if ($this->keyword) { $sql .= " AND (`name` like '%{$this->keyword}%') "; } $sql .= "ORDER BY `distance` LIMIT {$start},30"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $shop = null; foreach ($list as $index => $item) { if ($item['is_default'] == 1) { $shop = $item; } $list[$index]['distance'] = round($item['distance']/1000, 2).'km'; } return [ 'list' => $list, 'shop' => $shop ]; } // 获取起送规则 protected function getOfferRule($mch) { $res = [ 'is_allowed' => 0, 'total_price' => 0, 'msg' => '' ]; if (isset($mch['mch_id']) && $mch['mch_id'] > 0) { $res['msg'] = '商户不支持起送规则'; return $res; } if (!$this->address) { $res['msg'] = '请选择收货地址'; return $res; } $offerRule = OfferPrice::findOne(['store_id' => get_store_id(),'is_delete' => 0]); if (!$offerRule) { $res['msg'] = '起送规则不存在'; return $res; } if ($offerRule->is_enable == 0) { $res['msg'] = '起送规则未开启'; return $res; } $ruleList = Json::decode($offerRule->detail); $res['total_price'] = $offerRule->price; if (is_array($ruleList)) { foreach ($ruleList as $value) { foreach ($value['province_list'] as $item) { if (isset($item['districtAll'])) { if ($item['id'] == $this->address['city_id']) { $res['total_price'] = $value['offer_price']; } } else if (isset($item['cityAll'])){ if ($item['id'] == $this->address['province_id']) { $res['total_price'] = $value['offer_price']; } } else { if ($item['id'] == $this->address['district_id']) { $res['total_price'] = $value['offer_price']; } } } } } if ($mch['total_price'] >= $res['total_price']) { $res['is_allowed'] = 0; } else { $res['is_allowed'] = 1; } $value = round($res['total_price'] - $mch['total_price'], 2); $res['msg'] = "自营商品,还差{$value}元起送"; return $res; } protected function getTerritorialLimitation($mch) { $isArea = 0; if (isset($mch['mch_id']) && $mch['mch_id'] > 0) { return $isArea; } if ($this->address) { $area = TerritorialLimitation::findOne([ 'store_id' => $this->store_id, 'is_delete' => 0, 'is_enable' => 1, ]); if ($area) { $city_id = []; //限制的地区ID $detail = json_decode($area->detail); if (!is_array($detail)) { $detail = []; } foreach ($detail as $key => $value) { foreach ($value->province_list as $key2 => $value2) { $city_id[] = $value2->id; } } $addressArr = [ $this->address['province_id'], $this->address['city_id'], $this->address['district_id'] ]; $addressArray = array_intersect($addressArr, $city_id); if (empty($addressArray)) { $isArea = 1; } } } return $isArea; } // 包邮规则 protected function getFreeDeliveryRules($mch, $expressPrice) { if ($expressPrice == 0) { return $expressPrice; } $free = FreeDeliveryRules::find()->where(['store_id' => $mch['store_id'], 'is_delete' => 0])->asArray()->all(); foreach ($free as $k => $v) { $city = json_decode($v['city'], true); foreach ($city as $v1) { if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) { $expressPrice = 0; break; } } } return $expressPrice; } // 获取用户填写的自定义表单 protected function getForm(&$form) { if ($form['is_form'] == 1) { $formList = &$form['list']; foreach ($formList as $index => $value) { if ($value['required'] == 1) { if (in_array($value['type'], ['radio', 'checkbox'])) { $is_true = false; foreach ($value['default_list'] as $k => $v) { if ($v['checked'] == true) { $is_true = true; } } if (!$is_true) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } else { if (empty($value['default']) && strlen($value['default']) == 0) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } } if ($value['type'] == 'checkbox') { $formList[$index]['default'] = is_array($value['default']) ? implode(',', $value['default']) : $value['default']; } } } return $form; } protected function goodsCardList() { $list = []; foreach ($this->mch_list as $mch) { if($mch['mch_id'] == 0) { foreach ($mch['goods_list'] as $goods) { if (!empty($goods['goods_list'])) { foreach ($goods['goods_list'] as $val) { if(!$val['goods_card_list']) { $val['goods_card_list'] = []; } $list = array_merge($list, $val['goods_card_list']); } } } } } return $list; } public function getDateByInterval(int $num) :array { //var_dump($st, $et);die; $returnData = []; $i = 0; do { $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d')))); $returnData[] = $temp; $i++; } while ($i < $num); return $returnData; } /** * @param $goods * @param $type */ public function bookCheckGoodsNum($goods, $type) { // 酒店预约 if ($type == 1) { // $date_book = GoodsBook::findOne(['goods_id' => $goods['id']])->date_book; // $date_book = Json::decode($date_book); // if (!$date_book) { // return [ // 'code' => 1, // 'msg' => '数据异常' // ]; // } // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num')); // // foreach ($goods['book'] as $value) { // if ($data_config[$value['date']] < $goods['num']) { // return [ // 'code' => 1, // 'msg' => '选中日期' . $value['date'] . '内暂无房源' // ]; // } // } } // 服务预约 if ($type == 2) { $service_book = GoodsBook::findOne(['goods_id' => $goods['id']])->service_book; $service_book = Json::decode($service_book); if (!$service_book) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $service_book = $service_book['data']; $service_book_date = array_column($service_book, NULL, 'date'); $service_book_time = $service_book_date[$goods['service']['date']]['time']; foreach ($service_book_time as $m) { foreach ($m['times'] as $n) { if ($n['time'] == $goods['service']['time']) { if ($goods['num'] > $n['num']) { return [ 'code' => 1, 'msg' => $n['time'] . '时间段内暂不可预约' ]; } } } } } return [ 'code' => 0, 'msg' => 'success' ]; } /** * 计算初始价格 * @param $order_id * @return array */ public static function findPrice($order_id) { $order_detail = OrderDetail::findAll(['order_id' => $order_id]); $original_price = 0; foreach ($order_detail as $detail) { $goods = Goods::findOne($detail['goods_id']); if ($goods->use_attr > 0) { $attrs = Json::decode($goods->attr); // 当前选择的规格 $attrIdArr = []; foreach (Json::decode($detail['attr']) as $attrListItem) { $attrIdArr[] = $attrListItem['attr_id']; } foreach ($attrs as $attr) { $attrIds = []; foreach ($attr['attr_list'] as $item) { $attrIds[] = $item['attr_id']; } sort($attrIds); sort($attrIdArr); // 找出当前规格信息 if (implode($attrIds) === implode($attrIdArr)) { $original_price += $attr['price']; } } } else { $original_price += $goods->price; } } return [ 'original_price' => $original_price, 'md_price' => $original_price ]; } //分类起购数量 修正为当前分类下所有商品数量未达到数量后警告 private function checkCatShopCount($goodsList) { $new_goods_list = []; foreach ($goodsList as $item) { $cat_id_arr = GoodsCat::find()->where(['goods_id' => $item['goods_id'], 'is_delete' => 0])->select('cat_id')->column(); foreach ($cat_id_arr as $goods_cat_id) { $open = true; foreach ($new_goods_list as $new_goods_item) { if ($new_goods_item['cat_id'] === $goods_cat_id) { $new_goods_list[$goods_cat_id]['goods_list'][] = [ 'goods_id' => $item['goods_id'], 'num' => $item['num'] ]; $open = false; } } if ($open) { $new_goods_list[$goods_cat_id]['cat_id'] = $goods_cat_id; $cat = Cat::find()->where(['id' => $goods_cat_id]) ->select('shop_count, name')->asArray()->one(); $new_goods_list[$goods_cat_id]['shop_count'] = $cat['shop_count'] ?: 0; $new_goods_list[$goods_cat_id]['name'] = $cat['name'] ?: ''; $new_goods_list[$goods_cat_id]['goods_list'][] = [ 'goods_id' => $item['goods_id'], 'num' => $item['num'] ]; } } } foreach ($new_goods_list as $index => $goods_item) { if ($goods_item['shop_count'] > 0) { $goods_num_arr = array_column($goods_item['goods_list'], 'num'); if (array_sum($goods_num_arr) < $goods_item['shop_count']) { return [ 'code' => 1, 'msg' => '分类' . $goods_item['name'] . "起购数量为" . $goods_item['shop_count'] . ',当前购买数量为' . array_sum($goods_num_arr), ]; } } } return [ 'code' => 0, 'msg' => '检测起购数量完成', ]; } }