go-crm initial

This commit is contained in:
2026-04-21 17:34:58 -03:00
parent 5b8107c169
commit f8135d0e1b
24 changed files with 641 additions and 125 deletions

View File

@@ -2,7 +2,6 @@ package db
import (
"database/sql"
"fmt"
)
type Client struct {
@@ -36,15 +35,14 @@ type Service struct {
}
type Schedule struct {
ScheduleID int64 `json:"schedule_id"`
CustomerID int64 `json:"customer_id"`
ClientID int64 `json:"client_id"`
PlanDate string `json:"plan_date,omitempty"`
ConfirmedDate string `json:"confirmed_date,omitempty"`
Time string `json:"time,omitempty"`
Status string `json:"status"`
Notes string `json:"notes,omitempty"`
CreatedAt int64 `json:"created_at"`
ScheduleID int64 `json:"schedule_id"`
ClientID int64 `json:"client_id"`
CustomerID int64 `json:"customer_id"`
ServiceID int64 `json:"service_id"`
PlanDate string `json:"plan_date,omitempty"`
Time string `json:"time,omitempty"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
type Payment struct {
@@ -59,26 +57,6 @@ type Payment struct {
CreatedAt int64 `json:"created_at"`
}
type Question struct {
QuestionID int64 `json:"question_id"`
ClientID int64 `json:"client_id"`
CustomerID int64 `json:"customer_id"`
Question string `json:"question"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
type Answer struct {
AnswerID int64 `json:"answer_id"`
ClientID int64 `json:"client_id"`
QuestionID int64 `json:"question_id"`
Answer string `json:"answer"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
func (c *Client) Create(db *sql.DB) error {
result, err := db.Exec(
"INSERT INTO clients (name, phone, email, address, notes, created_at) VALUES (?, ?, ?, ?, ?, ?)",
@@ -236,8 +214,8 @@ func ListServices(db *sql.DB, clientID int64, limit, offset int) ([]Service, err
func (sch *Schedule) Create(db *sql.DB) error {
result, err := db.Exec(
"INSERT INTO scheduling (customer_id, client_id, plan_date, confirmed_date, time, status, notes, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
sch.CustomerID, sch.ClientID, sch.PlanDate, sch.ConfirmedDate, sch.Time, sch.Status, sch.Notes, sch.CreatedAt,
"INSERT INTO scheduling (client_id, customer_id, service_id, plan_date, time, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
sch.ClientID, sch.CustomerID, sch.ServiceID, sch.PlanDate, sch.Time, sch.Status, sch.CreatedAt,
)
if err != nil {
return err
@@ -251,7 +229,7 @@ func (sch *Schedule) Create(db *sql.DB) error {
}
func ListSchedules(db *sql.DB, clientID, customerID int64, limit, offset int) ([]Schedule, error) {
query := "SELECT schedule_id, customer_id, client_id, plan_date, confirmed_date, time, status, notes, created_at FROM scheduling WHERE 1=1"
query := "SELECT schedule_id, client_id, customer_id, service_id, plan_date, time, status, created_at FROM scheduling WHERE 1=1"
var args []interface{}
if clientID > 0 {
query += " AND client_id = ?"
@@ -273,7 +251,7 @@ func ListSchedules(db *sql.DB, clientID, customerID int64, limit, offset int) ([
var schedules []Schedule
for rows.Next() {
var sch Schedule
if err := rows.Scan(&sch.ScheduleID, &sch.CustomerID, &sch.ClientID, &sch.PlanDate, &sch.ConfirmedDate, &sch.Time, &sch.Status, &sch.Notes, &sch.CreatedAt); err != nil {
if err := rows.Scan(&sch.ScheduleID, &sch.ClientID, &sch.CustomerID, &sch.ServiceID, &sch.PlanDate, &sch.Time, &sch.Status, &sch.CreatedAt); err != nil {
return nil, err
}
schedules = append(schedules, sch)
@@ -326,6 +304,23 @@ func ListPayments(db *sql.DB, clientID int64, limit, offset int) ([]Payment, err
return payments, rows.Err()
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
type Question struct {
QuestionID int64 `json:"question_id"`
ClientID int64 `json:"client_id"`
CustomerID int64 `json:"customer_id"`
Question string `json:"question"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
func (q *Question) Create(db *sql.DB) error {
result, err := db.Exec(
"INSERT INTO questions (client_id, customer_id, question, timestamp, status, created_at) VALUES (?, ?, ?, ?, ?, ?)",
@@ -343,10 +338,10 @@ func (q *Question) Create(db *sql.DB) error {
}
func ListQuestions(db *sql.DB, clientID int64, limit, offset int) ([]Question, error) {
query := "SELECT question_id, client_id, customer_id, question, timestamp, status, created_at FROM questions"
query := "SELECT question_id, client_id, customer_id, question, timestamp, status, created_at FROM questions WHERE 1=1"
var args []interface{}
if clientID > 0 {
query += " WHERE client_id = ?"
query += " AND client_id = ?"
args = append(args, clientID)
}
query += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
@@ -369,9 +364,19 @@ func ListQuestions(db *sql.DB, clientID int64, limit, offset int) ([]Question, e
return questions, rows.Err()
}
type Answer struct {
AnswerID int64 `json:"answer_id"`
ClientID int64 `json:"client_id"`
QuestionID int64 `json:"question_id"`
Answer string `json:"answer"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
func (a *Answer) Create(db *sql.DB) error {
result, err := db.Exec(
"INSERT INTO answers (client_id, question_id, answer, timestamp, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
"INSERT INTO answers (client_id, question_id, answer, timestamp, status, created_at) VALUES (?, ?, ?, ?, ?, ?)",
a.ClientID, a.QuestionID, a.Answer, a.Timestamp, a.Status, a.CreatedAt,
)
if err != nil {
@@ -386,10 +391,10 @@ func (a *Answer) Create(db *sql.DB) error {
}
func ListAnswers(db *sql.DB, questionID int64, limit, offset int) ([]Answer, error) {
query := "SELECT answer_id, client_id, question_id, answer, timestamp, status, created_at FROM answers"
query := "SELECT answer_id, client_id, question_id, answer, timestamp, status, created_at FROM answers WHERE 1=1"
var args []interface{}
if questionID > 0 {
query += " WHERE question_id = ?"
query += " AND question_id = ?"
args = append(args, questionID)
}
query += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
@@ -412,9 +417,52 @@ func ListAnswers(db *sql.DB, questionID int64, limit, offset int) ([]Answer, err
return answers, rows.Err()
}
func boolToInt(b bool) int {
if b {
return 1
type AnswerWithDetails struct {
AnswerID int64 `json:"answer_id"`
ClientID int64 `json:"client_id"`
QuestionID int64 `json:"question_id"`
Answer string `json:"answer"`
Timestamp int64 `json:"timestamp"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
ClientName string `json:"client_name"`
CustomerName string `json:"customer_name"`
QuestionText string `json:"question_text"`
}
func ListAnswersWithDetails(db *sql.DB, questionID int64, limit, offset int) ([]AnswerWithDetails, error) {
query := `
SELECT
a.answer_id, a.client_id, a.question_id, a.answer, a.timestamp, a.status, a.created_at,
c.name as client_name,
cu.name as customer_name,
q.question as question_text
FROM answers a
JOIN questions q ON a.question_id = q.question_id
JOIN customers cu ON q.customer_id = cu.customer_id
JOIN clients c ON q.client_id = c.client_id
WHERE 1=1`
var args []interface{}
if questionID > 0 {
query += " AND a.question_id = ?"
args = append(args, questionID)
}
return 0
query += " ORDER BY a.created_at DESC LIMIT ? OFFSET ?"
args = append(args, limit, offset)
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var answers []AnswerWithDetails
for rows.Next() {
var a AnswerWithDetails
if err := rows.Scan(&a.AnswerID, &a.ClientID, &a.QuestionID, &a.Answer, &a.Timestamp, &a.Status, &a.CreatedAt, &a.ClientName, &a.CustomerName, &a.QuestionText); err != nil {
return nil, err
}
answers = append(answers, a)
}
return answers, rows.Err()
}