2021-11-28 12:19:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/beancount-gs/script"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SyncCommodityPriceForm struct {
|
|
|
|
|
Commodity string `form:"commodity" binding:"required" json:"commodity"`
|
|
|
|
|
Date string `form:"date" binding:"required" json:"date"`
|
|
|
|
|
Price string `form:"price" binding:"required" json:"price"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SyncCommodityPrice(c *gin.Context) {
|
|
|
|
|
var syncCommodityPriceForm SyncCommodityPriceForm
|
|
|
|
|
if err := c.ShouldBindJSON(&syncCommodityPriceForm); err != nil {
|
|
|
|
|
BadRequest(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
|
filePath := script.GetLedgerPriceFilePath(ledgerConfig.DataPath)
|
|
|
|
|
line := fmt.Sprintf("%s price %s %s %s", syncCommodityPriceForm.Date, syncCommodityPriceForm.Commodity, syncCommodityPriceForm.Price, ledgerConfig.OperatingCurrency)
|
|
|
|
|
// 写入文件
|
|
|
|
|
err := script.AppendFileInNewLine(filePath, line)
|
|
|
|
|
if err != nil {
|
|
|
|
|
InternalError(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-12-06 17:24:24 +00:00
|
|
|
|
2023-12-07 05:57:43 +00:00
|
|
|
// 刷新货币最新汇率值
|
2023-12-10 14:52:24 +00:00
|
|
|
err = script.LoadLedgerCurrencyMap(ledgerConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
InternalError(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-12-07 05:57:43 +00:00
|
|
|
OK(c, syncCommodityPriceForm)
|
2023-12-06 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func QueryAllCurrencies(c *gin.Context) {
|
|
|
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
|
// 查询货币获取当前汇率
|
2023-12-07 05:57:43 +00:00
|
|
|
currency := script.RefreshLedgerCurrency(ledgerConfig)
|
|
|
|
|
OK(c, currency)
|
2023-12-06 17:24:24 +00:00
|
|
|
}
|