StoreCouponUser.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\ebapi\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=0){
  64. $list=self::where('uid',$uid)->where('is_fail',0)->where('status',0)->where('use_min_price','<=',$price)->select();
  65. $list=count($list) ? $list->toArray() : [];
  66. foreach ($list as &$item){
  67. $item['add_time']=date('Y/m/d',$item['add_time']);
  68. $item['end_time']=date('Y/m/d',$item['end_time']);
  69. }
  70. return $list;
  71. }
  72. public static function validAddressWhere($model=null,$prefix = '')
  73. {
  74. self::checkInvalidCoupon();
  75. if($prefix) $prefix .='.';
  76. $model = self::getSelfModel($model);
  77. return $model->where("{$prefix}is_fail",0)->where("{$prefix}status",0);
  78. }
  79. public static function checkInvalidCoupon()
  80. {
  81. self::where('end_time','<',time())->where('status',0)->update(['status'=>2]);
  82. }
  83. public static function tidyCouponList($couponList)
  84. {
  85. $time = time();
  86. foreach ($couponList as $k=>$coupon){
  87. $coupon['_add_time'] = date('Y/m/d',$coupon['add_time']);
  88. $coupon['_end_time'] = date('Y/m/d',$coupon['end_time']);
  89. $coupon['use_min_price'] = number_format($coupon['use_min_price'],2);
  90. $coupon['coupon_price'] = number_format($coupon['coupon_price'],2);
  91. if($coupon['is_fail']){
  92. $coupon['_type'] = 0;
  93. $coupon['_msg'] = '已失效';
  94. }else if ($coupon['status'] == 1){
  95. $coupon['_type'] = 0;
  96. $coupon['_msg'] = '已使用';
  97. }else if ($coupon['status'] == 2){
  98. $coupon['_type'] = 0;
  99. $coupon['_msg'] = '已过期';
  100. }else if($coupon['add_time'] > $time || $coupon['end_time'] < $time){
  101. $coupon['_type'] = 0;
  102. $coupon['_msg'] = '已过期';
  103. }else{
  104. if($coupon['add_time']+ 3600*24 > $time){
  105. $coupon['_type'] = 2;
  106. $coupon['_msg'] = '可使用';
  107. }else{
  108. $coupon['_type'] = 1;
  109. $coupon['_msg'] = '可使用';
  110. }
  111. }
  112. $couponList[$k] = $coupon;
  113. }
  114. return $couponList;
  115. }
  116. public static function getUserValidCouponCount($uid)
  117. {
  118. self::checkInvalidCoupon();
  119. return self::where('uid',$uid)->where('status',0)->order('is_fail ASC,status ASC,add_time DESC')->count();
  120. }
  121. public static function useCoupon($id)
  122. {
  123. return self::where('id',$id)->update(['status'=>1,'use_time'=>time()]);
  124. }
  125. public static function addUserCoupon($uid,$cid,$type = 'get')
  126. {
  127. $couponInfo = StoreCoupon::find($cid);
  128. if(!$couponInfo) return self::setErrorInfo('优惠劵不存在!');
  129. $data = [];
  130. $data['cid'] = $couponInfo['id'];
  131. $data['uid'] = $uid;
  132. $data['coupon_title'] = $couponInfo['title'];
  133. $data['coupon_price'] = $couponInfo['coupon_price'];
  134. $data['use_min_price'] = $couponInfo['use_min_price'];
  135. $data['add_time'] = time();
  136. $data['end_time'] = $data['add_time']+$couponInfo['coupon_time']*86400;
  137. $data['type'] = $type;
  138. return self::set($data);
  139. }
  140. }