123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- declare (strict_types=1);
- namespace app\dao\store;
- use think\model;
- use app\dao\BaseDao;
- use app\model\user\User;
- use app\model\store\StoreUser;
- class UserStoreUserDao extends BaseDao
- {
-
- protected $alias = '';
-
- protected $join_alis = '';
-
- protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
-
- protected function setModel(): string
- {
- return User::class;
- }
- public function joinModel(): string
- {
- return StoreUser::class;
- }
-
- public function getModel(string $alias = 'u', string $join_alias = 's', $join = '')
- {
- $this->alias = $alias;
- $this->join_alis = $join_alias;
-
- $storeUser = app()->make($this->joinModel());
- $table = $storeUser->getName();
- return parent::getModel()->withTrashed()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
- }
-
- public function getList(array $where, $field = '*', int $page = 0, int $limit = 0)
- {
- return $this->getModel()->where($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->select()->toArray();
- }
-
- public function getCount(array $where): int
- {
- return $this->getModel()->where($where)->count();
- }
-
- public function getCountByWhere(array $where): int
- {
- return $this->searchWhere($where)->group($this->alias . '.uid')->count();
- }
-
- public function getListByModel(array $where, string $field = '', string $order = '', int $page = 0, int $limit = 0): array
- {
- return $this->searchWhere($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->group($this->alias . '.uid')->order(($order ? $order . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
- }
-
- public function searchWhere($where, ?array $field = [])
- {
- $model = $this->getModel();
- $userAlias = $this->alias . '.';
- $storeUserAlias = $this->join_alis . '.';
- if (isset($where['is_filter_del']) && $where['is_filter_del'] == 1) {
- $model = $model->where($userAlias . 'delete_time', null);
- }
-
- if (isset($where['store_id']) && $where['store_id'] !== '') {
- $model = $model->where($storeUserAlias . 'store_id', $where['store_id']);
- }
-
- if (isset($where['user_time_type']) && isset($where['user_time'])) {
-
- if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
- [$startTime, $endTime] = explode('-', $where['user_time']);
- if ($startTime && $endTime) {
- $endTime = strtotime($endTime) + 24 * 3600;
- $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
- }
- }
-
- if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
- [$startTime, $endTime] = explode('-', $where['user_time']);
- if ($startTime && $endTime) {
- $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
- $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
- }
- }
-
- if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
- [$startTime, $endTime] = explode('-', $where['user_time']);
- if ($startTime && $endTime) {
- $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
- $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
- }
- }
- }
-
- if (isset($where['pay_count']) && $where['pay_count'] != '') {
- if ($where['pay_count'] == '-1') {
- $model = $model->where($userAlias . 'pay_count', 0);
- } else {
- $model = $model->where($userAlias . 'pay_count', '>', $where['pay_count']);
- }
- }
-
- if (isset($where['level']) && $where['level']) {
- $model = $model->where($userAlias . 'level', $where['level']);
- }
-
- if (isset($where['group_id']) && $where['group_id']) {
- $model = $model->where($userAlias . 'group_id', $where['group_id']);
- }
-
- if (isset($where['status']) && $where['status'] != '') {
- $model = $model->where($userAlias . 'status', $where['status']);
- }
-
- if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
- $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
- }
-
- if (isset($where['label_id']) && $where['label_id']) {
- $model = $model->whereIn($userAlias . 'uid', function ($query) use ($where) {
- if (is_array($where['label_id'])) {
- $query->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
- } else {
- $query->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
- }
- });
- }
-
- if (isset($where['isMember']) && $where['isMember'] != '') {
- if ($where['isMember'] == 0) {
- $model = $model->where($userAlias . 'is_money_level', 0);
- } else {
- $model = $model->where($userAlias . 'is_money_level', '>', 0);
- }
- }
-
- $fieldKey = $where['field_key'] ?? '';
- $nickname = $where['nickname'] ?? '';
- if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
- switch ($fieldKey) {
- case "nickname":
- $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
- break;
- case "phone":
- $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname));
- break;
- case "uid":
- $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
- break;
- }
- } else if (!$fieldKey && $nickname) {
- $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
- }
-
- if (isset($where['user_type']) && $where['user_type']) {
- $model = $model->where($userAlias . 'user_type', $where['user_type']);
- }
- if (isset($where['time'])) {
- $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
- }
- return $field ? $model->field($field) : $model;
- }
- }
|