StoreCouponUser.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 self::page($model);
  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'] = time();
  61. $data[$k]['end_time'] = $data[$k]['add_time'] + $coupon['coupon_time'] * 86400;
  62. }
  63. $data_num = array_chunk($data, 30);
  64. self::beginTrans();
  65. $res = true;
  66. foreach ($data_num as $k => $v) {
  67. $res = $res && self::insertAll($v);
  68. }
  69. self::checkTrans($res);
  70. return $res;
  71. }
  72. /**
  73. * TODO 恢复优惠券
  74. * @param $id
  75. * @return StoreCouponUser|bool
  76. */
  77. public static function recoverCoupon($id)
  78. {
  79. $status = self::where('id', $id)->value('status');
  80. if ($status) return self::where('id', $id)->update(['status' => 0, 'use_time' => '']);
  81. else return true;
  82. }
  83. }