Coupon.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Coupon
  5. {
  6. # 获取分享活动的优惠券列表
  7. public function getData($shop_id)
  8. {
  9. $data = Dever::db('shop/coupon')->getData(array('shop_id' => $shop_id));
  10. if ($data) {
  11. foreach ($data as $k => $v) {
  12. $coupon = Dever::db('goods/coupon')->find($v['coupon_id']);
  13. $data[$k]['name'] = $coupon['name'];
  14. }
  15. }
  16. return $data;
  17. }
  18. # 获取分享活动的优惠券列表
  19. public function getAll($shop_id)
  20. {
  21. $data = Dever::db('shop/coupon_act_1')->getData(array('shop_id' => $shop_id));
  22. if ($data) {
  23. foreach ($data as $k => $v) {
  24. $shop_coupon = Dever::db('shop/coupon')->getOne($v['shop_coupon_id']);
  25. if ($shop_coupon && $shop_coupon['total'] > 0) {
  26. $info = Dever::db('goods/coupon')->one($shop_coupon['coupon_id']);
  27. if (!$info) {
  28. continue;
  29. }
  30. $data[$k]['cash'] = $info['cash'];
  31. $data[$k]['total_cash'] = $info['total_cash'];
  32. $data[$k]['total'] = $shop_coupon['total'];
  33. $data[$k]['shop_coupon_id'] = $v['shop_coupon_id'];
  34. $data[$k]['name'] = $info['name'];
  35. } else {
  36. continue;
  37. }
  38. }
  39. }
  40. return $data;
  41. }
  42. # 验证是否可以领取
  43. public function getOne($uid, $shop, $shop_coupon_id, $num = 1, $act = true)
  44. {
  45. if (!$shop_coupon_id) {
  46. return false;
  47. }
  48. if ($act) {
  49. $data = Dever::db('shop/coupon_act_1')->getOne(array('shop_id' => $shop['id'], 'shop_coupon_id' => $shop_coupon_id));
  50. if (!$data) {
  51. return false;
  52. }
  53. }
  54. $data = Dever::db('shop/coupon')->getOne($shop_coupon_id);
  55. if (!$data) {
  56. return false;
  57. }
  58. if ($data['total'] <= 0) {
  59. return false;
  60. }
  61. $user = Dever::db('shop/user_coupon')->find(array('uid' => $uid, 'shop_coupon_id' => $data['id'], 'status' => 1));
  62. if ($user) {
  63. return false;
  64. }
  65. $state = Dever::load('shop/lib/share')->getOne($uid, $shop['id'], 1);
  66. if (!$state) {
  67. return false;
  68. }
  69. $state = Dever::load('shop/lib/coupon.take_commit', $uid, $shop, $data, $num);
  70. if ($state) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. # 领取
  76. public function take_commit($uid, $shop, $shop_coupon, $num = 1)
  77. {
  78. $add_num = 0;
  79. for ($i = 0; $i < $num; $i++) {
  80. $data['uid'] = $uid;
  81. $data['shop_id'] = $shop['id'];
  82. $data['city'] = $shop['city'];
  83. $data['coupon_id'] = $shop_coupon['coupon_id'];
  84. $data['shop_coupon_id'] = $shop_coupon['id'];
  85. $data['edate'] = time() + ($shop_coupon['day'] * 86400);
  86. //$data['cash'] = $cash;
  87. $id = Dever::db('shop/user_coupon')->insert($data);
  88. if ($id) {
  89. $add_num += 1;
  90. }
  91. }
  92. if ($add_num > 0) {
  93. Dever::db('shop/coupon')->updateNum(array('where_id' => $shop_coupon['id'], 'act_num' => $add_num));
  94. }
  95. return $id;
  96. }
  97. }