StoreCouponUserDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\coupon;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\coupon\StoreCouponUser;
  15. /**
  16. * Class StoreCouponUserDao
  17. * @package app\common\dao\store\coupon
  18. * @author xaboy
  19. * @day 2020-05-14
  20. */
  21. class StoreCouponUserDao extends BaseDao
  22. {
  23. /**
  24. * @return BaseModel
  25. * @author xaboy
  26. * @day 2020-03-30
  27. */
  28. protected function getModel(): string
  29. {
  30. return StoreCouponUser::class;
  31. }
  32. public function search(array $where)
  33. {
  34. return StoreCouponUser::when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
  35. $query->hasWhere('user', [['nickname', 'LIKE', "%{$where['username']}%"]]);
  36. })->alias('StoreCouponUser')->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  37. $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon']}%");
  38. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  39. $query->where('StoreCouponUser.status', $where['status']);
  40. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  41. $query->where('StoreCouponUser.uid', $where['uid']);
  42. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  43. $query->where('StoreCouponUser.mer_id', $where['mer_id']);
  44. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  45. $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
  46. })->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  47. $query->whereLike('StoreCouponUser.coupon_title|StoreCouponUser.coupon_id', "%{$where['coupon']}%");
  48. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  49. $query->where('StoreCouponUser.type', $where['type']);
  50. })->when(isset($where['send_id']) && $where['send_id'] !== '', function ($query) use ($where) {
  51. $query->where('StoreCouponUser.send_id', $where['send_id'])->where('StoreCouponUser.type', 'send');
  52. })->when(isset($where['statusTag']) && $where['statusTag'] !== '', function ($query) use ($where) {
  53. if ($where['statusTag'] == 1) {
  54. $query->where('StoreCouponUser.status', 0);
  55. } else {
  56. $query->whereIn('StoreCouponUser.status', [1, 2])->where('StoreCouponUser.create_time', '>', date('Y-m-d H:i:s', strtotime('-60 day')));
  57. }
  58. })->order('StoreCouponUser.coupon_user_id DESC');
  59. }
  60. public function validIntersection($merId, $uid, array $ids): array
  61. {
  62. $time = date('Y-m-d H:i:s');
  63. return StoreCouponUser::getDB()->whereIn('coupon_user_id', $ids)->where('start_time', '<', $time)->where('end_time', '>', $time)
  64. ->where('is_fail', 0)->where('status', 0)->where('mer_id', $merId)->where('uid', $uid)->column('coupon_user_id');
  65. }
  66. public function validQuery()
  67. {
  68. $time = date('Y-m-d H:i:s');
  69. return StoreCouponUser::getDB()->where('start_time', '<', $time)->where('end_time', '>', $time)->where('is_fail', 0)->where('status', 0);
  70. }
  71. public function failCoupon()
  72. {
  73. $time = date('Y-m-d H:i:s');
  74. return StoreCouponUser::getDB()->where('end_time', '<', $time)->where('is_fail', 0)->where('status', 0)->update(['status' => 2]);
  75. }
  76. public function userTotal($uid)
  77. {
  78. return $this->validQuery()->where('uid', $uid)->count();
  79. }
  80. public function usedNum($couponId)
  81. {
  82. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->where('status', 1)->count();
  83. }
  84. public function sendNum($couponId, $sendId = null, $status = null)
  85. {
  86. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->when($sendId, function ($query, $sendId) {
  87. $query->where('type', 'send')->where('send_id', $sendId);
  88. })->when(isset($status), function ($query) use ($status) {
  89. $query->where('status', $status);
  90. })->count();
  91. }
  92. }