package jxprint

import (
	"encoding/json"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"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"
	"time"
)

const (
	EBailOrderNo     = "eBaiOrderNo"      // 品牌名称
	BusinessType     = "businessType"     // 是否为预定单
	PayOrderTime     = "payOrderTime"     // 下单时间
	TrySendTime      = "trySendTime"      // 预计送达时间
	OrderNo          = "orderNo"          // 订单编号
	VendorName       = "vendorName"       // 订单来源平台名称
	VendorID         = "vendorID"         // 订单来源平台id
	VendorOrderNo    = "vendorOrderNo"    // 订单序号1/2/
	EBaiCode         = "eBailCode"        // 饿百取货码
	QRCOrder         = "qrcOrder"         // 订单二维码单号，还是订单Id
	ConsigneeName    = "consigneeName"    // 客户名称
	ConsigneeMobile  = "consigneeMobile"  // 客户电话
	ConsigneeAddress = "consigneeAddress" // 客户地址
	BuyerComment     = "buyerComment"     // 客户备注
	SkuList          = "skuList"          // 商品列表
	SkuName          = "skuName"          // 商品名称
	SkuCount         = "skuCount"         // 商品件数
	SkuOnePrice      = "skuOnePrice"      // 商品单价
	SkuAllPrice      = "skuAllPrice"      // 商品总价 = 商品件数 x 商品件数
	AllSkuTypeCount  = "allSkuTypeCount"  // 商品种类
	AllSkuCount      = "allSkuCount"      // 商品总数量
	UserPayMoney     = "userPayMoney"     // 用户支付
	StoreName        = "storeName"        // 门店名称
	StoreTel         = "storeTel"         // 门店电话
	OfficialName     = "officialName"     // 官方名称
	BigFont          = "bigFont"          // 是否为大字体
	PrintNumber      = "printNumber"      // 打印次数
	AppID            = "appId"            // 应用id
	// 状态对应打印设置的变化
	OrderStatus    = "orderStatus"    // 订单状态
	WayBillStatus  = "wayBillStatus"  // 运单状态
	StoreStatus    = "storeStatus"    // 门店状态
	ReminderStatus = "reminderStatus" // 催单状态
	AfsOrderStatus = "afsOrderStatus" // 售后

	RiderVendorId        = "riderVendorId"        // 骑手所属平台id
	RiderName            = "riderName"            // 骑手名称
	RiderPhone           = "riderPhone"           // 骑手电话
	RejectionReason      = "rejectionReason"      // 拒收原因
	CustcareRefundReason = "custcareRefundReason" // 客服退款理由
	EnterTheStore        = "enterTheStore"        // 催单
)

type SkuListPrintOrder struct {
	SkuName         string `json:"skuName"`
	SkuCount        string `json:"skuCount"`
	SalePrice       string `json:"salePrice"`       // 单价
	TotalCountPrice string `json:"totalCountPrice"` // 总价
	Upc             string `json:"upc"`             // 条形码
}

