123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- declare(strict_types=0);
- require_once (BASE_HELPER_PATH . '/model/member_info.php');
- use mcard\user_mcards;
- interface ICalc
- {
- public function calc_vgoods_price($goods_info);
- public function calc_vorder_amount($order_info);
- public function calc_tips();
- }
- class CalcPrice implements ICalc
- {
- public const CalcTypeNormal = 1;
- public const CalcTypeVIP = 2;
- public const CalcTypeFirstOrder = 3;
- public const CalcTypeInvitees = 4;
- public const CalcTypeNone = 5;
- private const FIRST_ORDER_PRICE = 0.95;
- private const DEFAULT_ORDER_PRICE = 0.99;
- private const STEP_PRICE_ITEMS = [
- ['num' => 10, 'discount' => 0.95,'tip' => '邀请10人可享95折扣'],
- ['num' => 5, 'discount' => 0.96, 'tip' => '邀请5人可享96折扣'],
- ['num' => 3, 'discount' => 0.97, 'tip' => '邀请3人可享97折扣']
- ];
- private $mUserId;
- private $mUserCards = null;
- private $mMemberInfo = null;
- private $mInvitees = 0;
- private $mOrderCount = 0;
- private $mCalcType = 0;
- private $mUsedInviteesNum = 0;
- private $mOrderId = 0;
- public function __construct($userid)
- {
- $this->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']);
- }
- }
- private function isFirstorOrder()
- {
- return ($this->mOrderCount == 0);
- }
- private function select_invitees()
- {
- if($this->mUserId <= 0 || $this->mMemberInfo == null) {
- return false;
- }
- $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
- foreach (self::STEP_PRICE_ITEMS as $item) {
- $num = $item['num'];
- if($left_invitees >= $num) {
- $this->mUsedInviteesNum = $num;
- return $item;
- }
- }
- return false;
- }
- 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::CalcTypeFirstOrder) {
- }
- 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 $tips;
- }
- public function inviter_tips()
- {
- if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType != self::CalcTypeInvitees)
- {
- $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
- $cur = [];
- $next = [];
- foreach (self::STEP_PRICE_ITEMS as $item)
- {
- $num = $item['num'];
- if($left_invitees >= $num) {
- $cur = $item;
- } else {
- $next = $item;
- }
- }
- $count = $next['num'] - $left_invitees;
- $discount = $next['discount'] * 100;
- $tip = "再邀请{$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;
- $price = $this->mUserCards->calc_price($goods_id, $goods_price);
- return ['price_des' => '会员价', 'accu_price' => round($price,2)];
- }
- elseif($this->isFirstorOrder()) {
- $this->mCalcType = self::CalcTypeFirstOrder;
- return ['price_des' => '首单价', 'accu_price' => round($goods_price * self::FIRST_ORDER_PRICE,2)];
- }
- elseif(!empty($num_dis = $this->select_invitees())) {
- $this->mCalcType = self::CalcTypeInvitees;
- $discount = $num_dis['discount'];
- return ['price_des' => '邀请价', 'accu_price' => round($goods_price * $discount,2)];
- }
- else {
- $this->mCalcType = self::CalcTypeNormal;
- return ['price_des' => '售价', 'accu_price' => round($goods_price * self::DEFAULT_ORDER_PRICE,2)];
- }
- }
- 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;
- return $this->mUserCards->calc_amount($goods_id, $goods_price);
- }
- elseif($this->isFirstorOrder()) {
- $this->mCalcType = self::CalcTypeFirstOrder;
- return round($goods_price * self::FIRST_ORDER_PRICE * $num,2);
- }
- elseif(!empty($num_dis = $this->select_invitees())) {
- $this->mCalcType = self::CalcTypeInvitees;
- $discount = $num_dis['discount'];
- return round($goods_price * $discount * $num,2);
- }
- else {
- $this->mCalcType = self::CalcTypeNormal;
- return round($goods_price * $num * self::DEFAULT_ORDER_PRICE,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;
- }
- }
|