luck-draw/app/repository/AccountRepository.php

155 lines
4.0 KiB
PHP

<?php
namespace app\repository;
use app\exception\RepositoryException;
use app\service\Repository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
/**
* 账户域 相关操作(客户账号)
*
* Class AccountRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class AccountRepository extends Repository
{
/**
* 获取用户列表
*
* @throws RepositoryException
*/
public function list(): ?array
{
return $this->findList();
}
/**
* 获取指定账户记录By手机号
*
* @param string $phone
* @param array $fields
* @return Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByPhone(string $phone, array $fields = []): ?Model
{
$where[] = ['mobile', '=', $phone];
return $this->findOneByWhere($where, $fields);
}
/**
* 获取指定账户记录By用户名
*
* @param string $username
* @param array $fields
* @return Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByUsername(string $username, array $fields = []): ?Model
{
$where[] = ['username', '=', $username];
return $this->findOneByWhere($where, $fields);
}
/**
* 混合查找记录
*
* @param string $username 混合查询 手机号或用户名
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByHybrid(string $username)
{
return $this->model->whereOr('username', $username)->whereOr('mobile', $username)->find();
}
/**
* 通过微信小程序的openID查询
*
* @param string $openID
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByOpenID(string $openID)
{
return $this->model->where('openid', $openID)->find();
}
/**
* 通过个人邀请码查询用户信息
*
* @param string $inviteCode
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByInviteCode(string $inviteCode)
{
if (empty($inviteCode)) {
return null;
}
return $this->model->where('invite_code', $inviteCode)->find();
}
/**
* 通过个人编号查询用户信息
*
* @param string $coding
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByUserCoding(string $coding)
{
if (empty($coding)) {
return null;
}
return $this->model->where('coding', $coding)->find();
}
/**********************************************
* TODO 分割线,上述与本项目无关的代码需要清除
*********************************************/
/**
* 获取并处理用户列表 【后台用户列表】
*
* @param array $where
* @param array $field 必传字段coin_total
* @param int $page
* @param int $size
* @param callable|null $call
* @return array|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws RepositoryException
*/
public function getAndHandleAccountList(array $where, array $field, int $page = 1, int $size = 20, callable $call = null): ?array
{
return self::findList($where, $field, $page, $size, function ($q) use ($call) {
if ($call !== null) {
$q = $call($q);
}
return $q->order('id', 'desc');
});
}
}