diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1aab951..08b61d2 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1506,4 +1506,14 @@ SELECT u.*, COUNT(*) OVER() as total_count FROM YclientsAccountUsers u JOIN user_data a ON u.YclientsID = a.YclientsID WHERE u.Deleted = false +ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3; + +-- name: GetCompanyYclientsWithPagination :many +WITH user_data AS ( + SELECT YclientsID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false +) +SELECT u.*, COUNT(*) OVER() as total_count +FROM YclientsCompany u + JOIN user_data a ON u.YclientsID = a.YclientsID +WHERE u.Deleted = false ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3; \ No newline at end of file diff --git a/dal/schema/000028_init.down.sql b/dal/schema/000028_init.down.sql index d3794d0..d8a0e8e 100644 --- a/dal/schema/000028_init.down.sql +++ b/dal/schema/000028_init.down.sql @@ -1,2 +1,3 @@ -DROP TABLE If EXIST accountsYclients; -DROP TABLE If EXIST usersYclients; \ No newline at end of file +DROP TABLE If EXIST YclientsAccounts; +DROP TABLE If EXIST YclientsAccountUsers; +DROP TABLE If EXIST YclientsCompany; \ No newline at end of file diff --git a/dal/schema/000028_init.up.sql b/dal/schema/000028_init.up.sql index 16f5aab..3d2740c 100644 --- a/dal/schema/000028_init.up.sql +++ b/dal/schema/000028_init.up.sql @@ -17,4 +17,18 @@ CREATE TABLE IF NOT EXISTS YclientsAccountUsers ( Role INT NOT NULL DEFAULT 0, -- position Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS YclientsCompany ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + YclientsID INT NOT NULL, -- ID "компании" + YclientsCompanyID INT NOT NULL, + Title text NOT NULL, + ShortDecription text NOT NULL, + Active INT NOT NULL, + Country text NOT NULL, + GroupPriority INT NOT NULL, + Deleted BOOLEAN NOT NULL DEFAULT FALSE, + CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 056db3c..a758d58 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -419,3 +419,16 @@ type Yclientsaccountuser struct { Deleted bool `db:"deleted" json:"deleted"` Createdat time.Time `db:"createdat" json:"createdat"` } + +type Yclientscompany struct { + ID int64 `db:"id" json:"id"` + Yclientsid int32 `db:"yclientsid" json:"yclientsid"` + Yclientscompanyid int32 `db:"yclientscompanyid" json:"yclientscompanyid"` + Title string `db:"title" json:"title"` + Shortdecription string `db:"shortdecription" json:"shortdecription"` + Active int32 `db:"active" json:"active"` + Country string `db:"country" json:"country"` + Grouppriority int32 `db:"grouppriority" json:"grouppriority"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` +} diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index d9f0cfc..60d1945 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2383,6 +2383,72 @@ func (q *Queries) GetBitrixTokenById(ctx context.Context, accountid string) (Bit return i, err } +const getCompanyYclientsWithPagination = `-- name: GetCompanyYclientsWithPagination :many +WITH user_data AS ( + SELECT YclientsID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false +) +SELECT u.id, u.yclientsid, u.yclientscompanyid, u.title, u.shortdecription, u.active, u.country, u.grouppriority, u.deleted, u.createdat, COUNT(*) OVER() as total_count +FROM YclientsCompany u + JOIN user_data a ON u.YclientsID = a.YclientsID +WHERE u.Deleted = false +ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3 +` + +type GetCompanyYclientsWithPaginationParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetCompanyYclientsWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Yclientsid int32 `db:"yclientsid" json:"yclientsid"` + Yclientscompanyid int32 `db:"yclientscompanyid" json:"yclientscompanyid"` + Title string `db:"title" json:"title"` + Shortdecription string `db:"shortdecription" json:"shortdecription"` + Active int32 `db:"active" json:"active"` + Country string `db:"country" json:"country"` + Grouppriority int32 `db:"grouppriority" json:"grouppriority"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetCompanyYclientsWithPagination(ctx context.Context, arg GetCompanyYclientsWithPaginationParams) ([]GetCompanyYclientsWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getCompanyYclientsWithPagination, arg.Accountid, arg.Column2, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetCompanyYclientsWithPaginationRow + for rows.Next() { + var i GetCompanyYclientsWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Yclientsid, + &i.Yclientscompanyid, + &i.Title, + &i.Shortdecription, + &i.Active, + &i.Country, + &i.Grouppriority, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getCurrentBitrixCompany = `-- name: GetCurrentBitrixCompany :one SELECT id, accountid, bitrixid, deleted, createdat, subdomain FROM BitrixAccounts WHERE AccountID = $1 AND Deleted = false ` diff --git a/model/yclients.go b/model/yclients.go index 8757acd..c9cc3ef 100644 --- a/model/yclients.go +++ b/model/yclients.go @@ -28,3 +28,21 @@ type UserListYclientsResp struct { Count int64 `json:"count"` Items []YclientsAccountUser `json:"items"` } + +type YclientsCompany struct { + ID int64 `json:"id"` + YclientsID int32 `json:"yclientsID"` + YclientsCompanyID int32 `json:"yclientsCompanyID"` + Title string `json:"title"` + ShortDecription string `json:"shortDecription"` + Active int32 `json:"active"` + Country string `json:"country"` + GroupPriority int32 `json:"groupPriority"` + Deleted bool `json:"deleted"` + CreatedAt time.Time `json:"createdAt"` +} + +type CompanyListYclientsResp struct { + Count int64 `json:"count"` + Items []YclientsCompany `json:"items"` +} diff --git a/repository/yclients/yclients.go b/repository/yclients/yclients.go index 1a271c1..f914aa8 100644 --- a/repository/yclients/yclients.go +++ b/repository/yclients/yclients.go @@ -94,3 +94,45 @@ func (r *YclientsRepository) GettingUserWithPagination(ctx context.Context, req return &resp, nil } + +// company + +func (r *YclientsRepository) GettingCompanyWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.CompanyListYclientsResp, error) { + rows, err := r.queries.GetCompanyYclientsWithPagination(ctx, sqlcgen.GetCompanyYclientsWithPaginationParams{ + Accountid: accountID, + Column2: req.Page, + Limit: req.Size, + }) + if err != nil { + return nil, err + } + var users []model.YclientsCompany + var count int64 + for _, row := range rows { + users = append(users, model.YclientsCompany{ + ID: row.ID, + YclientsID: row.Yclientsid, + YclientsCompanyID: row.Yclientscompanyid, + Title: row.Title, + ShortDecription: row.Shortdecription, + Active: row.Active, + Country: row.Country, + GroupPriority: row.Grouppriority, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + }) + count = row.TotalCount + } + + resp := model.CompanyListYclientsResp{ + Count: count, + Items: users, + } + + return &resp, nil +} + +// todo +func (r *YclientsRepository) UpdateCompany() { + +}