165 lines
5.5 KiB
Go
165 lines
5.5 KiB
Go
package print_server
|
||
|
||
import (
|
||
"fmt"
|
||
"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"
|
||
putils "git.rosy.net.cn/jx-print/putils"
|
||
"git.rosy.net.cn/jx-print/services/api"
|
||
"github.com/jmoiron/sqlx"
|
||
|
||
"time"
|
||
)
|
||
|
||
//每日流量卡流量结算
|
||
func SimFlowDaySettle() (err error) {
|
||
var (
|
||
db = globals.GetDB()
|
||
now = time.Now()
|
||
monthBegin = utils.Str2Time(now.Format("2006-01") + "-01 00:00:00")
|
||
monthEnd = utils.Str2Time(monthBegin.AddDate(0, 1, 0).AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59")
|
||
)
|
||
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, 0, 0, "")
|
||
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, now.AddDate(0, 0, -1).Format("2006-01-02")); 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 {
|
||
if sumExpend, err := dao.GetSimFlowExpendSum(db, v.IccID, monthBegin, monthEnd); err == nil {
|
||
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)
|
||
}
|
||
}
|
||
//可能有精度误差
|
||
if flowExpend.Flow > 0.1 && flowExpend.FlowUnit != "" {
|
||
err = dao.Insert(db, flowExpend)
|
||
}
|
||
//算是否超了一个月的流量了
|
||
//一个月总的收入流量 - 一个月总的支出流量 <= 0
|
||
sumIncome, _ := dao.GetSimFlowIncomeSum(db, v.IccID, monthBegin, monthEnd)
|
||
sumExpend, _ := dao.GetSimFlowExpendSum(db, v.IccID, monthBegin, monthEnd)
|
||
if sumIncome != nil && sumExpend != nil {
|
||
if sumIncome.Flow-sumExpend.Flow <= 0 {
|
||
v.FlowFlag = 1
|
||
} else {
|
||
v.FlowFlag = 0
|
||
}
|
||
err = dao.Update(db, v, "flow_flag")
|
||
}
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
//每月流量卡流量划转
|
||
func SimFlowMonthSettle() {
|
||
var (
|
||
db = globals.GetDB()
|
||
)
|
||
//..每月1号划转
|
||
if time.Now().Day() != 1 {
|
||
return
|
||
}
|
||
//找出所有有iccid 卡的
|
||
printers, _ := dao.GetPrinters(db, 0, "", 0, 0, 0, "")
|
||
for _, v := range printers {
|
||
if v.IccID != "" {
|
||
if err := FlowIncome(db, v.IccID, 30, "MB", model.FlowIncomeTypeJX, ""); err == nil {
|
||
//划转后,每个月月初打印机都应该是不缺流量的状态
|
||
v.FlowFlag = 0
|
||
dao.Update(db, v, "flow_flag")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func FlowIncome(db *sqlx.DB, iccID string, flow float64, unit string, incomeType int, orderID string) (err error) {
|
||
var (
|
||
now = time.Now()
|
||
)
|
||
flowIncome := &model.SimFlowIncome{
|
||
IccID: iccID,
|
||
CreatedAt: &now,
|
||
UpdatedAt: &now,
|
||
LastOperator: "jxadmin",
|
||
Flow: flow,
|
||
FlowUnit: unit,
|
||
IncomeType: incomeType,
|
||
OrderID: orderID,
|
||
}
|
||
err = dao.Insert(db, flowIncome)
|
||
return err
|
||
}
|
||
|
||
func GetCardsInfo(tokenInfo *model.TokenInfo, appID int, printNo string, cardStatus int, iccID, beginDate, endDate string, offset, pagiSize int) (cardInfo *tibiotapi.BatchQueryCardInfoResult, err error) {
|
||
var (
|
||
db = globals.GetDB()
|
||
)
|
||
if printNo != "" {
|
||
printers, _ := dao.GetPrinters(db, appID, printNo, 0, 0, 0, "")
|
||
if len(printers) == 0 {
|
||
err = fmt.Errorf("未在该应用下查到此打印机!app_id: %d, print_no: %s", appID, printNo)
|
||
return nil, err
|
||
} else {
|
||
iccID = printers[0].IccID
|
||
}
|
||
}
|
||
if beginDate != "" {
|
||
beginDate = beginDate[:10]
|
||
}
|
||
if endDate != "" {
|
||
endDate = endDate[:10]
|
||
}
|
||
if cardInfo, err = api.TibiotAPI.BatchQueryCardInfo(&tibiotapi.BatchQueryCardInfoParam{
|
||
CardStatus: cardStatus,
|
||
IccID: iccID,
|
||
BeginPackageTime: beginDate,
|
||
EndPackageTime: endDate,
|
||
PageNum: offset,
|
||
PageSize: pagiSize,
|
||
}); err == nil && cardInfo != nil {
|
||
for _, v := range cardInfo.Records {
|
||
if printer, err2 := dao.GetPrinter(db, "", v.Iccid); err2 == nil && printer != nil {
|
||
v.PrintNo = printer.PrintNo
|
||
v.Name = printer.Name
|
||
}
|
||
}
|
||
}
|
||
return cardInfo, err
|
||
}
|
||
|
||
func GetChargeInfo(tokenInfo *model.TokenInfo, appID int, printNo string, iccID, beginDate, endDate string, offset, pageSize int) (paged *model.PagedInfo, err error) {
|
||
return dao.GetChargeInfo(globals.GetDB(), appID, printNo, iccID, beginDate, endDate, offset, pageSize)
|
||
}
|