StoreCouponUserDao.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\enum\store\StoreCouponUserEnum;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\coupon\StoreCouponUser;
  15. use app\entity\data\store\StoreCouponUserEntity;
  16. /**
  17. * Class StoreCouponUserDao
  18. * @package app\common\dao\store\coupon
  19. * @author xaboy
  20. * @day 2020-05-14
  21. */
  22. class StoreCouponUserDao extends BaseDao
  23. {
  24. /**
  25. * @return BaseModel
  26. * @author xaboy
  27. * @day 2020-03-30
  28. */
  29. protected function getModel(): string
  30. {
  31. return StoreCouponUser::class;
  32. }
  33. public function search(array $where)
  34. {
  35. return StoreCouponUser::when(isset($where['username']) && $where['username'] != '', function ($query) use ($where) {
  36. $query->hasWhere('user', [['nickname', 'LIKE', "%{$where['username']}%"]]);
  37. })->when(true, function ($query) use ($where) {
  38. $query->hasWhere('user', [['uid', '>', 0]]);
  39. })->alias('StoreCouponUser')->when(isset($where['coupon_title']) && $where['coupon_title'] !== '', function ($query) use ($where) {
  40. $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon_title']}%");
  41. })->when(isset($where['status']) && $where['status'] != '', function ($query) use ($where) {
  42. $query->where('StoreCouponUser.status', $where['status']);
  43. })->when(isset($where['uid']) && $where['uid'] != '', function ($query) use ($where) {
  44. $query->where('StoreCouponUser.uid', $where['uid']);
  45. })->when(isset($where['mer_id']) && $where['mer_id'] != '', function ($query) use ($where) {
  46. $query->where('StoreCouponUser.mer_id', $where['mer_id']);
  47. })->when(isset($where['coupon_id']) && $where['coupon_id'] != '', function ($query) use ($where) {
  48. $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
  49. })->order('StoreCouponUser.create_time 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)
  76. {
  77. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->count();
  78. }
  79. /**
  80. * @param int $merId
  81. * @param int $uid
  82. * @return StoreCouponUserEntity[]
  83. */
  84. public function getValidStoreCouponUserEntityListByMerIdAndUid(int $merId, int $uid): array
  85. {
  86. $entityList = [];
  87. try {
  88. $dateTime = date('Y-m-d H:i:s');
  89. /** @var StoreCouponUser $model */
  90. $model = $this->getModel();
  91. $dataListRes = $model::getDB()
  92. ->where('mer_id', '=', $merId)
  93. ->where('uid', '=', $uid)
  94. ->where('status', '=', StoreCouponUserEnum::STATUS['Unused']['code'])
  95. ->where('is_fail', '=', StoreCouponUserEnum::IS_FAIL['No']['code'])
  96. ->where('start_time', '<=', $dateTime)
  97. ->where('end_time', '>=', $dateTime)
  98. ->select()
  99. ->toArray();
  100. if (!empty($dataListRes)) {
  101. foreach ($dataListRes as $data) {
  102. $entityList[] = StoreCouponUserEntity::newInstance($data);
  103. }
  104. }
  105. } catch (\Exception $e) {
  106. }
  107. return $entityList;
  108. }
  109. }