StoreCouponUser.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\model\ump;
  8. use app\admin\model\wechat\WechatUser as UserModel;
  9. use traits\ModelTrait;
  10. use basic\ModelBasic;
  11. /**
  12. * Class StoreCategory
  13. * @package app\admin\model\store
  14. */
  15. class StoreCouponUser extends ModelBasic
  16. {
  17. use ModelTrait;
  18. /**
  19. * @param $where
  20. * @return array
  21. */
  22. public static function systemPage($where){
  23. $model = new self;
  24. if($where['status'] != '') $model = $model->where('status',$where['status']);
  25. if($where['is_fail'] != '') $model = $model->where('status',$where['is_fail']);
  26. if($where['coupon_title'] != '') $model = $model->where('coupon_title','LIKE',"%$where[coupon_title]%");
  27. if($where['nickname'] != ''){
  28. $uid = UserModel::where('nickname','LIKE',"%$where[nickname]%")->column('uid');
  29. $model = $model->where('uid','IN',implode(',',$uid));
  30. };
  31. // $model = $model->where('is_del',0);
  32. $model = $model->order('id desc');
  33. return self::page($model,function ($item){
  34. $item['nickname'] = UserModel::where('uid',$item['uid'])->value('nickname');
  35. },$where);
  36. }
  37. /**
  38. * 给用户发放优惠券
  39. * @param $coupon
  40. * @param $user
  41. * @return int|string
  42. */
  43. public static function setCoupon($coupon,$user){
  44. $data = array();
  45. foreach ($user as $k=>$v){
  46. $data[$k]['cid'] = $coupon['id'];
  47. $data[$k]['uid'] = $v;
  48. $data[$k]['coupon_title'] = $coupon['title'];
  49. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  50. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  51. $data[$k]['add_time'] = time();
  52. $data[$k]['end_time'] = $data[$k]['add_time']+$coupon['coupon_time']*86400;
  53. }
  54. $data_num = array_chunk($data,30);
  55. self::beginTrans();
  56. $res = true;
  57. foreach ($data_num as $k=>$v){
  58. $res = $res && self::insertAll($v);
  59. }
  60. self::checkTrans($res);
  61. return $res;
  62. }
  63. /**
  64. * TODO 恢复优惠券
  65. * @param $id
  66. * @return StoreCouponUser|bool
  67. */
  68. public static function recoverCoupon($id)
  69. {
  70. $status = self::where('id',$id)->value('status');
  71. if($status) return self::where('id',$id)->update(['status'=>0,'use_time'=>'']);
  72. else return true;
  73. }
  74. }