Core.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Code\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. public function createCodeByOrder($order, $num = 0)
  7. {
  8. $code = '';
  9. if ($order['type'] == 3 && !$order['code']) {
  10. Dever::setInput('set_product_id', $order['product_id']);
  11. # 购买兑换码
  12. $product_num = 1;
  13. $code_num = 1;
  14. if ($order['buy_id'] > 0) {
  15. $buy = Dever::load('act/order')->getBuy($order['buy_id']);
  16. $product_num = $buy['num'];
  17. $code_num = $buy['code'];
  18. }
  19. # 检查之前有多少个兑换码,防止多发
  20. $code_where['order_id'] = $order['order_id'];
  21. $total = Dever::db('code/info')->total($code_where);
  22. if ($total && $total > 0) {
  23. if ($code_num > $total) {
  24. $code_num = $code_num - $total;
  25. } else {
  26. $one = Dever::db('code/info')->one($code_where);
  27. return $one['code'];
  28. }
  29. }
  30. if ($code_num > 1) {
  31. $product_num = intval($product_num/$code_num);
  32. if ($num < 0) {
  33. # 订阅
  34. $state = Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
  35. if ($state) {
  36. $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);
  37. } else {
  38. $num = 0;
  39. }
  40. }
  41. $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);
  42. if ($num == 0) {
  43. $code = $result;
  44. }
  45. } else {
  46. # 订阅
  47. Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
  48. $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);
  49. }
  50. }
  51. return $code;
  52. }
  53. 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)
  54. {
  55. if ($product_id > 0) {
  56. Dever::setInput('set_product_id', $product_id);
  57. }
  58. //$data['product'] = $product;
  59. $data['cate_id'] = $cate_id;
  60. $data['product_id'] = $product_id;
  61. $data['seller_id'] = $seller_id;
  62. $data['product_num'] = $product_num;
  63. $data['type'] = $type;
  64. if ($order_id) {
  65. $data['order_id'] = $order_id;
  66. }
  67. if ($create_uid > 0) {
  68. $data['create_uid'] = $create_uid;
  69. if ($type == 3) {
  70. $data['ldate'] = time();
  71. $data['uid'] = $create_uid;
  72. }
  73. }
  74. $data['score'] = $score;
  75. $code = Dever::db('code/info')->insert($data, $num);
  76. return $code;
  77. }
  78. }