package yilianyun
import (
"fmt"
"strings"
"time"
"git.rosy.net.cn/baseapi/platformapi/yilianyunapi"
"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)
}
getCode := ""
if order.VendorID == model.VendorIDEBAI {
getCode = fmt.Sprintf("饿百取货码:%s\\n\n", jxutils.GetEbaiOrderGetCode(order))
}
//TODO 去掉单价和小计,2020-06-18
orderFmt := `
%s\n\n
手机买菜上京西
极速到家送惊喜\n
--------------------------------
下单时间: %s\n
预计送达: %s\n
订单编号: %s\n
\n
%s#%d\n\n
%s
` + getCode + `\n
客户: %s\n
电话: %s\n
地址: %s\n
\n
客户备注: \n
%s\n
\n
\n
商品明细: \n
品名 数量 单价 小计\n
--------------------------------\n`
// 实际支付:%s\n
orderParams := []interface{}{
globals.StoreName,
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\n`
orderFmt += `%8s%10s%10s\n`
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice*int64(sku.Count)))
}
orderFmt += `\n
共%d种%d件商品
\n
--------------------------------\n
商品质量问题请联系:
%s:%s\n
更多信息请关注官方微信: %s\n
--------------------------------\n
--------------------------------\n
`
// http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, order.StoreName, storeTel, globals.StoreName)
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
}
func (c *PrinterHandler) getOrderContentBig(order *model.GoodsOrder, storeTel string) (content string) {
expectedDeliveryTime := order.ExpectedDeliveredTime
if utils.IsTimeZero(expectedDeliveryTime) {
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
}
getCode := ""
if order.VendorID == model.VendorIDEBAI {
getCode = fmt.Sprintf("饿百取货码:%s\\n\n", jxutils.GetEbaiOrderGetCode(order))
}
orderFmt := `
%s\n\n
手机买菜上京西
极速到家送惊喜\n
--------------------------------
下单时间: %s\n\n
预计送达: %s\n\n
订单编号: %s\n
\n
%s#%d\n\n
%s
` + getCode + `\n
客户: %s\n
电话: %s\n
地址: %s\n
\n
客户备注: \n
%s\n
\n
\n
商品明细: \n
品名数量单价小计\n
--------------------------------\n`
// 实际支付:%s\n
orderParams := []interface{}{
globals.StoreName,
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\n`
orderFmt += `%s %s %s\n\n`
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice), jxutils.IntPrice2StandardCurrencyString(sku.SalePrice*int64(sku.Count)))
}
orderFmt += `\n
共%d种%d件商品
\n
--------------------------------\n
商品质量问题请联系:
%s:%s\n
更多信息请关注官方微信: %s\n
--------------------------------\n
--------------------------------\n
`
// http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, order.StoreName, storeTel, globals.StoreName)
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
}
func (c *PrinterHandler) GetVendorID() int {
return model.VendorIDYiLianYun
}
func (c *PrinterHandler) PrintMsg(ctx *jxcontext.Context, machineCode, possibleToken, msgTitle, msgContent string) (printerStatus *partner.PrinterStatus, err error) {
globals.SugarLogger.Debugf("PrintMsg machineCode:%s", machineCode)
if machineCode != "" {
if globals.EnableStoreWrite {
err = getApiByToken(possibleToken).PrintMsgWithToken(machineCode, msgTitle, msgContent, possibleToken)
}
if err == nil {
printerStatus, err = c.GetPrinterStatus(ctx, machineCode, possibleToken)
}
} else {
printerStatus = &partner.PrinterStatus{
PrintResult: partner.PrintResultNoPrinter,
}
}
return printerStatus, err
}
func (c *PrinterHandler) GetPrinterStatus(ctx *jxcontext.Context, machineCode, possibleToken string) (printerStatus *partner.PrinterStatus, err error) {
status, err := getApiByToken(possibleToken).GetPrintStatusWithToken(machineCode, possibleToken)
if err == nil {
printerStatus = &partner.PrinterStatus{
PrintResult: partner.PrintResultSuccess,
}
if status == yilianyunapi.PrintStatusOffline {
printerStatus.PrinterStatus = partner.PrinterStatusOffline
} else if status == yilianyunapi.PrintStatusOnline {
printerStatus.PrinterStatus = partner.PrinterStatusOnlineOK
} else {
printerStatus.PrinterStatus = partner.PrinterStatusOnlineAbnormal
}
}
return printerStatus, err
}
func (c *PrinterHandler) PrintOrder(ctx *jxcontext.Context, store *model.Store, order *model.GoodsOrder) (printerStatus *partner.PrinterStatus, err error) {
globals.SugarLogger.Debugf("yilianyun PrintOrderByOrder orderID:%s, storeID:%d", order.VendorOrderID, store.ID)
content := ""
if store.PrinterFontSize == partner.PrinterFontSizeBig {
content = c.getOrderContentBig(order, store.Tel1)
} else {
content = c.getOrderContent(order, store.Tel1)
}
return c.PrintMsg(ctx, store.PrinterSN, store.PrinterKey, order.VendorOrderID, content)
}
func (c *PrinterHandler) RegisterPrinter(ctx *jxcontext.Context, machineCode, secret, printerName string) (notUsed1, notUsed2 string, err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.AddPrinter(machineCode, secret, printerName)
}
return "", "", err
}
func (c *PrinterHandler) UnregisterPrinter(ctx *jxcontext.Context, machineCode, notUsed string) (err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.DeletePrinter(machineCode)
}
return err
}
func (c *PrinterHandler) BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *partner.BindPrinterResult, err error) {
machineCode := utils.Interface2String(mapData["machineCode"])
qrKey := utils.Interface2String(mapData["qrKey"])
if machineCode == "" || qrKey == "" {
return nil, fmt.Errorf("易联云扫描数据格式不正确")
}
tokenInfo, err := api.YilianyunAPI2.GetPrinterToken(machineCode, qrKey)
if err != nil {
return nil, err
}
return yilianyunToken2BindResult(tokenInfo), nil
}
func (c *PrinterHandler) RebindPrinter(ctx *jxcontext.Context, lastBindResult *partner.BindPrinterResult) (bindResult *partner.BindPrinterResult, err error) {
var tokenInfo *yilianyunapi.TokenInfo
if globals.EnableStoreWrite {
tokenInfo, err = api.YilianyunAPI2.RefreshToken(lastBindResult.PrinterKey2)
} else {
tokenInfo = &yilianyunapi.TokenInfo{}
}
if err == nil {
bindResult = yilianyunToken2BindResult(tokenInfo)
}
return bindResult, err
}
func yilianyunToken2BindResult(tokenInfo *yilianyunapi.TokenInfo) (bindResult *partner.BindPrinterResult) {
return &partner.BindPrinterResult{
PrinterSN: tokenInfo.MachineCode,
PrinterKey: tokenInfo.AccessToken,
PrinterKey2: tokenInfo.RefreshToken,
ExpiresAt: time.Now().Add(time.Duration(tokenInfo.ExpiresIn) * time.Second).Unix(),
}
}
func getApiByToken(possibleToken string) *yilianyunapi.API {
if yilianyunapi.IsStrToken(possibleToken) {
return api.YilianyunAPI2
}
return api.YilianyunAPI
}
func (c *PrinterHandler) EmptyPrintList(ctx *jxcontext.Context, id1, id2 string) (err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.CancelAll(id1, id2)
}
return err
}
func (c *PrinterHandler) PlayText(ctx *jxcontext.Context, id1, id2, orderID, text string) (printerStatus *partner.PrinterStatus, err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.PlayText(id1, orderID, text, id2)
}
return nil, err
}
func (c *PrinterHandler) SetSound(ctx *jxcontext.Context, id1, id2, sound string) (err error) {
if globals.EnableStoreWrite {
err = api.YilianyunAPI.SetSound(id1, sound)
}
return err
}