StoreCouponUser.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\ump;
  7. use app\admin\model\wechat\WechatUser as UserModel;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. /**
  11. * Class StoreCategory
  12. * @package app\admin\model\store
  13. */
  14. class StoreCouponUser extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'store_coupon_user';
  26. use ModelTrait;
  27. /**
  28. * @param $where
  29. * @return array
  30. */
  31. public static function systemPage($where)
  32. {
  33. $model = new self;
  34. $model = $model->alias('a')
  35. ->field('a.*,b.nickname')
  36. ->leftJoin('user b', 'a.uid=b.uid');
  37. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  38. if ($where['is_fail'] != '') $model = $model->where('a.status', $where['is_fail']);
  39. if ($where['coupon_title'] != '') $model = $model->where('a.coupon_title', 'LIKE', "%$where[coupon_title]%");
  40. if ($where['nickname'] != '') $model = $model->where('b.nickname|b.uid', 'LIKE', "%".$where['nickname']."%");
  41. // $model = $model->where('is_del',0);
  42. $model = $model->order('id desc');
  43. return $model->paginate(['list_rows'=>20,'query' => request()->param()]);
  44. }
  45. /**
  46. * 给用户发放优惠券
  47. * @param $coupon
  48. * @param $user
  49. * @return int|string
  50. */
  51. public static function setCoupon($coupon, $user)
  52. {
  53. $data = array();
  54. foreach ($user as $k => $v) {
  55. $data[$k]['cid'] = $coupon['id'];
  56. $data[$k]['uid'] = $v;
  57. $data[$k]['coupon_title'] = $coupon['title'];
  58. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  59. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  60. $data[$k]['add_time'] = $coupon['start_time'];
  61. $data[$k]['end_time'] = $coupon['end_time'];
  62. $data[$k]['code'] = generate_promotion_code(1);
  63. }
  64. $data_num = array_chunk($data, 30);
  65. self::beginTrans();
  66. $res = true;
  67. foreach ($data_num as $k => $v) {
  68. $res = $res && self::insertAll($v);
  69. }
  70. self::checkTrans($res);
  71. return $res;
  72. }
  73. /**
  74. * TODO 恢复优惠券
  75. * @param $id
  76. * @return StoreCouponUser|bool
  77. */
  78. public static function recoverCoupon($id)
  79. {
  80. $status = self::where('id', $id)->value('status');
  81. if ($status) return self::where('id', $id)->update(['status' => 0, 'use_time' => '']);
  82. else return true;
  83. }
  84. }