package jd

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/jdapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"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/partner"
	"git.rosy.net.cn/jx-callback/business/partner/im"
)

func (c *PurchaseHandler) GetOrderInvoice(vendorOrderId string, msg *jdapi.CallbackInvoiceMsg) (response *jdapi.CallbackResponse) {
	var (
		db = dao.GetDB()
		a  = getAPI(vendorOrderId)
	)

	localOrder, _ := partner.CurOrderManager.LoadOrder(msg.BillID, model.VendorIDJD)
	if localOrder == nil {
		return jdapi.Err2CallbackResponse(fmt.Errorf("订单不存在:%s", msg.BillID), "jd invoice")
	}
	invoiceData, _ := dao.GetInvoiceInfo(db, msg.BillID)
	invoice, err := a.QueryInvoiceDetail(msg.BillID)
	if err != nil {
		return jdapi.Err2CallbackResponse(err, "query invoice err")
	}
	if invoiceData == nil {
		invoiceObj := &model.InvoiceMsg{
			ModelIDCUL:            model.ModelIDCUL{},
			OrderId:               utils.Int64ToStr(invoice.OrderId),
			StoreID:               localOrder.JxStoreID,
			VendorID:              model.VendorIDJD,
			Status:                model.InvoiceStatusInit,
			InvoiceTaskId:         utils.Int64ToStr(invoice.ApplyId),
			PushType:              invoice.InvoiceType,
			InvoiceTitle:          invoice.InvoiceTitle,
			TaxpayerId:            invoice.ConsumerTaxId,
			NeedInvoiceByCategory: invoice.IvcContentType,
			CompanyAddress:        invoice.ConsumerAddress,
			CompanyPhone:          invoice.ConsumerPhone,
			AccountBank:           invoice.ConsumerBankName,
			AccountNumber:         invoice.ConsumerBankAccount,
			Email:                 invoice.CustomerEmail,
			InvoiceAmount:         utils.Float64TwoInt64(invoice.ShouldInvoiceAmount * 100),
			InvoiceUrl:            "",
			InvoiceId:             "",
		}
		// 发票状态 1:待开票, 3:开票中, 4:开票成功, 5:开票失败, 6:冲红中, 7:冲红成功, 8:冲红失败, 9:已驳回, 11:蓝票审核失败, 12:红票审核失败, 13:待换开, 14:换开中, 15:换开驳回, 16:退款关闭, 17:驳回关闭
		switch invoice.InvoiceStatus {
		case 1:
			invoiceObj.Status = model.InvoiceStatusInit
		case 3, 6:
			invoiceObj.Status = model.InvoiceStatusBilling
		case 4, 7:
			invoiceObj.Status = model.InvoiceStatusBillingSuccess
		case 5, 8, 9, 11, 12, 15, 16, 17:
			invoiceObj.Status = model.InvoiceStatusBillingFail
		case 13, 14:
			invoiceObj.Status = utils.Int2Str(invoice.InvoiceStatus)
		}
		dao.WrapAddIDCULEntity(invoiceObj, "system")
		if err = dao.CreateEntity(db, invoiceObj); err != nil {
			return jdapi.Err2CallbackResponse(err, "CreateEntity invoice err")
		}
	} else {
		// 发票状态 1:待开票, 3:开票中, 4:开票成功, 5:开票失败, 6:冲红中, 7:冲红成功, 8:冲红失败, 9:已驳回, 11:蓝票审核失败, 12:红票审核失败, 13:待换开, 14:换开中, 15:换开驳回, 16:退款关闭, 17:驳回关闭
		switch invoice.InvoiceStatus {
		case 1:
			invoiceData.Status = model.InvoiceStatusInit
		case 3, 6:
			invoiceData.Status = model.InvoiceStatusBilling
		case 4, 7:
			invoiceData.Status = model.InvoiceStatusBillingSuccess
		case 5, 8, 9, 11, 12, 15, 16, 17:
			invoiceData.Status = model.InvoiceStatusBillingFail
		case 13, 14:
			invoiceData.Status = utils.Int2Str(invoice.InvoiceStatus)
		}
		invoiceData.UpdatedAt = utils.Str2Time(msg.Modified)
		dao.UpdateEntity(db, invoiceData, "Status", "UpdatedAt")

		//invoiceData.InvoiceTaskId = utils.Int64ToStr(invoice.ApplyId)
		//invoiceData.PushType = invoice.InvoiceType
		//invoiceData.InvoiceTitle = invoice.InvoiceTitle
		//invoiceData.TaxpayerId = invoice.ConsumerTaxId
		//invoiceData.NeedInvoiceByCategory = invoice.IvcContentType
		//invoiceData.CompanyAddress = invoice.ConsumerAddress
		//invoiceData.CompanyPhone = invoice.ConsumerPhone
		//invoiceData.AccountBank = invoice.ConsumerBankName
		//invoiceData.AccountNumber = invoice.ConsumerBankAccount
		//invoiceData.Email = invoice.CustomerEmail
		//invoiceData.InvoiceAmount = utils.Float64TwoInt64(invoice.ShouldInvoiceAmount * 100)
		//dao.UpdateEntity(db, invoiceData)
	}

	content2 := fmt.Sprintf("订单:%s,用户申请发票,请老板提供发票信息!", localOrder.VendorOrderID)
	im.PushMsgByCid(localOrder.VendorStoreID, localOrder.VendorID, content2)
	weixinmsg.SendStoreMessage(jxcontext.AdminCtx, fmt.Sprintf("京东用户申请发票:%s", localOrder.StoreName), content2, []int{localOrder.JxStoreID}, nil, "", model.MessageTypeStore, true, true)
	return jdapi.Err2CallbackResponse(nil, "")
}
