12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace app\common\model;
- use think\Model;
- class Coupon extends Model
- {
- protected $autoWriteTimestamp=true;
- protected $updateTime=null;
- public static $types=[
- 1=>'满减券',
- 2=>'无门槛券'
- ];
- const T_FULL=1;
- const T_WU=2;
- /**
- * @return string[]
- */
- public static function getTypes(): array
- {
- return self::$types;
- }
- public function bindGoods(){
- return $this->hasMany(CouponGoods::class);
- }
- protected static function init()
- {
- self::beforeWrite(function (self $coupon){
- if($coupon['type']==2){
- unset($coupon['amount_full']);
- }
- });
- }
- public function sendToUser($users,$num){
- $users=array_unique($users);
- $users=array_filter($users);
- $this['num_send']=$this['num_send']+$num*count($users);
- if($this['num_send']>$this['num']){
- throw_user("[{$this['id']}]券数量不足");
- }
- $this->save();
- foreach ($users as $userId){
- $user=User::find($userId);
- if($user){
- for ($i=0;$i<$num;$i++) {
- UserCoupon::toUser($user, $this);
- }
- SiteMsg::sendMsg(SiteMsg::TYPE_COUPON,$user);
- }
- }
- }
- }
|