// ManagerOrderPrint 京西云参数组装(订单参数)
func ManagerOrderPrint(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail, printType string, afsOrder *model.AfsOrder) map[string]string {
	printOrder := make(map[string]string, 0)
	skuList := make([]*SkuListPrintOrder, 0, 0)
	// 获取品牌名称
	if storeDetail != nil && storeDetail.BrandIsPrint == model.NO {
		if order.VendorOrgCode == "34665" {
			printOrder[EBailOrderNo] = globals.StoreNameEbai2
		} else {
			printOrder[EBailOrderNo] = storeDetail.BrandName
		}
	}
	// 是否为预定单
	if order.BusinessType == model.BusinessTypeDingshida {
		printOrder[BusinessType] = utils.Int2Str(model.BusinessTypeDingshida)
	} else {
		printOrder[BusinessType] = utils.Int2Str(model.BusinessTypeImmediate)
	}

	printOrder[PayOrderTime] = utils.Time2Str(order.OrderCreatedAt)
	printOrder[TrySendTime] = utils.Time2Str(order.ExpectedDeliveredTime)
	if order.ExpectedDeliveredTime.Unix() == utils.DefaultTimeValue.Unix() {
		printOrder[TrySendTime] = utils.Time2Str(order.OrderCreatedAt.Add(time.Hour * 1))
	}
	printOrder[OrderNo] = order.VendorOrderID
	printOrder[VendorID] = utils.Int2Str(order.VendorID)
	printOrder[VendorName] = jxutils.GetVendorName(order.VendorID)
	printOrder[VendorOrderNo] = utils.Int2Str(order.OrderSeq)
	printOrder[EBaiCode] = jxutils.GetEbaiOrderGetCode(order)
	printOrder[QRCOrder] = order.VendorOrderID
	printOrder[ConsigneeName] = order.ConsigneeName
	printOrder[ConsigneeMobile] = order.ConsigneeMobile
	printOrder[ConsigneeAddress] = order.ConsigneeAddress
	printOrder[BuyerComment] = order.BuyerComment
	// 商品列表
	for _, sku := range order.Skus {
		skuList = append(skuList, &SkuListPrintOrder{
			SkuName:         sku.SkuName,
			SkuCount:        "X" + utils.Int2Str(sku.Count),
			SalePrice:       jxutils.IntPrice2StandardCurrencyString(sku.VendorPrice),
			TotalCountPrice: jxutils.IntPrice2StandardCurrencyString(sku.VendorPrice * int64(sku.Count)),
			Upc:             sku.Upc,
		})
	}
	skuListByte, _ := json.Marshal(skuList)
	printOrder[SkuList] = string(skuListByte)
	printOrder[AllSkuTypeCount] = utils.Int2Str(order.SkuCount)
	printOrder[AllSkuCount] = utils.Int2Str(order.GoodsCount)
	printOrder[UserPayMoney] = jxutils.IntPrice2StandardCurrencyString(order.ActualPayPrice)
	printOrder[StoreName] = order.StoreName
	printOrder[StoreTel] = storeTel
	printOrder[OfficialName] = globals.StoreName

	if order.WaybillVendorID != -1 && order.VendorWaybillID != "" {
		bill, _ := partner.CurOrderManager.LoadWaybill(order.VendorWaybillID, order.WaybillVendorID)
		//printOrder[StoreStatus] = utils.Int2Str(storeDetail.Status)
		printOrder[RiderVendorId] = utils.Int2Str(bill.WaybillVendorID)
		printOrder[RiderName] = bill.CourierName
		printOrder[RiderPhone] = bill.CourierMobile
	}
	if printType == "afs" {
		printOrder[AfsOrderStatus] = utils.Int2Str(afsOrder.Status)
		printOrder[RejectionReason] = afsOrder.VendorReasonType
		printOrder[AfsOrderStatus] = utils.Int2Str(afsOrder.Status)
		printOrder[CustcareRefundReason] = afsOrder.ReasonDesc
	}
	return printOrder
}

// ManageWayBillPrint 组装运单打印数据
//func ManageWayBillPrint(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail) map[string]string {
//	printOrder := make(map[string]string, 0)
//	// 获取品牌名称
//	if storeDetail != nil && storeDetail.BrandIsPrint == model.NO {
//		if order.VendorOrgCode == "34665" {
//			printOrder[EBailOrderNo] = globals.StoreNameEbai2
//		} else {
//			printOrder[EBailOrderNo] = storeDetail.BrandName
//		}
//	}
//
//	printOrder[VendOrID] = utils.Int2Str(order.VendorID)
//	printOrder[VendorName] = jxutils.GetVendorName(order.VendorID)
//	printOrder[StoreName] = order.StoreName
//	printOrder[StoreTel] = storeTel
//	printOrder[OfficialName] = globals.StoreName
//	return printOrder
//}
//
//// ManagerStore 送达提示数据组装
//func ManagerStore(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail) map[string]string {
//	printOrder := make(map[string]string, 0)
//	printOrder[OrderNo] = order.VendorOrderID
//	printOrder[VendorOrderNo] = utils.Int2Str(order.VendorID)
//	printOrder[VendorName] = jxutils.GetVendorName(order.VendorID)
//	printOrder[StoreName] = order.StoreName
//	printOrder[StoreTel] = storeTel
//	return printOrder
//}
//
//// ManagerEnterTheStore 进店咨询
//func ManagerEnterTheStore(order *model.GoodsOrder, storeTel string, storeDetail *dao.StoreDetail) map[string]string {
//	printOrder := make(map[string]string, 0)
//	return printOrder
//}
