UserMerchantDao.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\dao\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\user\UserLabel;
  6. use app\common\model\user\UserMerchant;
  7. use think\db\BaseQuery;
  8. /**
  9. * Class UserMerchantDao
  10. * @package app\common\dao\user
  11. * @author zfy
  12. * @day 2020/10/20
  13. */
  14. class UserMerchantDao extends BaseDao
  15. {
  16. /**
  17. * @return string
  18. * @author zfy
  19. * @day 2020/10/20
  20. */
  21. protected function getModel(): string
  22. {
  23. return UserMerchant::class;
  24. }
  25. /**
  26. * @param $uid
  27. * @param $mer_id
  28. * @return bool
  29. * @author zfy
  30. * @day 2020/10/20
  31. */
  32. public function isMerUser($uid, $mer_id)
  33. {
  34. return $this->existsWhere(compact('uid', 'mer_id'));
  35. }
  36. /**
  37. * @param $uid
  38. * @param $mer_id
  39. * @return int
  40. * @throws \think\db\exception\DbException
  41. * @author zfy
  42. * @day 2020/10/20
  43. */
  44. public function updateLastTime($uid, $mer_id)
  45. {
  46. return UserMerchant::getDB()->where(compact('uid', 'mer_id'))->update([
  47. 'last_time' => date('Y-m-d H:i:s')
  48. ]);
  49. }
  50. /**
  51. * @param array $where
  52. * @return mixed
  53. * @author zfy
  54. * @day 2020/10/20
  55. */
  56. public function search(array $where)
  57. {
  58. return UserMerchant::getDB()->alias('A')->leftJoin('User B', 'A.uid = B.uid')
  59. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  60. $query->where('A.mer_id', $where['mer_id']);
  61. })->when(isset($where['nickname']) && $where['nickname'], function (BaseQuery $query) use ($where) {
  62. return $query->where('B.nickname', 'like', '%' . $where['nickname'] . '%');
  63. })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
  64. return $query->where('B.sex', intval($where['sex']));
  65. })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
  66. return $query->where('B.is_promoter', $where['is_promoter']);
  67. })->when(isset($where['uids']), function (BaseQuery $query) use ($where) {
  68. return $query->whereIn('A.uid', $where['uids']);
  69. })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
  70. if ($where['user_time_type'] == 'visit') {
  71. getModelTime($query, $where['user_time'], 'A.last_time');
  72. }
  73. if ($where['user_time_type'] == 'add_time') {
  74. getModelTime($query, $where['user_time'], 'A.create_time');
  75. }
  76. })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
  77. if ($where['pay_count'] == -1) {
  78. $query->where('A.pay_num', 0);
  79. } else {
  80. $query->where('A.pay_num', '>', $where['pay_count']);
  81. }
  82. })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
  83. return $query->whereRaw('CONCAT(\',\',A.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
  84. })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
  85. return $query->where('B.user_type', $where['user_type']);
  86. })->where('A.status', 1);
  87. }
  88. public function numUserIds($mer_id, $min, $max = null)
  89. {
  90. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_num', '>=', $min)->when(!is_null($max), function ($query) use ($max) {
  91. $query->where('pay_num', '<=', $max);
  92. })->column('uid');
  93. }
  94. public function priceUserIds($mer_id, $min, $max = null)
  95. {
  96. return UserMerchant::getDB()->where('mer_id', $mer_id)->where('pay_price', '>=', $min)->when(!is_null($max), function ($query) use ($max, $min) {
  97. $query->where('pay_price', $min == $max ? '<=' : '<', $max);
  98. })->column('uid');
  99. }
  100. }