Coupon.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. self::T_FULL=>'满减券',
  10. self::T_WU=>'抵用券'
  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. public function goods(){
  25. return $this->belongsToMany(Goods::class,CouponGoods::class,'goods_id','coupon_id');
  26. }
  27. protected static function init()
  28. {
  29. self::beforeWrite(function (self $coupon){
  30. if($coupon['type']==2){
  31. //unset($coupon['amount_full']);
  32. }
  33. });
  34. }
  35. public function sendToUser($users,$num){
  36. $users=array_unique($users);
  37. $users=array_filter($users);
  38. $this['num_send']=$this['num_send']+$num*count($users);
  39. if($this['num_send']>$this['num']){
  40. throw_user("[{$this['id']}]券数量不足");
  41. }
  42. $this->save();
  43. foreach ($users as $userId){
  44. $user=User::find($userId);
  45. if($user){
  46. for ($i=0;$i<$num;$i++) {
  47. UserCoupon::toUser($user, $this);
  48. }
  49. SiteMsg::sendMsg(SiteMsg::TYPE_COUPON,$user);
  50. }
  51. }
  52. }
  53. }