From e600a4888af83cf457df1f4767dc71af054ea3d9 Mon Sep 17 00:00:00 2001 From: Pasha Date: Tue, 28 Oct 2025 16:35:42 +0300 Subject: [PATCH] added method GettingYclientsUsersTrueResults --- dal/db_query/queries.sql | 18 ++++++++ dal/schema/000028_init.down.sql | 1 + dal/schema/000028_init.up.sql | 9 ++++ dal/sqlcgen/models.go | 9 ++++ dal/sqlcgen/queries.sql.go | 74 +++++++++++++++++++++++++++++++++ model/yclients.go | 17 ++++++++ repository/yclients/yclients.go | 52 +++++++++++++++++++++++ 7 files changed, 180 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 915356d..6a75c15 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1657,3 +1657,21 @@ VALUES ($1, $2, $3, $4, $5, $6, $7); -- name: UpdateYclientsFieldRules :exec UPDATE YclientsRules SET FieldsRule = $1 WHERE SalonID = $2 AND QuizID = $3 AND Deleted = false; + +-- name: GettingYclientsUsersTrueResults :many +SELECT + a.quiz_id, a.id AS answer_id, a.result, + a.question_id, a.content, a.session, + COALESCE((SELECT a2.utm FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1), '{}'::jsonb) AS utm, + t.accesstoken, r.salonid, r.fieldsrule, r.staffid, r.services, + r.customcolor, u.accountid AS quiz_accountid +FROM answer a + INNER JOIN quiz q ON a.quiz_id = q.id + LEFT JOIN yclientscrmstatuses s ON a.id = s.AnswerID + INNER JOIN yclientsrules r ON q.id = r.QuizID + INNER JOIN yclientstokens t ON q.accountid = t.AccountID AND r.salonid = t.salonid + INNER JOIN yclientsaccounts u ON q.accountid = u.accountid AND r.salonid = u.salonid +WHERE a.result = true AND s.id IS NULL AND a.deleted = false + AND r.deleted = false AND q.deleted = false + AND u.deleted = false AND t.active = true + AND t.expiration = false; diff --git a/dal/schema/000028_init.down.sql b/dal/schema/000028_init.down.sql index d69896b..12232c6 100644 --- a/dal/schema/000028_init.down.sql +++ b/dal/schema/000028_init.down.sql @@ -6,6 +6,7 @@ DROP TABLE If EXIST YclientsServices; DROP TABLE If EXIST YclientsTimeSlots; DROP TABLE If EXIST YclientsRules; DROP TABLE If EXIST YclientsFields; +DROP TABLE If EXIST YclientsCRMStatuses; DROP INDEX if exist idx_unique_tokens_yclients; DROP INDEX if exist idx_unique_yclients_rules; \ No newline at end of file diff --git a/dal/schema/000028_init.up.sql b/dal/schema/000028_init.up.sql index ada6806..769bcdc 100644 --- a/dal/schema/000028_init.up.sql +++ b/dal/schema/000028_init.up.sql @@ -145,4 +145,13 @@ CREATE TABLE IF NOT EXISTS YclientsFields ( CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE IF NOT EXISTS YclientsCRMStatuses ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AnswerID BIGINT NOT NULL, + SalonID INT NOT NULL, + RecordID INT, -- ID созданной записи в YClients + Status TEXT NOT NULL DEFAULT '', -- запись о ошибке, либо успехе + CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + CREATE UNIQUE INDEX idx_unique_yclients_rules ON YclientsRules (SalonID, QuizID) WHERE Deleted = false; \ No newline at end of file diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 242846e..0569e3b 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -426,6 +426,15 @@ type Yclientsaccountuser struct { Createdat time.Time `db:"createdat" json:"createdat"` } +type Yclientscrmstatus struct { + ID int64 `db:"id" json:"id"` + Answerid int64 `db:"answerid" json:"answerid"` + Salonid int32 `db:"salonid" json:"salonid"` + Recordid sql.NullInt32 `db:"recordid" json:"recordid"` + Status string `db:"status" json:"status"` + Createdat time.Time `db:"createdat" json:"createdat"` +} + type Yclientsfield struct { ID int64 `db:"id" json:"id"` Yclientsid int32 `db:"yclientsid" json:"yclientsid"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index cdfebd1..4f91533 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -5147,6 +5147,80 @@ func (q *Queries) GettingBitrixUsersTrueResults(ctx context.Context) ([]GettingB return items, nil } +const gettingYclientsUsersTrueResults = `-- name: GettingYclientsUsersTrueResults :many +SELECT + a.quiz_id, a.id AS answer_id, a.result, + a.question_id, a.content, a.session, + COALESCE((SELECT a2.utm FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1), '{}'::jsonb) AS utm, + t.accesstoken, r.salonid, r.fieldsrule, r.staffid, r.services, + r.customcolor, u.accountid AS quiz_accountid +FROM answer a + INNER JOIN quiz q ON a.quiz_id = q.id + LEFT JOIN yclientscrmstatuses s ON a.id = s.AnswerID + INNER JOIN yclientsrules r ON q.id = r.QuizID + INNER JOIN yclientstokens t ON q.accountid = t.AccountID AND r.salonid = t.salonid + INNER JOIN yclientsaccounts u ON q.accountid = u.accountid AND r.salonid = u.salonid +WHERE a.result = true AND s.id IS NULL AND a.deleted = false + AND r.deleted = false AND q.deleted = false + AND u.deleted = false AND t.active = true + AND t.expiration = false +` + +type GettingYclientsUsersTrueResultsRow struct { + QuizID int64 `db:"quiz_id" json:"quiz_id"` + AnswerID int64 `db:"answer_id" json:"answer_id"` + Result sql.NullBool `db:"result" json:"result"` + QuestionID int64 `db:"question_id" json:"question_id"` + Content sql.NullString `db:"content" json:"content"` + Session sql.NullString `db:"session" json:"session"` + Utm interface{} `db:"utm" json:"utm"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Salonid int32 `db:"salonid" json:"salonid"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Staffid int32 `db:"staffid" json:"staffid"` + Services json.RawMessage `db:"services" json:"services"` + Customcolor string `db:"customcolor" json:"customcolor"` + QuizAccountid string `db:"quiz_accountid" json:"quiz_accountid"` +} + +func (q *Queries) GettingYclientsUsersTrueResults(ctx context.Context) ([]GettingYclientsUsersTrueResultsRow, error) { + rows, err := q.db.QueryContext(ctx, gettingYclientsUsersTrueResults) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GettingYclientsUsersTrueResultsRow + for rows.Next() { + var i GettingYclientsUsersTrueResultsRow + if err := rows.Scan( + &i.QuizID, + &i.AnswerID, + &i.Result, + &i.QuestionID, + &i.Content, + &i.Session, + &i.Utm, + &i.Accesstoken, + &i.Salonid, + &i.Fieldsrule, + &i.Staffid, + &i.Services, + &i.Customcolor, + &i.QuizAccountid, + ); 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 incrementQuizPrivilegeUsage = `-- name: IncrementQuizPrivilegeUsage :exec UPDATE quiz_privilege_usage SET used_count = used_count + 1, updated_at = CURRENT_TIMESTAMP WHERE quiz_id = $1 AND privilege_id = $2 diff --git a/model/yclients.go b/model/yclients.go index d997a55..7619199 100644 --- a/model/yclients.go +++ b/model/yclients.go @@ -176,3 +176,20 @@ type UserListYclientsFieldsResp struct { Count int64 `json:"count"` Items []YclientsField `json:"items"` } + +type YclientsUsersTrueResults struct { + QuizID int64 + AnswerID int64 + Result bool + QuestionID int64 + Content string + Session string + AccessToken string + SalonID int32 + UTMs UTMSavingMap + FieldsRule YclientsFieldRule + StaffID int32 + Services ServiceYclientsRule + CustomColor string + QuizAccountID string +} diff --git a/repository/yclients/yclients.go b/repository/yclients/yclients.go index f9cc21a..44e7fc1 100644 --- a/repository/yclients/yclients.go +++ b/repository/yclients/yclients.go @@ -810,3 +810,55 @@ func (r *YclientsRepository) DeleteFields(ctx context.Context, ids []int64) erro } return nil } + +// true results fetch +// todo нужно хорошенько проверить +func (r *YclientsRepository) GettingYclientsUsersTrueResults(ctx context.Context) ([]model.YclientsUsersTrueResults, error) { + rows, err := r.queries.GettingYclientsUsersTrueResults(ctx) + if err != nil { + return nil, err + } + + var results []model.YclientsUsersTrueResults + + for _, row := range rows { + var fieldsRule model.YclientsFieldRule + err = json.Unmarshal(row.Fieldsrule, &fieldsRule) + if err != nil { + return nil, err + } + + var utms model.UTMSavingMap + err = json.Unmarshal(row.Utm.([]byte), &utms) + if err != nil { + return nil, err + } + + var services model.ServiceYclientsRule + err = json.Unmarshal(row.Services, &services) + if err != nil { + return nil, err + } + + result := model.YclientsUsersTrueResults{ + QuizID: row.QuizID, + AnswerID: row.AnswerID, + Result: row.Result.Bool, + QuestionID: row.QuestionID, + Content: row.Content.String, + Session: row.Session.String, + AccessToken: row.Accesstoken, + SalonID: row.Salonid, + UTMs: utms, + FieldsRule: fieldsRule, + StaffID: row.Staffid, + Services: services, + CustomColor: row.Customcolor, + QuizAccountID: row.QuizAccountid, + } + + results = append(results, result) + } + + return results, nil +}