package search
import (
"bytes"
"context"
"encoding/json"
"fmt"
"gorm.io/gorm"
"io"
"loads_srv/loads_srv"
"loads_srv/loads_srv/models"
"loads_srv/loads_srv/postgres"
"net/http"
)
const AtiLoadsSearchUrl = "*URL*"
type searchFilterJson struct {
ExcludeGeoDicts bool `json:"exclude_geo_dicts"`
Page int `json:"page"`
ItemsPerPage int `json:"items_per_page"`
Filter struct {
From struct {
Id string `json:"id"`
Type int `json:"type"`
ExactOnly bool `json:"exact_only"`
} `json:"from"`
Dates struct {
DateOption string `json:"date_option"`
} `json:"dates"`
WithDimensions bool `json:"with_dimensions"`
ChangeDate int `json:"change_date"`
SortingType int `json:"sorting_type"`
WithAuction bool `json:"with_auction"`
Firm struct {
FirmRating int `json:"firm_rating"`
} `json:"firm"`
} `json:"filter"`
}
func createSearchFilterJson() searchFilterJson {
var searchFilter = searchFilterJson{
ExcludeGeoDicts: true,
Page: 1,
ItemsPerPage: 10,
}
searchFilter.Filter.WithAuction = false
searchFilter.Filter.WithDimensions = false
searchFilter.Filter.SortingType = 2
searchFilter.Filter.ChangeDate = 1
searchFilter.Filter.Dates.DateOption = "today-plus"
searchFilter.Filter.Firm.FirmRating = 0
searchFilter.Filter.From.ExactOnly = true
searchFilter.Filter.From.Type = 1
return searchFilter
}
func searchLoads(db *gorm.DB, regionId string) {
var region models.LoadsSearchRegion
err := db.Where(&models.LoadsSearchRegion{RegionId: regionId}).First(®ion)
if err.Error != nil {
panic(err.Error)
}
var searchFilter = createSearchFilterJson()
searchFilter.Filter.From.Id = regionId
var searchFilterJsonBytes, _ = json.Marshal(searchFilter)
req, _ := http.NewRequest("POST", AtiLoadsSearchUrl, bytes.NewBuffer(searchFilterJsonBytes))
//req.Header.Add("Cookie", "uicult2=ru; _gcl_au=1.1.333980688.1702924027")
//req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, reqErr := client.Do(req) // <- Ошибка тут
if reqErr != nil { // <- Ошибка тут
panic(reqErr)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
var body, _ = io.ReadAll(resp.Body)
fmt.Print(string(body))
//var headers = make(map[string][]string)
//headers["Cookie"] = append(headers["Cookie"], "uicult2=ru; _gcl_au=1.1.333980688.1702924027")
//headers["Content-Type"] = append(headers["Content-Type"], "application/json")
//var pagesCounter = 1
//var requestsCounter = 0
//var reqCtx, cancel = context.WithTimeout(context.Background(), time.Second*5)
//defer cancel()
}
func SearchingRoutine(workNowChan chan int, regionIdChan chan string, ctx context.Context, id int) {
var app = loads_srv.GetApplication()
db, err := postgres.Connect(app.Config)
if err != nil {
panic(err)
}
Loop:
for {
select {
case nowId := <-workNowChan:
if id == nowId {
var regionId = <-regionIdChan
searchLoads(db, regionId)
}
case <-ctx.Done():
break Loop
}
}
}