75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/muyuqingfeng/iloom/auth-service/internal/model"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ProfileRepo struct {
|
||
|
|
db *sql.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewProfileRepo(db *sql.DB) *ProfileRepo {
|
||
|
|
return &ProfileRepo{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *ProfileRepo) GetByID(ctx context.Context, userID string) (*model.Profile, error) {
|
||
|
|
query := `SELECT user_id, username, real_name, phone, email, avatar, status, created_at
|
||
|
|
FROM sys_user WHERE user_id = ? AND deleted_at IS NULL`
|
||
|
|
p := &model.Profile{}
|
||
|
|
err := r.db.QueryRowContext(ctx, query, userID).Scan(
|
||
|
|
&p.ID, &p.Username, &p.RealName, &p.Phone, &p.Email,
|
||
|
|
&p.Avatar, &p.Status, &p.CreatedAt,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
if err == sql.ErrNoRows {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("get profile by id: %w", err)
|
||
|
|
}
|
||
|
|
return p, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *ProfileRepo) GetByUsername(ctx context.Context, username string) (*model.Profile, error) {
|
||
|
|
query := `SELECT user_id, username, real_name, phone, email, avatar, status, created_at
|
||
|
|
FROM sys_user WHERE username = ? AND deleted_at IS NULL`
|
||
|
|
p := &model.Profile{}
|
||
|
|
err := r.db.QueryRowContext(ctx, query, username).Scan(
|
||
|
|
&p.ID, &p.Username, &p.RealName, &p.Phone, &p.Email,
|
||
|
|
&p.Avatar, &p.Status, &p.CreatedAt,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
if err == sql.ErrNoRows {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("get profile by username: %w", err)
|
||
|
|
}
|
||
|
|
return p, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *ProfileRepo) GetWithCompany(ctx context.Context, userID string) (*model.Profile, error) {
|
||
|
|
query := `SELECT su.user_id, su.username, su.real_name, su.phone, su.email, su.avatar, su.status, su.created_at,
|
||
|
|
COALESCE(cm.company_id, ''), COALESCE(c.name, ''), COALESCE(c.type, ''), COALESCE(cm.is_master, 0)
|
||
|
|
FROM sys_user su
|
||
|
|
LEFT JOIN ilm_company_member cm ON cm.user_id = su.user_id
|
||
|
|
LEFT JOIN ilm_company c ON c.id = cm.company_id
|
||
|
|
WHERE su.user_id = ? AND su.deleted_at IS NULL
|
||
|
|
LIMIT 1`
|
||
|
|
p := &model.Profile{}
|
||
|
|
err := r.db.QueryRowContext(ctx, query, userID).Scan(
|
||
|
|
&p.ID, &p.Username, &p.RealName, &p.Phone, &p.Email,
|
||
|
|
&p.Avatar, &p.Status, &p.CreatedAt,
|
||
|
|
&p.CompanyID, &p.CompanyName, &p.Role, &p.IsMaster,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
if err == sql.ErrNoRows {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("get profile with company: %w", err)
|
||
|
|
}
|
||
|
|
return p, nil
|
||
|
|
}
|