Coupon.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Coupon extends Model
  5. {
  6. protected $autoWriteTimestamp=true;
  7. protected $updateTime=null;
  8. public static $types=[
  9. 1=>'满减券',
  10. 2=>'无门槛券'
  11. ];
  12. const T_FULL=1;
  13. const T_WU=2;
  14. /**
  15. * @return string[]
  16. */
  17. public static function getTypes(): array
  18. {
  19. return self::$types;
  20. }
  21. public function bindGoods(){
  22. return $this->hasMany(CouponGoods::class);
  23. }
  24. protected static function init()
  25. {
  26. self::beforeWrite(function (self $coupon){
  27. if($coupon['type']==2){
  28. unset($coupon['amount_full']);
  29. }
  30. });
  31. }
  32. public function sendToUser($users,$num){
  33. $users=array_unique($users);
  34. $users=array_filter($users);
  35. $this['num_send']=$this['num_send']+$num*count($users);
  36. if($this['num_send']>$this['num']){
  37. throw_user("[{$this['id']}]券数量不足");
  38. }
  39. $this->save();
  40. foreach ($users as $userId){
  41. $user=User::find($userId);
  42. if($user){
  43. for ($i=0;$i<$num;$i++) {
  44. UserCoupon::toUser($user, $this);
  45. }
  46. SiteMsg::sendMsg(SiteMsg::TYPE_COUPON,$user);
  47. }
  48. }
  49. }
  50. }