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") + } +}