StoreCouponUserDao.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\dao\store\coupon;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\store\coupon\StoreCouponUser;
  6. /**
  7. * Class StoreCouponUserDao
  8. * @package app\common\dao\store\coupon
  9. * @author zfy
  10. * @day 2020-05-14
  11. */
  12. class StoreCouponUserDao extends BaseDao
  13. {
  14. /**
  15. * @return BaseModel
  16. * @author zfy
  17. * @day 2020-03-30
  18. */
  19. protected function getModel(): string
  20. {
  21. return StoreCouponUser::class;
  22. }
  23. public function search(array $where)
  24. {
  25. return StoreCouponUser::when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
  26. $query->hasWhere('user', [['nickname', 'LIKE', "%{$where['username']}%"]]);
  27. })->alias('StoreCouponUser')->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  28. $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon']}%");
  29. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  30. $query->where('StoreCouponUser.status', $where['status']);
  31. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  32. $query->where('StoreCouponUser.uid', $where['uid']);
  33. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  34. $query->where('StoreCouponUser.mer_id', $where['mer_id']);
  35. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  36. $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
  37. })->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  38. $query->whereLike('StoreCouponUser.coupon_title|StoreCouponUser.coupon_id', "%{$where['coupon']}%");
  39. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  40. $query->where('StoreCouponUser.type', $where['type']);
  41. })->when(isset($where['send_id']) && $where['send_id'] !== '', function ($query) use ($where) {
  42. $query->where('StoreCouponUser.send_id', $where['send_id'])->where('StoreCouponUser.type', 'send');
  43. })->when(isset($where['statusTag']) && $where['statusTag'] !== '', function ($query) use ($where) {
  44. if ($where['statusTag'] == 1) {
  45. $query->where('StoreCouponUser.status', 0);
  46. } else {
  47. $query->whereIn('StoreCouponUser.status', [1, 2])->where('StoreCouponUser.create_time', '>', date('Y-m-d H:i:s', strtotime('-60 day')));
  48. }
  49. })->order('StoreCouponUser.coupon_user_id DESC');
  50. }
  51. public function validIntersection($merId, $uid, array $ids): array
  52. {
  53. $time = date('Y-m-d H:i:s');
  54. return StoreCouponUser::getDB()->whereIn('coupon_user_id', $ids)->where('start_time', '<', $time)->where('end_time', '>', $time)
  55. ->where('is_fail', 0)->where('status', 0)->where('mer_id', $merId)->where('uid', $uid)->column('coupon_user_id');
  56. }
  57. public function validQuery()
  58. {
  59. $time = date('Y-m-d H:i:s');
  60. return StoreCouponUser::getDB()->where('start_time', '<', $time)->where('end_time', '>', $time)->where('is_fail', 0)->where('status', 0);
  61. }
  62. public function failCoupon()
  63. {
  64. $time = date('Y-m-d H:i:s');
  65. return StoreCouponUser::getDB()->where('end_time', '<', $time)->where('is_fail', 0)->where('status', 0)->update(['status' => 2]);
  66. }
  67. public function userTotal($uid)
  68. {
  69. return $this->validQuery()->where('uid', $uid)->count();
  70. }
  71. public function usedNum($couponId)
  72. {
  73. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->where('status', 1)->count();
  74. }
  75. public function sendNum($couponId, $sendId = null, $status = null)
  76. {
  77. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->when($sendId, function ($query, $sendId) {
  78. $query->where('type', 'send')->where('send_id', $sendId);
  79. })->when(isset($status), function ($query) use ($status) {
  80. $query->where('status', $status);
  81. })->count();
  82. }
  83. }