package ebai

import (
	"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

func (p *PurchaseHandler) UpdatePlaces() (err error) {
	provinces, err := api.EbaiAPI.CommonShopCities(0)
	if err == nil {
		task := tasksch.NewParallelTask("UpdatePlaces", nil, jxcontext.AdminCtx,
			func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
				province := batchItemList[0].(*ebaiapi.CityInfo)
				retSlice := make([]*ebaiapi.CityInfo, 0)
				if province.IsOpen != 0 {
					retSlice = append(retSlice, province)
					cities, err := api.EbaiAPI.CommonShopCities(province.ID)
					for _, city := range cities {
						if city.IsOpen != 0 {
							retSlice = append(retSlice, city)
							districts, err2 := api.EbaiAPI.CommonShopCities(city.ID)
							if err = err2; err == nil {
								for _, district := range districts {
									if district.IsOpen != 0 {
										retSlice = append(retSlice, district)
									}
								}
							} else {
								break
							}
						}
					}
				}
				return retSlice, err
			}, provinces)
		task.Run()
		places, err2 := task.GetResult(0)
		if err = err2; err == nil {
			globals.SugarLogger.Debug(utils.Format4Output(places, false))
		}
	}
	return err
}
