| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace app\common\repositories\user;
- use app\common\dao\user\UserDao;
- use app\common\dao\user\ZeroUserLineDao;
- use app\common\repositories\BaseRepository;
- use think\db\exception\DbException;
- use think\facade\Db;
- class ZeroUserRepository extends BaseRepository
- {
- private $systemUserId = 1;
- /**
- * @var ZeroUserLineDao
- */
- protected $dao;
- /**
- * UserGroupRepository constructor.
- * @param ZeroUserLineDao $dao
- */
- public function __construct(ZeroUserLineDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 插入或更新零号线用户数据
- *
- * @param int|null $id 用户ID,为null时表示新增
- * @param array $data 用户数据
- * @return \app\common\dao\BaseDao|bool|\think\Model
- * @throws DbException
- */
- public function insertOrUpdate(?int $id, array $data)
- {
- // 团队昵称为空时,取用户昵称
- $userDao = new UserDao();
- if (empty($data['team_name'])) {
- $userData = $userDao->get($data['team_uid']);
- $data['team_name'] = $userData->nickname;
- }
- // 判断是更新现有记录还是创建新记录
- if ($data['id'] > 0) {
- return $this->dao->update($id, $data);
- }
- try {
- Db::startTrans();
- // 0号线用户修改节点
- $userDao->update($data['team_uid'], ['level_id' => $this->systemUserId]);
- // 如果不带走团队,所有直推用户节点id修改成上级id
- if ($data['is_remove'] != 1) {
- $directUserIds = $userDao->getDirectUserIds($data['team_uid']);
- $upLevelId = $userData->level_id;
- $userDao->updates($directUserIds, ['level_id' => $upLevelId]);
- }
- $this->dao->create($data);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- return false;
- }
- }
- /**
- * @param $param
- * @return array
- * 获取列表
- */
- public function getList($param)
- {
- $where = $this->getWhere($param);
- $list = $this->dao->getList($where,$param->page,$param->limit);
- return $list->toArray();
- }
- /**
- * @param $param
- * @return array
- * 拼接搜索条件
- */
- private function getWhere($param): array
- {
- $where = [];
-
- // 状态条件 - 精确搜索
- if (!empty($param->status)) {
- $where[] = ['status', '=', $param->status];
- }
-
- // 团队昵称条件 - 模糊搜索
- if (!empty($param->teamName)) {
- $where[] = ['team_name', 'like', '%' . $param->teamName . '%'];
- }
-
- // 团队长uid条件 - 精确搜索
- if (!empty($param->account)) {
- $userDao = new UserDao();
- $userWhere[] = ['account','like','%' . $param->account . '%'];
- $userData = $userDao->getWhere($userWhere,'uid');
- $where[] = ['team_uid', '=', $userData->uid ?? 0];
- }
- return $where;
- }
- }
|