Core.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Code\Lib;
  3. use Dever;
  4. class Core
  5. {
  6. public function createCodeByOrder($order)
  7. {
  8. $code = '';
  9. if ($order['type'] == 3 && !$order['code']) {
  10. # 购买兑换码
  11. $product_num = 1;
  12. $code_num = 1;
  13. if ($order['buy_id'] > 0) {
  14. $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
  15. $product_num = $buy['num'];
  16. $code_num = $buy['code'];
  17. }
  18. # 检查之前有多少个兑换码,防止多发
  19. $code_where['order_id'] = $order['order_id'];
  20. $total = Dever::db('code/info')->total($code_where);
  21. if ($total && $total > 0) {
  22. if ($code_num > $total) {
  23. $code_num = $code_num - $total;
  24. } else {
  25. $one = Dever::db('code/info')->one($code_where);
  26. return $one['code'];
  27. }
  28. }
  29. if ($code_num > 1) {
  30. $product_num = intval($product_num/$code_num);
  31. for ($i = 0; $i < $code_num; $i++) {
  32. $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']);
  33. }
  34. } else {
  35. $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']);
  36. }
  37. }
  38. return $code;
  39. }
  40. public function createCode($product, $cate_id, $product_id, $seller_id, $product_num = 0, $create_uid = -1, $order_id = false, $score = 0)
  41. {
  42. $code = Dever::rand(8, 0);
  43. $data['code'] = $code;
  44. $total = Dever::db('code/info')->total($data);
  45. if ($total > 0) {
  46. return $this->createCode($product, $cate_id, $product_id, $seller_id, $product_num, $create_uid, $order_id, $score);
  47. }
  48. //$data['product'] = $product;
  49. $data['cate_id'] = $cate_id;
  50. $data['product_id'] = $product_id;
  51. $data['seller_id'] = $seller_id;
  52. $data['product_num'] = $product_num;
  53. $data['type'] = 1;
  54. if ($order_id) {
  55. $data['order_id'] = $order_id;
  56. }
  57. if ($create_uid > 0) {
  58. $data['create_uid'] = $create_uid;
  59. }
  60. $data['score'] = $score;
  61. Dever::db('code/info')->insert($data);
  62. return $code;
  63. }
  64. }