123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace mtopcard;
- class user_topcards
- {
- private $mMemberID;
- private $mTopCardModel;
- private $mCards;
- public function __construct($member_id)
- {
- $this->mMemberID = $member_id;
- $this->mTopCardModel = Model('member_topcard');
- $this->load_cards();
- }
- public function cards_format()
- {
- $ret = [];
- foreach ($this->mCards as $card) {
- $ret[] = $card->format();
- }
- return $ret;
- }
- private function load_cards()
- {
- $this->mCards = [];
- $items = $this->mTopCardModel->getAllCards($this->mMemberID);
- foreach ($items as $item) {
- $card = new topcard($item);
- $this->mCards[] = $card;
- }
- }
- public function priority_cards($types)
- {
- $items = [];
- foreach ($this->mCards as $card)
- {
- $type = $card->card_type();
- if($type == PetroChinaCard) {
- $items[PetroChinaCard] = $card;
- }
- elseif($type == SinopecCard) {
- $items[SinopecCard] = $card;
- }
- else {
- $items[PhoneCard] = $card;
- }
- }
- $ret = [];
- foreach ($items as $key => $val) {
- if(in_array($val->card_type(),$types))
- $ret[] = $val->format();
- }
- return $ret;
- }
- public function addPetroChina(string $card_no)
- {
- if(empty($card_no)) {
- return false;
- } else {
- return $this->addCard($card_no, PetroChinaCard);
- }
- }
- public function addSinopec(string $card_no)
- {
- if(empty($card_no)) {
- return false;
- } else {
- return $this->addCard($card_no, SinopecCard);
- }
- }
- public function addPhone(string $mobile)
- {
- if(empty($mobile)) {
- return false;
- } else {
- return $this->addCard($mobile,PhoneCard);
- }
- }
- public function addCard(string $card_no, int $card_type)
- {
- $card_id = $this->has_card($card_no, $card_type);
- if ($card_id === false) {
- $data['member_id'] = $this->mMemberID;
- $data['card_no'] = trim($card_no);
- $data['card_type'] = $card_type;
- $data['add_time'] = time();
- $data['total_amount'] = 0.00;
- $data['month_stamp'] = month_stamp();
- $data['month_amount'] = 0.00;
- $data['card_state'] = CardNormal;
- $ret = $this->mTopCardModel->addCard($data);
- } else {
- $data['card_state'] = CardNormal;
- $ret = $this->mTopCardModel->edit($card_id, $data);
- }
- return ($ret != false);
- }
- public function addMoney($card_id, $money)
- {
- $card = $this->getCard($card_id);
- if ($card != null) {
- $data['total_amount'] = $card->total_amount() + $money;
- $data['month_stamp'] = month_stamp();
- $data['month_amount'] = $card->month_total() + $money;
- $ret = $this->mTopCardModel->edit($card_id, $data);
- if($ret) {
- $this->load_cards();
- return true;
- }
- }
- return false;
- }
- public function getCard($card_id)
- {
- foreach ($this->mCards as $card) {
- if ($card->topcard_id() === $card_id) {
- return $card;
- }
- }
- return null;
- }
- private function has_card($card_no, $card_type)
- {
- foreach ($this->mCards as $card) {
- if ($card_no === $card->card_no() && $card_type === $card->card_type()) {
- return $card->topcard_id();
- }
- }
- return false;
- }
- }
|