UserMerchantDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\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. * @param $uid
  36. * @param $mer_id
  37. * @return bool
  38. * @author xaboy
  39. * @day 2020/10/20
  40. */
  41. public function isMerUser($uid, $mer_id)
  42. {
  43. return $this->existsWhere(compact('uid', 'mer_id'));
  44. }
  45. /**
  46. * @param $uid
  47. * @param $mer_id
  48. * @return int
  49. * @throws \think\db\exception\DbException
  50. * @author xaboy
  51. * @day 2020/10/20
  52. */
  53. public function updateLastTime($uid, $mer_id)
  54. {
  55. return UserMerchant::getDB()->where(compact('uid', 'mer_id'))->update([
  56. 'last_time' => date('Y-m-d H:i:s')
  57. ]);
  58. }
  59. /**
  60. * @param array $where
  61. * @return mixed
  62. * @author xaboy
  63. * @day 2020/10/20
  64. */
  65. public function search(array $where)
  66. {
  67. return UserMerchant::getDB()->alias('A')->leftJoin('User B', 'A.uid = B.uid')
  68. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  69. $query->where('A.mer_id', $where['mer_id']);
  70. })->when(isset($where['nickname']) && $where['nickname'], function (BaseQuery $query) use ($where) {
  71. return $query->where('B.nickname', 'like', '%' . $where['nickname'] . '%');
  72. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  73. return $query->where('B.sex', intval($where['sex']));
  74. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  75. return $query->where('B.is_promoter', $where['is_promoter']);
  76. })->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
  77. return $query->whereIn('A.uid', $where['uids']);
  78. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  79. if ($where['user_time_type'] == 'visit') {
  80. getModelTime($query, $where['user_time'], 'A.last_time');
  81. }
  82. if ($where['user_time_type'] == 'add_time') {
  83. getModelTime($query, $where['user_time'], 'A.create_time');
  84. }
  85. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  86. if ($where['pay_count'] == -1) {
  87. $query->where('A.pay_num', 0);
  88. } else {
  89. $query->where('A.pay_num', '>', $where['pay_count']);
  90. }
  91. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  92. return $query->whereRaw('CONCAT(\',\',A.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  93. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  94. return $query->where('B.user_type', $where['user_type']);
  95. })->where('A.status', 1);
  96. }
  97. public function numUserIds($mer_id, $min, $max = null)
  98. {
  99. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_num', '>=', $min)->when(!is_null($max), function ($query) use ($max) {
  100. $query->where('pay_num', '<=', $max);
  101. })->column('uid');
  102. }
  103. public function priceUserIds($mer_id, $min, $max = null)
  104. {
  105. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_price', '>=', $min)->when(!is_null($max), function ($query) use ($max, $min) {
  106. $query->where('pay_price', $min == $max ? '<=' : '<', $max);
  107. })->column('uid');
  108. }
  109. }