diff --git a/business/jxstore/cms/print.go b/business/jxstore/cms/print.go index f5ac907d6..c1cd94f93 100644 --- a/business/jxstore/cms/print.go +++ b/business/jxstore/cms/print.go @@ -20,7 +20,7 @@ func AddPrinter(appID int, printNo, printKey, name string) (err error) { PrintNo: printNo, PrintKey: printKey, Name: name, - Status: model.PrinterStatusNormal, + Status: model.PrinterStatusOffline, } dao.WrapAddIDCULDEntity(printer, "") if err = dao.CreateEntity(db, printer); err != nil { @@ -68,7 +68,7 @@ func DelPrinterSeq(appID int, printNo string) (err error) { if printers, _ := dao.GetPrinters(db, appID, printNo); len(printers) == 0 { return fmt.Errorf("该应用下未找到该打印机!print_no : %v", printNo) } else { - printMsgs, _ := dao.GetPrintMsgs(db, printNo) + printMsgs, _ := dao.GetPrintMsgs(db, printNo, "", model.PrintMsgWait) for _, v := range printMsgs { v.DeletedAt = time.Now() if _, err = dao.UpdateEntity(db, v, "DeletedAt"); err != nil { @@ -78,3 +78,35 @@ func DelPrinterSeq(appID int, printNo string) (err error) { } return err } + +func DoPrint(appID int, msgID, printNo, content string, orderNo int) (err error) { + var ( + db = dao.GetDB() + ) + + printMsg := &model.PrintMsg{ + PrintNo: printNo, + Content: content, + OrderNo: orderNo, + MsgID: msgID, + } + dao.WrapAddIDCULDEntity(printMsg, "") + if err = dao.CreateEntity(db, printMsg); err != nil { + return err + } + return err +} + +func GetPrintMsg(appID int, msgID string) (printMsg *model.PrintMsg, err error) { + var ( + db = dao.GetDB() + ) + if printMsgs, _ := dao.GetPrintMsgs(db, "", msgID, model.PrintMsgAll); len(printMsgs) > 0 { + for _, v := range printMsgs { + return v, err + } + } else { + return printMsg, fmt.Errorf("未找到该消息!msg_id :%v", msgID) + } + return printMsg, err +} diff --git a/business/model/dao/dao_print.go b/business/model/dao/dao_print.go index 3ddbf03f6..0b99231fc 100644 --- a/business/model/dao/dao_print.go +++ b/business/model/dao/dao_print.go @@ -26,19 +26,27 @@ func GetPrinters(db *DaoDB, appID int, printNo string) (printers []*model.Printe return printers, err } -func GetPrintMsgs(db *DaoDB, printNo string) (printMsgs []*model.PrintMsg, err error) { +func GetPrintMsgs(db *DaoDB, printNo, msgID string, status int) (printMsgs []*model.PrintMsg, err error) { sql := ` SELECT * FROM print_msg - WHERE 1 = 1 AND deleted_at = ? AND status = ? + WHERE 1 = 1 AND deleted_at = ? ` sqlParams := []interface{}{ - utils.DefaultTimeValue, model.PrinterStatusNormal, + utils.DefaultTimeValue, + } + if status != model.PrintMsgAll { + sql += " AND status = ?" + sqlParams = append(sqlParams, status) } if printNo != "" { sql += " AND print_no = ?" sqlParams = append(sqlParams, printNo) } + if msgID != "" { + sql += " AND msg_id = ?" + sqlParams = append(sqlParams, msgID) + } err = GetRows(db, &printMsgs, sql, sqlParams) return printMsgs, err } diff --git a/business/model/print.go b/business/model/print.go index bee1d14f6..667f7cb01 100644 --- a/business/model/print.go +++ b/business/model/print.go @@ -4,6 +4,13 @@ const ( PrinterStatusOnlineWithoutPaper = 2 //在线正常缺纸 PrinterStatusOnline = 1 //在线正常 PrinterStatusOffline = -1 //离线 + + PrintMsgAlreadySend = 2 //已经发出打印消息 + PrintMsgSuccess = 1 //打印成功 + PrintMsgWait = 0 //待打印 + PrintMsgFail = -1 //打印失败(打印机报出) + PrintMsgErr = -2 //京西报出 + PrintMsgAll = -9 ) type Printer struct { diff --git a/controllers/api_controller.go b/controllers/api_controller.go index 7b3e04302..e2ea1c298 100644 --- a/controllers/api_controller.go +++ b/controllers/api_controller.go @@ -2,6 +2,7 @@ package controllers import ( "crypto/md5" + "encoding/json" "fmt" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxstore/cms" @@ -33,6 +34,8 @@ const ( keyName = "name" keyStatus = "status" keyOrderNo = "order_no" + keyContent = "content" + keyMsgID = "msg_id" ) var ( @@ -273,27 +276,67 @@ func (c *ApiController) DelPrinterSeq(dataMap map[string]interface{}) (data, err return "", errCode, err } +//发送打印消息 func (c *ApiController) DoPrint(dataMap map[string]interface{}) (data, errCode string, err error) { var ( - content = dataMap["data"].(string) - printNo = dataMap["print_no"].(string) - orderNo = utils.Interface2Int64WithDefault(dataMap["order_no"], 0) - db = dao.GetDB() + printNo, content string + orderNo, appID int ) globals.SugarLogger.Debugf("Begin API DoPrint data: [%v]", utils.Format4Output(dataMap, false)) - if orderNo == 0 { - return data, model.ErrCodeOpenAPIParamErrNormal, fmt.Errorf("参数错误:order_no 不能为空") + if _, ok := dataMap[keyPrintNo].(string); !ok { + return buildParamErrCodeAndErr(keyPrintNo) + } else { + if printNo = dataMap[keyPrintNo].(string); printNo == "" { + return buildParamErrCodeAndErr(keyPrintNo) + } } + if _, ok := dataMap[keyContent].(string); !ok { + return buildParamErrCodeAndErr(keyContent) + } else { + if content = dataMap[keyContent].(string); content == "" { + return buildParamErrCodeAndErr(keyContent) + } + } + if _, ok := dataMap[keyOrderNo].(int); !ok { + return buildParamErrCodeAndErr(keyOrderNo) + } else { + if orderNo = dataMap[keyOrderNo].(int); orderNo == 0 { + return buildParamErrCodeAndErr(keyOrderNo) + } + } + appID = dataMap[keyAppID].(int) msgID := time.Now().Format("20060102150405") + "_" + jxutils.RandStringBytes(8) - printMsg := &model.PrintMsg{ - PrintNo: printNo, - Content: content, - OrderNo: int(orderNo), - MsgID: msgID, - } - dao.WrapAddIDCULDEntity(printMsg, "") - if err = dao.CreateEntity(db, printMsg); err != nil { - return data, model.ErrCodeGeneralFailed, err + if err = cms.DoPrint(appID, msgID, content, printNo, orderNo); err != nil { + return "", model.ErrCodeGeneralFailed, err } return msgID, errCode, err } + +//获取某打印消息 +func (c *ApiController) GetPrintMsg(dataMap map[string]interface{}) (data, errCode string, err error) { + var ( + msgID string + appID int + ) + globals.SugarLogger.Debugf("Begin API GetPrintMsg data: [%v]", utils.Format4Output(dataMap, false)) + if _, ok := dataMap[keyMsgID].(string); !ok { + return buildParamErrCodeAndErr(keyMsgID) + } else { + if msgID = dataMap[keyMsgID].(string); msgID == "" { + return buildParamErrCodeAndErr(keyMsgID) + } + } + appID = dataMap[keyAppID].(int) + if printMsg, err := cms.GetPrintMsg(appID, msgID); err != nil { + return "", model.ErrCodeGeneralFailed, err + } else if printMsg == nil { + return "", model.ErrCodeGeneralFailed, fmt.Errorf("未查询到该消息! msg_id: %v", msgID) + } else { + if byteData, err := json.Marshal(printMsg); err == nil { + data = string(byteData) + } else { + return "", model.ErrCodeGeneralFailed, err + } + } + return data, errCode, err +}