mUserId = intval($userid); if($userid > 0) { $this->mUserCards = new user_mcards($userid); $this->mMemberInfo = new member_info($userid); $this->mInvitees = $this->invitees(); $this->mOrderCount = $this->order_num() + $this->vrorder_num(); } } private function isVip() { if($this->mUserId > 0 && $this->mUserCards != null) { return $this->mUserCards->hasCards(); } else { return false; } } private function order_num() { if($this->mUserId <= 0) return 0; $mod_member = Model('order'); $items = $mod_member->field('COUNT(*) AS order_num') ->table('order') ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]]) ->select(); if(empty($items)) { return 0; } else { return intval($items[0]['order_num']); } } private function vrorder_num() { if($this->mUserId <= 0) return 0; $mod_member = Model('vr_order'); $items = $mod_member->field('COUNT(*) AS order_num') ->table('vr_order') ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]]) ->select(); if(empty($items)) { return 0; } else { return intval($items[0]['order_num']); } } public function left_invitees() { if($this->mUserId <= 0 || $this->mMemberInfo == null) { $left_invitees = 0; } else { $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees(); } return $left_invitees; } private function select_invitees($goods_id) { $left_invitees = $this->left_invitees(); $share_policy = $this->share_policy($goods_id); if(empty($share_policy)) return false; foreach ($share_policy as $item) { $num = $item['num']; if($left_invitees >= $num) { $policy = $item; break; } } return empty($policy) ? false : $policy; } public function share_policy($goods_id) { global $config; $share_policy = $config['goods_share_policy']; if(array_key_exists($goods_id,$share_policy)) { return $share_policy[$goods_id]; } else { return []; } } private function invitees() { $mod_member = Model('member'); $ret = $mod_member->field('COUNT(*) AS inviter_count') ->where(['inviter_id' => $this->mUserId]) ->select(); if(empty($ret)) { return 0; } else { return intval($ret[0]['inviter_count']); } } private function isExcluded($goods_id) { global $config; $exclue_gids = $config['exclude_preferential_goods_ids']; if(empty($exclue_gids)) { return false; } else { return in_array(intval($goods_id),$exclue_gids); } } public function deduct_order($order_id) { if($this->mCalcType == self::CalcTypeVIP) { $this->detuct_mcard($order_id); } elseif($this->mCalcType == self::CalcTypeInvitees) { $this->deduct_invitees($order_id); } else { } } private function detuct_mcard($vorder_id) { $mod_vorder = Model('vr_order'); $order = $mod_vorder->getOrderInfo(['order_id' => $vorder_id],'*',true); if(empty($order)) return true; global $config; $spec_card = $config['vgoods_spec_card']; $goods_id = intval($order['goods_id']); $num = intval($order['goods_num']); $goods_price = $order['goods_price']; if(array_key_exists($goods_id,$spec_card)) { $amount = $spec_card[$goods_id] * $num; } else { $amount = $goods_price * $num; } $this->mUserCards->deduct($amount); $mod_vorder->editOrder(['calctype' => self::CalcTypeVIP,'calcamount' => $amount],['order_id' => $vorder_id]); } private function deduct_invitees($vorder_id) { $model = Model('member'); $num = $this->mUsedInviteesNum; $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]); $mod_vorder = Model('vr_order'); $mod_vorder->editOrder(['calctype' => self::CalcTypeInvitees,'calcamount' => $num],['order_id' => $vorder_id]); } public function calc_tips() { // global $config; // // if($this->mUserId <= 0) { // return $config['tips']['first_order']; // } // // $fVip = $this->isVip(); // if($fVip) // { // if($this->isFirstorOrder()) { // $tips = $config['tips']['vip_first_order']; // } else { // $tips = $config['tips']['vip_user']; // } // } // elseif($this->isFirstorOrder()) { // $tips = $config['tips']['first_order']; // } else { // $tips = $config['tips']['none_vip']; // } return "全国通用,即刻到账"; } public function inviter_tips($goods_id) { // $now = time(); // $nine = strtotime(date('Y-m-d',$now)) + 9*3600; // $twenty_first = strtotime(date('Y-m-d',$now)) + 21*3600; // // if($now < $nine || $now > $twenty_first) { // return '试营业时间为9:00--21:00,其它时间会慢'; // } if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType == self::CalcTypeInvitees) { $left_invitees = $this->left_invitees(); $share_policy = $this->share_policy($goods_id); foreach ($share_policy as $item) { $num = $item['num']; if($left_invitees >= $num) { $cur = $item; break; } else { $next = $item; } } if(empty($next)) { $discount = $cur['discount']; $tip = "您已领补贴{$discount}元"; } else { $count = $next['num'] - $left_invitees; $discount = $next['discount']; $tip = "已领补贴{$cur['discount']}元,再分享{$count}人,可领补贴{$discount}元"; } return $tip; } else { return ""; } } public function calc_vgoods_price($goods_info) { $goods_id = intval($goods_info['goods_id']); $goods_price = $goods_info['goods_price']; if($this->isExcluded($goods_id)) { $this->mCalcType = self::CalcTypeNone; return ['price_des' => '售价', 'accu_price' => round($goods_price,2)]; } elseif($this->isVip()) { $this->mCalcType = self::CalcTypeVIP; $goods_price = $this->goods_spec_amount($goods_id,$goods_price); $price = $this->mUserCards->calc_price($goods_id, $goods_price); return ['price_des' => '会员价', 'accu_price' => round($price,2)]; } elseif(!empty($policy = $this->select_invitees($goods_id))) { $this->mCalcType = self::CalcTypeInvitees; $price = $policy['price']; return ['price_des' => '补贴价', 'accu_price' => round($price,2)]; } else { $this->mCalcType = self::CalcTypeNormal; return ['price_des' => '售价', 'accu_price' => round($goods_price,2)]; } } //获取商品面额 private function goods_spec_amount($goods_id,$goods_price) { global $config; $spec_card = $config['vgoods_spec_card']; if(array_key_exists($goods_id,$spec_card)) { return $spec_card[$goods_id]; } else { Log::record("cannot find goods_id = {$goods_id} spec",Log::ERR); return $goods_price; } } public function calc_vorder_amount($vorder_info) { $goods_id = intval($vorder_info['goods_id']); $goods_price = $vorder_info['goods_price']; $num = $vorder_info['quantity']; if($this->isExcluded($goods_id)) { $this->mCalcType = self::CalcTypeNone; return round($goods_price * $num,2); } elseif($this->isVip()) { $this->mCalcType = self::CalcTypeVIP; $goods_price = $this->goods_spec_amount($goods_id,$goods_price); return $this->mUserCards->calc_amount($goods_id, $goods_price * $num); } elseif(!empty($policy = $this->select_invitees($goods_id))) { $this->mCalcType = self::CalcTypeInvitees; $price = $policy['price']; return round($price * $num,2); } else { $this->mCalcType = self::CalcTypeNormal; return round($goods_price * $num,2); } } public static function onCancelVrOrder($order) { $calc_type = intval($order['calctype']); $calc_amount = $order['calcamount']; $userid = intval($order['buyer_id']); $order_id = $order['order_id']; if($calc_type === self::CalcTypeVIP) { $mod_cardkey = Model('card_key'); $items = $mod_cardkey->field('*')->where(['order_id' => $order_id,'member_id' => $userid])->select(); if(empty($items)) return false; $card = $items[0]; $amount = $card['amount']; $cards = new user_mcards($userid); $cards->add_amount($amount); $mod_vorder = Model('vr_order'); $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]); } elseif($calc_type === self::CalcTypeInvitees) { $num = intval($calc_amount); if($num > 0) { $mod_vorder = Model('vr_order'); $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]); $model = Model('member'); $ret = $model->editMember(['member_id' => $userid], ['used_invitees'=> ['exp', "used_invitees-{$num}"]]); return $ret; } } else { Log::record("onCancelOrder",Log::DEBUG); } return true; } }