StoreCouponUser.php 5.9 KB

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