UserCoupon.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(!is_null($notExpire)) {
  57. if ($notExpire) {
  58. $query->where('time_start', '<', $time)->where('time_end', '>', $time);
  59. } else {
  60. $query->where('time_end', '<', $time)->where('is_use', 0);
  61. }
  62. }
  63. }
  64. public function scopeFilterUse(Query $query,$used=1){
  65. $query->where('is_use',$used);
  66. }
  67. public function needMax(){
  68. return $this->getAttr('type')==Coupon::T_FULL;
  69. }
  70. public function checkCanUse($amount, $goods_id){
  71. $couponGoods=$this->bindGoods()->column('goods_id');
  72. if(empty($couponGoods)){
  73. $couponGoods[]=$goods_id;
  74. }
  75. $time=date('Y-m-d H:i:s');
  76. if(
  77. (($this['type']==Coupon::T_FULL && $amount>$this['amount_full'])||
  78. ($this['type']==Coupon::T_WU && $amount>$this['amount'])) &&
  79. in_array($goods_id,$couponGoods) &&
  80. $this['is_use']==0 &&
  81. $this['time_start']<$time && $this['time_end']>$time
  82. ){
  83. return true;
  84. }
  85. return false;
  86. }
  87. public function setUse(){
  88. $this['is_use']=1;
  89. $this->save();
  90. }
  91. public function getNoAttr($id,$model){
  92. return sprintf('%03d',$model['id']);
  93. }
  94. public function getIsExpireAttr($_,$model){
  95. return time()>strtotime($model['time_end']);
  96. }
  97. public static function makeUse($id,$use){
  98. $coupon=self::where('id',$id)->find();
  99. if($coupon){
  100. $coupon['is_use']=$use;
  101. $coupon->save();
  102. }
  103. }
  104. }