132 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			132 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package db
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| )
 | |
| 
 | |
| type ListBudgetConditionParam struct {
 | |
| 	TimeBegin  string
 | |
| 	TimeEnd    string
 | |
| 	ProjectID  int64
 | |
| 	BudgetType int32
 | |
| 	Category   int32
 | |
| 	IsTitle    bool
 | |
| 	Title      string
 | |
| 	Status     int16
 | |
| 	PageID     int32
 | |
| 	PageSize   int32
 | |
| }
 | |
| 
 | |
| type BudgetView struct {
 | |
| 	Budget
 | |
| 	ProjectName  string `json:"project_name"`
 | |
| 	CategoryName string `json:"category_name"`
 | |
| 	CreatedName  string `json:"created_name"`
 | |
| 	UpdatedName  string `json:"updated_name"`
 | |
| }
 | |
| 
 | |
| func (store *SQLStore) ListBudgetCondition(ctx context.Context, arg *ListBudgetConditionParam) ([]*BudgetView, int64, error) {
 | |
| 	query, args, err := BuildQuery(`
 | |
| 	SELECT COUNT(1)
 | |
| 	FROM budgets
 | |
| 	WHERE created_at BETWEEN @start AND @end
 | |
| 	{{if ne .status 9999}}AND status = @status{{end}}
 | |
| 	{{if ne .projecID 9999}}AND project_id = @projecID{{end}}
 | |
| 	{{if ne .budgetType 9999}}AND budget_type = @budgetType{{end}}
 | |
| 	{{if ne .category 9999}}AND category = @category{{end}}
 | |
| 	{{if .isTitle}}AND name LIKE @title{{end}};
 | |
| 	`, map[string]any{
 | |
| 		"projecID":   arg.ProjectID,
 | |
| 		"budgetType": arg.BudgetType,
 | |
| 		"category":   arg.Category,
 | |
| 		"start":      arg.TimeBegin,
 | |
| 		"end":        arg.TimeEnd,
 | |
| 		"status":     arg.Status,
 | |
| 		"isTitle":    arg.IsTitle,
 | |
| 		"title":      arg.Title,
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	var total int64
 | |
| 	if err := store.db.QueryRow(ctx, query, args...).Scan(&total); err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	if total <= 0 {
 | |
| 		return nil, 0, nil
 | |
| 	}
 | |
| 
 | |
| 	query, args, err = BuildQuery(`
 | |
| 	select id, name, budget_type,
 | |
|        project_id, COALESCE((SELECT name FROM projects WHERE id = budgets.project_id), '') as project_name,
 | |
|        category, COALESCE((SELECT name FROM categories WHERE id = budgets.category), '') as category_name,
 | |
|        start_at, end_at, amount, used_amount, remaining_amount, sort, created_at,
 | |
|        created_user_id, COALESCE((SELECT username FROM sys_user WHERE id = budgets.created_user_id), '') as created_name,
 | |
|        updated_at,
 | |
|        updated_user_id, COALESCE((SELECT username FROM sys_user WHERE id = budgets.updated_user_id), '') as updated_name
 | |
| 	FROM budgets
 | |
| 	WHERE created_at BETWEEN @start AND @end
 | |
| 	{{if ne .status 9999}}AND status = @status{{end}}
 | |
| 	{{if ne .projecID 9999}}AND project_id = @projecID{{end}}
 | |
| 	{{if ne .budgetType 9999}}AND budget_type = @budgetType{{end}}
 | |
| 	{{if ne .category 9999}}AND category = @category{{end}}
 | |
| 	{{if .isTitle}}AND name LIKE @title{{end}}
 | |
| 	ORDER BY id DESC
 | |
| 	LIMIT @limit OFFSET @offset;
 | |
| 	`, map[string]any{
 | |
| 		"start":      arg.TimeBegin,
 | |
| 		"end":        arg.TimeEnd,
 | |
| 		"projecID":   arg.ProjectID,
 | |
| 		"budgetType": arg.BudgetType,
 | |
| 		"category":   arg.Category,
 | |
| 		"status":     arg.Status,
 | |
| 		"isTitle":    arg.IsTitle,
 | |
| 		"title":      arg.Title,
 | |
| 		"limit":      arg.PageSize,
 | |
| 		"offset":     (arg.PageID - 1) * arg.PageSize,
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	rows, err := store.db.Query(ctx, query, args...)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 	defer rows.Close()
 | |
| 	items := []*BudgetView{}
 | |
| 	for rows.Next() {
 | |
| 		var i BudgetView
 | |
| 		if err := rows.Scan(
 | |
| 			&i.ID,
 | |
| 			&i.Name,
 | |
| 			&i.BudgetType,
 | |
| 			&i.ProjectID,
 | |
| 			&i.ProjectName,
 | |
| 			&i.Category,
 | |
| 			&i.CategoryName,
 | |
| 			&i.StartAt,
 | |
| 			&i.EndAt,
 | |
| 			&i.Amount,
 | |
| 			&i.UsedAmount,
 | |
| 			&i.RemainingAmount,
 | |
| 			&i.Sort,
 | |
| 			&i.CreatedAt,
 | |
| 			&i.CreatedUserID,
 | |
| 			&i.CreatedName,
 | |
| 			&i.UpdatedAt,
 | |
| 			&i.UpdatedUserID,
 | |
| 			&i.UpdatedName,
 | |
| 		); err != nil {
 | |
| 			return nil, 0, err
 | |
| 		}
 | |
| 		items = append(items, &i)
 | |
| 	}
 | |
| 	if err := rows.Err(); err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 	return items, total, nil
 | |
| }
 |