ZeroUserRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserDao;
  4. use app\common\dao\user\ZeroUserLineDao;
  5. use app\common\repositories\BaseRepository;
  6. use think\db\exception\DbException;
  7. use think\facade\Db;
  8. class ZeroUserRepository extends BaseRepository
  9. {
  10. private $systemUserId = 1;
  11. /**
  12. * @var ZeroUserLineDao
  13. */
  14. protected $dao;
  15. /**
  16. * UserGroupRepository constructor.
  17. * @param ZeroUserLineDao $dao
  18. */
  19. public function __construct(ZeroUserLineDao $dao)
  20. {
  21. $this->dao = $dao;
  22. }
  23. /**
  24. * 插入或更新零号线用户数据
  25. *
  26. * @param int|null $id 用户ID,为null时表示新增
  27. * @param array $data 用户数据
  28. * @return \app\common\dao\BaseDao|bool|\think\Model
  29. * @throws DbException
  30. */
  31. public function insertOrUpdate(?int $id, array $data)
  32. {
  33. // 团队昵称为空时,取用户昵称
  34. $userDao = new UserDao();
  35. if (empty($data['team_name'])) {
  36. $userData = $userDao->get($data['team_uid']);
  37. $data['team_name'] = $userData->nickname;
  38. }
  39. // 判断是更新现有记录还是创建新记录
  40. if ($data['id'] > 0) {
  41. return $this->dao->update($id, $data);
  42. }
  43. try {
  44. Db::startTrans();
  45. // 0号线用户修改节点
  46. $userDao->update($data['team_uid'], ['level_id' => $this->systemUserId]);
  47. // 如果不带走团队,所有直推用户节点id修改成上级id
  48. if ($data['is_remove'] != 1) {
  49. $directUserIds = $userDao->getDirectUserIds($data['team_uid']);
  50. $upLevelId = $userData->level_id;
  51. $userDao->updates($directUserIds, ['level_id' => $upLevelId]);
  52. }
  53. $this->dao->create($data);
  54. Db::commit();
  55. return true;
  56. } catch (\Exception $e) {
  57. Db::rollback();
  58. return false;
  59. }
  60. }
  61. /**
  62. * @param $param
  63. * @return array
  64. * 获取列表
  65. */
  66. public function getList($param)
  67. {
  68. $where = $this->getWhere($param);
  69. $list = $this->dao->getList($where,$param->page,$param->limit);
  70. return $list->toArray();
  71. }
  72. /**
  73. * @param $param
  74. * @return array
  75. * 拼接搜索条件
  76. */
  77. private function getWhere($param): array
  78. {
  79. $where = [];
  80. // 状态条件 - 精确搜索
  81. if (!empty($param->status)) {
  82. $where[] = ['status', '=', $param->status];
  83. }
  84. // 团队昵称条件 - 模糊搜索
  85. if (!empty($param->teamName)) {
  86. $where[] = ['team_name', 'like', '%' . $param->teamName . '%'];
  87. }
  88. // 团队长uid条件 - 精确搜索
  89. if (!empty($param->account)) {
  90. $userDao = new UserDao();
  91. $userWhere[] = ['account','like','%' . $param->account . '%'];
  92. $userData = $userDao->getWhere($userWhere,'uid');
  93. $where[] = ['team_uid', '=', $userData->uid ?? 0];
  94. }
  95. return $where;
  96. }
  97. }