add month stats api

This commit is contained in:
BaoXuebin 2021-11-25 15:41:28 +08:00
parent 47c21de613
commit d91869319a
3 changed files with 90 additions and 2 deletions

View File

@ -100,9 +100,9 @@ func BQLQueryListByCustomSelect(ledgerConfig *Config, selectBql string, queryPar
}
func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) {
bql := ""
var bql string
if selectBql == "" {
bql = "SELECT"
bql = "select"
queryResultPtrType := reflect.TypeOf(queryResultPtr)
queryResultType := queryResultPtrType.Elem()
@ -196,6 +196,13 @@ func parseResult(output string, queryResultPtr interface{}, selectOne bool) erro
jsonName = field.Name
}
switch field.Type.Kind() {
case reflect.Int:
i, err := strconv.Atoi(strings.Trim(val, " "))
if err != nil {
panic(err)
}
temp[jsonName] = i
break
// decimal
case reflect.String, reflect.Struct:
v := strings.Trim(val, " ")

View File

@ -56,6 +56,7 @@ func RegisterRouter(router *gin.Engine) {
authorized.GET("/stats/total", service.StatsTotal)
authorized.GET("/stats/account/percent", service.StatsAccountPercent)
authorized.GET("/stats/account/trend", service.StatsAccountTrend)
authorized.GET("/stats/month/total", service.StatsMonthTotal)
authorized.GET("/transaction", service.QueryTransactions)
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
authorized.GET("/transaction/template", service.QueryTransactionTemplates)
@ -67,6 +68,7 @@ func RegisterRouter(router *gin.Engine) {
// 兼容旧版本
authorized.GET("/entry", service.QueryTransactions)
authorized.GET("/payee", service.QueryTransactionPayees)
authorized.GET("/stats/month/incomeExpenses", service.StatsMonthTotal)
}
}

View File

@ -151,3 +151,82 @@ func StatsAccountTrend(c *gin.Context) {
}
OK(c, result)
}
type MonthTotalBQLResult struct {
Year int
Month int
Value string
}
type MonthTotal struct {
Type string `json:"type"`
Month string `json:"month"`
Amount float64 `json:"amount"`
OperatingCurrency string `json:"operatingCurrency"`
}
func StatsMonthTotal(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
monthSet := make(map[string]bool)
queryParams := script.QueryParams{
AccountLike: "Income",
Where: true,
OrderBy: "year, month",
}
// 按月查询收入
queryIncomeBql := fmt.Sprintf("select '\\', year, '\\', month, '\\', neg(sum(convert(value(position), '%s'))), '\\'", ledgerConfig.OperatingCurrency)
monthIncomeTotalResultList := make([]MonthTotalBQLResult, 0)
err := script.BQLQueryListByCustomSelect(ledgerConfig, queryIncomeBql, &queryParams, &monthIncomeTotalResultList)
if err != nil {
InternalError(c, err.Error())
return
}
monthIncomeMap := make(map[string]MonthTotalBQLResult)
for _, income := range monthIncomeTotalResultList {
month := fmt.Sprintf("%d-%d", income.Year, income.Month)
monthSet[month] = true
monthIncomeMap[month] = income
}
// 按月查询支出
queryParams.AccountLike = "Expenses"
queryExpensesBql := fmt.Sprintf("select '\\', year, '\\', month, '\\', sum(convert(value(position), '%s')), '\\'", ledgerConfig.OperatingCurrency)
monthExpensesTotalResultList := make([]MonthTotalBQLResult, 0)
err = script.BQLQueryListByCustomSelect(ledgerConfig, queryExpensesBql, &queryParams, &monthExpensesTotalResultList)
if err != nil {
InternalError(c, err.Error())
return
}
monthExpensesMap := make(map[string]MonthTotalBQLResult)
for _, expenses := range monthExpensesTotalResultList {
month := fmt.Sprintf("%d-%d", expenses.Year, expenses.Month)
monthSet[month] = true
monthExpensesMap[month] = expenses
}
monthTotalResult := make([]MonthTotal, 0)
// 合并结果
var monthIncome, monthExpenses MonthTotal
for month := range monthSet {
if monthIncomeMap[month].Value != "" {
fields := strings.Fields(monthIncomeMap[month].Value)
amount, _ := strconv.ParseFloat(fields[0], 64)
monthIncome = MonthTotal{Type: "收入", Month: month, Amount: amount, OperatingCurrency: fields[1]}
} else {
monthIncome = MonthTotal{Type: "收入", Month: month, Amount: 0, OperatingCurrency: ledgerConfig.OperatingCurrency}
}
monthTotalResult = append(monthTotalResult, monthIncome)
if monthExpensesMap[month].Value != "" {
fields := strings.Fields(monthExpensesMap[month].Value)
amount, _ := strconv.ParseFloat(fields[0], 64)
monthExpenses = MonthTotal{Type: "支出", Month: month, Amount: amount, OperatingCurrency: fields[1]}
} else {
monthExpenses = MonthTotal{Type: "支出", Month: month, Amount: 0, OperatingCurrency: ledgerConfig.OperatingCurrency}
}
monthTotalResult = append(monthTotalResult, monthExpenses)
monthTotalResult = append(monthTotalResult, MonthTotal{Type: "结余", Month: month, Amount: monthIncome.Amount - monthExpenses.Amount, OperatingCurrency: ledgerConfig.OperatingCurrency})
}
OK(c, monthTotalResult)
}