| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-04-28
- *
- *
- */
- namespace app\common\dao\user;
- use app\common\dao\BaseDao;
- use app\common\enum\user\UserEnum;
- use app\common\model\BaseModel;
- use app\common\model\user\User;
- use app\common\repositories\user\UserRepository;
- use app\entity\CommonEntity;
- use app\entity\data\user\UserEntity;
- use think\db\BaseQuery;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Db;
- use think\Model;
- /**
- * Class UserDao
- * @package app\common\dao\user
- * @author xaboy
- * @day 2020-04-28
- */
- class UserDao extends BaseDao
- {
- /**
- * @return BaseModel
- * @author xaboy
- * @day 2020-03-30
- */
- protected function getModel(): string
- {
- return User::class;
- }
- public function getUserGroupMap():array
- {
- return [
- 1 => '消费者',
- 2 => 'v客',
- 3 => '大v经理',
- 4 => '创客',
- 5 => '区县代理',
- 6 => '省级代理',
- 7 => '创客合伙人',
- ];
- }
- /**
- * @return string
- * @author xaboy
- * @day 2020/6/22
- */
- public function defaultPwd()
- {
- return '123456';
- }
- /**
- * @param array $where
- * @return BaseQuery
- * @author xaboy
- * @day 2020-05-07
- */
- public function search(array $where)
- {
- $get_is_show=(app()->make(UserRepository::class))->get_level_list();
- // ->when($where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
- // return $query->where('User.identity', intval($where['identity']));
- // })
- if (isset($where['province']) && $where['province'] !== '') {
- $query = User::hasWhere('wechat', function ($query) use ($where) {
- $query->where('province', $where['province']);
- if ($where['city'] !== '') $query->where('city', $where['city']);
- });
- } else {
- $query = User::getDB()->alias('User');
- }
- $query->hasWhere('userRich',[],'*','left');
- // $query->hasWhere('areaManage',[],'*','left');
- $query->when(isset($where['keyword']) && $where['keyword'], function (BaseQuery $query) use ($where) {
- return $query->where('User.real_name|User.nickname|User.phone', 'like', '%' . $where['keyword'] . '%');
- })->when($where['identity']!=='' && $where['identity'] >= 0 , function (BaseQuery $query) use ($where) {
- return $query->where('User.identity', intval($where['identity']));
- })->when($get_is_show, function ($query) {
- return $query->where('User.is_show',1);
- })->when(isset($where['user_type']) && $where['user_type'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.user_type', $where['user_type']);
- })->when(isset($where['status']) && $where['status'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.status', intval($where['status']));
- })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
- return $query->where('User.group_id', intval($where['group_id']));
- })->when(isset($where['group_id']) && $where['group_id'], function (BaseQuery $query) use ($where) {
- return $query->where('User.group_id', intval($where['group_id']));
- })->when(isset($where['user_group']) && $where['user_group'], function (BaseQuery $query) use ($where) {
- return $query->where('User.user_group', intval($where['user_group']));
- })->when(isset($where['label_id']) && $where['label_id'] !== '', function (BaseQuery $query) use ($where) {
- return $query->whereRaw('CONCAT(\',\',User.label_id,\',\') LIKE \'%,' . $where['label_id'] . ',%\'');
- })->when(isset($where['sex']) && $where['sex'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.sex', intval($where['sex']));
- })->when(isset($where['is_promoter']) && $where['is_promoter'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.is_promoter', $where['is_promoter']);
- })->when(isset($where['nickname']) && $where['nickname'] !== '', function (BaseQuery $query) use ($where) {
- return $query->whereLike('User.nickname', "%{$where['nickname']}%");
- })->when(isset($where['account']) && $where['account'] !== '', function (BaseQuery $query) use ($where) {
- return $query->whereLike('User.account', "%{$where['account']}%");
- })->when(isset($where['spread_time']) && $where['spread_time'] !== '', function (BaseQuery $query) use ($where) {
- getModelTime($query, $where['spread_time'], 'User.spread_time');
- })->when(isset($where['date']) && $where['date'] !== '', function (BaseQuery $query) use ($where) {
- getModelTime($query, $where['date'], 'User.create_time');
- })->when(isset($where['spread_uid']) && $where['spread_uid'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.spread_uid', intval($where['spread_uid']));
- })->when(isset($where['spread_uids']), function (BaseQuery $query) use ($where) {
- return $query->whereIn('User.spread_uid', $where['spread_uids']);
- })->when(isset($where['is_show']) && $where['is_show'] !== '', function (BaseQuery $query) use ($where) {
- return $query->where('User.is_show', $where['is_show']);
- })->when(isset($where['pay_count']) && $where['pay_count'] !== '', function ($query) use ($where) {
- if($where['pay_count'] == -1 ){
- $query->where('User.pay_count', 0);
- }else{
- $query->where('User.pay_count', '>', $where['pay_count']);
- }
- })->when(isset($where['user_time_type']) && $where['user_time_type'] !== '' && $where['user_time'] != '', function ($query) use ($where) {
- list($startTime, $endTime) = explode('-', $where['user_time']);
- if ($where['user_time_type'] == 'visitno') {
- $query->whereTime("User.last_time", 'between', [$startTime, $endTime]);
- }
- if ($where['user_time_type'] == 'visit') {
- $query->whereTime('User.last_time', '>=', $startTime);
- $query->whereTime('User.last_time', '<', $endTime);
- }
- if ($where['user_time_type'] == 'add_time') {
- $query->whereTime('User.create_time', '>=', $startTime);
- $query->whereTime('User.create_time', '<', $endTime);
- }
- })->when(isset($where['sort']) && in_array($where['sort'], ['pay_count ASC', 'pay_count DESC', 'pay_price DESC', 'pay_price ASC', 'spread_count ASC', 'spread_count DESC']), function (BaseQuery $query) use ($where) {
- $query->order('User.' . $where['sort']);
- })->when(isset($where['now_money']) , function (BaseQuery $query) use ($where) {
- if($where['now_money'] == 1 ){
- $query->order('User.now_money', "asc");
- }elseif($where['now_money'] == 2 ){
- $query->order('User.now_money', "desc");
- }
- }, function ($query) {
- $query->order('User.uid DESC');
- })->when((isset($where['user_rich_type']) && $where['user_rich_type']!=''), function ($query) use ($where) {
- if( $where['user_rich_type']=='0'){
- $query->where('userRich.id is null or userRich.type=0');
- }else {
- $query->where('userRich.type', $where['user_rich_type']);
- }
- })->when( (isset($where['stockholder']) && $where['stockholder']!='') , function ($query) use ($where) {
- $query->where('userRich.stockholder' , $where['stockholder']);
- })->when( (isset($where['subIdentify']) && $where['subIdentify']!='') , function ($query) use ($where) {
- $query->where('areaManage.type' , $where['subIdentify']);
- })->when(isset($where['uid']) , function ($query) use ($where) {
- $query->whereIn('User.uid' , $where['uid']);
- });
- $company = input('company','');
- $identify = input('identify','');
- $query->when( $company==1 , function ($query) {
- $query->join('enterprise_user eu','eu.uid=user.uid','left')->where('eu.mer_id',496);
- })->when( $company==1 && $identify , function ($query) use ($identify) {
- $query->where('eu.identity',$identify);
- })->when( $company==2 , function ($query) {
- //国盛小程序还未开发
- $query->where('user.uid',0);
- });
- return $query;
- }
- /**
- * @param $keyword
- * @return BaseQuery
- * @author xaboy
- * @day 2020/6/22
- */
- public function searchMerUser($keyword)
- {
- return User::getDB()->whereLike('nickname', "%$keyword%")->where('status', 1);
- }
- /**
- * @param array $ids
- * @param int $group_id
- * @return int
- * @throws DbException
- * @author xaboy
- * @day 2020-05-07
- */
- public function batchChangeGroupId(array $ids, int $group_id)
- {
- return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('group_id'));
- }
- /**
- * @param array $ids
- * @param array $label_id
- * @return int
- * @throws DbException
- * @author xaboy
- * @day 2020-05-07
- */
- public function batchChangeLabelId(array $ids, array $label_id)
- {
- $label_id = implode(',', $label_id);
- return User::getDB()->whereIn($this->getPk(), $ids)->update(compact('label_id'));
- }
- /**
- * @param int $id
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020-04-28
- */
- public function wechatUserIdBytUser(int $id)
- {
- return User::getDB()->where('wechat_user_id', $id)->find();
- }
- /**
- * @param $id
- * @return mixed
- * @author xaboy
- * @day 2020-04-28
- */
- public function wechatUserIdByUid($id)
- {
- return User::getDB()->where('wechat_user_id', $id)->value('uid');
- }
- /**
- * @param $id
- * @return mixed
- * @author xaboy
- * @day 2020/7/7
- */
- public function uidByWechatUserId($id)
- {
- return User::getDB()->where('uid', $id)->value('wechat_user_id');
- }
- /**
- * @param $account
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020/6/22
- */
- public function accountByUser($account)
- {
- return User::getDB()->where('account', $account)->where('is_del', 0)->find();
- }
- public function nicknameByUser($nickname)
- {
- return User::getDB()->where('nickname', $nickname)->where('is_del', 0)->find();
- }
- /**
- * @param $uid
- * @return array
- * @author xaboy
- * @day 2020/6/22
- */
- public function getSubIds($uid)
- {
- return User::getDB()->where('spread_uid', $uid)->column('uid');
- }
- /**
- * @param $uid
- * @return int
- * @author xaboy
- * @day 2020/6/22
- */
- public function getOneLevelCount($uid)
- {
- return User::getDB()->where('spread_uid', $uid)->count();
- }
- /**
- * @param $uid
- * @return int
- * @author xaboy
- * @day 2020/6/22
- */
- public function getTwoLevelCount($uid)
- {
- $ids = $this->getSubIds($uid);
- return count($ids) ? User::getDB()->whereIn('spread_uid', $ids)->count() : 0;
- }
- /**
- * @param $ids
- * @return array
- * @author xaboy
- * @day 2020/6/22
- */
- public function getSubAllIds(array $ids)
- {
- return User::getDB()->whereIn('spread_uid', $ids)->column('uid');
- }
- /**
- * @param array $ids
- * @param string $field
- * @return \think\Collection
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020/6/22
- */
- public function users(array $ids, $field = '*')
- {
- return User::getDB()->whereIn('uid', $ids)->field($field)->select();
- }
- public function newUserNum($date, $where = [])
- {
- return User::getDB()->where($where)->when($date, function ($query, $date) {
- getModelTime($query, $date, 'create_time');
- })->count();
- }
- public function userOrderDetail($uid)
- {
- return User::getDB()->alias('A')
- ->join('StoreOrder B', 'A.uid = B.uid and B.paid = 1 and B.pay_time between \'' . date('Y/m/d', strtotime('first day of')) . ' 00:00:00\' and \'' . date('Y/m/d H:i:s') . '\'')
- ->field('A.*,A.brokerage_price as commission, A.brokerage_price as brokerage_price, A.score as green_integral, A.score as score, A.create_time as create_time,A.uid,A.avatar,A.nickname,A.phone,A.now_money,A.pay_price,A.pay_count, sum(B.pay_price) as total_pay_price, count(B.order_id) as total_pay_count')
- ->where('A.uid', $uid)
- ->find();
- }
- public function userNumGroup($date)
- {
- return User::getDB()->when($date, function ($query, $date) {
- getModelTime($query, $date, 'create_time');
- })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(uid) as new'))
- ->group('time')->order('time ASC')->select();
- }
- public function userNumGroupHour($date)
- {
- return User::getDB()->when($date, function ($query, $date) {
- getModelTime($query, $date, 'create_time');
- })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%H\') as time, count(uid) as new'))
- ->group('time')->order('time ASC')->select();
- }
- public function userNumGroupYear($date)
- {
- return User::getDB()->when($date, function ($query, $date) {
- getModelTime($query, $date, 'create_time');
- })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m\') as time, count(uid) as new'))
- ->group('time')->order('time ASC')->select();
- }
- public function beforeUserNum($date)
- {
- return User::getDB()->where('create_time', '<', $date)->count();
- }
- public function selfUserList($phone)
- {
- return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
- }
- /**
- * 获取测试用户 uid List
- * @return array
- */
- public function getTestUidList(): array
- {
- return $this::getModel()::getDB()->where('test_user', '=', 1)->column('uid');
- }
- /**
- * 获取删除用户 uid List
- * @return array
- */
- public function getDelUidList(): array
- {
- return $this::getModel()::getDB()->where('is_del', '=', 1)->column('uid');
- }
- /**
- * 获取删除用户 uid List
- * @return array
- */
- public function getInvalidUidList(): array
- {
- return $this::getModel()::getDB()->where('test_user', '=', 1)->whereOr('is_del', '=', 1)->column('uid');
- }
- /**
- * @param array $uidList
- * @return UserEntity[]
- */
- public function getUserEntityListByUidList(array $uidList): array
- {
- $entityList = [];
- try {
- $dataList = User::getDB()
- ->whereIn('uid', $uidList)
- ->where('is_del', '=', UserEnum::IS_DEL['No']['code'])
- ->select()
- ->toArray();
- if (!empty($dataList)) {
- $entityList = array_map(function ($data) {
- return UserEntity::newInstance($data);
- }, $dataList);
- }
- } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
- }
- return $entityList;
- }
- /**
- * 批量更新
- * @param $dataList
- * @return array
- */
- public function saveAll($dataList): array
- {
- try {
- return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
- } catch (\Exception $e) {
- return [];
- }
- }
- /**
- * @param $userId
- * @return array
- * 获取直推用户ID
- */
- public function getDirectUserIds($userId)
- {
- return $this::getModel()::getDB()->where('level_id', '=', $userId)->column('uid');
- }
- /**
- * 通过uid获取spreadUid
- */
- public function getSpreadUidFromUid($uid)
- {
- try {
- return $this::getModel()::getDB()->where('uid', '=', $uid)->value('spread_uid');
- } catch (\Exception $e) {
- return [];
- }
- }
- /**
- * 根据spread查找用户信息
- */
- public function getSpreadUserFromUid($spreadUid)
- {
- try {
- return $this::getModel()::getDB()->where('uid', '=', $spreadUid)->find();
- } catch (\Exception $e) {
- return [];
- }
- }
- /**
- * @param $uid
- * @param $number
- * @return mixed
- * @author 史晨
- * @date 2026/2/27 15:15
- * 更新用户佣金
- */
- public function updateBrokeragePriceByUid($uid,$number)
- {
- return $this->getModel()::getDB()->where('uid', '=', $uid)->inc('brokerage_price',$number)->update();
- }
- }
|