Coupon.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. class Coupon extends Model
  6. {
  7. public function couponReceive()
  8. {
  9. return $this->hasMany('CouponReceive', 'coupon_id');
  10. }
  11. /**
  12. * 发放打折券
  13. * @param $params
  14. * @throws \Exception
  15. */
  16. public function grantDiscountsCoupon($params)
  17. {
  18. $CouponReceive = model('CouponReceive');
  19. if ($params['discounts'] && $CouponReceive->where(array(
  20. 'uid' => $params['uid'],
  21. 'coupon_id' => 1,
  22. 'coupon_type' => 1,
  23. ))->find()) {
  24. //发放半价券并锁定
  25. $CouponReceive->save(array(
  26. 'uid' => $params['uid'],
  27. 'coupon_id' => 1,
  28. 'coupon_type' => 1,
  29. 'status' => 2,
  30. ));
  31. }
  32. }
  33. /**
  34. * 发放缴费券
  35. * @param $params
  36. * @throws \Exception
  37. */
  38. public function grantDeductionCoupon($params)
  39. {
  40. $coupon = $this->get($params['cid']);
  41. $couponReceive = model('CouponReceive');
  42. $data = [];
  43. switch ($params['service_type']) {
  44. case 'monthly':
  45. $num = 1;
  46. break;
  47. case 'seasonal':
  48. $num = 3;
  49. break;
  50. case 'halfYearly':
  51. $num = 6;
  52. break;
  53. case 'yearly':
  54. $num = 12;
  55. break;
  56. default:
  57. $num = 1;
  58. break;
  59. }
  60. for ($i = 0; $i < $num; $i++) {
  61. array_push($data, array(
  62. 'uid' => $params['uid'],
  63. 'oid' => $params['oid'],
  64. 'cid' => $params['cid'],
  65. 'type' => $coupon['type'],
  66. 'name' => $coupon['name'],
  67. 'value' => $coupon['value'],
  68. 'end_timestamp' => date('Y-m-d H:i:s', strtotime("+$i month 1 day")),
  69. 'create_timestamp' => date('Y-m-d H:i:s',time()),
  70. 'status' => 1,
  71. ));
  72. }
  73. // 扣除一张缴费券
  74. $data[0]['status'] = 3;
  75. $res = Db::name('coupon_receive')->insertAll($data);
  76. $first_id = (int)Db::name('coupon_receive')->getLastInsID() + 1;
  77. return $first_id;
  78. }
  79. /**
  80. * 获取并锁定缴费券
  81. * @param $uid
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. public function getDeductionCoupon($uid)
  87. {
  88. $id = model('CouponReceive')->where([['uid', '=', $uid], ['status', '=', 1]])
  89. ->order('end_timestamp')
  90. ->value('id');
  91. return $id;
  92. }
  93. }