StoreCouponUserDao.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. use think\facade\Db;
  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. /**
  34. * 根据条件搜索优惠券用户信息
  35. *
  36. * 该方法通过接收一个包含搜索条件的数组,动态构建查询语句,以搜索符合指定条件的优惠券用户信息。
  37. * 支持的搜索条件包括用户名、优惠券类型、优惠券名称或ID、状态、用户ID、商家ID、优惠券ID、发送者ID、是否为商家以及状态标签。
  38. *
  39. * @param array $where 搜索条件数组,包含各种可能的搜索参数。
  40. */
  41. public function search(array $where)
  42. {
  43. // 使用查询构建器实例化StoreCouponUser模型
  44. return StoreCouponUser::when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
  45. // 如果指定了用户名,则添加对用户昵称的模糊搜索条件
  46. $query->hasWhere('user', [['nickname', 'LIKE', "%{$where['username']}%"]]);
  47. })->when(isset($where['coupon_type']) && $where['coupon_type'] !== '', function ($query) use ($where) {
  48. // 如果指定了优惠券类型,则添加对优惠券类型的条件查询
  49. $query->hasWhere('coupon', ['type' => $where['coupon_type']]);
  50. })->alias('StoreCouponUser')->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  51. // 如果指定了优惠券名称或ID,则添加对优惠券标题的模糊搜索条件
  52. $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon']}%");
  53. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  54. // 如果指定了状态,则添加对状态的条件查询
  55. $query->where('StoreCouponUser.status', $where['status']);
  56. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  57. // 如果指定了用户ID,则添加对用户ID的条件查询
  58. $query->where('StoreCouponUser.uid', $where['uid']);
  59. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  60. // 如果指定了商家ID,则添加对商家ID的条件查询
  61. $query->where('StoreCouponUser.mer_id', $where['mer_id']);
  62. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  63. // 如果指定了优惠券ID,则添加对优惠券ID的条件查询
  64. $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
  65. })->when(isset($where['coupon']) && $where['coupon'] !== '', function ($query) use ($where) {
  66. // 如果再次指定了优惠券名称或ID,则添加对优惠券标题或ID的模糊搜索条件
  67. $query->whereLike('StoreCouponUser.coupon_title|StoreCouponUser.coupon_id', "%{$where['coupon']}%");
  68. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  69. // 如果指定了类型,则添加对类型的条件查询
  70. $query->where('StoreCouponUser.type', $where['type']);
  71. })->when(isset($where['send_id']) && $where['send_id'] !== '', function ($query) use ($where) {
  72. // 如果指定了发送者ID,并且类型为发送,则添加相应的条件查询
  73. $query->where('StoreCouponUser.send_id', $where['send_id'])->where('StoreCouponUser.type', 'send');
  74. })->when(isset($where['is_mer']) && $where['is_mer'] !== '', function ($query) use ($where) {
  75. // 如果指定了是否为商家,则添加对商家ID的非空查询
  76. $query->where('StoreCouponUser.mer_id', '<>',0);
  77. })->when(isset($where['statusTag']) && $where['statusTag'] !== '', function ($query) use ($where) {
  78. // 根据状态标签的不同,添加不同的条件查询,包括未使用的优惠券或在指定时间内使用的优惠券
  79. if ($where['statusTag'] == 1) {
  80. $query->where('StoreCouponUser.status', 0);
  81. } else {
  82. $query->whereIn('StoreCouponUser.status', [1, 2])->where('StoreCouponUser.create_time', '>', date('Y-m-d H:i:s', strtotime('-60 day')));
  83. }
  84. })->order('StoreCouponUser.coupon_user_id DESC');
  85. }
  86. public function validIntersection($merId, $uid, array $ids): array
  87. {
  88. $time = date('Y-m-d H:i:s');
  89. return StoreCouponUser::getDB()->whereIn('coupon_user_id', $ids)->where('start_time', '<', $time)->where('end_time', '>', $time)
  90. ->where('is_fail', 0)->where('status', 0)->where('mer_id', $merId)->where('uid', $uid)->column('coupon_user_id');
  91. }
  92. public function validQuery($type)
  93. {
  94. $time = date('Y-m-d H:i:s');
  95. return StoreCouponUser::getDB()
  96. ->when($type, function ($query) use($time){
  97. $query->where('start_time', '<', $time);
  98. })
  99. ->where('end_time', '>', $time)->where('is_fail', 0)->where('status', 0);
  100. }
  101. public function failCoupon($uid = 0)
  102. {
  103. $time = date('Y-m-d H:i:s');
  104. $res = StoreCouponUser::getDB()
  105. ->where('end_time', '<', $time)
  106. ->where('is_fail', 0)
  107. ->where('status', 0)
  108. ->when($uid, function($query) use($uid){
  109. $query->where('uid', $uid);
  110. })
  111. ->update(['status' => 2]);
  112. return $res;
  113. }
  114. public function userTotal($uid, $type = 1)
  115. {
  116. return $this->validQuery($type)->where('uid', $uid)->count();
  117. }
  118. public function usedNum($couponId)
  119. {
  120. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->where('status', 1)->count();
  121. }
  122. public function sendNum($couponId, $sendId = null, $status = null)
  123. {
  124. return StoreCouponUser::getDB()->where('coupon_id', $couponId)->when($sendId, function ($query, $sendId) {
  125. $query->where('type', 'send')->where('send_id', $sendId);
  126. })->when(isset($status), function ($query) use ($status) {
  127. $query->where('status', $status);
  128. })->count();
  129. }
  130. public function validUserPlatformCoupon($uid)
  131. {
  132. $time = date('Y-m-d H:i:s');
  133. return StoreCouponUser::getDB()->where('uid', $uid)->where('mer_id', 0)->where('start_time', '<', $time)->where('end_time', '>', $time)
  134. ->where('is_fail', 0)->where('status', 0)
  135. ->with(['product' => function ($query) {
  136. $query->field('coupon_id,product_id');
  137. }, 'coupon' => function ($query) {
  138. $query->field('coupon_id,type,send_type');
  139. }])->order('coupon_price DESC, coupon_user_id ASC')->select();
  140. }
  141. /**
  142. * 获取用户可用优惠券
  143. *
  144. * @param integer $uid
  145. * @param integer $merId
  146. * @return void
  147. */
  148. public function validUserCoupon(int $uid, int $merId = 0, array $ids = [])
  149. {
  150. $time = date('Y-m-d H:i:s');
  151. $query = StoreCouponUser::getDB()
  152. ->where('uid', $uid)
  153. ->where('start_time', '<', $time)
  154. ->where('end_time', '>', $time)
  155. ->where('is_fail', 0)
  156. ->where('status', 0)
  157. ->with(['product','coupon'])
  158. ->order('coupon_price DESC, coupon_user_id ASC');
  159. if ($merId) {
  160. $query->whereIn('mer_id', [0, $merId]);
  161. }
  162. if ($ids) {
  163. $query->whereIn('coupon_user_id', $ids);
  164. }
  165. return $query->select();
  166. }
  167. public function clear($id,$field)
  168. {
  169. $this->getModel()::getDB()->where($field, $id)->delete();
  170. }
  171. }