From 027ffa19ca866fdcbc09ff4d1ddcc30290511139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=AE=97=E6=A5=A0?= Date: Thu, 18 Aug 2022 09:15:26 +0800 Subject: [PATCH] 1 --- business/jxstore/event/event_tcp.go | 20 ++++++++++++++ business/jxstore/event/event_tcp_utils.go | 2 -- business/model/dao/print_bill.go | 7 +++++ business/model/dao/print_bill_record.go | 32 +++++++++++++++++++++++ business/model/print_bill.go | 13 +++++++++ business/model/print_bill_record.go | 15 +++++++++++ 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 business/model/dao/print_bill.go create mode 100644 business/model/dao/print_bill_record.go create mode 100644 business/model/print_bill.go create mode 100644 business/model/print_bill_record.go diff --git a/business/jxstore/event/event_tcp.go b/business/jxstore/event/event_tcp.go index 68a0ecf26..010030126 100644 --- a/business/jxstore/event/event_tcp.go +++ b/business/jxstore/event/event_tcp.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "fmt" "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" "io" @@ -258,6 +259,25 @@ func (t *TcpClient) doPrint(key string) (err error) { if dataStr != "" { a, b := getCallbackMsgInfo(dataStr) t.changePrintMsg(dataStr, a, b) + // 查询打印机是否扣费,未扣费就扣费,已经扣费不做处理 + have, err := dao.QueryOrderDeductionRecord(db, b, utils.Int64ToStr(a)) + if err != nil && !have { + // 扣除打印机账号金额 + err = dao.DeductionPrintBalance(db, b) + // 添加打印记录(支出记录) + err = dao.AddPrintRecord(db, &model.PrintBillRecord{ + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + PrintNo: b, + PayType: 2, + PayMoney: 1, // 固定支出一分钱 + OrderId: utils.Int64ToStr(a), + UserId: "", + }) + globals.SugarLogger.Debugf("扣除用户打印机金额/添加打印机打印记录错误 %s", err) + } else { + globals.SugarLogger.Debugf("查询打印机扣费记录错误 %s", err) + } //判断音频暂停? //收到打印成功回调后,如果消息中有音频,需要等待一下,等上一个音频播完 //暂停时间就暂时取的sound标签内内容长度/2 diff --git a/business/jxstore/event/event_tcp_utils.go b/business/jxstore/event/event_tcp_utils.go index 6b4b67526..989ea3261 100644 --- a/business/jxstore/event/event_tcp_utils.go +++ b/business/jxstore/event/event_tcp_utils.go @@ -327,8 +327,6 @@ func getCallbackMsgInfo(data string) (orderNo int64, printNo string) { orderNo = h8l82int(data[len(data)-6:len(data)-4], data[len(data)-4:len(data)-2]) printNoData, _ := hex.DecodeString(data[len(printSuccessText) : len(data)-6]) printNo = string(printNoData) - globals.SugarLogger.Debug("=======================orderNo", orderNo) - globals.SugarLogger.Debug("=======================printNo", printNo) return orderNo, printNo } diff --git a/business/model/dao/print_bill.go b/business/model/dao/print_bill.go new file mode 100644 index 000000000..4f3950e72 --- /dev/null +++ b/business/model/dao/print_bill.go @@ -0,0 +1,7 @@ +package dao + +// DeductionPrintBalance 扣除打印机账号余额 +func DeductionPrintBalance(db *DaoDB, printNo string) error { + _, err := ExecuteSQL(db, `UPDATE print_bill SET print_balance = print_balance -1 WHERE print_no = ?`, []interface{}{printNo}...) + return err +} diff --git a/business/model/dao/print_bill_record.go b/business/model/dao/print_bill_record.go new file mode 100644 index 000000000..6de1e49f4 --- /dev/null +++ b/business/model/dao/print_bill_record.go @@ -0,0 +1,32 @@ +package dao + +import ( + "git.rosy.net.cn/jx-callback/business/model" + "time" +) + +// QueryOrderDeductionRecord 查询订单扣除记录 +func QueryOrderDeductionRecord(db *DaoDB, printNo string, orderNo string) (bool, error) { + sql := `SELECT * FROM print_bill_record WHERE print_no = ? AND order_no = ? AND created_at > ? AND created_at < ?` + + timeNow := time.Now() + startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) + endTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 23, 59, 59, 0, timeNow.Location()) + param := []interface{}{printNo, orderNo, startTime, endTime} + + var result []*model.PrintBillRecord + if err := GetRows(db, &result, sql, param...); err != nil { + return false, err + } + + if len(result) == 0 { + return false, nil + } + + return true, nil +} + +// AddPrintRecord 添加打印记录 +func AddPrintRecord(db *DaoDB, param *model.PrintBillRecord) error { + return CreateEntity(db, param) +} diff --git a/business/model/print_bill.go b/business/model/print_bill.go new file mode 100644 index 000000000..b8055916d --- /dev/null +++ b/business/model/print_bill.go @@ -0,0 +1,13 @@ +package model + +import "time" + +// PrintBill 打印机账户 +type PrintBill struct { + ID int `orm:"column(id)" json:"id" db:"id"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` + PrintNo string `orm:"type(varchar);size(32);index" json:"print_no" db:"print_no"` // 打印机编号 + PrintBalance int64 `orm:"type(int);size(16)" json:"print_balance" db:"print_balance"` // 账户余额 + UserId string `orm:"type(varchar);size(125)" json:"user_id" db:"user_id"` // 打印机所属用户 +} diff --git a/business/model/print_bill_record.go b/business/model/print_bill_record.go new file mode 100644 index 000000000..e2b6a514b --- /dev/null +++ b/business/model/print_bill_record.go @@ -0,0 +1,15 @@ +package model + +import "time" + +// PrintBillRecord 打印机充值/小费记录 +type PrintBillRecord struct { + ID int `orm:"column(id)" json:"id" db:"id"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` + PrintNo string `orm:"type(varchar);size(32);index" json:"print_no" db:"print_no"` // 打印机编号 + PayType int `orm:"type(int);size(2)" json:"pay_type" db:"pay_type"` // 支付类型[1-充值/2-支出] + PayMoney int `orm:"type(int);size(10)" json:"pay_money" db:"pay_money"` // 金额 + OrderId string `orm:"type(varchar);size(64);index" json:"order_id" db:"order_id"` // 订单号 + UserId string `orm:"type(varchar);size(125)" json:"user_id" db:"user_id"` // 打印机所属用户 +}