package defsch

import (
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/netprinter"
	push "git.rosy.net.cn/jx-callback/business/jxutils/unipush"
	"git.rosy.net.cn/jx-callback/business/jxutils/weixinmsg"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/msghub"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/globals"
)

var (
	autoRejectSkuMap = map[int]int{
		33996: 1,
		33995: 1,
		33994: 1,
		33991: 1,
	}
)

func (s *DefScheduler) OnAfsOrderNew(order *model.AfsOrder, isPending bool) (err error) {
	if order.Status == model.AfsOrderStatusWait4Approve {
		if !isPending {
			if isAutoRejectAfsOrder(order) {
				if handler := partner.GetPurchaseOrderHandlerFromVendorID(order.VendorID); handler != nil {
					if err := handler.AgreeOrRefuseRefund(jxcontext.AdminCtx, order, partner.AfsApproveTypeRefused, "抱歉，蟹券不接受退货或换货", nil); err != nil {
						globals.SugarLogger.Debugf("OnAfsOrderNew, orderID:%s, afsOrderID:%s failed with err:%v", order.VendorOrderID, order.AfsOrderID, err)
					}
				}
			}

			if order != nil && order.VendorID == model.VendorIDTaoVegetable {
				result, _ := dao.GetAfsOrders(dao.GetDB(), model.VendorIDTaoVegetable, order.VendorOrderID, "")
				if len(result) <= model.YES {
					msghub.OnNewWait4ApproveAfsOrder(order)
					weixinmsg.NotifyAfsOrderStatus(order)
					push.NotifyAfsOrder(order)
				}
			} else {
				msghub.OnNewWait4ApproveAfsOrder(order)
				weixinmsg.NotifyAfsOrderStatus(order)
				push.NotifyAfsOrder(order)
			}
		}
	}

	storeDetail, err := partner.CurOrderManager.LoadStoreDetail(order.JxStoreID, order.VendorID)
	if err == nil {
		if storeDetail.IsPrintRefundOrder == model.YES { // 打印退款订单
			order2, _ := partner.CurOrderManager.LoadOrder(order.VendorOrderID, order.VendorID)
			_, err = netprinter.PrintRefundOrCancelOrder(jxcontext.AdminCtx, model.NO, order2, order2.JxStoreID)
		}
	}

	//if order.Status == model.AfsOrderStatusWait4Approve || order.Status == model.AfsOrderStatusFinished || order.Status == model.AfsOrderStatusNew {
	//	// 京西云打印机打印数据-如果门店使用京西打印机才使用如下代码块
	//	store, err := dao.GetStoreDetail(dao.GetDB(), order.JxStoreID, order.VendorID, order.VendorOrgCode)
	//	if err == nil && store.PrinterVendorID == model.VendorIDJxprint && store.IsPrintCancelOrder == model.YES {
	//		orderObj, _ := partner.CurOrderManager.LoadOrder(order.VendorOrderID, order.VendorID)
	//		if orderObj != nil {
	//			netprinter.PrintOrderByOrder(jxcontext.AdminCtx, orderObj, model.PrintAfsOrder, order)
	//		}
	//	}
	//}
	return err
}

func (s *DefScheduler) OnAfsOrderStatusChanged(order *model.AfsOrder, status *model.OrderStatus, isPending bool) (err error) {
	if status.Status == model.AfsOrderStatusWait4ReceiveGoods {
		if !isPending {
			msghub.OnKeyAfsOrderStatusChanged(order)
			weixinmsg.NotifyAfsOrderStatus(order)
		}
	}
	return err
}

func isAutoRejectAfsOrder(order *model.AfsOrder) (isReject bool) {
	for _, v := range order.Skus {
		if autoRejectSkuMap[jxutils.GetSkuIDFromOrderSkuFinancial(v)] == 1 {
			isReject = true
			break
		}
	}
	return isReject
}
