Files
jx-print/dao/sim_dao.go
suyl 8d144a5dd2 aa
2021-07-21 14:04:35 +08:00

90 lines
2.3 KiB
Go

package dao
import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/model"
"github.com/jmoiron/sqlx"
"time"
)
type FlowSum struct {
Flow float64 `json:"flow" db:"flow"`
IccID string `json:"icc_id" db:"icc_id"`
FlowUnit string `json:"flow_unit" db:"flow_unit"`
}
func GetSimFlowExpend(db *sqlx.DB, iccid string, createdAtBegin, createdAtEnd time.Time) (simFlowExpnd []*model.SimFlowExpend, err error) {
return simFlowExpnd, err
}
func GetSimFlowExpendSum(db *sqlx.DB, iccid string, createdAtBegin, createdAtEnd time.Time) (simFlowExpnd *FlowSum, err error) {
var (
simFlowExpnds []*FlowSum
)
sql := `
SELECT SUM(
IF(flow_unit = 'KB', flow,
IF(flow_unit = 'MB', ROUND(flow * 1024),
IF(flow_unit = 'GB', ROUND(flow * 1024 * 1024), 0)
)
)
) flow, icc_id, 'KB' flow_unit
FROM sim_flow_expend
WHERE 1 = 1
`
sqlParams := []interface{}{}
if iccid != "" {
sql += " AND icc_id = ?"
sqlParams = append(sqlParams, iccid)
}
if !utils.IsTimeZero(createdAtBegin) {
sql += " AND created_at > ?"
sqlParams = append(sqlParams, createdAtBegin)
}
if !utils.IsTimeZero(createdAtEnd) {
sql += " AND created_at < ?"
sqlParams = append(sqlParams, createdAtEnd)
}
sql += " GROUP BY 2, 3"
if err = db.Select(&simFlowExpnds, sql, sqlParams...); err == nil && len(simFlowExpnds) > 0 {
return simFlowExpnds[0], err
}
return simFlowExpnd, err
}
func GetSimFlowIncomeSum(db *sqlx.DB, iccid string, createdAtBegin, createdAtEnd time.Time) (simFlowIncome *FlowSum, err error) {
var (
simFlowIncomes []*FlowSum
)
sql := `
SELECT SUM(
IF(flow_unit = 'KB', flow,
IF(flow_unit = 'MB', ROUND(flow * 1024),
IF(flow_unit = 'GB', ROUND(flow * 1024 * 1024), 0)
)
)
) flow, icc_id, 'KB' flow_unit
FROM sim_flow_income
WHERE 1 = 1
`
sqlParams := []interface{}{}
if iccid != "" {
sql += " AND icc_id = ?"
sqlParams = append(sqlParams, iccid)
}
if !utils.IsTimeZero(createdAtBegin) {
sql += " AND created_at > ?"
sqlParams = append(sqlParams, createdAtBegin)
}
if !utils.IsTimeZero(createdAtEnd) {
sql += " AND created_at < ?"
sqlParams = append(sqlParams, createdAtEnd)
}
sql += " GROUP BY 2, 3"
if err = db.Select(&simFlowIncomes, sql, sqlParams...); err == nil && len(simFlowIncomes) > 0 {
return simFlowIncomes[0], err
}
return simFlowIncome, err
}