From c0b6ae7de7df437df409abe81cf4db12540a574d Mon Sep 17 00:00:00 2001 From: gazebo Date: Fri, 2 Aug 2019 13:10:48 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=9C=A8=E4=BF=AE=E6=94=B9=E7=BE=8E?= =?UTF-8?q?=E5=9B=A2=E5=A4=96=E5=8D=96=E9=97=A8=E5=BA=97=E8=90=A5=E4=B8=9A?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=94=B1=E4=BA=8E=E9=85=8D=E9=80=81=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E9=99=90=E5=88=B6=E5=87=BA=E9=94=99=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E5=B0=9D=E8=AF=95=E6=A0=B9=E6=8D=AE=E9=99=90=E5=88=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=B0=83=E6=95=B4=E5=90=8E=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/partner/purchase/mtwm/store.go | 55 ++++++++++++++++++++ business/partner/purchase/mtwm/store_test.go | 17 +++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/business/partner/purchase/mtwm/store.go b/business/partner/purchase/mtwm/store.go index 50643480a..6444b41a7 100644 --- a/business/partner/purchase/mtwm/store.go +++ b/business/partner/purchase/mtwm/store.go @@ -2,6 +2,9 @@ package mtwm import ( "errors" + "math" + "regexp" + "strings" "git.rosy.net.cn/baseapi/platformapi/mtwmapi" "git.rosy.net.cn/baseapi/utils" @@ -21,6 +24,10 @@ const ( VendorStorePrefix = "美好菜市" ) +var ( + opTimeErrReg = regexp.MustCompile(`当前配送营业时间为:([\d:~,]*)`) +) + type tEbaiStoreInfo struct { model.Store VendorStoreID string `orm:"column(vendor_store_id)"` @@ -186,10 +193,58 @@ func (c *PurchaseHandler) UpdateStoreStatus(ctx *jxcontext.Context, storeID int, return err } +func errOpStr2Int16(str string) []int16 { + list := strings.Split(str, "~") + if len(list) >= 2 { + return []int16{jxutils.StrTime2JxOperationTime(list[0]+":00", 0), jxutils.StrTime2JxOperationTime(list[1]+":00", 2359)} + } + return nil +} + +func getOpTimeListFromErr(err error) (opTimeList []int16) { + if errExt, ok := err.(*utils.ErrorWithCode); ok && errExt.IntCode() == mtwmapi.ErrCodeOpFailed { + if result := opTimeErrReg.FindStringSubmatch(errExt.ErrMsg()); len(result) >= 2 { + timeStrList := strings.Split(result[1], ",") + for _, v := range timeStrList { + v = utils.TrimBlankChar(v) + if len(v) == len("00:00~02:00") { + opTimeList = append(opTimeList, errOpStr2Int16(v)...) + } + } + } + } + return opTimeList +} + +// 此函数只是简单实现,不支持区间切分,只做单一区间限制 +func constrainOpTimeList(opTimeList, validOpTimeList []int16) (newOpTimeList []int16) { + for k := 0; k < len(opTimeList); k += 2 { + beginTime := opTimeList[k] + endTime := opTimeList[k+1] + for k2 := 0; k2 < len(validOpTimeList); k2 += 2 { + beginTime2 := validOpTimeList[k2] + endTime2 := validOpTimeList[k2+1] + if beginTime >= beginTime2 && beginTime <= endTime2 { + newOpTimeList = append(newOpTimeList, beginTime) + newOpTimeList = append(newOpTimeList, int16(math.Min(float64(endTime), float64(endTime2)))) + } else if beginTime2 >= beginTime && beginTime2 <= endTime { + newOpTimeList = append(newOpTimeList, beginTime2) + newOpTimeList = append(newOpTimeList, int16(math.Min(float64(endTime), float64(endTime2)))) + } + } + } + return newOpTimeList +} + func (c *PurchaseHandler) UpdateStoreOpTime(ctx *jxcontext.Context, storeID int, vendorStoreID string, opTimeList []int16) (err error) { shippingTime := openTimeJX2Mtwm(opTimeList) if globals.EnableMtwmStoreWrite { err = api.MtwmAPI.PoiShipTimeUpdate(vendorStoreID, shippingTime) + if err != nil { + if validOpTimeList := getOpTimeListFromErr(err); len(validOpTimeList) > 0 { + err = api.MtwmAPI.PoiShipTimeUpdate(vendorStoreID, openTimeJX2Mtwm(constrainOpTimeList(opTimeList, validOpTimeList))) + } + } } return err } diff --git a/business/partner/purchase/mtwm/store_test.go b/business/partner/purchase/mtwm/store_test.go index 726fb8a74..48de8686e 100644 --- a/business/partner/purchase/mtwm/store_test.go +++ b/business/partner/purchase/mtwm/store_test.go @@ -10,7 +10,7 @@ import ( ) func TestReadStore(t *testing.T) { - store, err := new(PurchaseHandler).ReadStore(jxcontext.AdminCtx, "4351018") + store, err := curPurchaseHandler.ReadStore(jxcontext.AdminCtx, "4351018") if err != nil { t.Fatal(err) } @@ -18,8 +18,21 @@ func TestReadStore(t *testing.T) { } func TestUpdateStore(t *testing.T) { - err := new(PurchaseHandler).UpdateStore(nil, 100002, "test") + err := curPurchaseHandler.UpdateStore(nil, 100002, "test") if err != nil { t.Fatal(err) } } + +func TestConstrainOpTimeList(t *testing.T) { + timeList := constrainOpTimeList([]int16{830, 1800}, []int16{ + 0, + 200, + 930, + 1700, + }) + t.Log(utils.Format4Output(timeList, false)) + if timeList[0] != 930 || timeList[1] != 1700 { + t.Fatal("constrainOpTimeList failed") + } +}