UserMerchantDao.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\UserLabel;
  15. use app\common\model\user\UserMerchant;
  16. use think\db\BaseQuery;
  17. /**
  18. * Class UserMerchantDao
  19. * @package app\common\dao\user
  20. * @author xaboy
  21. * @day 2020/10/20
  22. */
  23. class UserMerchantDao extends BaseDao
  24. {
  25. /**
  26. * @return string
  27. * @author xaboy
  28. * @day 2020/10/20
  29. */
  30. protected function getModel(): string
  31. {
  32. return UserMerchant::class;
  33. }
  34. /**
  35. * 检查用户是否为特定商户的用户
  36. *
  37. * 本函数通过比较用户ID和商户ID来确定用户是否属于特定的商户。
  38. * 这对于在多商户平台上验证用户权限或进行特定的业务逻辑非常有用。
  39. *
  40. * @param int $uid 用户ID
  41. * @param int $mer_id 商户ID
  42. * @return bool 如果用户是该商户的用户返回true,否则返回false
  43. */
  44. public function isMerUser($uid, $mer_id)
  45. {
  46. // 使用compact函数创建包含uid和mer_id的数组,并通过existsWhere方法检查是否存在匹配的用户
  47. return $this->existsWhere(compact('uid', 'mer_id'));
  48. }
  49. /**
  50. * 更新用户在商户最后一次活动的时间
  51. *
  52. * 本函数用于记录用户在特定商户的最后活动时间,通过更新数据库中的相应记录来实现。
  53. * 参数$uid代表用户的唯一标识,$mer_id代表商户的唯一标识。函数通过查询匹配$uid和$mer_id的记录,
  54. * 并将最后活动时间更新为当前时间(格式为Y-m-d H:i:s)。
  55. *
  56. * @param int $uid 用户ID,用于定位特定用户的记录
  57. * @param int $mer_id 商户ID,用于定位特定商户的记录
  58. * @return int 返回更新操作的影响行数,用于确认更新是否成功
  59. */
  60. public function updateLastTime($uid, $mer_id)
  61. {
  62. // 构造查询条件,并更新'last_time'字段为当前时间
  63. return UserMerchant::getDB()->where(compact('uid', 'mer_id'))->update([
  64. 'last_time' => date('Y-m-d H:i:s')
  65. ]);
  66. }
  67. /**
  68. * @param array $where
  69. * @return mixed
  70. * @author xaboy
  71. * @day 2020/10/20
  72. */
  73. public function search(array $where)
  74. {
  75. return UserMerchant::getDB()->alias('A')->leftJoin('User B', 'A.uid = B.uid')
  76. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  77. $query->where('A.mer_id', $where['mer_id']);
  78. })->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
  79. return $query->where('B.nickname|B.uid', 'like', '%' . $where['keyword'] . '%');
  80. })->when(isset($where['nickname']) && $where['nickname'], function (BaseQuery $query) use ($where) {
  81. return $query->where('B.nickname', 'like', '%' . $where['nickname'] . '%');
  82. })->when(isset($where['phone']) && $where['phone'], function (BaseQuery $query) use ($where) {
  83. return $query->where('B.phone', 'like', '%' . $where['phone'] . '%');
  84. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  85. return $query->where('B.sex', intval($where['sex']));
  86. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  87. return $query->where('B.is_promoter', $where['is_promoter']);
  88. })->when(isset($where['uid']) && $where['uid'] !== '', function (BaseQuery $query) use ($where) {
  89. return $query->where('A.uid', $where['uid']);
  90. })->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
  91. return $query->whereIn('A.uid', $where['uids']);
  92. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  93. if ($where['user_time_type'] == 'visit') {
  94. getModelTime($query, $where['user_time'], 'A.last_time');
  95. }
  96. if ($where['user_time_type'] == 'add_time') {
  97. getModelTime($query, $where['user_time'], 'A.create_time');
  98. }
  99. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  100. if ($where['pay_count'] == -1) {
  101. $query->where('A.pay_num', 0);
  102. } else {
  103. $query->where('A.pay_num', '>', $where['pay_count']);
  104. }
  105. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  106. return $query->whereRaw('CONCAT(\',\',A.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  107. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  108. return $query->where('B.user_type', $where['user_type']);
  109. })->where('A.status', 1);
  110. }
  111. /**
  112. * 获取指定商家ID下的用户ID列表,这些用户的支付数目在指定的范围内。
  113. *
  114. * 本函数通过查询用户支付表,筛选出特定商家ID且支付数目在最小值和最大值(可选)之间的用户ID。
  115. * 使用了Laravel的查询构建器来构造数据库查询,并通过分组和列操作来优化结果集的返回形式。
  116. *
  117. * @param string $mer_id 商家ID,用于查询指定商家的用户。
  118. * @param int $min 最小支付数目,用于筛选支付数目不小于该值的用户。
  119. * @param int|null $max 最大支付数目,可选参数,用于筛选支付数目不大于该值的用户。
  120. * @return array 返回符合条件的用户ID列表。
  121. */
  122. public function numUserIds($mer_id, $min, $max = null)
  123. {
  124. // 构建查询条件,首先指定商家ID和支付数目最小值
  125. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_num', '>=', $min)->when(!is_null($max), function ($query) use ($max) {
  126. // 如果指定了最大支付数目,则添加该条件进行筛选
  127. $query->where('pay_num', '<=', $max);
  128. })->group('uid')->column('uid');
  129. }
  130. /**
  131. * 获取指定商家ID和价格范围内的用户ID列表
  132. *
  133. * 此方法用于查询与特定商家相关联的,并且交易价格在指定范围内的用户的唯一ID列表。
  134. * 查询条件包括商家ID和支付价格,支付价格支持设定最小值和最大值。
  135. * 如果只指定了最小值而没有最大值,则查询大于等于最小值的所有记录。
  136. * 如果同时指定了最小值和最大值,则查询大于等于最小值且小于等于最大值的所有记录。
  137. *
  138. * @param int $mer_id 商家ID,用于限定查询的商家范围
  139. * @param int $min 最小支付价格,用于限定查询的最低价格范围
  140. * @param int|null $max 最大支付价格,可选参数,用于限定查询的最高价格范围
  141. * @return array 返回符合条件的用户ID列表
  142. */
  143. public function priceUserIds($mer_id, $min, $max = null)
  144. {
  145. // 使用UserMerchant类的数据库连接,并构造查询条件
  146. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_price', '>=', $min)->when(!is_null($max), function ($query) use ($max, $min) {
  147. // 如果指定了最大值,则进一步限定支付价格小于等于最大值
  148. $query->where('pay_price', $min == $max ? '<=' : '<', $max);
  149. })->group('uid')->column('uid');
  150. }
  151. }