StoreCouponUserDao.php 3.1 KB

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