Code.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $cash = 0;
  88. if (strstr($card, ',')) {
  89. $temp = explode(',', $card);
  90. $card = $temp[0];
  91. $cash = $temp[1];
  92. }
  93. $where['clear'] = true;
  94. $where['card'] = $card;
  95. $state = Dever::db('card/code')->find($where);
  96. if (!$state) {
  97. $where['card_id'] = $info['id'];
  98. if ($cash > 0) {
  99. $where['total_cash'] = $cash;
  100. }
  101. Dever::db('card/code')->insert($where);
  102. return $card;
  103. } else {
  104. $where['where_id'] = $state['id'];
  105. $where['card_id'] = $info['id'];
  106. if ($cash > 0) {
  107. $where['total_cash'] = $cash;
  108. }
  109. Dever::db('card/code')->update($where);
  110. return $card;
  111. }
  112. }
  113. private function createCode($info, $update = false, $result = false)
  114. {
  115. # 生成卡号
  116. $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
  117. $where['clear'] = true;
  118. $where['card'] = $card;
  119. $state = Dever::db('card/code')->find($where);
  120. if (!$state) {
  121. $where['card_id'] = $info['id'];
  122. if ($update) {
  123. $where = array_merge($where, $update);
  124. }
  125. $id = Dever::db('card/code')->insert($where);
  126. if ($result) {
  127. return $id;
  128. } else {
  129. return $card;
  130. }
  131. } else {
  132. return $this->createCode($info);
  133. }
  134. }
  135. # 直接创建已绑定的礼品卡
  136. public function create($info, $type, $uid, $order_id, $price, $bdate = false)
  137. {
  138. $update['status'] = 2;
  139. $update['total_cash'] = $price;
  140. $update['type'] = $type;
  141. $update['uid'] = $uid;
  142. $update['order_id'] = $order_id;
  143. if (!$bdate) {
  144. $bdate = time();
  145. }
  146. $update['bdate'] = $bdate;
  147. return $this->createCode($info, $update, true);
  148. }
  149. /**
  150. * 作废
  151. *
  152. * @return mixed
  153. */
  154. public function drop_api($id)
  155. {
  156. $update['where_id'] = $id;
  157. $update['type'] = 4;
  158. Dever::db('card/code')->update($update);
  159. return 'ok';
  160. }
  161. public function recovery_api($id)
  162. {
  163. $update['where_id'] = $id;
  164. $update['type'] = 1;
  165. Dever::db('card/code')->update($update);
  166. return 'ok';
  167. }
  168. # 验证卡是否可用
  169. public function check(&$code)
  170. {
  171. $code['info'] = Dever::db('card/info')->find($code['card_id']);
  172. if (!$code['info']) {
  173. return -1;
  174. }
  175. if ($code['info']['status'] == 2) {
  176. return -2;
  177. }
  178. if ($code['info']['end_dh_day'] && $code['info']['end_dh_day'] > 0) {
  179. $code['eddate'] = ($code['bdate'] ? $code['bdate'] : $code['cdate']) + ($code['info']['end_dh_day'] * 86400);
  180. }
  181. if (isset($code['eddate']) && $code['eddate'] && $code['eddate'] > 0) {
  182. $time = time();
  183. if ($code['eddate'] <= $time) {
  184. return -3;
  185. }
  186. }
  187. return 1;
  188. }
  189. }