This commit is contained in:
suyl
2021-07-20 17:21:38 +08:00
parent 0fd6aa4207
commit d750bfe18d
10 changed files with 374 additions and 16 deletions

101
services/sim.go Normal file
View File

@@ -0,0 +1,101 @@
package services
import (
"git.rosy.net.cn/baseapi/platformapi/tibiotapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/dao"
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
"git.rosy.net.cn/jx-print/services/api"
putils "git.rosy.net.cn/jx-print/utils"
"time"
)
//每日流量卡流量结算
func SimFlowDaySettle() (err error) {
var (
db = globals.GetDB()
now = time.Now()
monthBegin = utils.Str2Time(utils.Int2Str(now.Year()) + "-" + utils.Int2Str(int(now.Month())) + "-01 00:00:00")
monthEnd = monthBegin.AddDate(0, 0, -1).AddDate(0, 1, 0)
)
globals.SugarLogger.Debugf("SimFlowDaySettle Begin monthBegin %v, monthEnd %v", monthBegin, monthEnd)
//月末好像不用算超不超了
if now.Month().String() != now.AddDate(0, 0, -1).Month().String() {
return
}
//找出所有状态不为 超流量的打印机iccid卡
printers, _ := dao.GetPrinters(db, 0, "", 0, model.PrinterStatusOverFlow)
for _, v := range printers {
//查询前一日使用的流量数
if v.IccID != "" {
flowExpend := &model.SimFlowExpend{
IccID: v.IccID,
CreatedAt: &now,
UpdatedAt: &now,
LastOperator: "jxadmin",
}
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" || getCardInfoResult.CardFlow == "" { //未同步
//先查当月用的总的, 减去当月已经用的总的,就是昨天用的
if getCardInfoResultMonth, err = api.TibiotAPI.IotDataMonth(v.IccID); err == nil {
//表示当月用的总的也还没同步。只有去查卡信息中的总流量使用,减去所有总使用,就是昨天用的。。太折磨了先不写
if getCardInfoResultMonth == nil || getCardInfoResultMonth.CardFlow == "" || getCardInfoResultMonth.CardFlow == "0KB" {
//api.TibiotAPI.BatchQueryCardInfo(1)
} else {
sumExpend, _ := dao.GetSimFlowExpendSum(db, v.IccID, monthBegin, monthEnd)
cardFlow := putils.Flow2KB(putils.SplitFlowAndUnit(getCardInfoResultMonth.CardFlow))
flowExpend.Flow, flowExpend.FlowUnit = putils.FlowKB2Other(cardFlow - sumExpend.Flow)
}
}
} else {
flowExpend.Flow, flowExpend.FlowUnit = putils.SplitFlowAndUnit(getCardInfoResult.CardFlow)
}
}
dao.Insert(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.PrinterStatusOverFlow
dao.Update(db, v, "Status")
}
}
}
return err
}
//每月流量卡流量划转
func SimFlowMonthSettle() {
var (
db = globals.GetDB()
now = time.Now()
)
//..每月1号划转
if time.Now().Day() != 1 {
return
}
//找出所有有iccid 卡的
printers, _ := dao.GetPrinters(db, 0, "", 0, model.PrinterStatusOverFlow)
for _, v := range printers {
if v.IccID != "" {
flowIncome := &model.SimFlowIncome{
IccID: v.IccID,
CreatedAt: &now,
UpdatedAt: &now,
LastOperator: "jxadmin",
IncomeType: model.FlowIncomeTypeJX,
Flow: 30,
FlowUnit: "MB",
}
dao.Insert(db, flowIncome)
}
}
}