CronSendService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: CGOD
  5. * Date: 2019/12/18
  6. * Time: 17:33
  7. */
  8. namespace app\frontend\modules\coupon\services;
  9. use app\common\models\Coupon;
  10. use app\common\models\MemberCoupon;
  11. class CronSendService
  12. {
  13. public $record;
  14. public $coupon;
  15. public $type;
  16. public $sendNum;
  17. public $numReason;
  18. public function __construct($record,$numReason,$type)
  19. {
  20. $this->record = $record;
  21. $this->numReason = $numReason;
  22. $this->type = $type;//1:订单完成 2:每月发放 3:订单支付
  23. }
  24. public function sendCoupon()
  25. {
  26. $coupon = Coupon::uniacid()->where('id',$this->record->coupon_id)->first();
  27. if($coupon) {
  28. $this->coupon = $coupon;
  29. }else {
  30. $this->numReason = $this->numReason.'优惠券不存在';
  31. return;
  32. }
  33. $res = $this->judgeCoupon();
  34. if($res && $this->sendNum>0) {
  35. for ($i = 1; $i <= $this->sendNum; $i++) {
  36. (new CouponSendService())->sendCouponToMember($this->record->hasOneOrderGoods->uid, $this->record->coupon_id, 4, $this->record->hasOneOrderGoods->hasOneOrder->order_sn);
  37. }
  38. }
  39. if($this->type == 1) {
  40. $this->endOrderSend();
  41. } elseif ($this->type == 2) {
  42. $this->endMonthSend();
  43. } elseif ($this->type == 3) {
  44. $this->endOrderSend();
  45. }
  46. }
  47. private function judgeCoupon()//判断能发多少张
  48. {
  49. $this->sendNum = $num = $this->record->coupon_several;
  50. if($this->coupon->total != -1)
  51. {
  52. $all = MemberCoupon::uniacid()->where("coupon_id", $this->record->coupon_id)->count();//优惠券发放总数
  53. $afterAll = bcadd($all,$num);
  54. if($afterAll > $this->coupon->total)
  55. {
  56. if($all >= $this->coupon->total)
  57. {
  58. $this->numReason = $this->numReason.'优惠券已达发放总数';
  59. return false;
  60. } else{
  61. $num = bcsub($afterAll,$this->coupon->total);
  62. $this->numReason = $this->numReason.'优惠券发放后达总数,发'.$num.'张';
  63. }
  64. }
  65. }
  66. // if($this->coupon->get_type == 1){
  67. // if($this->coupon->get_max != -1)
  68. // {
  69. // $person = MemberCoupon::uniacid()
  70. // ->where(["coupon_id"=>$this->record->coupon_id,"uid"=>$this->record->hasOneOrderGoods->uid])
  71. // ->count();//会员已有数量
  72. // $afterPerson = bcadd($person,$num);
  73. // if($afterPerson > $this->coupon->get_max)
  74. // {
  75. // if($person >= $this->coupon->get_max)
  76. // {
  77. // $this->numReason = $this->numReason.'优惠券已达个人领取总数';
  78. // return false;
  79. // } else{
  80. // $num = bcsub($afterPerson,$this->coupon->get_max);
  81. // $this->numReason = $this->numReason.'优惠券发放后达个人领取总数,发'.$num.'张';
  82. // }
  83. // }
  84. // }
  85. // }
  86. $this->sendNum = $num;
  87. return true;
  88. }
  89. private function endOrderSend()
  90. {
  91. $model = $this->record;
  92. $model->status = 1;
  93. $model->num_reason = $this->numReason;
  94. $model->save();
  95. }
  96. private function endMonthSend()
  97. {
  98. $model = $this->record;
  99. $model->end_send_num += 1;
  100. $model->num_reason = $this->numReason;
  101. if($model->end_send_num >= $model->send_num)
  102. {
  103. $model->status = 1;
  104. }
  105. $model->save();
  106. }
  107. }