mUserId = $userid; $this->mCardModel = Model('member_card'); $this->load_cards(); } public function hasCards() { foreach ($this->mCards as $key => $cards) { if(!empty($cards)) { return true; } } return false; } public function add_amount($amound) { $mod_card = Model('member_card'); $left_amount = $amound; foreach ($this->mCards as $key => $cards) { if($key != 'period') { foreach ($cards as $card) { if($left_amount <= 0) { break; } $card_id = $card->card_id(); $total = $card->total_amount(); $left = $card->left_amount(); if($total > $left + $left_amount) { $mod_card->add_amound($card_id,$left_amount); $left_amount = 0; } else { $amound = $total - $left; $mod_card->add_amound($card_id,$amound); $left_amount = $left_amount - $amound; } } } } } public function left_amount() { $total = 0.00; foreach ($this->mCards as $key => $cards) { if($key != 'period') { foreach ($cards as $card) { $total += $card->left_amount(); } } } return round($total,2); } public function addCard($params) { $type = intval($params['card_type']); $discount = round(doubleval($params['discount']), 2); $data = ['member_id' => $this->mUserId, 'card_type' => $type, 'discount' => $discount, 'state' => STATE_USING, 'add_time' => time()]; if ($type === AmountType) { $amount = round(doubleval($params['total_amount']), 2); $data['total_amount'] = $amount; $data['left_amount'] = $amount; } elseif ($type === PeriodType) { $data['package_type'] = $this->package_type($params['package_type']); } elseif ($type === BothType) { $amount = round(doubleval($params['total_amount']), 2); $data['total_amount'] = $amount; $data['left_amount'] = $amount; $data['package_type'] = $this->package_type($params['package_type']); } else { return false; } if (isset($data['package_type'])) { [$start, $end] = $this->calc_time($data['package_type']); $data['start_time'] = $start; $data['end_time'] = $end; } $ret = $this->mCardModel->addCard($data); if ($ret > 0) { $this->load_cards(); return true; } else { return false; } } private function calc_time($type) { $start = time(); if ($type == MonthPachage) { $end = strtotime('+1 months 1 day', $start); } elseif ($type == QuarterPackage) { $end = strtotime('+3 months 1 day', $start); } else { $end = strtotime('+1 year 1 day', $start); } $end = strtotime(date('Y-m-d', $end)); $start = strtotime(date('Y-m-d', $start)); return [$start, $end]; } private function package_type($package) { $package = strtolower($package); if ($package == 'month') { return MonthPachage; } elseif ($package == 'quarter') { return QuarterPackage; } elseif ($package == 'year') { return YearPackage; } else { throw new Exception("会员卡时间类型错误."); } } private function load_cards() { $this->mCards['amount'] = []; $this->mCards['period'] = []; $this->mCards['aperiod'] = []; $cards = $this->mCardModel->getUsableCards($this->mUserId); foreach ($cards as $item) { $card_type = intval($item['card_type']); if ($card_type == AmountType) { $card = new amount_card($item); $name = 'amount'; } elseif ($card_type == PeriodType) { $card = new period_card($item); $name = 'period'; } else { $card = new amount_period_card($item); $name = 'aperiod'; } if ($card->usable()) { $this->mCards[$name][] = $card; } } } public function enough($amount) { if (!empty($this->mCards['period'])) return true; $total = 0.0; $total += $this->account($this->mCards['aperiod']); $total += $this->account($this->mCards['amount']); return intval($total * 100) >= intval($amount * 100); } public function calc_price($goods_id,$price) { if(canUseVip($goods_id)) { return $this->_calc_amount($price); } else { return $price; } } public function calc_amount($goods_id,$amount) { if(canUseVip($goods_id)) { return $this->_calc_amount($amount); } else { return $amount; } } private function _calc_amount($amount) { if($this->enough($amount)) { $amount = intval($amount * 100); $total_price = 0.00; $calc_price = function (card $card, int $cent) use(&$total_price) { [$consumer,$useup,$price] = $card->calc($cent); $total_price += $price; return $consumer; }; foreach ($this->mCards['aperiod'] as $card) { if ($amount <= 0) { break; } else { $amount -= $calc_price($card, $amount); } } foreach ($this->mCards['amount'] as $card) { if ($amount <= 0) { break; } else { $amount -= $calc_price($card, $amount); } } return $total_price; } else { return $amount; } } private function account($cards) { $total = 0.0; foreach ($cards as $card) { $total += $card->left_amount(); } return $total; } public function deduct($amount) { if (!empty($this->mCards['period'])) return true; $amount = intval($amount * 100); $self = $this; $deduct_money = function (card $card, int $cent) use($self) { [$consumer,$useup] = $card->deduct($cent); $self->mCardModel->detuct($card->card_id(),$consumer,$useup); //增加消费额度减少记录 return $consumer; }; foreach ($this->mCards['aperiod'] as $card) { if ($amount <= 0) { break; } else { $amount -= $deduct_money($card, $amount); } } foreach ($this->mCards['amount'] as $card) { if ($amount <= 0) { break; } else { $amount -= $deduct_money($card, $amount); } } $this->load_cards(); return ($amount <= 0); } }