161 lines
4.9 KiB
Go
161 lines
4.9 KiB
Go
package zhongwu
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/platformapi/zhongwuapi"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/partner"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"git.rosy.net.cn/jx-callback/globals/api"
|
|
)
|
|
|
|
var (
|
|
CurPrinterHandler *PrinterHandler
|
|
)
|
|
|
|
type PrinterHandler struct {
|
|
}
|
|
|
|
func init() {
|
|
CurPrinterHandler = new(PrinterHandler)
|
|
partner.RegisterPrinterPlatform(CurPrinterHandler)
|
|
}
|
|
|
|
func (c *PrinterHandler) getOrderContent(order *model.GoodsOrder, storeTel string) (content string) {
|
|
expectedDeliveryTime := order.ExpectedDeliveredTime
|
|
if utils.IsTimeZero(expectedDeliveryTime) {
|
|
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
|
|
}
|
|
orderFmt := `
|
|
<CB>京西菜市</CB><BR>
|
|
<C>手机买菜上京西</C><BR>
|
|
<C>极速到家送惊喜</C><BR>
|
|
--------------------------------<BR>
|
|
下单时间: %s<BR>
|
|
预计送达: %s<BR>
|
|
订单编号: %s<BR>
|
|
<BR>
|
|
|
|
<B>%s#%d</B><BR><BR>
|
|
<QR>%s</QR>
|
|
<BR>
|
|
客户: %s<BR>
|
|
电话: %s<BR>
|
|
地址: %s<BR>
|
|
<BR>
|
|
客户备注: <BR>
|
|
<B>%s</B><BR>
|
|
<BR>
|
|
|
|
<BOLD>实际支付:</BOLD>%s<BR>
|
|
<BR>
|
|
商品明细: <BR>
|
|
品名 数量 单价 小计<BR>
|
|
--------------------------------<BR>`
|
|
orderParams := []interface{}{
|
|
utils.Time2Str(order.OrderCreatedAt),
|
|
utils.Time2Str(expectedDeliveryTime),
|
|
order.VendorOrderID,
|
|
jxutils.GetVendorName(order.VendorID),
|
|
order.OrderSeq,
|
|
order.VendorOrderID,
|
|
order.ConsigneeName,
|
|
order.ConsigneeMobile,
|
|
order.ConsigneeAddress,
|
|
order.BuyerComment,
|
|
jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice),
|
|
}
|
|
|
|
for _, sku := range order.Skus {
|
|
orderFmt += `%s<BR>`
|
|
orderFmt += `%10s%10s%10s<BR>`
|
|
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice*int64(sku.Count)))
|
|
}
|
|
orderFmt += `<BR>
|
|
<BOLD>共%d种%d件商品</BOLD>
|
|
<BR>
|
|
--------------------------------<BR>
|
|
<C><L><BOLD>商品质量问题请联系:</BOLD></L><BR></C>
|
|
<C><L><BOLD>%s:%s</BOLD></L><BR></C><BR>
|
|
<BR>
|
|
官方服务热线: 18011516898<BR>
|
|
更多信息请关注官方微信: 京西菜市<BR>
|
|
<BR>
|
|
<BR><BR>
|
|
--------------------------------<BR>
|
|
--------------------------------<BR>
|
|
<BR><BR>
|
|
`
|
|
// <QR>http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk</QR>
|
|
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, order.StoreName, storeTel)
|
|
return fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...)
|
|
}
|
|
|
|
func (c *PrinterHandler) GetVendorID() int {
|
|
return model.VendorIDZhongWu
|
|
}
|
|
|
|
func (c *PrinterHandler) PrintMsg(ctx *jxcontext.Context, deviceID, deviceSecret, msgTitle, msgContent string) (printerStatus *partner.PrinterStatus, err error) {
|
|
globals.SugarLogger.Debugf("PrintMsg deviceID:%s", deviceID)
|
|
if deviceID != "" {
|
|
var status int
|
|
_, status, err = api.ZhongwuAPI.PrintMsg(deviceID, deviceSecret, msgContent)
|
|
if err == nil {
|
|
printerStatus = c.translateStatus(status)
|
|
}
|
|
} else {
|
|
printerStatus = &partner.PrinterStatus{
|
|
PrintResult: partner.PrintResultNoPrinter,
|
|
}
|
|
}
|
|
return printerStatus, err
|
|
}
|
|
|
|
func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, deviceID, deviceSecret string) (printerStatus *partner.PrinterStatus, err error) {
|
|
status, err := api.ZhongwuAPI.GetPrinterStatus(deviceID, deviceSecret)
|
|
if err == nil {
|
|
printerStatus = c.translateStatus(status)
|
|
}
|
|
return printerStatus, err
|
|
}
|
|
|
|
func (c *PrinterHandler) PrintOrder(ctx *jxcontext.Context, store *model.Store, order *model.GoodsOrder) (printerStatus *partner.PrinterStatus, err error) {
|
|
globals.SugarLogger.Debugf("zhongwu PrintOrderByOrder orderID:%s", order.VendorOrderID)
|
|
content := c.getOrderContent(order, store.Tel1)
|
|
return c.PrintMsg(ctx, store.PrinterSN, store.PrinterKey, order.VendorOrderID, content)
|
|
}
|
|
|
|
func (c *PrinterHandler) RegisterPrinter(ctx *jxcontext.Context, deviceID, deviceSecret, printerName string) (notUsed1, notUsed2 string, err error) {
|
|
if deviceID == "" || deviceSecret == "" {
|
|
err = fmt.Errorf("打印机ID与打印机密钥都不能为空")
|
|
}
|
|
// 中午云不需要注册
|
|
return "", "", err
|
|
}
|
|
|
|
func (c *PrinterHandler) UnregisterPrinter(ctx *jxcontext.Context, deviceID, deviceSecret string) (err error) {
|
|
// 中午云不需要注册
|
|
return err
|
|
}
|
|
|
|
func (c *PrinterHandler) translateStatus(status int) (printerStatus *partner.PrinterStatus) {
|
|
printerStatus = &partner.PrinterStatus{
|
|
PrintResult: partner.PrintResultNoPrinter,
|
|
}
|
|
if status == zhongwuapi.PrinterStatusOnline {
|
|
printerStatus.PrinterStatus = partner.PrinterStatusOnlineOK
|
|
} else if status == zhongwuapi.PrinterStatusLackPaper {
|
|
printerStatus.PrinterStatus = partner.PrinterStatusOnlineAbnormal
|
|
} else {
|
|
printerStatus.PrinterStatus = partner.PrinterStatusOffline
|
|
}
|
|
return printerStatus
|
|
}
|