UserCoupon.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public function goods(){
  22. return $this->belongsToMany(Goods::class,UserCouponGoods::class,'goods_id','coupon_id');
  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 static function toUser(User $user,Coupon $coupon){
  33. /** @var static $userCoupon */
  34. $userCoupon=$user->coupon()->save([
  35. 'name'=>$coupon['name'],
  36. 'type'=>$coupon['type'],
  37. 'amount'=>$coupon['amount'],
  38. 'amount_full'=>$coupon['amount_full']??null,
  39. 'time_start'=>$coupon['time_start'],
  40. 'time_end'=>$coupon['time_end'],
  41. ]);
  42. $goods_ids=$coupon->bindGoods()->column('goods_id');
  43. $arr=[];
  44. foreach ($goods_ids as $goods_id){
  45. $arr[]=[
  46. 'goods_id'=>$goods_id,
  47. ];
  48. }
  49. $userCoupon->bindGoods()->saveAll($arr);
  50. }
  51. public function scopeCanUse(Query $query){
  52. $time=date('Y-m-d H:i:s');
  53. $query->where('time_start','<',$time)
  54. ->where('is_use',0)
  55. ->where('time_end','>',$time);
  56. }
  57. public function scopeFilterExpired(Query $query,$notExpire=true){
  58. $time = date('Y-m-d H:i:s');
  59. if(!is_null($notExpire)) {
  60. if ($notExpire) {
  61. $query->where('time_start', '<', $time)->where('time_end', '>', $time);
  62. } else {
  63. $query->where('time_end', '<', $time)->where('is_use', 0);
  64. }
  65. }
  66. }
  67. public function scopeFilterUse(Query $query,$used=1){
  68. $query->where('is_use',$used);
  69. }
  70. public function needMax(){
  71. return $this->getAttr('type')==Coupon::T_FULL;
  72. }
  73. public function checkCanUse($amount, $goods_id){
  74. $couponGoods=$this->bindGoods()->column('goods_id');
  75. if(empty($couponGoods)){
  76. $couponGoods[]=$goods_id;
  77. }
  78. $time=date('Y-m-d H:i:s');
  79. if(
  80. (($this['type']==Coupon::T_FULL && $amount>$this['amount_full'])||
  81. ($this['type']==Coupon::T_WU && $amount>$this['amount'])) &&
  82. in_array($goods_id,$couponGoods) &&
  83. $this['is_use']==0 &&
  84. $this['time_start']<$time && $this['time_end']>$time
  85. ){
  86. return true;
  87. }
  88. return false;
  89. }
  90. public function setUse(){
  91. $this['is_use']=1;
  92. $this->save();
  93. }
  94. public function getNoAttr($id,$model){
  95. return sprintf('%03d',$model['id']);
  96. }
  97. public function getIsExpireAttr($_,$model){
  98. return time()>strtotime($model['time_end']);
  99. }
  100. public static function makeUse($id,$use){
  101. $coupon=self::where('id',$id)->find();
  102. if($coupon){
  103. $coupon['is_use']=$use;
  104. $coupon->save();
  105. }
  106. }
  107. }