137 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package db
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| )
 | |
| 
 | |
| type ListIncomeConditionParam struct {
 | |
| 	TimeBegin  string
 | |
| 	TimeEnd    string
 | |
| 	ProjectID  int64
 | |
| 	BudgetID   int64
 | |
| 	IncomeType int32
 | |
| 	IsTitle    bool
 | |
| 	Title      string
 | |
| 	Status     int16
 | |
| 	PageID     int32
 | |
| 	PageSize   int32
 | |
| }
 | |
| 
 | |
| type IncomeView struct {
 | |
| 	Income
 | |
| 	ProjectName    string `json:"project_name"`
 | |
| 	BudgetName     string `json:"budget_name"`
 | |
| 	IncomeTypeName string `json:"income_type_name"`
 | |
| 	IncomeBankName string `json:"income_bank_name"`
 | |
| 	CreatedName    string `json:"created_name"`
 | |
| 	UpdatedName    string `json:"updated_name"`
 | |
| }
 | |
| 
 | |
| func (store *SQLStore) ListIncomeCondition(ctx context.Context, arg *ListIncomeConditionParam) ([]*IncomeView, int64, error) {
 | |
| 	query, args, err := BuildQuery(`
 | |
| 	SELECT COUNT(1)
 | |
| 	FROM incomes
 | |
| 	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 .budgetID 9999}}AND budget_id = @budgetID{{end}}
 | |
| 	{{if ne .incomeType 9999}}AND income_type = @incomeType{{end}}
 | |
| 	{{if .isTitle}}AND name LIKE @title{{end}};
 | |
| 	`, map[string]any{
 | |
| 		"projecID":   arg.ProjectID,
 | |
| 		"budgetID":   arg.BudgetID,
 | |
| 		"incomeType": arg.IncomeType,
 | |
| 		"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,
 | |
|        project_id, COALESCE((SELECT name FROM projects WHERE id = incomes.project_id), '') as project_name,
 | |
|        budget_id, COALESCE((SELECT name FROM budgets WHERE id = incomes.budget_id), '') as budget_name,
 | |
|        amount, income_at,
 | |
| 	   income_type, COALESCE((SELECT name FROM categories WHERE id = incomes.income_type), '') as income_type_name,
 | |
| 	   income_bank, COALESCE((SELECT name FROM categories WHERE id = incomes.income_bank), '') as income_bank_name,
 | |
| 	   remark, status, created_at,
 | |
|        created_user_id, COALESCE((SELECT username FROM sys_user WHERE id = incomes.created_user_id), '') as created_name,
 | |
|        updated_at,
 | |
|        updated_user_id, COALESCE((SELECT username FROM sys_user WHERE id = incomes.updated_user_id), '') as updated_name
 | |
| 	FROM incomes
 | |
| 	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 .budgetID 9999}}AND budget_id = @budgetID{{end}}
 | |
| 	{{if ne .incomeType 9999}}AND income_type = @incomeType{{end}}
 | |
| 	{{if .isTitle}}AND name LIKE @title{{end}}
 | |
| 	ORDER BY id DESC
 | |
| 	LIMIT @limit OFFSET @offset;
 | |
| 	`, map[string]any{
 | |
| 		"projecID":   arg.ProjectID,
 | |
| 		"budgetID":   arg.BudgetID,
 | |
| 		"incomeType": arg.IncomeType,
 | |
| 		"start":      arg.TimeBegin,
 | |
| 		"end":        arg.TimeEnd,
 | |
| 		"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 := []*IncomeView{}
 | |
| 	for rows.Next() {
 | |
| 		var i IncomeView
 | |
| 		if err := rows.Scan(
 | |
| 			&i.ID,
 | |
| 			&i.ProjectID,
 | |
| 			&i.ProjectName,
 | |
| 			&i.BudgetID,
 | |
| 			&i.BudgetName,
 | |
| 			&i.Amount,
 | |
| 			&i.IncomeAt,
 | |
| 			&i.IncomeType,
 | |
| 			&i.IncomeTypeName,
 | |
| 			&i.IncomeBank,
 | |
| 			&i.IncomeBankName,
 | |
| 			&i.Remark,
 | |
| 			&i.Status,
 | |
| 			&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
 | |
| }
 |