123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\model\user\User;
- use app\model\wechat\WechatUser;
- use qiniu\basic\BaseModel;
- use qiniu\basic\BaseServices;
- use think\db\exception\DbException;
- /**
- * Class UserWechatuserServices
- * @package app\services\user
- * @mixin User
- */
- class UserWechatuserServices extends BaseServices
- {
- /**
- * @var string
- */
- protected $alias = '';
- /**
- * @var string
- */
- protected $join_alis = '';
- /**
- * 精确搜索白名单
- * @var string[]
- */
- protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
- protected $join_model;
- /**
- * UserWechatuserServices constructor.
- * @param User $model
- * @param WechatUser $join_model
- */
- public function __construct(User $model, WechatUser $join_model)
- {
- $this->model = $model;
- $this->join_model = $join_model;
- }
- /**
- * 关联模型
- * @param string $alias
- * @param string $join_alias
- * @param string $join
- * @return BaseModel
- */
- public function setModel(string $alias = 'u', string $join_alias = 'w', string $join = 'left')
- {
- $this->alias = $alias;
- $this->join_alis = $join_alias;
- $wechatUser = $this->join_model;
- $table = $wechatUser->getName();
- return $this->model->withTrashed()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
- }
- /**
- * 自定义简单查询总数
- * @param array $where
- * @return int
- * @throws DbException
- */
- public function getCount(array $where): int
- {
- return $this->setModel()->where($where)->count();
- }
- /**
- * 复杂条件搜索列表
- * @param array $where
- * @param string $field
- * @return array
- */
- public function getWhereUserList(array $where, string $field): array
- {
- [$page, $limit] = $this->getPageValue();
- $order_string = '';
- $order_arr = ['asc', 'desc'];
- if (isset($where['now_money']) && in_array($where['now_money'], $order_arr)) {
- $order_string = 'now_money ' . $where['now_money'];
- }
- $list = $this->searchWhere($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->group($this->alias . '.uid')->order(($order_string ? $order_string . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
- $count = $this->searchWhere($where)->group($this->alias . '.uid')->count();
- return [$list, $count];
- }
- /**
- * @param $where
- * @param array|null $field
- * @return BaseModel
- */
- public function searchWhere($where, ?array $field = [])
- {
- $model = $this->setModel();
- $userAlias = $this->alias . '.';
- $wechatUserAlias = $this->join_alis . '.';
- if (isset($where['is_filter_del']) && $where['is_filter_del'] == 1) {
- $model = $model->where($userAlias . 'delete_time', null);
- }
- // 用户访问时间
- 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['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']);
- }
- //用户昵称,uid,手机号搜索
- $fieldKey = $where['field_key'] ?? '';
- $nickname = $where['nickname'] ?? '';
- if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
- switch ($fieldKey) {
- case "nickname":
- 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 || $fieldKey == 'all') && $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['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
- $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
- $query->where($userAlias . 'sex', $where['sex']);
- });
- }
- //所在国家 所在省份 所在城市
- if ((isset($where['country']) && $where['country']) || (isset($where['province']) && $where['province']) || (isset($where['city']) && $where['city'])) {
- $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
- $query->when(isset($where['country']) && $where['country'], function ($g) use ($wechatUserAlias, $userAlias, $where) {
- if ($where['country'] == 'domestic') {
- $g->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
- } else if ($where['country'] == 'abroad') {
- $g->whereOr($wechatUserAlias . 'country', 'not in', ['中国', 'China']);
- }
- })->when(isset($where['province']) && $where['province'], function ($q) use ($wechatUserAlias, $userAlias, $where) {
- $q->whereOr($wechatUserAlias . 'province', $where['province'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['province'] . '%');
- })->when(isset($where['city']) && $where['city'], function ($c) use ($wechatUserAlias, $userAlias, $where) {
- $c->whereOr($wechatUserAlias . 'city', $where['city'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['city'] . '%');
- });
- });
- }
- if (isset($where['time'])) {
- $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
- }
- return $field ? $model->field($field) : $model;
- }
- }
|