514 lines
16 KiB
Go
514 lines
16 KiB
Go
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/model/dao"
|
||
"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, storeDetail *dao.StoreDetail) (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("<FS2>淘宝闪购取货码:%s</FS2>\\n\n", jxutils.GetEbaiOrderGetCode(order))
|
||
}
|
||
orderParams := []interface{}{}
|
||
orderFmt := ``
|
||
if storeDetail != nil {
|
||
if storeDetail.BrandIsPrint == model.NO {
|
||
orderFmt += `<FS2><center>%s</center></FS2>\n`
|
||
if order.VendorOrgCode == "34665" {
|
||
orderParams = append(orderParams, globals.StoreNameEbai2)
|
||
} else {
|
||
orderParams = append(orderParams, storeDetail.BrandName)
|
||
}
|
||
}
|
||
}
|
||
orderFmt += `
|
||
<FS2>%s#%d</FS2>\n\n
|
||
<FS>门店名称: %s</FS>\n
|
||
--------------------------------\n
|
||
下单时间: %s\n
|
||
预计送达: %s\n
|
||
订单编号: %s\n
|
||
`
|
||
|
||
if order.BusinessType == model.BusinessTypeDingshida {
|
||
orderFmt += `
|
||
<FS>预订单</FS>\n
|
||
`
|
||
} else {
|
||
orderFmt += `
|
||
<FS>立即送达</FS>\n
|
||
`
|
||
}
|
||
orderFmt += `
|
||
\n
|
||
<QR>%s</QR>
|
||
` + getCode + `\n
|
||
客户: %s\n
|
||
电话: %s\n
|
||
地址: %s\n
|
||
\n
|
||
客户备注: \n
|
||
<FS>%s</FS>\n
|
||
\n
|
||
\n
|
||
商品明细: \n`
|
||
orderFmt += `
|
||
品名 数量 小计\n
|
||
--------------------------------\n`
|
||
// quote := `
|
||
//品名 数量 \n
|
||
//--------------------------------\n`
|
||
//switch order.EarningType {
|
||
//case model.EarningTypePoints: // 扣点
|
||
// orderFmt += points
|
||
//case model.EarningTypeQuote: // 报价
|
||
// orderFmt += quote
|
||
//}
|
||
orderParams = append(orderParams,
|
||
jxutils.GetVendorName(order.VendorID),
|
||
order.OrderSeq,
|
||
order.StoreName,
|
||
utils.Time2Str(order.OrderCreatedAt),
|
||
utils.Time2Str(expectedDeliveryTime),
|
||
order.VendorOrderID,
|
||
order.VendorOrderID,
|
||
order.ConsigneeName,
|
||
order.ConsigneeMobile,
|
||
order.ConsigneeAddress,
|
||
order.BuyerComment,
|
||
)
|
||
|
||
for _, sku := range order.Skus {
|
||
orderFmt += `<FH><LR>%s,%s</LR></FH>\n`
|
||
orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count))
|
||
//switch order.EarningType {
|
||
//case model.EarningTypePoints: // 扣点
|
||
// orderFmt += `<FH>%s</FH>\n`
|
||
// orderFmt += `<FH>%20s%8s</FH>\n`
|
||
// orderParams = append(orderParams, sku.SkuName, "X"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.VendorPrice*int64(sku.Count)))
|
||
//case model.EarningTypeQuote: // 报价
|
||
// orderFmt += `<FH>%s%25s</FH>\n`
|
||
// orderParams = append(orderParams, sku.SkuName, "x"+utils.Int2Str(sku.Count))
|
||
//}
|
||
|
||
//标品需要打印条形码
|
||
if sku.Upc != "" {
|
||
orderFmt += `upc码: %s\n`
|
||
orderParams = append(orderParams, sku.Upc)
|
||
}
|
||
}
|
||
orderFmt += `\n
|
||
<FB>共%d种%d件商品</FB>\n
|
||
<FB>实际支付:</FB>%s\n
|
||
--------------------------------\n
|
||
<center><FB>商品质量问题请联系:</FB></center>
|
||
<center><FB>%s:%s</FB></center>\n
|
||
更多信息请关注官方微信: %s\n
|
||
--------------------------------\n
|
||
--------------------------------\n
|
||
`
|
||
// <QR>http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk</QR>
|
||
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice), 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, storeDetail *dao.StoreDetail) (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("<FS2>淘宝闪购取货码:%s</FS2>\\n\n", jxutils.GetEbaiOrderGetCode(order))
|
||
}
|
||
orderParams := []interface{}{}
|
||
orderFmt := ``
|
||
if storeDetail != nil {
|
||
if storeDetail.BrandIsPrint == model.NO {
|
||
orderFmt += `<FS2><center>%s</center></FS2>\n`
|
||
if order.VendorOrgCode == "34665" {
|
||
orderParams = append(orderParams, globals.StoreNameEbai2)
|
||
} else {
|
||
orderParams = append(orderParams, storeDetail.BrandName)
|
||
}
|
||
}
|
||
}
|
||
orderFmt += `
|
||
<FS2>%s#%d</FS2>\n\n
|
||
<FS>门店名称: %s\n\n</FS>
|
||
--------------------------------\n
|
||
<FS>下单时间: %s\n\n</FS>
|
||
<FS>预计送达: %s\n\n</FS>
|
||
<FS>订单编号: %s\n</FS>
|
||
\n`
|
||
if order.BusinessType == model.BusinessTypeDingshida {
|
||
orderFmt += `
|
||
<FS>预订单</FS>\n
|
||
`
|
||
} else {
|
||
orderFmt += `
|
||
<FS>立即送达</FS>\n
|
||
`
|
||
}
|
||
orderFmt += `<QR>%s</QR>
|
||
` + getCode + `\n
|
||
<FS>客户: %s\n</FS>
|
||
<FS>电话: %s\n</FS>
|
||
<FS>地址: %s\n</FS>
|
||
\n
|
||
<FS>客户备注: \n</FS>
|
||
<FS>%s</FS>\n
|
||
\n
|
||
\n
|
||
<FS>商品明细: \n</FS>`
|
||
|
||
orderFmt += `品名 小计\n
|
||
--------------------------------\n`
|
||
//switch order.EarningType {
|
||
//case model.EarningTypePoints: // 扣点
|
||
// orderFmt += points
|
||
//case model.EarningTypeQuote: // 报价
|
||
// orderFmt += quote
|
||
//}
|
||
|
||
orderParams = append(orderParams,
|
||
jxutils.GetVendorName(order.VendorID),
|
||
order.OrderSeq,
|
||
order.StoreName,
|
||
utils.Time2Str(order.OrderCreatedAt),
|
||
utils.Time2Str(expectedDeliveryTime),
|
||
order.VendorOrderID,
|
||
order.VendorOrderID,
|
||
order.ConsigneeName,
|
||
order.ConsigneeMobile,
|
||
order.ConsigneeAddress,
|
||
order.BuyerComment,
|
||
)
|
||
|
||
for _, sku := range order.Skus {
|
||
orderFmt += `<FH2><LR>%s,%s</LR></FH2>\n`
|
||
orderParams = append(orderParams, sku.SkuName, "X"+utils.Int2Str(sku.Count))
|
||
//switch order.EarningType {
|
||
//case model.EarningTypePoints: // 扣点
|
||
// orderFmt += `<FH2>%s\n</FH2>`
|
||
// orderFmt += `<FS><FB>%15s%8s</FB>\n</FS>`
|
||
// orderParams = append(orderParams, sku.SkuName, "X"+utils.Int2Str(sku.Count), jxutils.IntPrice2StandardCurrencyString(sku.VendorPrice*int64(sku.Count)))
|
||
//case model.EarningTypeQuote: // 报价
|
||
// orderFmt += `<FH2>%s%25s\n</FH2>`
|
||
// orderParams = append(orderParams, sku.SkuName, "X"+utils.Int2Str(sku.Count))
|
||
//}
|
||
|
||
//标品需要打印条形码
|
||
if sku.Upc != "" {
|
||
orderFmt += `<FS>upc码: %s\n</FS>`
|
||
orderParams = append(orderParams, sku.Upc)
|
||
}
|
||
}
|
||
orderFmt += `\n
|
||
<FS><FB>共%d种%d件商品</FB></FS>\n
|
||
<FS2><FB>实际支付:</FB>%s\n</FS2>
|
||
--------------------------------\n
|
||
<center><FB>商品质量问题请联系:</FB></center>\n
|
||
<center><FB>%s:%s</FB></center>\n
|
||
<FS>更多信息请关注官方微信: %s\n</FS>
|
||
--------------------------------\n
|
||
--------------------------------\n
|
||
`
|
||
// <QR>http://weixin.qq.com/r/tkkDGzTERmk5rXB49xyk</QR>
|
||
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice), order.StoreName, storeTel, globals.StoreName)
|
||
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
|
||
}
|
||
|
||
// 打印取消或者退款订单
|
||
func (c *PrinterHandler) getCancelOrRefundOrderContent(order *model.GoodsOrder, printType int, storeDetail *dao.StoreDetail) (content string) {
|
||
expectedDeliveryTime := order.ExpectedDeliveredTime
|
||
if utils.IsTimeZero(expectedDeliveryTime) {
|
||
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
|
||
}
|
||
var orderParams []interface{}
|
||
orderFmt := ``
|
||
if storeDetail != nil {
|
||
if storeDetail.BrandIsPrint == model.NO {
|
||
orderFmt += `<FS2><center>%s</center></FS2>\n`
|
||
if order.VendorOrgCode == "34665" {
|
||
orderParams = append(orderParams, globals.StoreNameEbai2)
|
||
} else {
|
||
orderParams = append(orderParams, storeDetail.BrandName)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 加载用户取消订单售后单
|
||
|
||
title := ``
|
||
if printType == model.YES { // 取消订单
|
||
title = `<FS2><center>取消订单详情</center></FS2>\n`
|
||
} else { // 售后订单
|
||
title = `<FS2><center>售后订单详情</center></FS2>\n`
|
||
}
|
||
|
||
orderFmt +=
|
||
`--------------------------------\n
|
||
<FS>门店名称: %s\n\n</FS>
|
||
下单时间: %s\n
|
||
订单编号: %s\n
|
||
`
|
||
|
||
orderFmt += `
|
||
\n
|
||
<FS>%s <FS2>#%d</FS2></FS>\n\n
|
||
\n
|
||
` + title + `
|
||
--------------------------------\n`
|
||
orderParams = append(orderParams,
|
||
order.StoreName,
|
||
utils.Time2Str(order.OrderCreatedAt),
|
||
order.VendorOrderID,
|
||
jxutils.GetVendorName(order.VendorID),
|
||
order.OrderSeq,
|
||
)
|
||
orderFmt += `
|
||
--------------------------------\n
|
||
`
|
||
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice))
|
||
return strings.Replace(fmt.Sprintf(strings.Replace(orderFmt, "\n", "", -1), orderParams...), "\\n", "\r\n", -1)
|
||
}
|
||
|
||
// 打印取消或者退款订单,大字体
|
||
func (c *PrinterHandler) getCancelOrRefundOrderContentBig(order *model.GoodsOrder, printType int, storeDetail *dao.StoreDetail) (content string) {
|
||
expectedDeliveryTime := order.ExpectedDeliveredTime
|
||
if utils.IsTimeZero(expectedDeliveryTime) {
|
||
expectedDeliveryTime = order.OrderCreatedAt.Add(1 * time.Hour)
|
||
}
|
||
var orderParams []interface{}
|
||
orderFmt := ``
|
||
if storeDetail != nil {
|
||
if storeDetail.BrandIsPrint == model.NO {
|
||
orderFmt += `<FS2><center>%s</center></FS2>\n`
|
||
if order.VendorOrgCode == "34665" {
|
||
orderParams = append(orderParams, globals.StoreNameEbai2)
|
||
} else {
|
||
orderParams = append(orderParams, storeDetail.BrandName)
|
||
}
|
||
}
|
||
}
|
||
title := ``
|
||
if printType == model.YES { // 退单
|
||
title = `<FS2><center>取消订单详情</center></FS2>\n`
|
||
} else {
|
||
title = `<FS2><center>售后订单详情</center></FS2>\n`
|
||
}
|
||
orderFmt += `--------------------------------\n
|
||
<FS>门店名称: %s\n\n</FS>
|
||
<FS>下单时间: %s\n\n</FS>
|
||
<FS>订单编号: %s\n</FS>
|
||
\n
|
||
<FS>%s <FS2>#%d</FS2></FS>\n\n
|
||
\n
|
||
` + title + `
|
||
--------------------------------\n`
|
||
orderParams = append(orderParams,
|
||
order.StoreName,
|
||
utils.Time2Str(order.OrderCreatedAt),
|
||
order.VendorOrderID,
|
||
jxutils.GetVendorName(order.VendorID),
|
||
order.OrderSeq,
|
||
//order.VendorOrderID,
|
||
)
|
||
|
||
orderFmt += `
|
||
--------------------------------\n
|
||
`
|
||
orderParams = append(orderParams, order.SkuCount, order.GoodsCount, jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice))
|
||
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) {
|
||
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, storeDetail *dao.StoreDetail, order *model.GoodsOrder, printType string, asfOrder *model.AfsOrder) (printerStatus *partner.PrinterStatus, err error) {
|
||
if len(order.Skus) == 0 {
|
||
return
|
||
}
|
||
content := ""
|
||
if store.PrinterFontSize == partner.PrinterFontSizeBig || store.PrinterFontSize == partner.PrinterFontSizeBig2 {
|
||
content = c.getOrderContentBig(order, store.Tel1, storeDetail)
|
||
} else {
|
||
content = c.getOrderContent(order, store.Tel1, storeDetail)
|
||
}
|
||
|
||
var count = 0
|
||
for {
|
||
printerStatus, err := c.PrintMsg(ctx, store.PrinterSN, store.PrinterKey, order.VendorOrderID, content)
|
||
if (store.PrinterFontSize == partner.PrinterFontSizeBig2 || store.PrinterFontSize == partner.PrinterFontSizeNormal2) && count < 1 {
|
||
count = count + 1
|
||
continue
|
||
} else {
|
||
return printerStatus, err
|
||
}
|
||
}
|
||
}
|
||
|
||
func (c *PrinterHandler) PrintStore(ctx *jxcontext.Context, store *model.Store, storeDetail *dao.StoreDetail, vendorId int) (printerStatus *partner.PrinterStatus, err error) {
|
||
return nil, err
|
||
}
|
||
|
||
func (c *PrinterHandler) PrintCancelOrRefundOrder(ctx *jxcontext.Context, printType int, store *model.Store, storeDetail *dao.StoreDetail, order *model.GoodsOrder) (printerStatus *partner.PrinterStatus, err error) {
|
||
if len(order.Skus) == 0 {
|
||
return
|
||
}
|
||
content := ""
|
||
if store.PrinterFontSize == partner.PrinterFontSizeBig || store.PrinterFontSize == partner.PrinterFontSizeBig2 {
|
||
content = c.getCancelOrRefundOrderContent(order, printType, storeDetail)
|
||
} else {
|
||
content = c.getCancelOrRefundOrderContentBig(order, printType, storeDetail)
|
||
}
|
||
|
||
if content == "" {
|
||
return nil, nil
|
||
}
|
||
|
||
return c.PrintMsg(ctx, store.PrinterSN, store.PrinterKey, order.VendorOrderID, content)
|
||
}
|
||
|
||
func (c *PrinterHandler) RegisterPrinter(ctx *jxcontext.Context, machineCode, secret, printerName string, storeID int64) (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"])
|
||
msign := utils.Interface2String(mapData["msign"])
|
||
if machineCode == "" || (qrKey == "" && msign == "") {
|
||
return nil, fmt.Errorf("易联云扫描数据格式不正确")
|
||
}
|
||
|
||
tokenInfo, err := api.YilianyunAPI2.GetPrinterToken(machineCode, qrKey, msign)
|
||
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
|
||
}
|