From 632aa6aa3d1a2eb6d6f1fd22e355a2329ba5f560 Mon Sep 17 00:00:00 2001 From: renyutian Date: Wed, 3 Apr 2019 17:49:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E8=B4=A6=E7=AC=AC=E4=BA=8C=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxcallback/orderman/financial.go | 313 +++++++++++++++--- business/jxcallback/orderman/order.go | 42 +++ business/jxcallback/orderman/orderman_ext.go | 112 +++++++ business/model/api.go | 12 + business/model/order_financial.go | 183 ++++++---- business/partner/partner.go | 11 + business/partner/purchase/ebai/financial.go | 189 ++++++++--- .../partner/purchase/ebai/financial_test.go | 13 +- business/partner/purchase/ebai/order.go | 2 +- business/partner/purchase/jd/financial.go | 123 +++++-- .../partner/purchase/jd/financial_test.go | 4 +- business/partner/purchase/mtwm/financial.go | 198 ++++++++--- .../partner/purchase/mtwm/financial_test.go | 4 +- business/partner/purchase/mtwm/order.go | 18 +- controllers/financial.go | 23 ++ globals/beegodb/beegodb.go | 3 +- routers/commentsRouter_controllers.go | 9 + 17 files changed, 1028 insertions(+), 231 deletions(-) diff --git a/business/jxcallback/orderman/financial.go b/business/jxcallback/orderman/financial.go index e039d1887..6b7b0d89e 100644 --- a/business/jxcallback/orderman/financial.go +++ b/business/jxcallback/orderman/financial.go @@ -1,64 +1,293 @@ package orderman import ( + "git.rosy.net.cn/baseapi/utils" "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" ) const ( - PublicWelfareDonation = 1 // 美团公益捐款金额现阶段是每单一分钱 - TotalRate = 200 // 总费率(千分之200)= 第三方平台费+京西品牌费+京西服务费,现阶段都是合计200‰ + TotalRate = 200 // 总费率(千分之200)= 第三方平台费+京西品牌费+京西服务费,现阶段都是合计200‰ + MaxFreightMoneyFromJxByShop = 7 // 商家自送实际转美团/达达后由商家承担的运费金额上限 ) // 处理正向订单结账信息 -func SaveOrderFinancialInfo(order *model.OrderFinancial) (err error) { - if order.VendorID == model.VendorIDJD { //京东订单单独处理部分 - } - if order.VendorID == model.VendorIDEBAI { //饿百订单单独处理部分 - order.FreightDiscountMoney = order.TotalDiscountMoney - order.DiscountMoney - } - if order.VendorID == model.VendorIDMTWM { //美团订单单独处理部分 - order.DonationMoney = PublicWelfareDonation - } - order.ShopMoneyByCal = order.SalePriceMoney - order.TotalDiscountMoney + order.VendorFreightDiscountMoney - order.DistanceFreightMoney - order.FreightTipsMoney - order.DonationMoney + order.SelfDeliveryDiscountMoney + order.PmSubsidyMoney + order.BoxMoney - order.PmMoney - order.PmMoneyFormJx = (order.ShopMoney+order.PmMoney)*TotalRate/1000 - order.PmMoney // 京西平台费 = 总金额*20%-第三方平台费 - if order.PmMoneyFormJx < 0 { // 如果算出京西平台费为负数,则置0 - order.PmMoneyFormJx = 0 - } - order.ShopMoneyFromJx = order.ShopMoney - order.PmMoneyFormJx + order.SubsidyMoneyFromJx - order.FreightMoneyFromJxByShop - err = dao.CreateEntity(dao.GetDB(), order) - return err -} - -// 处理售后订单结账信息 -func SaveAfsOrderFinancialInfo(afsOrder *model.AfterSalesOrder) (err error) { +func (c *OrderManager) SaveOrderFinancialInfo(order *model.OrderFinancial, operation string) (err error) { db := dao.GetDB() dao.Begin(db) - defer dao.Rollback(db) - if afsOrder.VendorID == model.VendorIDJD { //京东订单单独处理部分 - } - if afsOrder.VendorID == model.VendorIDEBAI { //饿百订单单独处理部分 - } - if afsOrder.VendorID == model.VendorIDMTWM { //美团订单单独处理部分 - } - globals.SugarLogger.Debug(afsOrder.Skus) - for _, orderSku := range afsOrder.Skus { - orderSku.SkuRefundMoneyByCal = orderSku.SkuUserMoney + orderSku.SkuVendorMoney + afsOrder.FreightUserMoney + afsOrder.AfsFreightMoney + afsOrder.BoxMoney + afsOrder.TongchengFreightMoney + afsOrder.MealBoxMoney - // orderSku.SkuTotalMoney+=orderSku.SkuJxMoney // 退款单京西补贴部分先不作计算 - afsOrder.SkuUserMoney += orderSku.SkuUserMoney - afsOrder.PmSubsidyMoney += orderSku.SkuVendorMoney - // order.SkuJxMoney += orderSku.SkuJxMoney // 退款单京西补贴部分先不作计算 - if err = dao.CreateEntity(db, orderSku); err != nil { + defer func() { + if r := recover(); r != nil || err != nil { + dao.Rollback(db) + if r != nil { + panic(r) + } + } + }() + if operation == partner.UpdatedPeration { + // db := orm.NewOrm() + err = utils.CallFuncLogError(func() error { + _, err = dao.ExecuteSQL(db, "DELETE FROM order_Financial WHERE vendor_order_id = ? AND vendor_id = ?", order.VendorOrderID, order.VendorID) + return err + }, "SaveOrderFinancialInfo delete order_Financial, orderID:%s", order.VendorOrderID) + if err != nil { + return err + } + err = utils.CallFuncLogError(func() error { + _, err = dao.ExecuteSQL(db, "DELETE FROM order_sku_financial WHERE vendor_order_id = ? AND vendor_id = ?", order.VendorOrderID, order.VendorID) + return err + }, "SaveOrderFinancialInfo delete order_sku_financial, orderID:%s", order.VendorOrderID) + if err != nil { + return err + } + err = utils.CallFuncLogError(func() error { + _, err = dao.ExecuteSQL(db, "DELETE FROM order_discount_financial WHERE vendor_order_id = ? AND vendor_id = ?", order.VendorOrderID, order.VendorID) + return err + }, "SaveOrderFinancialInfo delete order_sku, orderID:%s", order.VendorOrderID) + if err != nil { return err } } - afsOrder.RefundMoneyByCal = afsOrder.SkuUserMoney + afsOrder.PmSubsidyMoney + afsOrder.FreightUserMoney + afsOrder.AfsFreightMoney + afsOrder.BoxMoney + afsOrder.TongchengFreightMoney + afsOrder.MealBoxMoney - // order.TotalMoney += order.SkuJxMoney // 退款单京西补贴部分先不作计算 - if err = dao.CreateEntity(db, afsOrder); err != nil { - globals.SugarLogger.Warnf("On SaveAfsOrderFinancialInfo err: SaveAfsOrder is err") + for _, activity := range order.Discounts { + if err = dao.CreateEntity(db, activity); err != nil { + return err + } + } + order.ShopMoneyByCal = order.SalePriceMoney - order.TotalDiscountMoney - order.PointsDeductionMoney + order.PmFreightDiscountMoney - order.DistanceFreightMoney - order.FreightTipsMoney - order.DonationMoney + order.SelfDeliveryDiscountMoney + order.PmSubsidyMoney + order.SkuBoxMoney + order.BoxMoney - order.PmMoney + order.JxPmMoney = (order.ShopMoney+order.PmMoney)*TotalRate/1000 - order.PmMoney // 京西平台费 = 总金额*20%-第三方平台费 + if order.JxPmMoney < 0 { // 如果算出京西平台费为负数,则置0 + order.JxPmMoney = 0 + } + + order.JxShopMoney = order.ShopMoney - order.JxPmMoney + order.JxSubsidyMoney - order.JxFreightMoneyByShop + // 订单结算金额拆分计算,拆分到单条sku + // 先获取订单主体优惠金额 + platOrderGoodsDiscountMoney := order.PmSubsidyMoney - order.PmSkuSubsidyMoney + order.SelfDeliveryDiscountMoney + // 结算平台扣除项汇总---平台佣金,商家设置运费减免,远距离费,小费,公益捐款、、、、 + deductionsByPm := order.FreightDiscountMoney + order.DistanceFreightMoney + order.FreightTipsMoney + order.DonationMoney + order.PmMoney + // 结算京西扣除项汇总---京西佣金,京西配送运费 + deductionsByJx := order.JxPmMoney + order.JxFreightMoneyByShop + for _, sku := range order.Skus[1:] { + // 用户支付菜品金额,用户为这一条sku支付的菜品金额 + sku.UserMoney = utils.Float64TwoInt64(float64(order.SalePriceMoney-order.DiscountMoney-order.PointsDeductionMoney+order.BoxMoney+order.SkuBoxMoney) * float64(sku.SalePrice*int64(sku.Count)+sku.SkuBoxMoney) / float64(order.SalePriceMoney+order.SkuBoxMoney)) // 林峰确认,比如49-2满减,算餐盒费满49元,不算餐盒费不足49元,是可以参加满减活动的,那么餐盒费也应该和商品金额一起进行折算 + order.Skus[0].UserMoney += sku.UserMoney + // 计算平台订单主体补贴拆分到单条sku的金额 + sku.PmSubsidyMoneyForSku + sku.PmSubsidyMoney = sku.PmSkuSubsidyMoney + utils.Float64TwoInt64(float64(platOrderGoodsDiscountMoney*sku.SalePrice*int64(sku.Count)+sku.SkuBoxMoney)/float64(order.SalePriceMoney+order.SkuBoxMoney)) + order.Skus[0].PmSubsidyMoney += sku.PmSubsidyMoney + // 计算京西满减补贴拆分到单条sku的金额 + sku.JxSubsidyMoneyForSku + sku.JxSubsidyMoney = sku.JxSkuSubsidyMoney + utils.Float64TwoInt64(float64(order.JxSubsidyMoney-order.PmSkuSubsidyMoney)*float64(sku.SalePrice*int64(sku.Count)+sku.SkuBoxMoney)/float64(order.SalePriceMoney+order.SkuBoxMoney)) + order.Skus[0].JxSubsidyMoney += sku.JxSubsidyMoney + // 计算单条sku需要承担的平台扣费金额 + sku.PmDeductionsMoney = utils.Float64TwoInt64(float64(deductionsByPm) * (float64(sku.UserMoney + sku.PmSubsidyMoney)) / (float64(order.SalePriceMoney + order.BoxMoney + order.SkuBoxMoney - order.DiscountMoney - order.PointsDeductionMoney + order.PmSubsidyMoney))) + order.Skus[0].PmDeductionsMoney += sku.PmDeductionsMoney + // 计算单条sku平台应该结算给京西的金额 + sku.ShopMoneyByCal = sku.UserMoney + sku.PmSubsidyMoney - sku.PmDeductionsMoney + order.Skus[0].ShopMoneyByCal += sku.ShopMoneyByCal + // j计算单条sku需要承担的京西扣费金额 + sku.JxDeductionsMoney = utils.Float64TwoInt64(float64(deductionsByJx*sku.ShopMoneyByCal) / float64(order.ShopMoney)) + order.Skus[0].JxDeductionsMoney += sku.JxDeductionsMoney + // 计算单条sku京西应该结算给商家的金额 + sku.JxShopMoney = sku.ShopMoneyByCal + sku.JxSubsidyMoney - sku.JxDeductionsMoney + order.Skus[0].JxShopMoney += sku.JxShopMoney + if err = dao.CreateEntity(db, sku); err != nil { + return err + } + } + if len(order.Skus) > 0 { + sku := order.Skus[0] + sku.UserMoney = order.SalePriceMoney - order.DiscountMoney - sku.UserMoney + sku.PmSubsidyMoney = platOrderGoodsDiscountMoney + order.SelfDeliveryDiscountMoney - sku.PmSubsidyMoney + sku.JxSubsidyMoney = order.JxSubsidyMoney - sku.JxSubsidyMoney + sku.PmDeductionsMoney = deductionsByPm - sku.PmDeductionsMoney + sku.ShopMoneyByCal = order.ShopMoney - sku.ShopMoneyByCal + sku.JxDeductionsMoney = deductionsByJx - sku.JxDeductionsMoney + sku.JxShopMoney = order.JxShopMoney - sku.JxShopMoney + if err = dao.CreateEntity(db, sku); err != nil { + return err + } + } else { + + } + // 加上京西对单条sku的补贴,再存数据库 + order.JxSubsidyMoney += order.JxSkuSubsidyMoney + if err = dao.CreateEntity(db, order); err != nil { + globals.SugarLogger.Warnf("On SaveOrderFinancialInfo err: order is err") return err } dao.Commit(db) return err } + +// 处理售后订单结账信息 +func (c *OrderManager) SaveAfsOrderFinancialInfo(afsOrder *model.AfsOrder) (err error) { + db := dao.GetDB() + dao.Begin(db) + defer func() { + if r := recover(); r != nil || err != nil { + dao.Rollback(db) + if r != nil { + panic(r) + } + } + }() + globals.SugarLogger.Debug(afsOrder.Skus) + // 平台结算扣除汇总--平台补贴,售后产生运费,平台收包装费,同城运费、、、 + deductionsByPm := afsOrder.PmSubsidyMoney + afsOrder.AfsFreightMoney + afsOrder.BoxMoney + afsOrder.TongchengFreightMoney + afsOrder.RefundMoneyByCal = afsOrder.SkuUserMoney + afsOrder.FreightUserMoney + deductionsByPm + // order.TotalMoney += order.SkuJxMoney // 退款单京西补贴部分先不作计算 + if err = dao.CreateEntity(db, afsOrder); err != nil { + globals.SugarLogger.Warnf("On SaveAfsOrderFinancialInfo err: SaveAfsOrder is err") + return err + } + + // 京西结算扣除汇总,先不作计算,计算单条sku最终扣款金额(+该条sku承担的平台结算扣除金额) + for _, orderSku := range afsOrder.Skus[1:] { + orderSku.RefundMoneyByCal = orderSku.PmSkuSubsidyMoney + + utils.Float64TwoInt64(float64(afsOrder.RefundMoneyByCal-afsOrder.PmSkuSubsidyMoney)*float64(orderSku.UserMoney+orderSku.PmSubsidyMoney-orderSku.PmSkuSubsidyMoney)/float64(afsOrder.SkuUserMoney+afsOrder.PmSubsidyMoney-afsOrder.PmSkuSubsidyMoney)) + afsOrder.Skus[0].RefundMoneyByCal += orderSku.RefundMoneyByCal + if err = dao.CreateEntity(db, orderSku); err != nil { + return err + } + } + if len(afsOrder.Skus) > 0 { + orderSku := afsOrder.Skus[0] + orderSku.RefundMoneyByCal = afsOrder.RefundMoneyByCal - orderSku.RefundMoneyByCal + if err = dao.CreateEntity(db, orderSku); err != nil { + return err + } + } + dao.Commit(db) + return err +} + +// 转自送实际使用美团/达达配送,调用此方法 +func (c *OrderManager) UpdataOrderFinancialInfo(orderFinancial *model.OrderFinancial, wayBill *model.Waybill) (err error) { + // 通过本地数据库去取是否转美团/达达,并计算运费,写在上级调用函数里面传递过来 + // wayBill, err2 := partner.CurOrderManager.LoadWaybill(orderFinancial.VendorOrderID, orderFinancial.VendorID) + // if err = err2; err == nil { + // orderFinancial.JxFreightMoney = wayBill.DesiredFee + // } + db := dao.GetDB() + dao.Begin(db) + defer func() { + if r := recover(); r != nil || err != nil { + dao.Rollback(db) + if r != nil { + panic(r) + } + } + }() + orderFinancial.JxFreightMoney = wayBill.DesiredFee + if (orderFinancial.JxFreightMoney - orderFinancial.SelfDeliveryDiscountMoney) > MaxFreightMoneyFromJxByShop { + orderFinancial.JxFreightMoneyByShop = orderFinancial.SelfDeliveryDiscountMoney + MaxFreightMoneyFromJxByShop + } else if (orderFinancial.JxFreightMoney - orderFinancial.SelfDeliveryDiscountMoney) < 0 { + orderFinancial.JxFreightMoneyByShop = orderFinancial.SelfDeliveryDiscountMoney + } else { + orderFinancial.JxFreightMoneyByShop = orderFinancial.JxFreightMoney + } + orderFinancial.JxShopMoney -= orderFinancial.JxFreightMoneyByShop + // orderFinancial.JxFreightMoneyByShop 的改变直接影响到单条sku的京西结算金额的改变 + jxDecMoney := orderFinancial.JxFreightMoneyByShop + for _, sku := range orderFinancial.Skus[1:] { + // 重新计算单条sku京西应该结算的金额 + skuJxDecMoney := utils.Float64TwoInt64(float64(jxDecMoney*sku.SalePrice*int64(sku.Count)+sku.SkuBoxMoney) / float64(orderFinancial.SalePriceMoney+orderFinancial.SkuBoxMoney)) + sku.JxDeductionsMoney += skuJxDecMoney + sku.JxShopMoney -= skuJxDecMoney + jxDecMoney -= skuJxDecMoney + if _, err = dao.UpdateEntity(db, sku); err != nil { + return err + } + } + if len(orderFinancial.Skus) > 0 { + sku := orderFinancial.Skus[0] + sku.JxDeductionsMoney += jxDecMoney + sku.JxShopMoney -= jxDecMoney + if _, err = dao.UpdateEntity(db, sku); err != nil { + return err + } + } + if err = dao.CreateEntity(db, orderFinancial); err != nil { + globals.SugarLogger.Warnf("On SaveOrderFinancialInfo err: order is err") + return err + } + dao.Commit(db) + // orderFinancial 和 OrderSkuFinancial 数据计算完毕,准备进行更新 + return err +} + +// 正向订单设置小费后更新信息,需要再去订单查询列表接口查询小费金额,然后传递过来,感觉这样一个个写非常麻烦(以下demo是以京东为例) +// 为什么不直接用OrderDetail2Financial再处理一遍,通过传递一个type=新建/更新,在SaveOrderFinancialInfo进行相应的数据库操作 + +// func UpdataOrderFinancialInfoByTips(orderFinancial *model.OrderFinancial, orderData map[string]interface{}) (err error) { +// db := dao.GetDB() +// dao.Begin(db) +// defer dao.Rollback(db) +// order1, err2 := api.JdAPI.OrderShoudSettlementService(orderFinancial.VendorOrderID) // 考虑放到上层实现然后传递过来 +// orderFinancial.FreightTipsMoney = utils.MustInterface2Int64(orderData["tips"]) +// pmMoney := orderFinancial.PmMoney +// JxPmMoney := orderFinancial.JxPmMoney +// orderFinancial.PmMoney = 0 +// // 京东/京西平台费可能会变化 +// if err = err2; err == nil { +// orderFinancial.ShopMoney = utils.Interface2Int64WithDefault(order1["settlementAmount"], 0) +// orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["goodsCommission"], 0) +// orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["freightCommission"], 0) +// orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["packageCommission"], 0) +// orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["guaranteedCommission"], 0) +// orderFinancial.JxPmMoney = (orderFinancial.ShopMoney+orderFinancial.PmMoney)*TotalRate/1000 - orderFinancial.PmMoney // 京西平台费 = 总金额*20%-第三方平台费 +// if orderFinancial.JxPmMoney < 0 { // 如果算出京西平台费为负数,则置0 +// orderFinancial.JxPmMoney = 0 +// } +// // 计算在此次变动中,第三方平台结算费用的变化值 +// decMoney := orderFinancial.FreightTipsMoney + orderFinancial.PmMoney - pmMoney +// orderFinancial.ShopMoneyByCal -= decMoney +// decMoney2 := decMoney +// // 计算在此次变动中,京西结算费用的变化值 +// jxDecMoney := decMoney + orderFinancial.JxPmMoney - JxPmMoney +// orderFinancial.JxShopMoney -= jxDecMoney +// jxDecMoney2 := jxDecMoney + +// for _, sku := range orderFinancial.Skus[1:] { +// // 重新计算单条sku平台应该结算的金额 +// skuDecMoney := utils.Float64TwoInt64(float64(decMoney*sku.SalePrice*int64(sku.Count)+sku.MealBoxMoney) / float64(orderFinancial.SalePriceMoney+orderFinancial.MealBoxMoney)) +// sku.PmDeductionsMoney += skuDecMoney +// sku.ShopMoneyByCal -= skuDecMoney +// decMoney2 -= skuDecMoney + +// // 重新计算单条sku京西应该结算的金额 +// skuJxDecMoney := utils.Float64TwoInt64(float64(jxDecMoney*sku.SalePrice*int64(sku.Count)+sku.MealBoxMoney) / float64(orderFinancial.SalePriceMoney+orderFinancial.MealBoxMoney)) +// sku.JxDeductionsMoney += skuJxDecMoney +// sku.JxShopMoney -= skuJxDecMoney +// jxDecMoney2 -= skuJxDecMoney +// if err = dao.UpdateEntity(db, sku); err != nil { +// return err +// } +// } +// if len(orderFinancial.Skus) > 0 { +// sku := orderFinancial.Skus[0] + +// sku.PmDeductionsMoney += decMoney2 +// sku.ShopMoneyByCal -= decMoney2 + +// sku.JxDeductionsMoney += jxDecMoney2 +// sku.JxShopMoney -= jxDecMoney2 +// if err = dao.UpdateEntity(db, sku); err != nil { +// return err +// } +// } +// // orderFinancial 和 OrderSkuFinancial 数据计算完毕,准备进行更新 +// // 更新 +// // 更新 + +// } else { +// globals.SugarLogger.Warnf("jd OrderDetail2Financial, orderID:%s is not found from api.JdAPI.OrderShoudSettlementService", orderFinancial.VendorOrderID) +// } +// return err +// } + +// 正向订单转自配送后更新信息 +// func SaveOrderFinancialInfoBySelf(orderFinancial *model.OrderFinancial) (err error) { +// // 此时考虑之前有没有设置小费,如果设置了小费,要扣掉 +// return err +// } diff --git a/business/jxcallback/orderman/order.go b/business/jxcallback/orderman/order.go index 932618fed..0251a1c44 100644 --- a/business/jxcallback/orderman/order.go +++ b/business/jxcallback/orderman/order.go @@ -402,6 +402,48 @@ func (c *OrderManager) LoadOrder2(vendorOrderID2 string, vendorID int) (order *m return c.loadOrder("", vendorOrderID2, vendorID) } +func (c *OrderManager) LoadOrderFinancial(vendorOrderID string, vendorID int) (order *model.OrderFinancial, err error) { + return c.loadOrderFinancial(vendorOrderID, "", vendorID) +} + +func (c *OrderManager) LoadOrderFinancial2(vendorOrderID2 string, vendorID int) (order *model.OrderFinancial, err error) { + return c.loadOrderFinancial("", vendorOrderID2, vendorID) +} + +func (c *OrderManager) loadOrderFinancial(vendorOrderID, vendorOrderID2 string, vendorID int) (order *model.OrderFinancial, err error) { + db := orm.NewOrm() + order = &model.OrderFinancial{ + VendorOrderID: vendorOrderID, + VendorOrderID2: vendorOrderID2, + VendorID: vendorID, + } + keyFields := []string{ + model.FieldVendorID, + } + if vendorOrderID != "" { + keyFields = append(keyFields, model.FieldVendorOrderID) + } + if vendorOrderID2 != "" { + keyFields = append(keyFields, model.FieldVendorOrderID2) + } + + // 这块不知道怎么写了、、、标注一下 + if err = db.Read(order, keyFields...); err == nil { + vendorOrderID = order.VendorOrderID + err = utils.CallFuncLogError(func() error { + _, err = db.QueryTable("order_sku_financial").Filter("vendor_order_id", vendorOrderID).Filter("vendor_id", vendorID).All(&order.Skus) + return err + }, "LoadOrder orderID:%s", vendorOrderID) + } + if err != nil { + if err == orm.ErrNoRows { + err = ErrCanNotFindOrder + } + globals.SugarLogger.Infof("LoadOrderFinancial orderID:%s failed with error:%v", vendorOrderID, err) + } + return order, err +} + func (c *OrderManager) UpdateOrderStatusAndFlag(order *model.GoodsOrder) (err error) { db := orm.NewOrm() utils.CallFuncLogError(func() error { diff --git a/business/jxcallback/orderman/orderman_ext.go b/business/jxcallback/orderman/orderman_ext.go index 6f5911a26..1f0c04e40 100644 --- a/business/jxcallback/orderman/orderman_ext.go +++ b/business/jxcallback/orderman/orderman_ext.go @@ -490,3 +490,115 @@ func (c *OrderManager) GetOrderStatusList(ctx *jxcontext.Context, vendorOrderID } return statusList, nil } + +func (c *OrderManager) GetOrdersFinancial(ctx *jxcontext.Context, fromDateStr, toDateStr string, params map[string]interface{}, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) { + globals.SugarLogger.Debugf("GetOrdersFinancial from:%s to:%s", fromDateStr, toDateStr) + pageSize = jxutils.FormalizePageSize(pageSize) + if offset < 0 { + offset = 0 + } + sql := ` + SELECT SQL_CALC_FOUND_ROWS t1.*, + t2.store_name,t2.vendor_store_id,t2.store_id,t2.jx_store_id,t2.status,t2.order_finished_at + FROM order_financial t1 + LEFT JOIN goods_order t2 ON t1.vendor_order_id = t2.vendor_order_id + ` + var ( + sqlWhere string + sqlParams []interface{} + ) + if params["orderID"] != nil { + sqlWhere = " WHERE (t1.vendor_order_id = ? OR t1.vendor_order_id2 = ?)" + sqlParams = []interface{}{ + params["orderID"], + params["orderID"], + } + } else { + fromDate, err2 := utils.TryStr2Time(fromDateStr) + if err = err2; err != nil { + return nil, err + } + if toDateStr == "" { + toDateStr = fromDateStr + } + toDate, err2 := utils.TryStr2Time(toDateStr) + if err = err2; err != nil { + return nil, err + } + toDate = toDate.Add(24 * time.Hour) + + sqlWhere = ` + WHERE t2.order_finished_at >= ? AND t2.order_finished_at < ? + ` + sqlParams = []interface{}{ + fromDate, + toDate, + } + if params["vendorIDs"] != nil { + var vendorIDs []int + if err = utils.UnmarshalUseNumber([]byte(params["vendorIDs"].(string)), &vendorIDs); err != nil { + return nil, err + } + if len(vendorIDs) > 0 { + sqlWhere += " AND t1.vendor_id IN (" + dao.GenQuestionMarks(len(vendorIDs)) + ")" + sqlParams = append(sqlParams, vendorIDs) + } + } + if params["storeIDs"] != nil { + var storeIDs []int + if err = utils.UnmarshalUseNumber([]byte(params["storeIDs"].(string)), &storeIDs); err != nil { + return nil, err + } + if len(storeIDs) > 0 { + sqlWhere += " AND IF(t1.vendor_id = ?, t2.store_id, IF(t2.jx_store_id != 0, t2.jx_store_id, t2.store_id)) IN (" + dao.GenQuestionMarks(len(storeIDs)) + ")" + sqlParams = append(sqlParams, model.VendorIDWSC, storeIDs) + } + } + if params["statuss"] != nil { + var statuss []int + if err = utils.UnmarshalUseNumber([]byte(params["statuss"].(string)), &statuss); err != nil { + return nil, err + } + if len(statuss) > 0 { + sqlWhere += " AND t2.status IN (" + dao.GenQuestionMarks(len(statuss)) + ")" + sqlParams = append(sqlParams, statuss) + } + } + if params["cities"] != nil { + var cities []int + if err = utils.UnmarshalUseNumber([]byte(params["cities"].(string)), &cities); err != nil { + return nil, err + } + if len(cities) > 0 { + sql += "JOIN store st ON t2.store_id = st.id" + sqlWhere += " AND st.city_code IN (" + dao.GenQuestionMarks(len(cities)) + ")" + sqlParams = append(sqlParams, cities) + } + } + } + sql += sqlWhere + sql += ` + ORDER BY t1.delivery_confirm_time DESC + LIMIT ? OFFSET ? + ` + sqlParams = append(sqlParams, pageSize, offset) + + var orders []*model.OrderFinancialExt + db := dao.GetDB() + dao.Begin(db) + defer func() { + if r := recover(); r != nil { + dao.Rollback(db) + panic(r) + } + }() + if err = dao.GetRows(db, &orders, sql, sqlParams...); err == nil { + pagedInfo = &model.PagedInfo{ + TotalCount: dao.GetLastTotalRowCount(db), + Data: orders, + } + } + dao.Commit(db) + + return pagedInfo, err +} diff --git a/business/model/api.go b/business/model/api.go index 152d1a473..9b2c5993a 100644 --- a/business/model/api.go +++ b/business/model/api.go @@ -43,3 +43,15 @@ type PagedInfo struct { TotalCount int `json:"totalCount"` Data interface{} `json:"data"` } + +type OrderFinancialExt struct { + OrderFinancial + VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // + OrderFinishedAt time.Time `orm:"type(datetime)" json:"orderFinishedAt"` // 订单妥投/完成时间 + StoreID int `orm:"column(store_id)" json:"storeID"` // 外部系统里记录的 jxstoreid + JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid + StoreName string `orm:"size(64)" json:"storeName"` // + Status int `json:"status"` // 参见OrderStatus*相关的常量定义 + Skus []*OrderSkuFinancial `orm:"-" json:"skus"` // 正向订单购买商品列表 + Discounts []*OrderDiscountFinancial `orm:"-" json:"discounts"` // 正向订单享受优惠列表 +} diff --git a/business/model/order_financial.go b/business/model/order_financial.go index ba87e4fc0..dcddbec2e 100644 --- a/business/model/order_financial.go +++ b/business/model/order_financial.go @@ -5,33 +5,39 @@ import "time" type OrderFinancial struct { ModelIDCUL - VendorID int `orm:"column(vendor_id)" json:"vendorID"` // 平台id - VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 订单ID - VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 订单ID2,饿百独有 - DeliveryConfirmTime time.Time `orm:"type(datetime);index" json:"deliveryConfirmTime"` // 订单妥投/完成时间 - ShopPriceMoney int64 `json:"shopPriceMoney"` // 门店价-左右商品在京西的价格总和(报价模式会用到)0 - SalePriceMoney int64 `json:"salePriceMoney"` // 商品标价总额(用户下单满减之前的商品售价)+ - ActualPayMoney int64 `json:"actualPayMoney"` // 用户实际支付金额0 - TotalDiscountMoney int64 `json:"totalDiscountMoney"` // 订单总优惠(订单主体+运费)- - DiscountMoney int64 `json:"discountMoney"` // 订单主体优惠0 - ReceivableFreight int64 `json:"receivableFreight"` // 订单应收运费0 - FreightMoney int64 `json:"freightMoney"` // 用户支付运费0 - FreightDiscountMoney int64 `json:"freightDiscountMoney"` // 订单运费优惠(商家设置满减discountType == 8)0 - VendorFreightDiscountMoney int64 `json:"vendorFreightDiscountMoney"` // 订单运费优惠(平台运费优惠discountType == 7/12/15)+ - DistanceFreightMoney int64 `json:"distanceFreightMoney"` // 订单远距离费- - FreightTipsMoney int64 `json:"freightTipsMoney"` // 订单小费- - DonationMoney int64 `json:"donationMoney"` // 公益捐款- - SelfDeliveryDiscountMoney int64 `json:"selfDeliveryDiscountMoney"` // 平台承担运费补贴(商家自送)+ - PmSubsidyMoney int64 `json:"pmSubsidyMoney"` // 平台订单活动补贴1+ - BoxMoney int64 `json:"boxMoney"` // 餐盒费1+ - PmMoney int64 `json:"pmMoney"` // 平台费1- - ShopMoney int64 `json:"shopMoney"` // 应结金额-第三方平台结算给京西的金额M - ShopMoneyByCal int64 `json:"shopMoneyByCal"` // 应结金额-通过公式计算平台结给京西的金额M - PmMoneyFormJx int64 `json:"pmMoneyFormJx"` // 京西平台费- - FreightMoneyFromJx int64 `json:"freightMoneyFromJx"` // 转自送-->美团/达达订单产生运费0 - FreightMoneyFromJxByShop int64 `json:"freightMoneyFromJxByShop"` // 转自送-->美团/达达订单产生运费,由商家承担的部分- - SubsidyMoneyFromJx int64 `json:"subsidyMoneyFromJx"` // 京西补贴,针对单品活动补贴+ - ShopMoneyFromJx int64 `json:"shopMoneyFromJx"` // 店铺应结金额-京西结算给店铺的金额M + VendorID int `orm:"column(vendor_id)" json:"vendorID"` // 平台id + VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 订单ID + VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 订单ID2,饿百独有 + // DeliveryConfirmTime time.Time `orm:"type(datetime);index" json:"deliveryConfirmTime"` // 订单妥投/完成时间 + ShopPriceMoney int64 `json:"shopPriceMoney"` // 门店价-左右商品在京西的价格总和(报价模式会用到)0 + SalePriceMoney int64 `json:"salePriceMoney"` // 商品标价总额(用户下单满减之前的商品售价)+ + ActualPayMoney int64 `json:"actualPayMoney"` // 用户实际支付金额0 + TotalDiscountMoney int64 `json:"totalDiscountMoney"` // 订单总优惠(订单主体+运费)- + DiscountMoney int64 `json:"discountMoney"` // 订单主体优惠0 + PointsDeductionMoney int64 `json:"pointsDeductionMoney"` // 积分抵扣金额0 + ReceivableFreight int64 `json:"receivableFreight"` // 订单应收运费0 + FreightMoney int64 `json:"freightMoney"` // 用户支付运费0 + FreightDiscountMoney int64 `json:"freightDiscountMoney"` // 订单运费优惠(商家设置满减discountType == 8)0 + PmFreightDiscountMoney int64 `json:"pmFreightDiscountMoney"` // 订单运费优惠(平台运费优惠discountType == 7/12/15)+ + DistanceFreightMoney int64 `json:"distanceFreightMoney"` // 订单远距离费- + FreightTipsMoney int64 `json:"freightTipsMoney"` // 订单小费- + DonationMoney int64 `json:"donationMoney"` // 公益捐款- + SelfDeliveryDiscountMoney int64 `json:"selfDeliveryDiscountMoney"` // 平台承担运费补贴(商家自送)+ + PmSkuSubsidyMoney int64 `json:"pmSkuSubsidyMoney"` // 平台对单条sku补贴的金额 + PmSubsidyMoney int64 `json:"pmSubsidyMoney"` // 平台活动补贴(订单主体活动补贴+订单单条sku补贴)1+ + BoxMoney int64 `json:"boxMoney"` // order餐盒费1+ + SkuBoxMoney int64 `json:"skuBoxMoney"` // sku餐盒费 + PmMoney int64 `json:"pmMoney"` // 平台费1- + ShopMoney int64 `json:"shopMoney"` // 应结金额-第三方平台结算给京西的金额M + ShopMoneyByCal int64 `json:"shopMoneyByCal"` // 应结金额-通过公式计算平台结给京西的金额M + JxPmMoney int64 `json:"jxPmMoney"` // 京西平台费- + JxFreightMoney int64 `json:"jxFreightMoney"` // 转自送-->美团/达达订单产生运费0 + JxFreightMoneyByShop int64 `json:"jxFreightMoneyByShop"` // 转自送-->美团/达达订单产生运费,由商家承担的部分- + JxSubsidyMoney int64 `json:"jxSubsidyMoney"` // 京西总补贴 + JxSkuSubsidyMoney int64 `json:"jxSkuSubsidyMoney"` // 京西补贴给单条sku的金额 + JxShopMoney int64 `json:"jxShopMoney"` // 店铺应结金额-京西结算给店铺的金额M + Skus []*OrderSkuFinancial `orm:"-" json:"skus"` // 正向订单购买商品列表 + Discounts []*OrderDiscountFinancial `orm:"-" json:"discounts"` // 正向订单享受优惠列表 } func (o *OrderFinancial) TableUnique() [][]string { @@ -40,62 +46,99 @@ func (o *OrderFinancial) TableUnique() [][]string { } } -type AfterSalesOrder struct { +type OrderDiscountFinancial struct { ModelIDCUL - VendorID int `orm:"column(vendor_id)" json:"vendorID"` - VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 关联原始订单ID - VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 关联原始订单ID2,饿百独有 - AfterSalesOrderID string `orm:"column(after_sales_order_id);size(48)" json:"afterSalesOrderID"` // 售后订单ID - ConfirmTime time.Time `orm:"type(datetime);index" json:"confirmTime"` // 售后单完成时间 - VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // 外部系统里记录的storeid - StoreID int `orm:"column(store_id)" json:"storeID"` // 接口返回的京西门店ID - JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid - SkuUserMoney int64 `json:"skuUserMoney"` // 用户支付菜品金额 - FreightUserMoney int64 `json:"freightUserMoney"` // 用户支付运费金额 - AfsFreightMoney int64 `json:"afsFreightMoney"` // 退货取件费 - BoxMoney int64 `json:"boxMoney"` // 应退包装费金额 - TongchengFreightMoney int64 `json:"tongchengFreightMoney"` // 退货单产生的同城送费用 - MealBoxMoney int64 `json:"mealBoxMoney"` // 应退订单餐盒费 - PmSubsidyMoney int64 `json:"pmSubsidyMoney"` // 平台补贴金额 - RefundMoney int64 `json:"refundMoney"` // 平台扣款总额 1 - RefundMoneyByCal int64 `json:"refundMoneyByCal"` // 平台扣款总额-通过公式计算平台扣除京西的金额M - // SkuJxMoney int64 `json:"SkuJxMoney"` // 京西补贴金额,现阶段是平台扣京西多少钱,京西扣商家多少钱,暂不考虑撤回京西补贴 - Skus []*AfterSalesOrderSku `orm:"-" json:"-"` + VendorID int `orm:"column(vendor_id)" json:"vendorID"` + VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 订单ID + VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 订单ID2,饿百独有 + VendorActivityID string `orm:"column(vendor_activity_id);size(48)" json:"vendorActivityID"` // 活动ID 活动ID的存在,使原本的通过城市/下单时间-活动时间/金额……一系列匹配来确认活动归属的费时且准确度低的行为有了改变的可能--通过编码精确定位 + // ActivityName string `orm:"size(255)" json:"activityName"` // 活动名 + // ActivityMoney int64 `json:"activityMoney"` // 优惠金额 + // Remark string `orm:"size(255)" json:"remark"` // 备注 } -func (o *AfterSalesOrder) TableUnique() [][]string { +// 设置主键 "VendorOrderID", "DiscountCode", "VendorID" 一个订单不可能享受同一优惠2次 +func (o *OrderDiscountFinancial) TableUnique() [][]string { return [][]string{ - []string{"AfterSalesOrderID", "VendorID"}, + []string{"VendorOrderID", "VendorActivityID", "VendorID"}, } } -type AfterSalesOrderSku struct { +type AfsOrder struct { ModelIDCUL - VendorID int `orm:"column(vendor_id)" json:"vendorID"` // 平台id - VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 关联原始订单ID - VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 关联原始订单ID2,饿百独有 - AfterSalesOrderID string `orm:"column(after_sales_order_id);size(48)" json:"afterSalesOrderID"` // 售后订单ID - ConfirmTime time.Time `orm:"type(datetime)" json:"confirmTime"` // 订单完成时间 - VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // 外部系统里记录的storeid - StoreID int `orm:"column(store_id)" json:"storeID"` // 接口返回的京西门店ID - JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid - VendorSkuID string `orm:"column(vendor_sku_id);size(48)" json:"vendorSkuID"` // 平台skuid - SkuID int `orm:"column(sku_id)" json:"skuID"` // 平台返回的京西skuid - JxSkuID int `orm:"column(jx_sku_id)" json:"jxSkuID"` // 京西skuid - Name string `orm:"size(255)" json:"name"` // 商品名 - SkuUserMoney int64 `json:"skuUserMoney"` // 用户支付金额 - SkuVendorMoney int64 `json:"skuVendorMoney"` // 平台补贴金额 - SkuRefundMoney int64 `json:"skuRefundMoney"` // 平台扣款总额 1 - SkuRefundMoneyByCal int64 `json:"skuRefundMoneyByCal"` // 平台扣款总额-通过公式计算平台扣除京西的金额M - // CreatedAt time.Time `orm:"type(datetime);index" json:"createdAt"` // 订单创建时间 - // SkuJxMoney int64 `json:"skuJxFee"` // 京西补贴金额,现阶段是平台扣京西多少钱,京西扣商家多少钱,暂不考虑撤回京西补贴 + VendorID int `orm:"column(vendor_id)" json:"vendorID"` + VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 关联原始订单ID + VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 关联原始订单ID2,饿百独有 + AfsOrderID string `orm:"column(afs_order_id);size(48)" json:"afsOrderID"` // 售后订单ID + AfsCreateAt time.Time `orm:"type(datetime);index" json:"afsCreateAt"` // 订单生成时间 + VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // 外部系统里记录的storeid + StoreID int `orm:"column(store_id)" json:"storeID"` // 接口返回的京西门店ID + JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid + SkuUserMoney int64 `json:"skuUserMoney"` // 用户支付菜品金额 + FreightUserMoney int64 `json:"freightUserMoney"` // 用户支付运费金额 + AfsFreightMoney int64 `json:"afsFreightMoney"` // 退货取件费 + BoxMoney int64 `json:"boxMoney"` // 应退包装费金额 + TongchengFreightMoney int64 `json:"tongchengFreightMoney"` // 退货单产生的同城送费用 + SkuBoxMoney int64 `json:"skuBoxMoney"` // 应退订单餐盒费 + PmSubsidyMoney int64 `json:"pmSubsidyMoney"` // 平台总补贴金额 + PmSkuSubsidyMoney int64 `json:"pmSkuSubsidyMoney"` // 平台sku补贴金额 + RefundMoney int64 `json:"refundMoney"` // 平台扣款总额 1 + RefundMoneyByCal int64 `json:"refundMoneyByCal"` // 平台扣款总额-通过公式计算平台扣除京西的金额M + // JxSkuMoney int64 `json:"jxSkuMoney"` // 京西补贴金额,现阶段是平台扣京西多少钱,京西扣商家多少钱,暂不考虑撤回京西补贴 + Skus []*OrderSkuFinancial `orm:"-" json:"skus"` } -func (o *AfterSalesOrderSku) TableIndex() [][]string { +func (o *AfsOrder) TableUnique() [][]string { return [][]string{ - []string{"AfterSalesOrderID", "SkuID", "VendorID"}, + []string{"AfsOrderID", "VendorID"}, + } +} + +type OrderSkuFinancial struct { + ModelIDCUL + + VendorID int `orm:"column(vendor_id)" json:"vendorID"` // 平台id + VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"` // 关联原始订单ID + VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"` // 关联原始订单ID2,饿百独有 + AfsOrderID string `orm:"column(order_financial_id);size(48)" json:"afsOrderID"` // 订单结账ID + IsAfsOrder int8 `json:"isAfsOrder"` // 0--正向单, 1--售后单 + + // ConfirmTime time.Time `orm:"type(datetime)" json:"confirmTime"` // 订单生成/完成时间 + VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // 外部系统里记录的storeid + StoreID int `orm:"column(store_id)" json:"storeID"` // 接口返回的京西门店ID + JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid + VendorSkuID string `orm:"column(vendor_sku_id);size(48)" json:"vendorSkuID"` // 平台skuid + SkuID int `orm:"column(sku_id)" json:"skuID"` // 平台返回的京西skuid + JxSkuID int `orm:"column(jx_sku_id)" json:"jxSkuID"` // 京西skuid + PromotionType int `json:"promotionType"` // 商品级别促销类型 (1、无优惠;2、秒杀(已经下线);3、单品直降;4、限时抢购;1202、加价购;1203、满赠(标识商品);6、买赠(买A送B,标识B);9999、表示一个普通商品参与捆绑促销,设置的捆绑类型;9998、表示一个商品参与了捆绑促销,并且还参与了其他促销类型;9997、表示一个商品参与了捆绑促销,但是金额拆分不尽,9996:组合购,8001:轻松购会员价,8:第二件N折,9:拼团促销) + Name string `orm:"size(255)" json:"name"` // 商品名 + ShopPrice int64 `json:"shopPrice"` // 门店标价 + SalePrice int64 `json:"salePrice"` // 售卖价 + Count int `json:"count"` // 订单下单数量 + SkuBoxMoney int64 `json:"skuBoxMoney"` // sku餐盒费(美团/饿百的单条sku存在这个概念) + UserMoney int64 `json:"userMoney"` // 用户支付金额 + PmSubsidyMoney int64 `json:"pmSubsidyMoney"` // 平台补贴总金额 + PmSkuSubsidyMoney int64 `json:"pmSkuSubsidyMoney"` // 平台补贴sku金额 + PmDeductionsMoney int64 `json:"pmDeductionsMoney"` // 单条sku需要承担的平台扣费金额 + ShopMoney int64 `json:"shopMoney"` // 应结金额-第三方平台结算给京西的金额M + ShopMoneyByCal int64 `json:"shopMoneyByCal"` // 应结金额-通过公式计算平台结给京西的金额M + + JxSubsidyMoney int64 `json:"jxSubsidyMoney"` // 京西总补贴 + JxSkuSubsidyMoney int64 `json:"jxSkuSubsidyMoney"` // 京西补贴,针对单品活动补贴 + JxDeductionsMoney int64 `json:"jxDeductionsMoney"` // 单条sku需要承担的京西扣费金额 + JxShopMoney int64 `json:"jxShopMoney"` // 店铺应结金额-京西结算给店铺的金额M + RefundMoney int64 `json:"refundMoney"` // 平台扣款总额 暂时无用 + RefundMoneyByCal int64 `json:"refundMoneyByCal"` // 平台扣款总额-通过公式计算平台扣除京西的金额M + // CreatedAt time.Time `orm:"type(datetime);index" json:"createdAt"` // 订单创建时间 + // JxSkuMoney int64 `json:"jxSkuMoney"` // 京西补贴金额,现阶段是平台扣京西多少钱,京西扣商家多少钱,暂不考虑撤回京西补贴 + // SkuType int `json:"-"` // 当前如果为gift就为1,否则缺省为0 +} + +func (o *OrderSkuFinancial) TableUnique() [][]string { + return [][]string{ + []string{"VendorOrderID", "SkuID", "PromotionType", "IsAfsOrder", "VendorID"}, } } diff --git a/business/partner/partner.go b/business/partner/partner.go index 60b67b2d6..542171c06 100644 --- a/business/partner/partner.go +++ b/business/partner/partner.go @@ -16,6 +16,11 @@ const ( StoreNameSeparator = "-" ) +const ( + CreatedPeration = "create" + UpdatedPeration = "update" +) + const ( CancelWaybillReasonNotAcceptIntime = 1 CancelWaybillReasonSwitch2SelfFailed = 2 @@ -80,11 +85,17 @@ type IOrderManager interface { LoadOrder(vendorOrderID string, vendorID int) (order *model.GoodsOrder, err error) LoadOrder2(vendorOrderID2 string, vendorID int) (order *model.GoodsOrder, err error) + LoadOrderFinancial(vendorOrderID string, vendorID int) (order *model.OrderFinancial, err error) + LoadOrderFinancial2(vendorOrderID2 string, vendorID int) (order *model.OrderFinancial, err error) + UpdateWaybillVendorID(bill *model.Waybill, revertStatus bool) (err error) UpdateOrderStatusAndFlag(order *model.GoodsOrder) (err error) LoadWaybill(vendorWaybillID string, waybillVendorID int) (bill *model.Waybill, err error) OnOrderComments(orderCommentList []*model.OrderComment) (err error) + + SaveOrderFinancialInfo(order *model.OrderFinancial, operation string) (err error) + SaveAfsOrderFinancialInfo(afsOrder *model.AfsOrder) (err error) } // purchase handler中 diff --git a/business/partner/purchase/ebai/financial.go b/business/partner/purchase/ebai/financial.go index d40a3f55c..1db965357 100644 --- a/business/partner/purchase/ebai/financial.go +++ b/business/partner/purchase/ebai/financial.go @@ -3,7 +3,6 @@ package ebai import ( "git.rosy.net.cn/baseapi/platformapi/ebaiapi" "git.rosy.net.cn/baseapi/utils" - "git.rosy.net.cn/jx-callback/business/jxcallback/orderman" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/partner" "git.rosy.net.cn/jx-callback/globals" @@ -17,7 +16,7 @@ func OnFinancialMsg(msg *ebaiapi.CallbackMsg) (err error) { orderData, err2 := api.EbaiAPI.OrderPartrefundGet(utils.Interface2String(msg.Body["refund_id"])) if err = err2; err == nil { afsOrder := CurPurchaseHandler.AfsOrderDetail2Financial(orderData) - err = orderman.SaveAfsOrderFinancialInfo(afsOrder) + err = partner.CurOrderManager.SaveAfsOrderFinancialInfo(afsOrder) } } } else if msg.Cmd == ebaiapi.CmdOrderUserCancel { // 全额退款处理 @@ -25,16 +24,81 @@ func OnFinancialMsg(msg *ebaiapi.CallbackMsg) (err error) { if utils.Int64ToStr(utils.MustInterface2Int64(msg.Body["cancel_type"])) == ebaiapi.AfterOrderFinishedCancelType && (messageType == ebaiapi.OrderUserCancelSuccessA || messageType == ebaiapi.OrderUserCancelSuccessB) { globals.SugarLogger.Debug(utils.Interface2String(msg.Body["refund_id"])) // 获得退款订单ID,去本地数据库拿?饿百消息推送只给了订单号,但是没有查询全额退款的接口,只有部分退款才可以查询 + afsOrderID := utils.Interface2String(msg.Body["refund_id"]) + orderFinancial, err := partner.CurOrderManager.LoadOrderFinancial(afsOrderID, model.VendorIDEBAI) + if err == nil { + globals.SugarLogger.Debug(utils.Format4Output(orderFinancial, false)) + err = partner.CurOrderManager.SaveAfsOrderFinancialInfo(CurPurchaseHandler.OrderFinancialDetail2Refund(orderFinancial, msg)) + } else { + globals.SugarLogger.Warnf("ebai OnFinancialMsg, afsOrderID:%s is not found from partner.CurOrderManager.LoadOrderFinancial", afsOrderID) + } + } + } else if msg.Cmd == ebaiapi.CmdOrderDeliveryStatus { // 转自送调整 + if msg.Body["status"].(string) == ebaiapi.WaybillStatusSelfDelivery { + vendorOrderID := GetOrderIDFromMsg(msg) + orderMap, err := api.EbaiAPI.OrderGet(vendorOrderID) + if err == nil { + err = CurPurchaseHandler.OnOrderDetail(orderMap, partner.UpdatedPeration) + } } } return err } -func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interface{}) (afsOrder *model.AfterSalesOrder) { - afsOrder = &model.AfterSalesOrder{ - VendorID: model.VendorIDEBAI, - AfterSalesOrderID: utils.Interface2String(orderData["order_id"]), - VendorOrderID: utils.Interface2String(orderData["order_id"]), +func (p *PurchaseHandler) OrderFinancialDetail2Refund(orderFinancial *model.OrderFinancial, msg *ebaiapi.CallbackMsg) (afsOrder *model.AfsOrder) { + afsOrder = &model.AfsOrder{ + VendorID: model.VendorIDEBAI, + AfsOrderID: utils.Interface2String(msg.Body["refund_id"]), + VendorOrderID: utils.Interface2String(msg.Body["refund_id"]), + AfsCreateAt: utils.Timestamp2Time(utils.Str2Int64(utils.Interface2String(msg.Body["timestamp"]))), + // BoxMoney: orderFinancial.BoxMoney, // 饿百的餐盒费已经拆分到单条Sku里面,退款时直接计算用户支付sku金额就好了 + // SkuBoxMoney: orderFinancial.SkuBoxMoney, + FreightUserMoney: orderFinancial.FreightMoney, + SkuUserMoney: orderFinancial.ActualPayMoney - orderFinancial.FreightMoney, + PmSubsidyMoney: orderFinancial.PmSubsidyMoney, + } + order, err := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID) + globals.SugarLogger.Debug(utils.Format4Output(order, false)) + if err == nil { + afsOrder.JxStoreID = order.JxStoreID + afsOrder.VendorStoreID = order.VendorStoreID + afsOrder.StoreID = order.StoreID + } else { + globals.SugarLogger.Warnf("ebai AfsOrderDetail2Financial, afsOrderID:%s is not found from partner.CurOrderManager.LoadOrder", afsOrder.VendorOrderID) + err = nil + } + for _, sku := range orderFinancial.Skus { + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: sku.VendorID, + VendorOrderID: sku.VendorOrderID, + VendorOrderID2: sku.VendorOrderID2, + AfsOrderID: sku.VendorOrderID, + // ConfirmTime: afsOrder.AfsCreateAt, + VendorStoreID: afsOrder.VendorStoreID, + StoreID: afsOrder.StoreID, + JxStoreID: afsOrder.JxStoreID, + VendorSkuID: sku.VendorSkuID, + SkuID: sku.SkuID, + PromotionType: sku.PromotionType, + Name: sku.Name, + ShopPrice: sku.ShopPrice, + SalePrice: sku.SalePrice, + Count: sku.Count, + SkuBoxMoney: sku.SkuBoxMoney, + UserMoney: sku.UserMoney, + PmSubsidyMoney: sku.PmSubsidyMoney, + IsAfsOrder: 1, + } + afsOrder.Skus = append(afsOrder.Skus, orderSkuFinancial) + } + return afsOrder +} + +func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interface{}) (afsOrder *model.AfsOrder) { + afsOrder = &model.AfsOrder{ + VendorID: model.VendorIDEBAI, + AfsOrderID: utils.Interface2String(orderData["order_id"]), + VendorOrderID: utils.Interface2String(orderData["order_id"]), } order, err := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID) if err == nil { @@ -47,21 +111,24 @@ func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interfac refundDetail := orderData["refund_detail"].([]interface{}) for _, refundInfo := range refundDetail { xMap := refundInfo.(map[string]interface{}) - orderSku := &model.AfterSalesOrderSku{ - VendorID: model.VendorIDEBAI, - AfterSalesOrderID: afsOrder.AfterSalesOrderID, - VendorOrderID: afsOrder.VendorOrderID, - ConfirmTime: getTimeFromInterface(xMap["apply_time"]), - VendorSkuID: utils.Interface2String(xMap["sku_id"]), - SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["custom_sku_id"]))), - Name: utils.Interface2String(xMap["name"]), - SkuUserMoney: utils.MustInterface2Int64(xMap["total_refund"]), - SkuVendorMoney: utils.MustInterface2Int64(xMap["shop_ele_refund"]), + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: model.VendorIDEBAI, + AfsOrderID: afsOrder.AfsOrderID, + VendorOrderID: afsOrder.VendorOrderID, + // ConfirmTime: getTimeFromInterface(xMap["apply_time"]), + VendorSkuID: utils.Interface2String(xMap["sku_id"]), + SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["custom_sku_id"]))), + Name: utils.Interface2String(xMap["name"]), + UserMoney: utils.MustInterface2Int64(xMap["total_refund"]), + PmSubsidyMoney: utils.MustInterface2Int64(xMap["shop_ele_refund"]), + IsAfsOrder: 1, } - afsOrder.Skus = append(afsOrder.Skus, orderSku) + afsOrder.Skus = append(afsOrder.Skus, orderSkuFinancial) + afsOrder.SkuUserMoney += orderSkuFinancial.UserMoney + afsOrder.PmSubsidyMoney += orderSkuFinancial.PmSubsidyMoney } if len(refundDetail) > 0 { - afsOrder.ConfirmTime = afsOrder.Skus[0].ConfirmTime + afsOrder.AfsCreateAt = getTimeFromInterface(refundDetail[0].(map[string]interface{})["apply_time"]) } else { globals.SugarLogger.Warnf("ebai AfsOrderDetail2Financial, orderID:%s have no refund_detail", afsOrder.VendorOrderID) } @@ -72,38 +139,48 @@ func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interfac } // 存储饿百正向订单结账信息 -func (p *PurchaseHandler) OnOrderDetail(result map[string]interface{}) (err error) { - err = orderman.SaveOrderFinancialInfo(p.OrderDetail2Financial(result)) +func (p *PurchaseHandler) OnOrderDetail(result map[string]interface{}, operation string) (err error) { + err = partner.CurOrderManager.SaveOrderFinancialInfo(p.OrderDetail2Financial(result), operation) return err } -func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) (orderDetail *model.OrderFinancial) { - orderDetail = &model.OrderFinancial{ +func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) (orderFinancial *model.OrderFinancial) { + orderFinancial = &model.OrderFinancial{ VendorID: model.VendorIDEBAI, } order1 := result["order"].(map[string]interface{}) - orderDetail.VendorOrderID = utils.Interface2String(order1["order_id"]) - orderDetail.VendorOrderID2 = utils.Interface2String(order1["eleme_order_id"]) - orderDetail.DeliveryConfirmTime = getTimeFromInterface(order1["finished_time"]) - orderDetail.TotalDiscountMoney = utils.MustInterface2Int64(order1["discount_fee"]) - orderDetail.ReceivableFreight = utils.MustInterface2Int64(order1["send_fee"]) + orderFinancial.VendorOrderID = utils.Interface2String(order1["order_id"]) + orderFinancial.VendorOrderID2 = utils.Interface2String(order1["eleme_order_id"]) + // orderFinancial.DeliveryConfirmTime = getTimeFromInterface(order1["finished_time"]) + orderFinancial.TotalDiscountMoney = utils.MustInterface2Int64(order1["discount_fee"]) + orderFinancial.ReceivableFreight = utils.MustInterface2Int64(order1["send_fee"]) if int(utils.MustInterface2Int64(order1["send_immediately"])) == ebaiapi.SendImmediatelySelf { - orderDetail.SelfDeliveryDiscountMoney = orderDetail.ReceivableFreight + orderFinancial.SelfDeliveryDiscountMoney = orderFinancial.ReceivableFreight + orderFinancial.DistanceFreightMoney = 0 + // 通过本地数据库去取是否转美团/达达,并计算运费 + // wayBill, err := partner.CurOrderManager.LoadWaybill(orderFinancial.VendorOrderID, orderFinancial.VendorID) + // if err == nil { + // orderFinancial.JxFreightMoney = wayBill.DesiredFee + // } } - orderDetail.BoxMoney = utils.MustInterface2Int64(order1["package_fee"]) - orderDetail.ActualPayMoney = utils.MustInterface2Int64(order1["user_fee"]) - orderDetail.PmMoney = utils.MustInterface2Int64(order1["commission"]) - orderDetail.ShopMoney = utils.MustInterface2Int64(order1["shop_fee"]) - order, err := partner.CurOrderManager.LoadOrder(orderDetail.VendorOrderID, orderDetail.VendorID) + orderFinancial.BoxMoney = utils.MustInterface2Int64(order1["package_fee"]) + orderFinancial.ActualPayMoney = utils.MustInterface2Int64(order1["user_fee"]) + orderFinancial.PmMoney = utils.MustInterface2Int64(order1["commission"]) + orderFinancial.ShopMoney = utils.MustInterface2Int64(order1["shop_fee"]) + order, err := partner.CurOrderManager.LoadOrder(orderFinancial.VendorOrderID, orderFinancial.VendorID) + storeID := 0 + jxStoreID := 0 if err == nil { + storeID = order.StoreID + jxStoreID = order.JxStoreID skus := order.Skus if skus != nil { for _, x := range skus { - orderDetail.ShopPriceMoney += x.ShopPrice + orderFinancial.ShopPriceMoney += x.ShopPrice * int64(x.Count) } } } else { - globals.SugarLogger.Warnf("ebai OrderDetail2Financial, orderID:%s is not found from partner.CurOrderManager.LoadOrder", orderDetail.VendorOrderID) + globals.SugarLogger.Warnf("ebai OrderDetail2Financial, orderID:%s is not found from partner.CurOrderManager.LoadOrder", orderFinancial.VendorOrderID) err = nil } if result["products"] != nil { @@ -111,7 +188,27 @@ func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) ( for _, x := range products { for _, y := range x.([]interface{}) { product := y.(map[string]interface{}) - orderDetail.SalePriceMoney += utils.MustInterface2Int64(product["product_price"]) * utils.MustInterface2Int64(product["product_amount"]) + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + VendorOrderID2: orderFinancial.VendorOrderID2, + // OrderFinancialID: orderFinancial.VendorOrderID, + // ConfirmTime: getTimeFromInterface(order1["create_time"]), + VendorStoreID: result["app_poi_code"].(string), + StoreID: storeID, + JxStoreID: jxStoreID, + VendorSkuID: utils.Interface2String(product["baidu_product_id"]), + SkuID: int(utils.Str2Int64(utils.Interface2String(product["custom_sku_id"]))), + Name: utils.Interface2String(product["product_name"]), + SalePrice: utils.MustInterface2Int64(product["product_price"]), + Count: int(utils.MustInterface2Int64(product["product_amount"])), + SkuBoxMoney: utils.MustInterface2Int64(product["package_fee"]), + IsAfsOrder: 0, + } + orderFinancial.Skus = append(orderFinancial.Skus, orderSkuFinancial) + orderFinancial.SalePriceMoney += orderSkuFinancial.SalePrice * int64(orderSkuFinancial.Count) + orderFinancial.SkuBoxMoney += orderSkuFinancial.SkuBoxMoney + orderFinancial.BoxMoney -= orderSkuFinancial.SkuBoxMoney } } } @@ -119,9 +216,21 @@ func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) ( discount := result["discount"].([]interface{}) for _, x := range discount { xMap := x.(map[string]interface{}) - orderDetail.DiscountMoney += utils.MustInterface2Int64(xMap["fee"]) - orderDetail.PmSubsidyMoney += utils.MustInterface2Int64(xMap["baidu_rate"]) // 平台承担补贴 + discountPrice := utils.MustInterface2Int64(xMap["fee"]) + orderFinancial.DiscountMoney += discountPrice + orderFinancial.PmSubsidyMoney += utils.MustInterface2Int64(xMap["baidu_rate"]) // 平台承担补贴 + activity := &model.OrderDiscountFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + // ActivityName: utils.Interface2String(xMap["desc"]), + // ActivityMoney: discountPrice, + VendorActivityID: utils.Interface2String(xMap["activity_id"]), + } + orderFinancial.Discounts = append(orderFinancial.Discounts, activity) + // 通过活动Id去取,京西活动补贴 + // orderFinancial.JxSubsidyMoney += } } - return orderDetail + orderFinancial.FreightDiscountMoney = orderFinancial.TotalDiscountMoney - orderFinancial.DiscountMoney + return orderFinancial } diff --git a/business/partner/purchase/ebai/financial_test.go b/business/partner/purchase/ebai/financial_test.go index b466a4781..9e601de51 100644 --- a/business/partner/purchase/ebai/financial_test.go +++ b/business/partner/purchase/ebai/financial_test.go @@ -1,19 +1,26 @@ package ebai import ( + "encoding/json" "fmt" "testing" + "time" "git.rosy.net.cn/baseapi/platformapi/ebaiapi" + "git.rosy.net.cn/baseapi/utils" ) func TestOnFinancialMsg(t *testing.T) { msg := &ebaiapi.CallbackMsg{ - Cmd: "order.partrefund.push", + // Cmd: "order.partrefund.push", + Cmd: "order.user.cancel", Body: make(map[string]interface{}), } - msg.Body["refund_id"] = "15518443822344" - msg.Body["status"] = "20" + msg.Body["refund_id"] = "15531567190216" + msg.Body["timestamp"] = utils.Int64ToStr(time.Now().Unix()) + // msg.Body["status"] = json.Number("20") + msg.Body["type"] = json.Number("40") + msg.Body["cancel_type"] = json.Number("2") res := OnFinancialMsg(msg) fmt.Println(res) } diff --git a/business/partner/purchase/ebai/order.go b/business/partner/purchase/ebai/order.go index cc1d13453..d07b0370b 100644 --- a/business/partner/purchase/ebai/order.go +++ b/business/partner/purchase/ebai/order.go @@ -241,7 +241,7 @@ func (c *PurchaseHandler) onOrderNew(msg *ebaiapi.CallbackMsg) (response *ebaiap if err == nil { if err = partner.CurOrderManager.OnOrderNew(order, order.VendorStatus); err == nil { utils.CallFuncAsync(func() { - c.OnOrderDetail(orderMap) + c.OnOrderDetail(orderMap, partner.CreatedPeration) }) } } diff --git a/business/partner/purchase/jd/financial.go b/business/partner/purchase/jd/financial.go index 636ee7a51..f226ed390 100644 --- a/business/partner/purchase/jd/financial.go +++ b/business/partner/purchase/jd/financial.go @@ -3,7 +3,6 @@ package jd import ( "git.rosy.net.cn/baseapi/platformapi/jdapi" "git.rosy.net.cn/baseapi/utils" - "git.rosy.net.cn/jx-callback/business/jxcallback/orderman" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/partner" "git.rosy.net.cn/jx-callback/globals" @@ -13,14 +12,18 @@ import ( // 京东正向/退款订单类型处理--存储 func OnFinancialMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackResponse) { var err error - if msg.StatusID == jdapi.OrderStatusPayFinishedSettle || msg.StatusID == jdapi.OrderStatusAdjustSettle || msg.StatusID == jdapi.OrderStatusSwitch2SelfSettle { // 如果是正向单 + if msg.StatusID == jdapi.OrderStatusPayFinishedSettle || msg.StatusID == jdapi.OrderStatusTipChanged || msg.StatusID == jdapi.OrderStatusAdjustSettle || msg.StatusID == jdapi.OrderStatusSwitch2SelfSettle { // 如果是正向单 order, err2 := partner.CurOrderManager.LoadOrder(msg.BillID, model.VendorIDJD) if err = err2; err == nil { orderData, err2 := api.JdAPI.QuerySingleOrder(msg.BillID) if err = err2; err == nil { orderFinancial, err2 := curPurchaseHandler.OrderDetail2Financial(orderData, false, order) if err = err2; err == nil { - err = orderman.SaveOrderFinancialInfo(orderFinancial) + if msg.StatusID == jdapi.OrderStatusPayFinishedSettle { + err = partner.CurOrderManager.SaveOrderFinancialInfo(orderFinancial, partner.CreatedPeration) + } else { + err = partner.CurOrderManager.SaveOrderFinancialInfo(orderFinancial, partner.UpdatedPeration) + } } } } else { @@ -29,7 +32,7 @@ func OnFinancialMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackResponse } else if msg.StatusID == jdapi.SaleBillStatusRefundSuccess || msg.StatusID == jdapi.SaleBillStatusSaleReturnSuccess { // 如果是退款单 orderData, err2 := api.JdAPI.GetAfsService(msg.BillID) if err = err2; err == nil { - err = orderman.SaveAfsOrderFinancialInfo(curPurchaseHandler.AfsOrderDetail2Financial(orderData)) + err = partner.CurOrderManager.SaveAfsOrderFinancialInfo(curPurchaseHandler.AfsOrderDetail2Financial(orderData)) } } return jdapi.Err2CallbackResponse(err, "jd OnFinancialMsg") @@ -46,25 +49,60 @@ func (p *PurchaseHandler) OrderDetail2Financial(orderData map[string]interface{} DiscountMoney: utils.MustInterface2Int64(orderData["orderDiscountMoney"]), DistanceFreightMoney: utils.MustInterface2Int64(orderData["merchantPaymentDistanceFreightMoney"]), FreightTipsMoney: utils.MustInterface2Int64(orderData["tips"]), - // BoxFee: utils.MustInterface2Int64(orderData["packagingMoney"]), // 京东包装(塑料袋)由京东提供,相应钱款也归京东,不记录/记录之后优化算法 + PointsDeductionMoney: utils.MustInterface2Int64(orderData["platformPointsDeductionMoney"]), + // BoxMoney: utils.MustInterface2Int64(orderData["packagingMoney"]), // 京东包装(塑料袋)由京东提供,相应钱款也归京东,不记录/记录之后优化算法 } skus := order.Skus if skus != nil { for _, x := range skus { - orderFinancial.ShopPriceMoney += x.ShopPrice + orderFinancial.ShopPriceMoney += x.ShopPrice * int64(x.Count) } } if orderData["product"] != nil { product := orderData["product"].([]interface{}) for _, x := range product { xMap := x.(map[string]interface{}) - orderFinancial.SalePriceMoney += utils.MustInterface2Int64(xMap["skuJdPrice"]) * utils.MustInterface2Int64(xMap["skuCount"]) + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + // OrderFinancialID: orderFinancial.VendorOrderID, + // ConfirmTime: utils.Str2Time(utils.Interface2String(orderData["orderStartTime"])), + VendorStoreID: utils.Interface2String(orderData["deliveryStationNo"]), + StoreID: int(utils.Str2Int64(utils.Interface2String(orderData["deliveryStationNoIsv"]))), + JxStoreID: order.JxStoreID, + VendorSkuID: utils.Int64ToStr(utils.MustInterface2Int64(xMap["skuId"])), + // SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["skuIdIsv"]))), + PromotionType: int(utils.MustInterface2Int64(xMap["promotionType"])), + Name: utils.Interface2String(xMap["skuName"]), + ShopPrice: utils.MustInterface2Int64(xMap["skuStorePrice"]), + SalePrice: utils.MustInterface2Int64(xMap["skuJdPrice"]), + Count: int(utils.MustInterface2Int64(xMap["skuCount"])), + IsAfsOrder: 0, + // MealBoxMoney: utils.MustInterface2Int64(xMap["canteenMoney"]), // 京东的目前不考虑结算餐盒费,因为都归京东所有 + } + + // PromotionType 是一个关键数据,可能某商品活动限购,用户超出限购数量,超出部分不享受优惠,那么除了要在活动表根据skuId,活动起止日期,活动城市来查询,还需要判断活动类型PromotionType + // 平台/京西单条sku补贴金额,应该去京西后台活动库去取,活动里针对单条SKU,如果京东/京西有补贴,应该有记录 + // orderSkuFinancial.PmSubsidyMoneyForSku = + // orderSkuFinancial.JxSubsidyMoneyForSku = + // orderFinancial.JxSubsidyMoneyToSku += orderSkuFinancial.JxSubsidyMoneyForSku + // orderFinancial.JxSubsidyMoney += orderSkuFinancial.JxSubsidyMoneyForSku + orderFinancial.SalePriceMoney += orderSkuFinancial.SalePrice * int64(orderSkuFinancial.Count) + orderSkuFinancial.SkuID = int(utils.Str2Int64WithDefault(utils.Interface2String(xMap["skuIdIsv"]), 0)) + + orderFinancial.Skus = append(orderFinancial.Skus, orderSkuFinancial) } } - orderFinancial.DeliveryConfirmTime = utils.Str2TimeWithDefault(utils.Interface2String(orderData["deliveryConfirmTime"]), utils.DefaultTimeValue) + // orderFinancial.DeliveryConfirmTime = utils.Str2TimeWithDefault(utils.Interface2String(orderData["deliveryConfirmTime"]), utils.DefaultTimeValue) if utils.Interface2String(orderData["deliveryCarrierNo"]) == jdapi.SelfDeliveryCarrierNo { + // 如果为自配送,自配送补贴=订单初始运费,远距离费=0 orderFinancial.SelfDeliveryDiscountMoney = utils.MustInterface2Int64(orderData["orderReceivableFreight"]) orderFinancial.DistanceFreightMoney = 0 + // 通过本地数据库去取是否转美团/达达,并计算运费 + // wayBill, err2 := partner.CurOrderManager.LoadWaybill(orderFinancial.VendorOrderID, orderFinancial.VendorID) + // if err = err2; err == nil { + // orderFinancial.JxFreightMoney = wayBill.DesiredFee + // } } if orderData["discount"] != nil { discount := orderData["discount"].([]interface{}) @@ -74,12 +112,33 @@ func (p *PurchaseHandler) OrderDetail2Financial(orderData map[string]interface{} discountType := int(utils.MustInterface2Int64(xMap["discountType"])) if discountType == jdapi.FreightDiscountTypeByShop { orderFinancial.FreightDiscountMoney = discountPrice - } - if discountType == jdapi.FreightDiscountTypeByVip || discountType == jdapi.FreightDiscountTypeByActivity || discountType == jdapi.FreightDiscountTypeByCoupons { - orderFinancial.VendorFreightDiscountMoney = discountPrice + } else if discountType == jdapi.FreightDiscountTypeByVip || discountType == jdapi.FreightDiscountTypeByActivity { + orderFinancial.PmFreightDiscountMoney = discountPrice + } else if discountType == jdapi.FreightDiscountTypeByCoupons { + if xMap["platPayMoney"] == nil { + orderFinancial.PmFreightDiscountMoney = discountPrice + } else { + orderFinancial.PmFreightDiscountMoney = utils.MustInterface2Int64(xMap["platPayMoney"]) + orderFinancial.FreightDiscountMoney = discountPrice - orderFinancial.PmFreightDiscountMoney + } } orderFinancial.TotalDiscountMoney += discountPrice + if xMap["orderShareRatioData"] != nil { + orderShareRatioData, _ := utils.HTTPBody2Values([]byte(utils.Interface2String(xMap["orderShareRatioData"])), false) + activity := &model.OrderDiscountFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + // ActivityName: utils.Interface2String(xMap["discountName"]), + // ActivityMoney: discountPrice, + VendorActivityID: utils.Interface2String(orderShareRatioData["promotionId"]), + // Remark: utils.Interface2String(xMap["orderShareRatioData"]), + } + orderFinancial.Discounts = append(orderFinancial.Discounts, activity) + // 通过活动Id去取,京西活动补贴 + // orderFinancial.JxSubsidyMoney += + } } + globals.SugarLogger.Debug(utils.Format4Output(orderFinancial.Discounts, false)) } order1, err2 := api.JdAPI.OrderShoudSettlementService(orderFinancial.VendorOrderID) if err = err2; err == nil { @@ -88,8 +147,8 @@ func (p *PurchaseHandler) OrderDetail2Financial(orderData map[string]interface{} orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["freightCommission"], 0) orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["packageCommission"], 0) orderFinancial.PmMoney += utils.Interface2Int64WithDefault(order1["guaranteedCommission"], 0) - orderFinancial.PmSubsidyMoney += utils.Interface2Int64WithDefault(order1["platOrderGoodsDiscountMoney"], 0) - orderFinancial.PmSubsidyMoney += utils.Interface2Int64WithDefault(order1["platSkuGoodsDiscountMoney"], 0) + orderFinancial.PmSkuSubsidyMoney = utils.Interface2Int64WithDefault(order1["platSkuGoodsDiscountMoney"], 0) + orderFinancial.PmSubsidyMoney = utils.Interface2Int64WithDefault(order1["platOrderGoodsDiscountMoney"], 0) + orderFinancial.PmSkuSubsidyMoney } else { if !isFromOrderDetail { globals.SugarLogger.Warnf("jd OrderDetail2Financial, orderID:%s is not found from api.JdAPI.OrderShoudSettlementService", orderFinancial.VendorOrderID) @@ -99,19 +158,19 @@ func (p *PurchaseHandler) OrderDetail2Financial(orderData map[string]interface{} } // 处理京东售后订单结账信息 -func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interface{}) (afsOrder *model.AfterSalesOrder) { - afsOrder = &model.AfterSalesOrder{ +func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interface{}) (afsOrder *model.AfsOrder) { + afsOrder = &model.AfsOrder{ VendorID: model.VendorIDJD, - AfterSalesOrderID: utils.Interface2String(orderData["afsServiceOrder"]), + AfsOrderID: utils.Interface2String(orderData["afsServiceOrder"]), VendorOrderID: utils.Interface2String(orderData["orderId"]), VendorStoreID: utils.Interface2String(orderData["stationId"]), StoreID: int(utils.Str2Int64(utils.Interface2String(orderData["stationNumOutSystem"]))), - ConfirmTime: utils.Timestamp2Time(utils.MustInterface2Int64(orderData["updateTime"].(map[string]interface{})["time"]) / 1000), + AfsCreateAt: utils.Timestamp2Time(utils.MustInterface2Int64(orderData["updateTime"].(map[string]interface{})["time"]) / 1000), FreightUserMoney: utils.MustInterface2Int64(orderData["orderFreightMoney"]), AfsFreightMoney: utils.MustInterface2Int64(orderData["afsFreight"]), BoxMoney: utils.MustInterface2Int64(orderData["packagingMoney"]), TongchengFreightMoney: utils.MustInterface2Int64(orderData["tongchengFreightMoney"]), - MealBoxMoney: utils.MustInterface2Int64(orderData["mealBoxMoney"]), + SkuBoxMoney: utils.MustInterface2Int64(orderData["mealBoxMoney"]), } order, err := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID) if err == nil { @@ -123,24 +182,32 @@ func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData map[string]interfac refundDetail := orderData["afsDetailList"].([]interface{}) for _, x := range refundDetail { xMap := x.(map[string]interface{}) - orderSku := &model.AfterSalesOrderSku{ - VendorID: model.VendorIDJD, - AfterSalesOrderID: afsOrder.AfterSalesOrderID, - VendorOrderID: afsOrder.VendorOrderID, - VendorStoreID: afsOrder.VendorStoreID, - StoreID: afsOrder.StoreID, - ConfirmTime: afsOrder.ConfirmTime, + orderSku := &model.OrderSkuFinancial{ + VendorID: model.VendorIDJD, + AfsOrderID: afsOrder.AfsOrderID, + VendorOrderID: afsOrder.VendorOrderID, + VendorStoreID: afsOrder.VendorStoreID, + StoreID: afsOrder.StoreID, + // ConfirmTime: afsOrder.AfsCreateAt, VendorSkuID: utils.Int64ToStr(utils.MustInterface2Int64(xMap["wareId"])), SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["skuIdIsv"]))), Name: utils.Interface2String(xMap["wareName"]), - SkuUserMoney: utils.MustInterface2Int64(xMap["afsMoney"]), + UserMoney: utils.MustInterface2Int64(xMap["afsMoney"]), + PmSkuSubsidyMoney: utils.MustInterface2Int64(xMap["platPayMoney"]), + IsAfsOrder: 1, } + afsOrder.PmSkuSubsidyMoney += orderSku.PmSkuSubsidyMoney + orderSku.PmSubsidyMoney += orderSku.PmSkuSubsidyMoney + if xMap["afsSkuDiscountList"] != nil { afsSkuDiscountList := xMap["afsSkuDiscountList"].([]interface{}) for _, y := range afsSkuDiscountList { - orderSku.SkuVendorMoney += utils.MustInterface2Int64(y.(map[string]interface{})["platPayMoney"]) + orderSku.PmSubsidyMoney += utils.MustInterface2Int64(y.(map[string]interface{})["platPayMoney"]) } } + + afsOrder.SkuUserMoney += orderSku.UserMoney + afsOrder.PmSubsidyMoney += orderSku.PmSubsidyMoney afsOrder.Skus = append(afsOrder.Skus, orderSku) } if len(refundDetail) <= 0 { @@ -157,7 +224,7 @@ func (p *PurchaseHandler) OnOrderDetail(orderDetail map[string]interface{}) (err if err == nil { orderFinancial, err2 := curPurchaseHandler.OrderDetail2Financial(orderDetail, true, order) if err = err2; err == nil { - err = orderman.SaveOrderFinancialInfo(orderFinancial) + err = partner.CurOrderManager.SaveOrderFinancialInfo(orderFinancial, partner.CreatedPeration) } } return err diff --git a/business/partner/purchase/jd/financial_test.go b/business/partner/purchase/jd/financial_test.go index f4c265b2a..3ce5a73fe 100644 --- a/business/partner/purchase/jd/financial_test.go +++ b/business/partner/purchase/jd/financial_test.go @@ -9,8 +9,8 @@ import ( func TestOnFinancialMsg(t *testing.T) { msg := &jdapi.CallbackOrderMsg{ - BillID: "817102100000041", - StatusID: "330901", + BillID: "907315020000322", + StatusID: "330902", } res := OnFinancialMsg(msg) fmt.Println(res) diff --git a/business/partner/purchase/mtwm/financial.go b/business/partner/purchase/mtwm/financial.go index f5bfd14fa..882fcdad7 100644 --- a/business/partner/purchase/mtwm/financial.go +++ b/business/partner/purchase/mtwm/financial.go @@ -5,37 +5,96 @@ import ( "git.rosy.net.cn/baseapi/platformapi/mtwmapi" "git.rosy.net.cn/baseapi/utils" - "git.rosy.net.cn/jx-callback/business/jxcallback/orderman" "git.rosy.net.cn/jx-callback/business/jxutils" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/partner" "git.rosy.net.cn/jx-callback/globals" ) +const ( + PublicWelfareDonation = 1 // 美团公益捐款金额现阶段是每单一分钱 +) + // 存储美团退款订单结账信息 func OnFinancialMsg(msg *mtwmapi.CallbackMsg) (err error) { if msg.Cmd == mtwmapi.MsgTypeOrderPartialRefund { // 部分退款处理 orderData := msg.Data if orderData.Get("notify_type") == mtwmapi.NotifyTypeSuccess { - err = orderman.SaveAfsOrderFinancialInfo(curPurchaseHandler.AfsOrderDetail2Financial(orderData)) + err = partner.CurOrderManager.SaveAfsOrderFinancialInfo(curPurchaseHandler.AfsOrderDetail2Financial(orderData)) } } if msg.Cmd == mtwmapi.MsgTypeOrderRefund { // todo 全额退款处理 orderData := msg.Data if orderData.Get("notify_type") == mtwmapi.NotifyTypeSuccess { globals.SugarLogger.Debug(orderData.Get("order_id")) // 获得退款订单ID,去本地数据库拿?饿百消息推送只给了订单号,也没有通过订单号查询退款信息的接口 + afsOrderID := orderData.Get("order_id") + orderFinancial, err := partner.CurOrderManager.LoadOrderFinancial(afsOrderID, model.VendorIDMTWM) + if err == nil { + globals.SugarLogger.Debug(utils.Format4Output(orderFinancial, false)) + err = partner.CurOrderManager.SaveAfsOrderFinancialInfo(curPurchaseHandler.OrderFinancialDetail2Refund(orderFinancial, orderData)) + } else { + globals.SugarLogger.Warnf("mtwm OnFinancialMsg, afsOrderID:%s is not found from partner.CurOrderManager.LoadOrderFinancial", afsOrderID) + } } } return err } -func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData url.Values) (afsOrder *model.AfterSalesOrder) { - afsOrder = &model.AfterSalesOrder{ - VendorID: model.VendorIDMTWM, - AfterSalesOrderID: orderData.Get("order_id"), - VendorOrderID: orderData.Get("order_id"), - ConfirmTime: utils.Timestamp2Time(utils.Str2Int64(orderData.Get("timestamp"))), - RefundMoney: jxutils.StandardPrice2Int(utils.Str2Float64(orderData.Get("money"))), +func (p *PurchaseHandler) OrderFinancialDetail2Refund(orderFinancial *model.OrderFinancial, orderData url.Values) (afsOrder *model.AfsOrder) { + afsOrder = &model.AfsOrder{ + VendorID: model.VendorIDMTWM, + AfsOrderID: orderData.Get("order_id"), + VendorOrderID: orderData.Get("order_id"), + AfsCreateAt: utils.Timestamp2Time(utils.Str2Int64(orderData.Get("timestamp"))), + // BoxMoney: orderFinancial.BoxMoney, + // SkuBoxMoney: orderFinancial.SkuBoxMoney, // 美团的餐盒费已经拆到单条SKU里面去了,退款时直接计算用户支付sku金额就好了 + FreightUserMoney: orderFinancial.FreightMoney, + SkuUserMoney: orderFinancial.ActualPayMoney - orderFinancial.FreightMoney, + PmSubsidyMoney: orderFinancial.PmSubsidyMoney, + } + order, err := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID) + globals.SugarLogger.Debug(utils.Format4Output(order, false)) + if err == nil { + afsOrder.JxStoreID = order.JxStoreID + afsOrder.VendorStoreID = order.VendorStoreID + afsOrder.StoreID = order.StoreID + } else { + globals.SugarLogger.Warnf("mtwm AfsOrderDetail2Financial, afsOrderID:%s is not found from partner.CurOrderManager.LoadOrder", afsOrder.VendorOrderID) + err = nil + } + for _, sku := range orderFinancial.Skus { + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: sku.VendorID, + VendorOrderID: sku.VendorOrderID, + VendorOrderID2: sku.VendorOrderID2, + // OrderFinancialID: sku.VendorOrderID, + // ConfirmTime: afsOrder.AfsCreateAt, + VendorStoreID: afsOrder.VendorStoreID, + StoreID: afsOrder.StoreID, + JxStoreID: afsOrder.JxStoreID, + VendorSkuID: sku.VendorSkuID, + SkuID: sku.SkuID, + PromotionType: sku.PromotionType, + Name: sku.Name, + ShopPrice: sku.ShopPrice, + SalePrice: sku.SalePrice, + Count: sku.Count, + UserMoney: sku.UserMoney, + PmSubsidyMoney: sku.PmSubsidyMoney, + IsAfsOrder: 1, + } + afsOrder.Skus = append(afsOrder.Skus, orderSkuFinancial) + } + return afsOrder +} + +func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData url.Values) (afsOrder *model.AfsOrder) { + afsOrder = &model.AfsOrder{ + VendorID: model.VendorIDMTWM, + AfsOrderID: orderData.Get("order_id"), + VendorOrderID: orderData.Get("order_id"), + AfsCreateAt: utils.Timestamp2Time(utils.Str2Int64(orderData.Get("timestamp"))), + RefundMoney: jxutils.StandardPrice2Int(utils.Str2Float64(orderData.Get("money"))), } order, err := partner.CurOrderManager.LoadOrder(afsOrder.VendorOrderID, afsOrder.VendorID) if err == nil { @@ -48,18 +107,22 @@ func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData url.Values) (afsOrd var refundDetail []map[string]interface{} utils.UnmarshalUseNumber([]byte(food), &refundDetail) for _, xMap := range refundDetail { - orderSku := &model.AfterSalesOrderSku{ - VendorID: model.VendorIDMTWM, - AfterSalesOrderID: afsOrder.AfterSalesOrderID, - VendorOrderID: afsOrder.VendorOrderID, - ConfirmTime: afsOrder.ConfirmTime, - VendorSkuID: utils.Interface2String(xMap["app_food_code"]), - SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["sku_id"]))), - Name: utils.Interface2String(xMap["food_name"]), - SkuUserMoney: jxutils.StandardPrice2Int(utils.MustInterface2Float64(xMap["refund_price"]))*utils.MustInterface2Int64(xMap["count"]) + jxutils.StandardPrice2Int(utils.MustInterface2Float64(xMap["box_price"]))*int64(utils.MustInterface2Float64(xMap["box_num"])), + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: model.VendorIDMTWM, + AfsOrderID: afsOrder.AfsOrderID, + VendorOrderID: afsOrder.VendorOrderID, + // ConfirmTime: afsOrder.AfsCreateAt, + VendorSkuID: utils.Interface2String(xMap["app_food_code"]), + SkuID: int(utils.Str2Int64(utils.Interface2String(xMap["sku_id"]))), + Name: utils.Interface2String(xMap["food_name"]), + UserMoney: jxutils.StandardPrice2Int(utils.MustInterface2Float64(xMap["refund_price"]))*utils.MustInterface2Int64(xMap["count"]) + jxutils.StandardPrice2Int(utils.MustInterface2Float64(xMap["box_price"]))*int64(utils.MustInterface2Float64(xMap["box_num"])), + IsAfsOrder: 1, } - afsOrder.Skus = append(afsOrder.Skus, orderSku) + afsOrder.Skus = append(afsOrder.Skus, orderSkuFinancial) + afsOrder.SkuUserMoney += orderSkuFinancial.UserMoney + // afsOrder.PmSubsidyMoney += orderSkuFinancial.PmSubsidyMoney // 美团只给了一个扣款金额,很尴尬、、 } + afsOrder.PmSubsidyMoney += afsOrder.RefundMoney - afsOrder.SkuUserMoney if len(refundDetail) <= 0 { globals.SugarLogger.Warnf("mtwm AfsOrderDetail2Financial, orderID:%s have no refund_detail", afsOrder.VendorOrderID) } @@ -67,57 +130,112 @@ func (p *PurchaseHandler) AfsOrderDetail2Financial(orderData url.Values) (afsOrd } // 存储美团正向订单结账信息 -func (p *PurchaseHandler) OnOrderDetail(result map[string]interface{}) (err error) { - err = orderman.SaveOrderFinancialInfo(p.OrderDetail2Financial(result)) +func (p *PurchaseHandler) OnOrderDetail(result map[string]interface{}, operation string) (err error) { + err = partner.CurOrderManager.SaveOrderFinancialInfo(p.OrderDetail2Financial(result), operation) return err } -func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) (orderDetail *model.OrderFinancial) { - orderDetail = &model.OrderFinancial{ +func (p *PurchaseHandler) OrderDetail2Financial(result map[string]interface{}) (orderFinancial *model.OrderFinancial) { + orderFinancial = &model.OrderFinancial{ VendorID: model.VendorIDMTWM, VendorOrderID: utils.Int64ToStr(utils.MustInterface2Int64(result["order_id"])), } - orderDetail.DeliveryConfirmTime = utils.Str2TimeWithDefault(utils.Interface2String(result["order_completed_time"]), utils.DefaultTimeValue) - order, err := partner.CurOrderManager.LoadOrder(orderDetail.VendorOrderID, orderDetail.VendorID) + // orderFinancial.DeliveryConfirmTime = utils.Str2TimeWithDefault(utils.Interface2String(result["order_completed_time"]), utils.DefaultTimeValue) + order, err := partner.CurOrderManager.LoadOrder(orderFinancial.VendorOrderID, orderFinancial.VendorID) + jxStoreID := 0 if err == nil { + jxStoreID = order.JxStoreID if order.Skus != nil { for _, x := range order.Skus { - orderDetail.ShopPriceMoney += x.ShopPrice + orderFinancial.ShopPriceMoney += x.ShopPrice * int64(x.Count) } } } else { - globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s is not found from partner.CurOrderManager.LoadOrder", orderDetail.VendorOrderID) + globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s is not found from partner.CurOrderManager.LoadOrder", orderFinancial.VendorOrderID) err = nil } + if result["package_bag_money"] != nil { + orderFinancial.BoxMoney = utils.MustInterface2Int64(result["package_bag_money"]) + } detail := result["detail"] if detail != nil { var data []map[string]interface{} utils.UnmarshalUseNumber([]byte(utils.Interface2String(detail)), &data) for _, x := range data { - orderDetail.SalePriceMoney += jxutils.StandardPrice2Int(utils.MustInterface2Float64(x["price"])) * utils.MustInterface2Int64(x["quantity"]) - orderDetail.BoxMoney += jxutils.StandardPrice2Int(utils.MustInterface2Float64(x["box_price"])) * utils.MustInterface2Int64(x["box_num"]) + orderSkuFinancial := &model.OrderSkuFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + // OrderFinancialID: orderFinancial.VendorOrderID, + // ConfirmTime: utils.Str2TimeWithDefault(utils.Interface2String(result["ctime"]), utils.DefaultTimeValue), + VendorStoreID: result["app_poi_code"].(string), + StoreID: 0, + JxStoreID: jxStoreID, + VendorSkuID: utils.Interface2String(x["sku_id"]), + Name: utils.Interface2String(x["food_name"]), + SalePrice: jxutils.StandardPrice2Int(utils.MustInterface2Float64(x["price"])), + Count: int(utils.MustInterface2Int64(x["quantity"])), + SkuBoxMoney: jxutils.StandardPrice2Int(utils.MustInterface2Float64(x["box_price"])) * utils.MustInterface2Int64(x["box_num"]), + IsAfsOrder: 0, + } + + orderFinancial.Skus = append(orderFinancial.Skus, orderSkuFinancial) + orderFinancial.SalePriceMoney += orderSkuFinancial.SalePrice * int64(orderSkuFinancial.Count) + orderFinancial.SkuBoxMoney += orderSkuFinancial.SkuBoxMoney } } else { - globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s have no detail", orderDetail.VendorOrderID) + globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s have no detail", orderFinancial.VendorOrderID) + } + extras := result["extras"] + if extras != nil { + var data []map[string]interface{} + utils.UnmarshalUseNumber([]byte(utils.Interface2String(extras)), &data) + for _, x := range data { + if x["rider_fee"] == nil { + activity := &model.OrderDiscountFinancial{ + VendorID: orderFinancial.VendorID, + VendorOrderID: orderFinancial.VendorOrderID, + // ActivityName: utils.Interface2String(x["remark"]), + // ActivityMoney: jxutils.StandardPrice2Int(utils.MustInterface2Float64(x["reduce_fee"])), + VendorActivityID: utils.Interface2String(x["act_detail_id"]), + } + orderFinancial.Discounts = append(orderFinancial.Discounts, activity) + // 通过活动Id去取,京西活动补贴 + // orderFinancial.JxSubsidyMoney += + } + } } poiReceiveDetail := result["poi_receive_detail"] if poiReceiveDetail != nil { var data map[string]interface{} utils.UnmarshalUseNumber([]byte(utils.Interface2String(poiReceiveDetail)), &data) - orderDetail.ReceivableFreight = utils.MustInterface2Int64(data["logisticsFee"]) - orderDetail.FreightMoney = utils.MustInterface2Int64(data["logisticsFee"]) - orderDetail.ActualPayMoney = utils.MustInterface2Int64(data["onlinePayment"]) - orderDetail.PmMoney = utils.MustInterface2Int64(data["foodShareFeeChargeByPoi"]) - orderDetail.ShopMoney = utils.MustInterface2Int64(data["wmPoiReceiveCent"]) + orderFinancial.ReceivableFreight = utils.MustInterface2Int64(data["logisticsFee"]) + orderFinancial.FreightMoney = utils.MustInterface2Int64(data["logisticsFee"]) + orderFinancial.ActualPayMoney = utils.MustInterface2Int64(data["onlinePayment"]) + orderFinancial.PmMoney = utils.MustInterface2Int64(data["foodShareFeeChargeByPoi"]) + orderFinancial.ShopMoney = utils.MustInterface2Int64(data["wmPoiReceiveCent"]) for _, x := range data["actOrderChargeByMt"].([]interface{}) { - orderDetail.TotalDiscountMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) - orderDetail.PmSubsidyMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) + orderFinancial.TotalDiscountMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) + orderFinancial.PmSubsidyMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) } for _, x := range data["actOrderChargeByPoi"].([]interface{}) { - orderDetail.TotalDiscountMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) + orderFinancial.TotalDiscountMoney += utils.MustInterface2Int64(x.(map[string]interface{})["moneyCent"]) } } else { - globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s have no poi_receive_detail", orderDetail.VendorOrderID) + globals.SugarLogger.Warnf("mtwm OrderDetail2Financial, orderID:%s have no poi_receive_detail", orderFinancial.VendorOrderID) } - return orderDetail + if utils.MustInterface2Int64(result["is_third_shipping"]) == 1 { // is_third_shipping int 是否是第三方配送平台配送,0表否,1表是) + orderFinancial.SelfDeliveryDiscountMoney = orderFinancial.ReceivableFreight + orderFinancial.DistanceFreightMoney = 0 + // 通过本地数据库去取是否转美团/达达,并计算运费 + wayBill, err := partner.CurOrderManager.LoadWaybill(orderFinancial.VendorOrderID, orderFinancial.VendorID) + if err == nil { + orderFinancial.JxFreightMoney = wayBill.DesiredFee + } + } + // // 美团订单单独处理部分,美团正向订单接口推送时总结算金额没有计算公益捐款一分钱,是否在这里直接提前扣除? + // // 3/18之后的订单一直都不显示公益捐款金额,而且结算现在结算到了3/18之后的订单,没有的已经不计算了,先注释 + // // 2019-04-03 10.52 询问赵mf, 此计划是必须参加的,而且是长期的,每单固定扣除一分钱 + orderFinancial.DonationMoney = PublicWelfareDonation + orderFinancial.ShopMoney -= PublicWelfareDonation + return orderFinancial } diff --git a/business/partner/purchase/mtwm/financial_test.go b/business/partner/purchase/mtwm/financial_test.go index 843983ef9..f483e6a50 100644 --- a/business/partner/purchase/mtwm/financial_test.go +++ b/business/partner/purchase/mtwm/financial_test.go @@ -12,11 +12,11 @@ import ( func TestOnFinancialMsg(t *testing.T) { msg := &mtwmapi.CallbackMsg{ - Cmd: "orderPartialRefund", + Cmd: "orderRefund", Data: url.Values{}, } msg.Data.Set("timestamp", utils.Int64ToStr(time.Now().Unix())) - msg.Data.Set("order_id", "123458") + msg.Data.Set("order_id", "33762863167364867") msg.Data.Set("notify_type", "agree") msg.Data.Set("money", "23.56") food := []map[string]interface{}{ diff --git a/business/partner/purchase/mtwm/order.go b/business/partner/purchase/mtwm/order.go index 549c65feb..5f529e2d1 100644 --- a/business/partner/purchase/mtwm/order.go +++ b/business/partner/purchase/mtwm/order.go @@ -21,7 +21,9 @@ const ( FakeMsgTypeOrderReceived = "orderReceived" FakeMsgTypeOrderDelivering = "orderDelivering" ) - +const ( + SelfDeliveryCarrierNo = 1 // 美团配送方式:0-美团专送,1-商家自送 +) const ( pickupOrderDelay = 260 * time.Second @@ -156,13 +158,25 @@ func (c *PurchaseHandler) onOrderMsg(msg *mtwmapi.CallbackMsg) (response *mtwmap } if err == nil { utils.CallFuncAsync(func() { - c.OnOrderDetail(orderMap) + if msg.Cmd == mtwmapi.MsgTypeNewOrder { + c.OnOrderDetail(orderMap, partner.CreatedPeration) + } else { + c.OnOrderDetail(orderMap, partner.UpdatedPeration) + } }) } } } else { status := c.callbackMsg2Status(msg) err = partner.CurOrderManager.OnOrderStatusChanged(status) + if err == nil && msg.Cmd == mtwmapi.MsgTypeOrderFinished { + utils.CallFuncAsync(func() { + orderMap, err := api.MtwmAPI.OrderGetOrderDetail(utils.Str2Int64(GetOrderIDFromMsg(msg)), true) + if err == nil && utils.MustInterface2Int64(orderMap["is_third_shipping"]) == SelfDeliveryCarrierNo { + c.OnOrderDetail(orderMap, partner.UpdatedPeration) + } + }) + } } return mtwmapi.Err2CallbackResponse(err, "") } diff --git a/controllers/financial.go b/controllers/financial.go index 8f581a147..18b915519 100644 --- a/controllers/financial.go +++ b/controllers/financial.go @@ -6,6 +6,7 @@ import ( "github.com/astaxie/beego" + "git.rosy.net.cn/jx-callback/business/jxcallback/orderman" "git.rosy.net.cn/jx-callback/business/jxstore/financial" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/legacymodel" @@ -92,3 +93,25 @@ func (c *FinancialController) ShowStoreLatestBill() { io.WriteString(w, htmlResult) } } + +// @Title 查询正向订单结账信息 +// @Description 查询正向订单结账信息 +// @Param token header string true "认证token" +// @Param orderID query string false "订单号,如果此项不为空,忽略其它所有查询条件" +// @Param fromDate query string false "开始日期(包含),格式(2006-01-02),如果订单号为空此项必须要求" +// @Param toDate query string false "结束日期(包含),格式(2006-01-02),如果订单号为空此项必须要求" +// @Param vendorIDs query string false "订单所属厂商列表[1,2,3],缺省不限制" +// @Param storeIDs query string false "京西门店ID列表[1,2,3],缺省不限制" +// @Param statuss query string false "订单状态列表[1,2,3],缺省不限制" +// @Param cities query string false "城市code列表[1,2,3],缺省不限制" +// @Param offset query int false "结果起始序号(以0开始,缺省为0)" +// @Param pageSize query int false "结果页大小(缺省为50,-1表示全部)" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /GetOrdersFinancial [get] +func (c *FinancialController) GetOrdersFinancial() { + c.callGetOrdersFinancial(func(params *tFinancialGetOrdersFinancialParams) (retVal interface{}, errCode string, err error) { + retVal, err = orderman.FixedOrderManager.GetOrdersFinancial(params.Ctx, params.FromDate, params.ToDate, params.MapData, params.Offset, params.PageSize) + return retVal, "", err + }) +} diff --git a/globals/beegodb/beegodb.go b/globals/beegodb/beegodb.go index 7ceb2f16b..45862f24e 100644 --- a/globals/beegodb/beegodb.go +++ b/globals/beegodb/beegodb.go @@ -36,7 +36,8 @@ func Init() { orm.RegisterModel(&model.Promotion{}, &model.PromotionStore{}, &model.PromotionSku{}) orm.RegisterModel(&model.AuthBind{}, &model.User{}) - orm.RegisterModel(&model.OrderFinancial{}, &model.AfterSalesOrder{}, &model.AfterSalesOrderSku{}) + orm.RegisterModel(&model.OrderFinancial{}, &model.AfsOrder{}, &model.OrderDiscountFinancial{}, &model.OrderSkuFinancial{}) + // orm.RegisterModel(&model.ActivityForSku{}) // orm.RegisterModel(&legacymodel.JxBadComments2{}) diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 94e66eb90..db5b7a313 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -295,6 +295,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:FinancialController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:FinancialController"], + beego.ControllerComments{ + Method: "GetOrdersFinancial", + Router: `/GetOrdersFinancial`, + AllowHTTPMethods: []string{"get"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:FinancialController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:FinancialController"], beego.ControllerComments{ Method: "GetStoreBills",