Core.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Code\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. public function create_test()
  7. {
  8. $order_id = Dever::input('order_id');
  9. $order = Dever::db('act/order')->one(array('order_id' => $order_id));
  10. print_r($order);
  11. $code = $this->createCodeByOrder($order, -1);
  12. return $code;
  13. }
  14. public function createCodeByOrder($order, $num = 0)
  15. {
  16. $code = '';
  17. if ($order['type'] == 3 && !$order['code']) {
  18. if (!$order['order_id']) {
  19. $order['order_id'] = $order['id'];
  20. }
  21. Dever::setInput('set_product_id', $order['product_id']);
  22. # 购买兑换码
  23. $product_num = 1;
  24. $code_num = 1;
  25. if ($order['buy_id'] > 0) {
  26. $buy = Dever::load('act/order')->getBuy($order['buy_id']);
  27. $product_num = $buy['num'];
  28. $code_num = $buy['code'];
  29. }
  30. # 检查之前有多少个兑换码,防止多发
  31. $code_where['order_id'] = $order['order_id'];
  32. $total = Dever::db('code/info')->total($code_where);
  33. if ($total && $total > 0) {
  34. if ($code_num > $total) {
  35. $code_num = $code_num - $total;
  36. } else {
  37. $one = Dever::db('code/info')->one($code_where);
  38. return $one['code'];
  39. }
  40. }
  41. if ($code_num > 1) {
  42. $product_num = intval($product_num/$code_num);
  43. if ($num < 0) {
  44. # 订阅
  45. $state = Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
  46. if ($state) {
  47. $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 3, 1);
  48. } else {
  49. $num = 0;
  50. }
  51. }
  52. $result = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 1, $code_num + $num);
  53. if ($num == 0) {
  54. $code = $result;
  55. }
  56. } else {
  57. # 订阅
  58. $state = false;
  59. if ($num < 0) {
  60. $state = Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
  61. }
  62. if ($state) {
  63. $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 3);
  64. } else {
  65. $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 1);
  66. }
  67. }
  68. }
  69. return $code;
  70. }
  71. public function createCode($product, $cate_id, $product_id, $seller_id, $product_num = 0, $create_uid = -1, $order_id = false, $score = 0, $type = 1, $num = 1)
  72. {
  73. if ($product_id > 0) {
  74. Dever::setInput('set_product_id', $product_id);
  75. }
  76. //$data['product'] = $product;
  77. $data['cate_id'] = $cate_id;
  78. $data['product_id'] = $product_id;
  79. $data['seller_id'] = $seller_id;
  80. $data['product_num'] = $product_num;
  81. $data['type'] = $type;
  82. if ($order_id) {
  83. $data['order_id'] = $order_id;
  84. }
  85. if ($create_uid > 0) {
  86. $data['create_uid'] = $create_uid;
  87. if ($type == 3) {
  88. $data['ldate'] = time();
  89. $data['uid'] = $create_uid;
  90. }
  91. }
  92. $data['score'] = $score;
  93. $db = Dever::db('code/info');
  94. if ($product_id > 0) {
  95. $code = $db->insert($data, $num);
  96. $code = $this->view($code, true);
  97. return $code;
  98. } else {
  99. for ($i = 0; $i < $num; $i++) {
  100. $data['i'] = $i;
  101. $id = $db->insert($data);
  102. if ($id) {
  103. $code = $this->view($id, false);
  104. $db->update(array('code' => $code, 'where_id' => $id));
  105. }
  106. }
  107. return $code;
  108. }
  109. }
  110. public function view($id, $state)
  111. {
  112. if ($state) {
  113. $code = Dever::uid($id * 10000000);
  114. $code = 'M' . $code;
  115. } else {
  116. $code = Dever::rand(4);
  117. $code = 'T' . $code . $id;
  118. }
  119. return $code;
  120. }
  121. }