package dao import ( "fmt" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-print/model" "github.com/jmoiron/sqlx" "time" ) 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 *model.SimFlowExpend, err error) { var ( simFlowExpnds []*model.SimFlowExpend ) 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 *model.SimFlowIncome, err error) { var ( simFlowIncomes []*model.SimFlowIncome ) 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 }