123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?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\dao\user;
- use app\dao\BaseDao;
- use app\model\user\User;
- /**
- * 用户
- * Class UserDao
- * @package app\dao\user
- */
- class UserDao extends BaseDao
- {
- protected function setModel(): string
- {
- return User::class;
- }
- /**
- * @param array $where
- * @return \crmeb\basic\BaseModel|mixed|\think\Model
- */
- public function search(array $where = [])
- {
- return parent::search($where)->when(isset($where['label_id']) && $where['label_id'], function($query) use ($where) {
- $query->whereIn('uid', function ($q) use ($where) {
- if (is_array($where['label_id'])) {
- $q->name('user_label_relation')->whereIn('label_id', $where['label_id'])->field('uid')->select();
- } else {
- if (strpos($where['label_id'], ',') !== false) {
- $q->name('user_label_relation')->whereIn('label_id', explode(',', $where['label_id']))->field('uid')->select();
- } else {
- $q->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
- }
- }
- });
- });
- }
- /**
- * 是否存在
- * @param int $uid
- * @return bool
- */
- public function exist(int $uid)
- {
- return !!$this->getModel()->where('uid', $uid)->count();
- }
- /**
- * @param array $where
- * @return mixed
- */
- public function getWithTrashedCount(array $where = [], array $time = [])
- {
- return $this->getModel()->withTrashed()->where($where)->whereTime('add_time', 'between', $time)->count();
- }
- /**
- * 获取删除和没有删除的用户信息
- * @param int $uid
- * @param $field
- * @return mixed
- */
- public function getUserWithTrashedInfo(int $uid, $field = '*')
- {
- return $this->getModel()->withTrashed()->field($field)->find($uid);
- }
- /**
- * 获取用户列表
- * @param array $where
- * @param string $field
- * @param int $page
- * @param int $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0): array
- {
- return $this->search($where)->field($field)->with(['label'])->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->select()->toArray();
- }
- /**
- * 获取特定条件的总数
- * @param array $where
- * @param bool $is_list
- * @return array|int
- */
- public function getCount(array $where, bool $is_list = false)
- {
- if ($is_list)
- return $this->getModel()->where($where)->group('uid')->fetchSql(true)->column('count(*) as user_count', 'uid');
- else
- return $this->getModel()->where($where)->count();
- }
- /**
- * 用户支付成功个数增加
- * @param int $uid
- * @return mixed
- */
- public function incPayCount(int $uid)
- {
- event('user.update', [$uid]);
- return $this->getModel()->where('uid', $uid)->inc('pay_count', 1)->update();
- }
- /**
- * 某个字段累加某个数值
- * @param string $field
- * @param int $num
- */
- public function incField(int $uid, string $field, int $num = 1)
- {
- event('user.update', [$uid]);
- return $this->getModel()->where('uid', $uid)->inc($field, $num)->update();
- }
- /**
- * @param $uid
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserLabel($uid, $field = '*')
- {
- return $this->search(['uid' => $uid])->field($field)->with(['label'])->select()->toArray();
- }
- /**
- * 获取分销用户
- * @param array $where
- * @param string $field
- * @param int $page
- * @param int $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAgentUserList(array $where, string $field = '*', int $page = 0, int $limit = 0)
- {
- return $this->search($where)->field($field)->with([
- 'extract' => function ($query) {
- $query->field('sum(extract_price + extract_fee) as extract_count_price,count(id) as extract_count_num,uid')->where('status', '1')->group('uid');
- }, 'order' => function ($query) {
- $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('pid', '>=', 0)->where('paid', 1)->where('is_del', 0)->where('is_system_del', 0)->where('refund_status', 'IN', [0, 3])->group('uid');
- }, 'brokerage' => function ($query) {
- $query->field('sum(number) as brokerage_money,uid')->where('status', 1)->where('pm', 1)->group('uid');
- }, 'spreadCount' => function ($query) {
- $query->field('count(`uid`) as spread_count,spread_uid')->group('spread_uid');
- }, 'spreadUser' => function ($query) {
- $query->field('uid,phone,nickname');
- }, 'agentLevel' => function ($query) {
- $query->field('id,name');
- }
- ])->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->order('uid desc')->select()->toArray();
- }
- /**
- * 获取推广人列表
- * @param array $where
- * @param string $field
- * @param int $page
- * @param int $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getSairList(array $where, string $field = '*', int $page = 0, int $limit = 0)
- {
- return $this->search($where)->field($field)->with([
- 'order' => function ($query) {
- $query->field('sum(pay_price) as order_price,count(id) as order_count,uid')->where('paid', 1)->where('refund_status', 0)->group('uid');
- }, 'spreadCount' => function ($query) {
- $query->field('count(`uid`) as spread_count,spread_uid')->group('spread_uid');
- }, 'spreadUser' => function ($query) {
- $query->field('uid,phone,nickname');
- }
- ])->page($page, $limit)->order('uid desc')->select()->toArray();
- }
- /**
- * 获取推广人排行
- * @param array $time
- * @param string $field
- * @param int $page
- * @param int $limit
- */
- public function getAgentRankList(array $time, string $field = '*', int $page = 0, int $limit = 0)
- {
- return $this->getModel()->alias('t0')
- ->field($field)
- ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
- ->where('t1.spread_uid', '<>', 0)
- ->order('count desc')
- ->order('t0.uid desc')
- ->where('t1.spread_time', 'BETWEEN', $time)
- ->page($page, $limit)
- ->group('t0.uid')
- ->select()->toArray();
- }
- /**
- * 获取推广员ids
- * @param array $where
- * @return array
- */
- public function getAgentUserIds(array $where)
- {
- return $this->search($where)->column('uid');
- }
- /**
- * 某个条件 用户某个字段总和
- * @param array $where
- * @param string $filed
- * @return float
- */
- public function getWhereSumField(array $where, string $filed)
- {
- return $this->search($where)->sum($filed);
- }
- /**
- * 根据条件查询对应的用户信息以数组形式返回
- * @param array $where
- * @param string $field
- * @param string $key
- * @return array
- */
- public function getUserInfoArray(array $where, string $field, string $key)
- {
- return $this->search($where)->column($field, $key);
- }
- /**
- * 获取特定时间用户访问量
- * @param $time
- * @param $week
- * @return int
- */
- public function todayLastVisit($time, $week)
- {
- switch ($week) {
- case 1:
- return $this->search(['time' => $time ?: 'today', 'timeKey' => 'last_time'])->count();
- case 2:
- return $this->search(['time' => $time ?: 'week', 'timeKey' => 'last_time'])->count();
- }
- }
- /**
- * 获取特定时间用户访问量
- * @param $time
- * @return int
- */
- public function totalUserCount($time)
- {
- return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time'])->count();
- }
- /**
- * 获取特定时间内用户列表
- * @param $starday
- * @param $yesterday
- * @return mixed
- */
- public function userList($starday, $yesterday)
- {
- return $this->getModel()->where('add_time', 'between time', [$starday, $yesterday])
- ->field("FROM_UNIXTIME(add_time,'%Y-%m-%d') as day,count(*) as count")
- ->group("FROM_UNIXTIME(add_time, '%Y%m%d')")
- ->order('add_time asc')
- ->select()->toArray();
- }
- /**
- * 购买量范围的用户数量
- * @param $status
- * @return int
- */
- public function userCount($status)
- {
- switch ($status) {
- case 1:
- return $this->getModel()->where('pay_count', '>', 1)->where('pay_count', '<=', 4)->count();
- case 2:
- return $this->getModel()->where('pay_count', '>', 4)->count();
- }
- }
- /**
- * 获取用户统计数据
- * @param $time
- * @param $type
- * @param $timeType
- * @return mixed
- */
- public function getTrendData($time, $type, $timeType)
- {
- return $this->getModel()->when($type != '', function ($query) use ($type) {
- $query->where('user_type', $type);
- })->where(function ($query) use ($time) {
- if ($time[0] == $time[1]) {
- $query->whereDay('add_time', $time[0]);
- } else {
- $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
- $query->whereTime('add_time', 'between', $time);
- }
- })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as num")->group('days')->select()->toArray();
- }
- /**
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserInfoList(array $where, $field = "*"): array
- {
- return $this->search($where)->field($field)->select()->toArray();
- }
- /**
- * 获取用户会员数量
- * @param $where (time type)
- * @return int
- */
- public function getMemberCount($where, int $overdue_time = 0)
- {
- if (!$overdue_time) $overdue_time = time();
- return $this->search($where)->where('is_ever_level', 1)->whereOr(function ($qeury) use ($overdue_time) {
- $qeury->where('is_money_level', '>', 0)->where('overdue_time', '>', $overdue_time);
- })->count();
- }
- public function getOutOne(int $uid, $field = "*")
- {
- return $this->getModel()->where('uid|phone', $uid)->field($field)->find();
- }
- }
|