StoreCouponUser.php 2.6 KB

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