This commit is contained in:
suyl
2021-07-20 16:52:41 +08:00
parent 59cc2d4ffd
commit b94de1d201
5 changed files with 112 additions and 22 deletions

View File

@@ -1,7 +1,9 @@
package cms
import (
"git.rosy.net.cn/baseapi/platformapi/tibiotapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
@@ -9,8 +11,12 @@ import (
"time"
)
const (
overFlow = 30 * 1024 // kb
)
//每日流量卡流量结算
func SimFlowDaySettle(ctx *jxcontext.Context) {
func SimFlowDaySettle(ctx *jxcontext.Context) (err error) {
var (
db = dao.GetDB()
monthBegin = utils.Str2Time(utils.Int2Str(time.Now().Year()) + "-" + utils.Int2Str(int(time.Now().Month())) + "-01 00:00:00")
@@ -21,25 +27,44 @@ func SimFlowDaySettle(ctx *jxcontext.Context) {
for _, v := range printers {
//查询前一日使用的流量数
if v.IccID != "" {
if getCardInfoResult, err := api.TibiotAPI.IotData(v.IccID, utils.Time2Str(utils.Time2Date(time.Now().AddDate(0, 0, -1)))); err == nil {
flowExpend := &model.SimFlowExpend{
IccID: v.IccID,
}
dao.WrapAddIDCULEntity(flowExpend, ctx.GetUserName())
var (
getCardInfoResult *tibiotapi.IotDataResult
getCardInfoResultMonth *tibiotapi.IotDataMonthResult
)
if getCardInfoResult, err = api.TibiotAPI.IotData(v.IccID, utils.Time2Str(utils.Time2Date(time.Now().AddDate(0, 0, -1)))); err == nil {
//表示还没有同步前一天的流量,只能自己算了
if getCardInfoResult == nil || getCardInfoResult.SyncStatus == "1" || getCardInfoResult.CardFlow == "0KB" { //未同步
if getCardInfoResult == nil || getCardInfoResult.SyncStatus == "1" || getCardInfoResult.CardFlow == "0KB" || getCardInfoResult.CardFlow == "" { //未同步
//先查当月用的总的, 减去当月已经用的总的,就是昨天用的
if getCardInfoResultMonth, err2 := api.TibiotAPI.IotDataMonth(v.IccID); err2 == nil {
//表示当月用的总的也还没同步。只有去查卡信息中的总流量使用,减去所有总使用,就是昨天用的。。先不写
if getCardInfoResultMonth, err = api.TibiotAPI.IotDataMonth(v.IccID); err == nil {
//表示当月用的总的也还没同步。只有去查卡信息中的总流量使用,减去所有总使用,就是昨天用的。。太折磨了先不写
if getCardInfoResultMonth == nil || getCardInfoResultMonth.CardFlow == "" || getCardInfoResultMonth.CardFlow == "0KB" {
//api.TibiotAPI.BatchQueryCardInfo(1)
} else if getCardInfoResultMonth.CardFlow != "" {
} else {
sumExpend, _ := dao.GetSimFlowExpendSum(db, v.IccID, monthBegin, monthEnd)
cardFlow := jxutils.Flow2KB(jxutils.SplitFlowAndUnit(getCardInfoResultMonth.CardFlow))
flowExpend.Flow, flowExpend.FlowUnit = jxutils.FlowKB2Other(cardFlow - sumExpend.Flow)
}
}
} else if getCardInfoResult.CardFlow != "" {
} else {
flowExpend.Flow, flowExpend.FlowUnit = jxutils.SplitFlowAndUnit(getCardInfoResult.CardFlow)
}
}
dao.CreateEntity(db, flowExpend)
//算是否超了一个月的流量了
//一个月总的收入流量 - 一个月总的支出流量 <= 0
sumIncome, _ := dao.GetSimFlowIncomeSum(db, v.IccID, monthBegin, monthEnd)
sumExpend, _ := dao.GetSimFlowExpendSum(db, v.IccID, monthBegin, monthEnd)
if sumExpend.Flow-sumIncome.Flow <= 0 {
v.Status = model.PrinterStatusNoFlow
dao.UpdateEntity(db, v, "Status")
}
}
}
return err
}
//每月流量卡流量结算
@@ -47,7 +72,7 @@ func SimFlowMonthSettle(ctx *jxcontext.Context) {
var (
db = dao.GetDB()
)
//..每月1号结算上月
//..每月1号划转
if time.Now().Day() != 1 {
return
}

View File

@@ -1,8 +1,6 @@
package misc
import (
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"time"
"git.rosy.net.cn/baseapi/utils"
@@ -11,12 +9,12 @@ import (
)
func Init() {
ScheduleTimerFunc("SimFlowDaySettle", func() {
cms.SimFlowDaySettle(jxcontext.AdminCtx)
cms.SimFlowMonthSettle(jxcontext.AdminCtx)
}, []string{
"00:05:00",
})
//ScheduleTimerFunc("SimFlowDaySettle", func() {
// cms.SimFlowDaySettle(jxcontext.AdminCtx)
// cms.SimFlowMonthSettle(jxcontext.AdminCtx)
//}, []string{
// "00:05:00",
//})
}
// 按时间序列循环

View File

@@ -37,7 +37,11 @@ var (
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
flowUnitMap = map[string]string{}
flowUnitMap = map[string]string{
"KB": "KB",
"MB": "MB",
"GB": "GB",
}
)
const fileExt = ".xlsx"
@@ -679,5 +683,38 @@ func BuildErr(errs []error) (err error) {
}
func SplitFlowAndUnit(flowStr string) (flow float64, unit string) {
for _, v := range flowUnitMap {
if strings.Contains(flowStr, v) {
return utils.Str2Float64WithDefault(flowStr[:len(flowStr)-2], 0), flowStr[len(flowStr)-2:]
}
}
return flow, unit
}
func Flow2KB(flow float64, unit string) (flowKB float64) {
if unit == "KB" {
return flow
} else if unit == "MB" {
return flow * 1024
} else if unit == "GB" {
return flow * 1024 * 1024
}
return flowKB
}
func FlowKB2Other(flowKB float64) (flow float64, unit string) {
if flowKB < 1024 {
return flowKB, "KB"
} else {
flowMB := math.Round(flowKB / float64(1024))
if flowMB < 1024 {
return flowMB, "MB"
} else {
flowGB := math.Round(flowMB / float64(1024))
if flowGB < 1024 {
return flowGB, "GB"
}
}
}
return flow, unit
}

View File

@@ -39,3 +39,32 @@ func GetSimFlowExpendSum(db *DaoDB, iccid string, createdAtBegin, createdAtEnd t
err = GetRow(db, &simFlowExpnd, sql, sqlParams)
return simFlowExpnd, err
}
func GetSimFlowIncomeSum(db *DaoDB, iccid string, createdAtBegin, createdAtEnd time.Time) (simFlowIncome *model.SimFlowIncome, err error) {
sql := `
SELECT SUM(
IF(flow_unit = 'KB', flow,
IF(flow_unit = 'MB', ROUND(flow * 1024)),
IF(flow_unit = "GB", ROUND(flow * 1024 * 1024), 0)
)
), 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"
err = GetRow(db, &simFlowIncome, sql, sqlParams)
return simFlowIncome, err
}

View File

@@ -70,9 +70,10 @@ func (v *SimFlowExpend) TableIndex() [][]string {
type SimFlowIncome struct {
ModelIDCUL
IccID string `orm:"column(icc_id)" json:"iccID"` //sim卡iccid
Flow float64 `json:"flow"` //流量数
FlowUnit string `json:"flowUnit"` //流量单位
IccID string `orm:"column(icc_id)" json:"iccID"` //sim卡iccid
Flow float64 `json:"flow"` //流量数
FlowUnit string `json:"flowUnit"` //流量单位
IncomeType int `json:"incomeType"` //1 表示系统每月自动划转2 表示商户自己冲的
}
func (v *SimFlowIncome) TableIndex() [][]string {