This commit is contained in:
邹宗楠
2023-05-30 09:17:38 +08:00
parent f9913ead46
commit 9e2b93b6cd

View File

@@ -276,191 +276,191 @@ func (t *TcpClient) doPrint(key string) (err error) {
return err
}
fn := func() {
if t.TimeoutMap[key] == true {
select {
case printMsg, ok := <-t.MsgMap[key]:
if !ok {
globals.SugarLogger.Debugf("doPrint err !ok ...")
return
}
var (
data []byte
c net.Conn
)
if printMsg != nil {
if err = checkPrintMsg(db, printMsg); err == nil {
status := t.getPrintStatus(printMsg.PrintNo)
switch status {
//只有在线才打印内容
case printerStatusOnline:
if c = t.getPrintConn(printMsg.PrintNo); c != nil {
data, err = buildMsg(printMsg)
}
case printerStatusOffline:
err = fmt.Errorf("打印机离线!")
case printerStatusOnlineWithoutPaper:
err = fmt.Errorf("打印机缺纸!")
default:
err = fmt.Errorf("打印机状态未知!")
}
}
if err != nil {
printMsg.Status = printMsgErr
printMsg.Comment = err.Error()
dao.UpdateEntity(db, printMsg, "Status", "Comment")
delete(t.TimeoutMap, key)
} else {
if c != nil {
if _, err = c.Write(data); err != nil {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
//close(t.TimeoutMap[key])
delete(t.TimeoutMap, key)
} else {
//等待回调
dataStr := <-t.CallBackMap[key]
if dataStr != "" {
a, b := getCallbackMsgInfo(dataStr)
t.changePrintMsg(dataStr, a, b)
// 查询打印机是否扣费,未扣费就扣费,已经扣费不做处理
have, err := dao.QueryOrderDeductionRecord(db, b, utils.Int64ToStr(a))
if err != nil && !have {
// 扣除打印机账号金额
if err = dao.DeductionPrintBalance(db, b); err != nil {
globals.SugarLogger.Debugf("扣除用户打印机金额错误 %s", err)
} else {
// 添加打印记录(支出记录)
if err = dao.AddPrintRecord(db, &model.PrintBillRecord{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
PrintNo: b,
PayType: 2,
PayMoney: 1, // 固定支出一分钱
OrderId: utils.Int64ToStr(a),
UserId: "",
}); err != nil {
globals.SugarLogger.Debugf("添加打印机订单支付记录错误 %s", err)
}
}
} else {
globals.SugarLogger.Debugf("查询打印机扣费记录错误 %s", err)
}
//判断音频暂停?
//收到打印成功回调后,如果消息中有音频,需要等待一下,等上一个音频播完
//暂停时间就暂时取的sound标签内内容长度/2
if sounds := regexpSoundSpan.FindStringSubmatch(printMsg.Content); len(sounds) > 0 {
sound := sounds[1]
lenTime := time.Duration(utf8.RuneCountInString(sound)) * time.Second
time.Sleep(lenTime / 2)
}
}
}
}
}
} else {
globals.SugarLogger.Debugf("msgMap is nil")
}
}
} else {
globals.SugarLogger.Debugf("doPrint timeout")
return
}
}
Poll.AddJob(fn)
//go func() {
// for {
// if t.TimeoutMap[key] == true {
// select {
// case printMsg, ok := <-t.MsgMap[key]:
// if !ok {
// globals.SugarLogger.Debugf("doPrint err !ok ...")
// return
// }
// var (
// data []byte
// c net.Conn
// )
// if printMsg != nil {
// if err = checkPrintMsg(db, printMsg); err == nil {
// status := t.getPrintStatus(printMsg.PrintNo)
// switch status {
// //只有在线才打印内容
// case printerStatusOnline:
// if c = t.getPrintConn(printMsg.PrintNo); c != nil {
// data, err = buildMsg(printMsg)
// }
// case printerStatusOffline:
// err = fmt.Errorf("打印机离线!")
// case printerStatusOnlineWithoutPaper:
// err = fmt.Errorf("打印机缺纸!")
// default:
// err = fmt.Errorf("打印机状态未知!")
//fn := func() {
// if t.TimeoutMap[key] == true {
// select {
// case printMsg, ok := <-t.MsgMap[key]:
// if !ok {
// globals.SugarLogger.Debugf("doPrint err !ok ...")
// return
// }
// var (
// data []byte
// c net.Conn
// )
// if printMsg != nil {
// if err = checkPrintMsg(db, printMsg); err == nil {
// status := t.getPrintStatus(printMsg.PrintNo)
// switch status {
// //只有在线才打印内容
// case printerStatusOnline:
// if c = t.getPrintConn(printMsg.PrintNo); c != nil {
// data, err = buildMsg(printMsg)
// }
// case printerStatusOffline:
// err = fmt.Errorf("打印机离线!")
// case printerStatusOnlineWithoutPaper:
// err = fmt.Errorf("打印机缺纸!")
// default:
// err = fmt.Errorf("打印机状态未知!")
// }
// if err != nil {
// printMsg.Status = printMsgErr
// printMsg.Comment = err.Error()
// dao.UpdateEntity(db, printMsg, "Status", "Comment")
// delete(t.TimeoutMap, key)
// } else {
// if c != nil {
// if _, err = c.Write(data); err != nil {
// globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
// //close(t.TimeoutMap[key])
// delete(t.TimeoutMap, key)
// } else {
// //等待回调
// dataStr := <-t.CallBackMap[key]
// if dataStr != "" {
// a, b := getCallbackMsgInfo(dataStr)
// t.changePrintMsg(dataStr, a, b)
// // 查询打印机是否扣费,未扣费就扣费,已经扣费不做处理
// have, err := dao.QueryOrderDeductionRecord(db, b, utils.Int64ToStr(a))
// if err != nil && !have {
// // 扣除打印机账号金额
// if err = dao.DeductionPrintBalance(db, b); err != nil {
// globals.SugarLogger.Debugf("扣除用户打印机金额错误 %s", err)
// } else {
// // 添加打印记录(支出记录)
// if err = dao.AddPrintRecord(db, &model.PrintBillRecord{
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// PrintNo: b,
// PayType: 2,
// PayMoney: 1, // 固定支出一分钱
// OrderId: utils.Int64ToStr(a),
// UserId: "",
// }); err != nil {
// globals.SugarLogger.Debugf("添加打印机订单支付记录错误 %s", err)
// }
// }
// }
// if err != nil {
// printMsg.Status = printMsgErr
// printMsg.Comment = err.Error()
// dao.UpdateEntity(db, printMsg, "Status", "Comment")
// delete(t.TimeoutMap, key)
// } else {
// if c != nil {
// if _, err = c.Write(data); err != nil {
// globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
// //close(t.TimeoutMap[key])
// delete(t.TimeoutMap, key)
// } else {
// //等待回调
// dataStr := <-t.CallBackMap[key]
// if dataStr != "" {
// a, b := getCallbackMsgInfo(dataStr)
// t.changePrintMsg(dataStr, a, b)
// // 查询打印机是否扣费,未扣费就扣费,已经扣费不做处理
// have, err := dao.QueryOrderDeductionRecord(db, b, utils.Int64ToStr(a))
// if err != nil && !have {
// // 扣除打印机账号金额
// if err = dao.DeductionPrintBalance(db, b); err != nil {
// globals.SugarLogger.Debugf("扣除用户打印机金额错误 %s", err)
// } else {
// globals.SugarLogger.Debugf("查询打印机扣费记录错误 %s", err)
// }
// //判断音频暂停?
// //收到打印成功回调后,如果消息中有音频,需要等待一下,等上一个音频播完
// //暂停时间就暂时取的sound标签内内容长度/2
// if sounds := regexpSoundSpan.FindStringSubmatch(printMsg.Content); len(sounds) > 0 {
// sound := sounds[1]
// lenTime := time.Duration(utf8.RuneCountInString(sound)) * time.Second
// time.Sleep(lenTime / 2)
// // 添加打印记录(支出记录)
// if err = dao.AddPrintRecord(db, &model.PrintBillRecord{
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// PrintNo: b,
// PayType: 2,
// PayMoney: 1, // 固定支出一分钱
// OrderId: utils.Int64ToStr(a),
// UserId: "",
// }); err != nil {
// globals.SugarLogger.Debugf("添加打印机订单支付记录错误 %s", err)
// }
// }
// } else {
// globals.SugarLogger.Debugf("查询打印机扣费记录错误 %s", err)
// }
// //判断音频暂停?
// //收到打印成功回调后,如果消息中有音频,需要等待一下,等上一个音频播完
// //暂停时间就暂时取的sound标签内内容长度/2
// if sounds := regexpSoundSpan.FindStringSubmatch(printMsg.Content); len(sounds) > 0 {
// sound := sounds[1]
// lenTime := time.Duration(utf8.RuneCountInString(sound)) * time.Second
// time.Sleep(lenTime / 2)
// }
// }
// }
// }
// } else {
// globals.SugarLogger.Debugf("msgMap is nil")
// }
// } else {
// globals.SugarLogger.Debugf("msgMap is nil")
// }
// } else {
// globals.SugarLogger.Debugf("doPrint timeout")
// return
// }
//
// } else {
// globals.SugarLogger.Debugf("doPrint timeout")
// return
// }
//}()
//}
//
//Poll.AddJob(fn)
go func() {
for {
if t.TimeoutMap[key] == true {
select {
case printMsg, ok := <-t.MsgMap[key]:
if !ok {
globals.SugarLogger.Debugf("doPrint err !ok ...")
return
}
var (
data []byte
c net.Conn
)
if printMsg != nil {
if err = checkPrintMsg(db, printMsg); err == nil {
status := t.getPrintStatus(printMsg.PrintNo)
switch status {
//只有在线才打印内容
case printerStatusOnline:
if c = t.getPrintConn(printMsg.PrintNo); c != nil {
data, err = buildMsg(printMsg)
}
case printerStatusOffline:
err = fmt.Errorf("打印机离线!")
case printerStatusOnlineWithoutPaper:
err = fmt.Errorf("打印机缺纸!")
default:
err = fmt.Errorf("打印机状态未知!")
}
}
if err != nil {
printMsg.Status = printMsgErr
printMsg.Comment = err.Error()
dao.UpdateEntity(db, printMsg, "Status", "Comment")
delete(t.TimeoutMap, key)
} else {
if c != nil {
if _, err = c.Write(data); err != nil {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
//close(t.TimeoutMap[key])
delete(t.TimeoutMap, key)
} else {
//等待回调
dataStr := <-t.CallBackMap[key]
if dataStr != "" {
a, b := getCallbackMsgInfo(dataStr)
t.changePrintMsg(dataStr, a, b)
// 查询打印机是否扣费,未扣费就扣费,已经扣费不做处理
have, err := dao.QueryOrderDeductionRecord(db, b, utils.Int64ToStr(a))
if err != nil && !have {
// 扣除打印机账号金额
if err = dao.DeductionPrintBalance(db, b); err != nil {
globals.SugarLogger.Debugf("扣除用户打印机金额错误 %s", err)
} else {
// 添加打印记录(支出记录)
if err = dao.AddPrintRecord(db, &model.PrintBillRecord{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
PrintNo: b,
PayType: 2,
PayMoney: 1, // 固定支出一分钱
OrderId: utils.Int64ToStr(a),
UserId: "",
}); err != nil {
globals.SugarLogger.Debugf("添加打印机订单支付记录错误 %s", err)
}
}
} else {
globals.SugarLogger.Debugf("查询打印机扣费记录错误 %s", err)
}
//判断音频暂停?
//收到打印成功回调后,如果消息中有音频,需要等待一下,等上一个音频播完
//暂停时间就暂时取的sound标签内内容长度/2
if sounds := regexpSoundSpan.FindStringSubmatch(printMsg.Content); len(sounds) > 0 {
sound := sounds[1]
lenTime := time.Duration(utf8.RuneCountInString(sound)) * time.Second
time.Sleep(lenTime / 2)
}
}
}
}
}
} else {
globals.SugarLogger.Debugf("msgMap is nil")
}
}
} else {
globals.SugarLogger.Debugf("doPrint timeout")
return
}
}
}()
return err
}