StoreCouponUser.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\routine\model\store;
  8. use basic\ModelBasic;
  9. use traits\ModelTrait;
  10. class StoreCouponUser extends ModelBasic
  11. {
  12. use ModelTrait;
  13. /**
  14. * 获取用户优惠券(全部)
  15. * @return \think\response\Json
  16. */
  17. public static function getUserAllCoupon($uid)
  18. {
  19. self::checkInvalidCoupon();
  20. $couponList = self::where('uid',$uid)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  21. return self::tidyCouponList($couponList);
  22. }
  23. /**
  24. * 获取用户优惠券(未使用)
  25. * @return \think\response\Json
  26. */
  27. public static function getUserValidCoupon($uid)
  28. {
  29. self::checkInvalidCoupon();
  30. $couponList = self::where('uid',$uid)->where('status',0)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  31. return self::tidyCouponList($couponList);
  32. }
  33. /**
  34. * 获取用户优惠券(已使用)
  35. * @return \think\response\Json
  36. */
  37. public static function getUserAlreadyUsedCoupon($uid)
  38. {
  39. self::checkInvalidCoupon();
  40. $couponList = self::where('uid',$uid)->where('status',1)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  41. return self::tidyCouponList($couponList);
  42. }
  43. /**
  44. * 获取用户优惠券(已过期)
  45. * @return \think\response\Json
  46. */
  47. public static function getUserBeOverdueCoupon($uid)
  48. {
  49. self::checkInvalidCoupon();
  50. $couponList = self::where('uid',$uid)->where('status',2)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  51. return self::tidyCouponList($couponList);
  52. }
  53. public static function beUsableCoupon($uid,$price)
  54. {
  55. return self::where('uid',$uid)->where('is_fail',0)->where('status',0)->where('use_min_price','<=',$price)->find();
  56. }
  57. /**
  58. * 获取用户可以使用的优惠券
  59. * @param $uid
  60. * @param $price
  61. * @return false|\PDOStatement|string|\think\Collection
  62. */
  63. public static function beUsableCouponList($uid,$price){
  64. return self::where('uid',$uid)->where('is_fail',0)->where('status',0)->where('use_min_price','<=',$price)->select();
  65. }
  66. public static function validAddressWhere($model=null,$prefix = '')
  67. {
  68. self::checkInvalidCoupon();
  69. if($prefix) $prefix .='.';
  70. $model = self::getSelfModel($model);
  71. return $model->where("{$prefix}is_fail",0)->where("{$prefix}status",0);
  72. }
  73. public static function checkInvalidCoupon()
  74. {
  75. self::where('end_time','<',time())->where('status',0)->update(['status'=>2]);
  76. }
  77. public static function tidyCouponList($couponList)
  78. {
  79. $time = time();
  80. foreach ($couponList as $k=>$coupon){
  81. $coupon['_add_time'] = date('Y/m/d',$coupon['add_time']);
  82. $coupon['_end_time'] = date('Y/m/d',$coupon['end_time']);
  83. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  84. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  85. if($coupon['is_fail']){
  86. $coupon['_type'] = 0;
  87. $coupon['_msg'] = '已失效';
  88. }else if ($coupon['status'] == 1){
  89. $coupon['_type'] = 0;
  90. $coupon['_msg'] = '已使用';
  91. }else if ($coupon['status'] == 2){
  92. $coupon['_type'] = 0;
  93. $coupon['_msg'] = '已过期';
  94. }else if($coupon['add_time'] > $time || $coupon['end_time'] < $time){
  95. $coupon['_type'] = 0;
  96. $coupon['_msg'] = '已过期';
  97. }else{
  98. if($coupon['add_time']+ 3600*24 > $time){
  99. $coupon['_type'] = 2;
  100. $coupon['_msg'] = '可使用';
  101. }else{
  102. $coupon['_type'] = 1;
  103. $coupon['_msg'] = '可使用';
  104. }
  105. }
  106. $couponList[$k] = $coupon;
  107. }
  108. return $couponList;
  109. }
  110. public static function getUserValidCouponCount($uid)
  111. {
  112. self::checkInvalidCoupon();
  113. return self::where('uid',$uid)->where('status',0)->order('is_fail ASC,status ASC,add_time DESC')->count();
  114. }
  115. public static function useCoupon($id)
  116. {
  117. return self::where('id',$id)->update(['status'=>1,'use_time'=>time()]);
  118. }
  119. /**
  120. * TODO 恢复优惠券
  121. * @param $id
  122. * @return StoreCouponUser|bool
  123. */
  124. public static function recoverCoupon($id)
  125. {
  126. $status = self::where('id',$id)->value('status');
  127. if($status) return self::where('id',$id)->update(['status'=>0,'use_time'=>'']);
  128. else return true;
  129. }
  130. public static function addUserCoupon($uid,$cid,$type = 'get')
  131. {
  132. $couponInfo = StoreCoupon::find($cid);
  133. if(!$couponInfo) return self::setErrorInfo('优惠劵不存在!');
  134. $data = [];
  135. $data['cid'] = $couponInfo['id'];
  136. $data['uid'] = $uid;
  137. $data['coupon_title'] = $couponInfo['title'];
  138. $data['coupon_price'] = $couponInfo['coupon_price'];
  139. $data['use_min_price'] = $couponInfo['use_min_price'];
  140. $data['add_time'] = time();
  141. $data['end_time'] = $data['add_time']+$couponInfo['coupon_time']*86400;
  142. $data['type'] = $type;
  143. return self::set($data);
  144. }
  145. }