Code.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $num = Dever::param('num', $param);
  64. $info_id = Dever::param('info_id', $param);
  65. $info = Dever::db('card/info')->find($info_id);
  66. if ($info && $num > 0) {
  67. for ($i = 0; $i< $num;$i++) {
  68. $this->createCode($info);
  69. }
  70. }
  71. }
  72. private function createCode($info, $update = false)
  73. {
  74. # 生成卡号
  75. $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
  76. $where['clear'] = true;
  77. $where['card'] = $card;
  78. $state = Dever::db('card/code')->find($where);
  79. if (!$state) {
  80. $where['card_id'] = $info['id'];
  81. if ($update) {
  82. $where = array_merge($where, $update);
  83. }
  84. Dever::db('card/code')->insert($where);
  85. return $card;
  86. } else {
  87. return $this->createCode($info);
  88. }
  89. }
  90. /**
  91. * 作废
  92. *
  93. * @return mixed
  94. */
  95. public function drop_api($id)
  96. {
  97. $update['where_id'] = $id;
  98. $update['type'] = 4;
  99. Dever::db('card/code')->update($update);
  100. return 'ok';
  101. }
  102. public function recovery_api($id)
  103. {
  104. $update['where_id'] = $id;
  105. $update['type'] = 1;
  106. Dever::db('card/code')->update($update);
  107. return 'ok';
  108. }
  109. }