StoreCouponUserDao.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. })->when(isset($where['coupon_type']) && $where['coupon_type'] !== '', function ($query) use ($where) {
  37. $query->hasWhere('coupon', ['type' => $where['coupon_type']]);
  38. })->alias('StoreCouponUser')->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  39. $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon']}%");
  40. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  41. $query->where('StoreCouponUser.status', $where['status']);
  42. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  43. $query->where('StoreCouponUser.uid', $where['uid']);
  44. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  45. $query->where('StoreCouponUser.mer_id', $where['mer_id']);
  46. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  47. $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
  48. })->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  49. $query->whereLike('StoreCouponUser.coupon_title|StoreCouponUser.coupon_id', "%{$where['coupon']}%");
  50. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  51. $query->where('StoreCouponUser.type', $where['type']);
  52. })->when(isset($where['send_id']) && $where['send_id'] !== '', function ($query) use ($where) {
  53. $query->where('StoreCouponUser.send_id', $where['send_id'])->where('StoreCouponUser.type', 'send');
  54. })->when(isset($where['statusTag']) && $where['statusTag'] !== '', function ($query) use ($where) {
  55. if ($where['statusTag'] == 1) {
  56. $query->where('StoreCouponUser.status', 0);
  57. } else {
  58. $query->whereIn('StoreCouponUser.status', [1, 2])->where('StoreCouponUser.create_time', '>', date('Y-m-d H:i:s', strtotime('-60 day')));
  59. }
  60. })->order('StoreCouponUser.coupon_user_id DESC');
  61. }
  62. public function validIntersection($merId, $uid, array $ids): array
  63. {
  64. $time = date('Y-m-d H:i:s');
  65. return StoreCouponUser::getDB()->whereIn('coupon_user_id', $ids)->where('start_time', '<', $time)->where('end_time', '>', $time)
  66. ->where('is_fail', 0)->where('status', 0)->where('mer_id', $merId)->where('uid', $uid)->column('coupon_user_id');
  67. }
  68. public function validQuery($type)
  69. {
  70. $time = date('Y-m-d H:i:s');
  71. return StoreCouponUser::getDB()
  72. ->when($type, function ($query) use($time){
  73. $query->where('start_time', '<', $time);
  74. })
  75. ->where('end_time', '>', $time)->where('is_fail', 0)->where('status', 0);
  76. }
  77. public function failCoupon()
  78. {
  79. $time = date('Y-m-d H:i:s');
  80. return StoreCouponUser::getDB()->where('end_time', '<', $time)->where('is_fail', 0)->where('status', 0)->update(['status' => 2]);
  81. }
  82. public function userTotal($uid, $type = 1)
  83. {
  84. return $this->validQuery($type)->where('uid', $uid)->count();
  85. }
  86. public function usedNum($couponId)
  87. {
  88. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->where('status', 1)->count();
  89. }
  90. public function sendNum($couponId, $sendId = null, $status = null)
  91. {
  92. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->when($sendId, function ($query, $sendId) {
  93. $query->where('type', 'send')->where('send_id', $sendId);
  94. })->when(isset($status), function ($query) use ($status) {
  95. $query->where('status', $status);
  96. })->count();
  97. }
  98. public function validUserPlatformCoupon($uid)
  99. {
  100. $time = date('Y-m-d H:i:s');
  101. return StoreCouponUser::getDB()->where('uid', $uid)->where('mer_id', 0)->where('start_time', '<', $time)->where('end_time', '>', $time)
  102. ->where('is_fail', 0)->where('status', 0)
  103. ->with(['product' => function ($query) {
  104. $query->field('coupon_id,product_id');
  105. }, 'coupon' => function ($query) {
  106. $query->field('coupon_id,type,send_type');
  107. }])->order('coupon_price DESC, coupon_user_id ASC')->select();
  108. }
  109. }