StoreCouponUser.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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,c.name')
  36. ->leftJoin('system_store c', 'a.store_id = c.id')
  37. ->leftJoin('user b', 'a.uid=b.uid');
  38. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  39. if ($where['is_fail'] != '') $model = $model->where('a.status', $where['is_fail']);
  40. if ($where['coupon_title'] != '') $model = $model->where('a.coupon_title', 'LIKE', "%$where[coupon_title]%");
  41. if ($where['nickname'] != '') $model = $model->where('b.nickname|b.uid', 'LIKE', "%".$where['nickname']."%");
  42. // $model = $model->where('is_del',0);
  43. $model = $model->order('id desc');
  44. return $model->paginate(['list_rows'=>20,'query' => request()->param()]);
  45. }
  46. /**
  47. * 给用户发放优惠券
  48. * @param $coupon
  49. * @param $user
  50. * @return int|string
  51. */
  52. public static function setCoupon($coupon, $user)
  53. {
  54. $data = array();
  55. foreach ($user as $k => $v) {
  56. $data[$k]['cid'] = $coupon['id'];
  57. $data[$k]['uid'] = $v;
  58. $data[$k]['coupon_title'] = $coupon['title'];
  59. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  60. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  61. $data[$k]['add_time'] = $coupon['start_time'];
  62. $data[$k]['end_time'] = $coupon['end_time'];
  63. $data[$k]['code'] = generate_promotion_code(1);
  64. $data[$k]['store_id'] = $coupon['store_id'];
  65. }
  66. $data_num = array_chunk($data, 30);
  67. self::beginTrans();
  68. $res = true;
  69. foreach ($data_num as $k => $v) {
  70. $res = $res && self::insertAll($v);
  71. }
  72. self::checkTrans($res);
  73. return $res;
  74. }
  75. /**
  76. * TODO 恢复优惠券
  77. * @param $id
  78. * @return StoreCouponUser|bool
  79. */
  80. public static function recoverCoupon($id)
  81. {
  82. $status = self::where('id', $id)->value('status');
  83. if ($status) return self::where('id', $id)->update(['status' => 0, 'use_time' => '']);
  84. else return true;
  85. }
  86. }