Core.php 4.6 KB

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