user_topcards.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace mtopcard;
  3. class user_topcards
  4. {
  5. private $mMemberID;
  6. private $mTopCardModel;
  7. private $mCards;
  8. public function __construct($member_id)
  9. {
  10. $this->mMemberID = $member_id;
  11. $this->mTopCardModel = Model('member_topcard');
  12. $this->load_cards();
  13. }
  14. public function cards_format()
  15. {
  16. $ret = [];
  17. foreach ($this->mCards as $card) {
  18. $ret[] = $card->format();
  19. }
  20. return $ret;
  21. }
  22. private function load_cards()
  23. {
  24. $this->mCards = [];
  25. $items = $this->mTopCardModel->getAllCards($this->mMemberID);
  26. foreach ($items as $item) {
  27. $card = new topcard($item);
  28. $this->mCards[] = $card;
  29. }
  30. }
  31. public function priority_cards($types)
  32. {
  33. $items = [];
  34. foreach ($this->mCards as $card)
  35. {
  36. $type = $card->card_type();
  37. if($type == PetroChinaCard) {
  38. $items[PetroChinaCard] = $card;
  39. }
  40. elseif($type == SinopecCard) {
  41. $items[SinopecCard] = $card;
  42. }
  43. else {
  44. $items[PhoneCard] = $card;
  45. }
  46. }
  47. $ret = [];
  48. foreach ($items as $key => $val) {
  49. if(in_array($val->card_type(),$types))
  50. $ret[] = $val->format();
  51. }
  52. return $ret;
  53. }
  54. public function addPetroChina(string $card_no)
  55. {
  56. if(empty($card_no)) {
  57. return false;
  58. } else {
  59. return $this->addCard($card_no, PetroChinaCard);
  60. }
  61. }
  62. public function addSinopec(string $card_no)
  63. {
  64. if(empty($card_no)) {
  65. return false;
  66. } else {
  67. return $this->addCard($card_no, SinopecCard);
  68. }
  69. }
  70. public function addPhone(string $mobile)
  71. {
  72. if(empty($mobile)) {
  73. return false;
  74. } else {
  75. return $this->addCard($mobile,PhoneCard);
  76. }
  77. }
  78. public function addCard(string $card_no, int $card_type)
  79. {
  80. $card_id = $this->has_card($card_no, $card_type);
  81. if ($card_id === false) {
  82. $data['member_id'] = $this->mMemberID;
  83. $data['card_no'] = trim($card_no);
  84. $data['card_type'] = $card_type;
  85. $data['add_time'] = time();
  86. $data['total_amount'] = 0.00;
  87. $data['month_stamp'] = month_stamp();
  88. $data['month_amount'] = 0.00;
  89. $data['card_state'] = CardNormal;
  90. $ret = $this->mTopCardModel->addCard($data);
  91. } else {
  92. $data['card_state'] = CardNormal;
  93. $ret = $this->mTopCardModel->edit($card_id, $data);
  94. }
  95. return ($ret != false);
  96. }
  97. public function addMoney($card_id, $money)
  98. {
  99. $card = $this->getCard($card_id);
  100. if ($card != null) {
  101. $data['total_amount'] = $card->total_amount() + $money;
  102. $data['month_stamp'] = month_stamp();
  103. $data['month_amount'] = $card->month_total() + $money;
  104. $ret = $this->mTopCardModel->edit($card_id, $data);
  105. if($ret) {
  106. $this->load_cards();
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public function getCard($card_id)
  113. {
  114. foreach ($this->mCards as $card) {
  115. if ($card->topcard_id() === $card_id) {
  116. return $card;
  117. }
  118. }
  119. return null;
  120. }
  121. private function has_card($card_no, $card_type)
  122. {
  123. foreach ($this->mCards as $card) {
  124. if ($card_no === $card->card_no() && $card_type === $card->card_type()) {
  125. return $card->topcard_id();
  126. }
  127. }
  128. return false;
  129. }
  130. }