UserCoupon.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\common\model;
  3. use Carbon\Carbon;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * @method Query|$this filterUse($used=1)
  8. * @method Query|$this canUse()
  9. * @method Query|$this filterExpired($notExpired=true)
  10. * @property float amount_full
  11. * @property float amount
  12. */
  13. class UserCoupon extends Model
  14. {
  15. protected $autoWriteTimestamp=true;
  16. protected $updateTime=null;
  17. protected $append=[
  18. 'no',
  19. 'is_expire',
  20. ];
  21. public function bindGoods(){
  22. return $this->hasMany(UserCouponGoods::class,'coupon_id');
  23. }
  24. public function goods(){
  25. return $this->belongsToMany(Goods::class,UserCouponGoods::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 static function toUser(User $user,Coupon $coupon){
  36. /** @var static $userCoupon */
  37. $userCoupon=$user->coupon()->save([
  38. 'name'=>$coupon['name'],
  39. 'type'=>$coupon['type'],
  40. 'amount'=>$coupon['amount'],
  41. 'amount_full'=>$coupon['amount_full']??null,
  42. 'time_start'=>$coupon['time_start'],
  43. 'time_end'=>$coupon['time_end'],
  44. ]);
  45. $goods_ids=$coupon->bindGoods()->column('goods_id');
  46. $arr=[];
  47. foreach ($goods_ids as $goods_id){
  48. $arr[]=[
  49. 'goods_id'=>$goods_id,
  50. ];
  51. }
  52. $userCoupon->bindGoods()->saveAll($arr);
  53. }
  54. public function scopeCanUse(Query $query){
  55. $time=date('Y-m-d H:i:s');
  56. $query->where('time_start','<',$time)
  57. ->where('is_use',0)
  58. ->where('time_end','>',$time);
  59. }
  60. public function scopeFilterExpired(Query $query,$notExpire=true){
  61. $time = date('Y-m-d H:i:s');
  62. if(!is_null($notExpire)) {
  63. if ($notExpire) {
  64. $query->where('time_start', '<', $time)->where('time_end', '>', $time);
  65. } else {
  66. $query->where('time_end', '<', $time)->where('is_use', 0);
  67. }
  68. }
  69. }
  70. public function scopeFilterUse(Query $query,$used=1){
  71. $query->where('is_use',$used);
  72. }
  73. public function needMax(){
  74. return $this->getAttr('type')==Coupon::T_FULL;
  75. }
  76. public function checkCanUse($amount, $goods_id,$allGoods=[]){
  77. $time=Carbon::now()->toDateTimeString();
  78. $couponGoods=$this->bindGoods()->column('goods_id');
  79. if($this['type']==Coupon::T_FULL){
  80. if(empty($couponGoods)){
  81. $couponGoods[]=$goods_id;
  82. }
  83. if($amount>$this['amount_full'] &&
  84. in_array($goods_id,$couponGoods) &&
  85. $this['is_use']==0 &&
  86. $this['time_start']<$time && $this['time_end']>$time
  87. ){
  88. return true;
  89. }
  90. return false;
  91. }elseif ($this['type']==Coupon::T_WU){
  92. $selfFit=!$this['is_use'] && $this['time_start']<$time && $this['time_end']>$time;
  93. $goodsAmount=0;
  94. foreach ($allGoods as $goods){
  95. if(in_array($goods['goods_id'],$couponGoods)){
  96. $goodsAmount=bcadd($goodsAmount,$goods['amount_goods']);
  97. }
  98. }
  99. return $selfFit && $goodsAmount>$this->amount_full;
  100. }
  101. return false;
  102. }
  103. public function isFor($for){
  104. switch ($this->getAttr('type')){
  105. case Coupon::T_WU:
  106. return 'order'==$for;
  107. case Coupon::T_FULL:
  108. return 'info'==$for;
  109. default:
  110. return false;
  111. }
  112. }
  113. public function setUse(){
  114. $this['is_use']=1;
  115. $this->save();
  116. }
  117. public function getNoAttr($id,$model){
  118. return sprintf('%03d',$model['id']);
  119. }
  120. public function getIsExpireAttr($_,$model){
  121. return time()>strtotime($model['time_end']);
  122. }
  123. public static function makeUse($id,$use){
  124. if(!$id){
  125. return;
  126. }
  127. $coupon=self::where('id',$id)->find();
  128. if($coupon){
  129. $coupon['is_use']=$use;
  130. $coupon->save();
  131. }
  132. }
  133. }