package svc import ( offical "applet/app/db/official" "applet/app/task/md" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/xiaoju" "encoding/json" "fmt" "github.com/jinzhu/copier" "github.com/tidwall/gjson" "time" ) func OilAddEs() { oilAddEsTime := offical.SysCfgByKeyStr("oil_add_es_time") today := utils.GetTimeRange("today") if utils.TimeStdParseUnix(oilAddEsTime) > today["start"] { return } PessimismLockValue := "zhiyingOilAddEsTime" key := fmt.Sprintf("zhiyingOilAddEsTime") //TODO::增加“悲观锁”防止串行 getString, _ := cache.GetString(key) if getString == PessimismLockValue { fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次未执行完") return } cache.SetEx(key, PessimismLockValue, 3600*15) //8小时 offical.DbsSysCfgUpdate("oil_add_es_time", time.Now().Format("2006-01-02 15:04:05")) CommOilAddEs(1) cache.Del(key) } func CommOilAddEs(p int) { platformKey := XiaojuKey() token := GetXiaojuToken(platformKey) data := map[string]interface{}{ "pageIndex": p, "pageSize": 100, } list, _ := xiaoju.XiaojuStation(platformKey, token, data) //处理数据 storeInfoList := gjson.Get(list, "storeInfoList").String() var store = make([]md.XiaojuGoods, 0) json.Unmarshal([]byte(storeInfoList), &store) count := len(store) for _, v := range store { var tmp md.XiaojuGoodsEs copier.Copy(&tmp, &v) tmp.HuiLabels = utils.SerializeStr(v.HuiLabels) for _, v1 := range v.ItemInfoList { var tmp2 = tmp tmp2.ItemName = v1.ItemName tmp2.IndexId = tmp2.StoreId + tmp2.ItemName tmp2.CityPrice = float64(v1.CityPrice) / 100 tmp2.StorePrice = float64(v1.StorePrice) / 100 tmp2.Location.Lon = tmp2.Lon tmp2.Location.Lat = tmp2.Lat doc, _ := es.FirstDoc(md.EggOilStation, tmp2.IndexId) if doc == nil { createDocRet, _ := es.CreateDoc(md.EggOilStation, tmp2.IndexId, tmp2) fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } else { createDocRet, _ := es.UpdateDoc(md.EggOilStation, tmp2.IndexId, tmp2) fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } } } if count == 100 { p++ CommOilAddEs(p) return } return }