Code.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Card\Lib;
  3. use Dever;
  4. class Code
  5. {
  6. /**
  7. * 分配礼品卡资源
  8. *
  9. * @return mixed
  10. */
  11. public function assign($order_id)
  12. {
  13. $card = Dever::db('card/order_card')->select(array('order_id' => $order_id));
  14. if ($card) {
  15. foreach ($card as $k => $v) {
  16. $info = Dever::db('card/info')->find($v['card_id']);
  17. if (!$info) {
  18. continue;
  19. }
  20. $update = array();
  21. $update['where_id'] = $v['id'];
  22. $update['cards'] = array();
  23. $w['option_card_id'] = $v['card_id'];
  24. $w['option_status'] = 1;
  25. $w['limit'] = $v['num'];
  26. $code = Dever::db('card/code')->getData($w);
  27. $total = $v['num'] - count($code);
  28. $u = array();
  29. $u['status'] = 2;
  30. $u['uid'] = $v['uid'];
  31. $u['order_id'] = $v['order_id'];
  32. $u['order_card_id'] = $v['id'];
  33. $u['city'] = $v['city'];
  34. $u['total_cash'] = $info['value'];
  35. $u['bdate'] = time();
  36. if ($code) {
  37. foreach ($code as $k1 => $v1) {
  38. $u['where_id'] = $v1['id'];
  39. Dever::db('card/code')->update($u);
  40. $update['cards'][$v1['card']] = $v1['card'];
  41. }
  42. }
  43. if ($total > 0) {
  44. for ($i = 0; $i< $total;$i++) {
  45. $card = $this->createCode($info, $u);
  46. $update['cards'][$card] = $card;
  47. }
  48. }
  49. if ($update['cards']) {
  50. $update['cards'] = implode(',', $update['cards']);
  51. Dever::db('card/order_card')->update($update);
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * 创建兑换码
  58. *
  59. * @return mixed
  60. */
  61. public function createCard($id, $name, $param)
  62. {
  63. $type = Dever::param('type', $param);
  64. $content = Dever::param('content', $param);
  65. $num = Dever::param('num', $param);
  66. $card_id = Dever::param('card_id', $param);
  67. $info = Dever::db('card/info')->find($card_id);
  68. if ($info) {
  69. if ($type == 1 && $num > 0) {
  70. for ($i = 0; $i< $num;$i++) {
  71. $this->createCode($info);
  72. }
  73. } elseif ($type == 2 && $content) {
  74. $data = Dever::split($content);
  75. foreach ($data as $k => $v) {
  76. $this->upCode($info, $v);
  77. }
  78. }
  79. }
  80. }
  81. private function upCode($info, $card)
  82. {
  83. $card = trim($card);
  84. if (!$card) {
  85. return;
  86. }
  87. $where['clear'] = true;
  88. $where['card'] = $card;
  89. $state = Dever::db('card/code')->find($where);
  90. if (!$state) {
  91. $where['card_id'] = $info['id'];
  92. Dever::db('card/code')->insert($where);
  93. return $card;
  94. } else {
  95. return false;
  96. }
  97. }
  98. private function createCode($info, $update = false)
  99. {
  100. # 生成卡号
  101. $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
  102. $where['clear'] = true;
  103. $where['card'] = $card;
  104. $state = Dever::db('card/code')->find($where);
  105. if (!$state) {
  106. $where['card_id'] = $info['id'];
  107. if ($update) {
  108. $where = array_merge($where, $update);
  109. }
  110. Dever::db('card/code')->insert($where);
  111. return $card;
  112. } else {
  113. return $this->createCode($info);
  114. }
  115. }
  116. /**
  117. * 作废
  118. *
  119. * @return mixed
  120. */
  121. public function drop_api($id)
  122. {
  123. $update['where_id'] = $id;
  124. $update['type'] = 4;
  125. Dever::db('card/code')->update($update);
  126. return 'ok';
  127. }
  128. public function recovery_api($id)
  129. {
  130. $update['where_id'] = $id;
  131. $update['type'] = 1;
  132. Dever::db('card/code')->update($update);
  133. return 'ok';
  134. }
  135. }