UserCoupon.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * @method Query|$this filterUse($used=1)
  7. * @method Query|$this canUse()
  8. * @method Query|$this filterExpired($notExpired=true)
  9. */
  10. class UserCoupon extends Model
  11. {
  12. protected $autoWriteTimestamp=true;
  13. protected $updateTime=null;
  14. protected $append=[
  15. 'no',
  16. 'is_expire',
  17. ];
  18. public function bindGoods(){
  19. return $this->hasMany(UserCouponGoods::class,'coupon_id');
  20. }
  21. protected static function init()
  22. {
  23. self::beforeWrite(function (self $coupon){
  24. if($coupon['type']==2){
  25. unset($coupon['amount_full']);
  26. }
  27. });
  28. }
  29. public static function toUser(User $user,Coupon $coupon){
  30. /** @var static $userCoupon */
  31. $userCoupon=$user->coupon()->save([
  32. 'name'=>$coupon['name'],
  33. 'type'=>$coupon['type'],
  34. 'amount'=>$coupon['amount'],
  35. 'amount_full'=>$coupon['amount_full']??null,
  36. 'time_start'=>$coupon['time_start'],
  37. 'time_end'=>$coupon['time_end'],
  38. ]);
  39. $goods_ids=$coupon->bindGoods()->column('goods_id');
  40. $arr=[];
  41. foreach ($goods_ids as $goods_id){
  42. $arr[]=[
  43. 'goods_id'=>$goods_id,
  44. ];
  45. }
  46. $userCoupon->bindGoods()->saveAll($arr);
  47. }
  48. public function scopeCanUse(Query $query){
  49. $time=date('Y-m-d H:i:s');
  50. $query->where('time_start','<',$time)
  51. ->where('is_use',0)
  52. ->where('time_end','>',$time);
  53. }
  54. public function scopeFilterExpired(Query $query,$notExpire=true){
  55. $time = date('Y-m-d H:i:s');
  56. if($notExpire) {
  57. $query->where('time_start', '<', $time)->where('time_end', '>', $time);
  58. }else{
  59. $query->where('time_end','<',$time)->where('is_use',0);
  60. }
  61. }
  62. public function scopeFilterUse(Query $query,$used=1){
  63. $query->where('is_use',$used);
  64. }
  65. public function needMax(){
  66. return $this->getAttr('type')==Coupon::T_FULL;
  67. }
  68. public function checkCanUse($amount, $goods_id){
  69. $couponGoods=$this->bindGoods()->column('goods_id');
  70. $time=date('Y-m-d H:i:s');
  71. if(
  72. (($this['type']==Coupon::T_FULL && $amount>=$this['amount_full'])||
  73. ($this['type']==Coupon::T_WU && $amount>$this['amount'])) &&
  74. in_array($goods_id,$couponGoods) &&
  75. $this['is_use']==0 &&
  76. $this['time_start']<$time && $this['time_end']>$time
  77. ){
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function setUse(){
  83. $this['is_use']=1;
  84. $this->save();
  85. }
  86. public function getNoAttr($id,$model){
  87. return sprintf('%03d',$model['id']);
  88. }
  89. public function getIsExpireAttr($_,$model){
  90. return time()>strtotime($model['time_end']);
  91. }
  92. public static function makeUse($id,$use){
  93. $coupon=self::where('id',$id)->find();
  94. if($coupon){
  95. $coupon['is_use']=$use;
  96. $coupon->save();
  97. }
  98. }
  99. }