123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- os := import("os")
- fmt := import("fmt")
- json := import("json")
- args := os.args()[2:]
- file_path := args[0]
- contact_field_id := 231711
- company_field_id := 1300614
- lead_field_id := 686597
- holders := json.decode(os.read_file(file_path))
- //fmt.println(len(holders))
- //os.exit(0)
- patch := []
- find_by_field_id := func(id, fields){
- for _, field in fields {
- if field.field_id == id {
- return field
- }
- }
- }
- is_field_correct := func(field){
- if !int(field.values[0].value) {
- return false
- }
- str := string(field.values[0].value)
- if len(str) <9 || len(str) > 12 {
- return false
- }
- return true
- }
- //holders = holders[:2]
- for i, h in holders {
- company := {}
- if !h.company_id {
- continue
- }
- company.id = h.company_id
- company_inn := find_by_field_id(company_field_id, h.company_inns)
- //fmt.println(i, " reachd", company_inn)
- if company_inn {
- continue
- }
- contact_inn := find_by_field_id(contact_field_id, h.contact_inns)
- lead_inn := find_by_field_id(lead_field_id, h.lead_inns)
- if !contact_inn {
- if !lead_inn || !is_field_correct(lead_inn) {
- continue
- }
- company.__reason = "Взято из лида (в контакте не было)"
- company.custom_fields_values = [{
- field_id: company_field_id,
- values: [{
- value: string(lead_inn.values[0].value)
- }]
- }]
- patch += [company]
- continue
- }
- if !lead_inn {
- if !is_field_correct(contact_inn) {
- continue
- }
- company.__reason = "Взято из контакта (в лиде не было)"
- company.custom_fields_values =[{
- field_id: company_field_id,
- values: [{
- value: string(contact_inn.values[0].value)
- }]
- }]
- } else {
- if int(contact_inn.values[0].value) != int(lead_inn.values[0].value) {
- continue
- }
- if !is_field_correct(contact_inn) {
- continue
- }
- company.__reason = "Взято из лида и контакта"
- company.custom_fields_values =[{
- field_id: company_field_id,
- values: [{
- value: string(contact_inn.values[0].value)
- }]
- }]
- }
- patch += [company]
- /*if h.company_inns {
- for _, field in h.company_inns {
- company.custom_fields_values += [{
- field_id: field.field_id,
- values: [{
- value: field.values[0].value
- }]
- }]
- }
- }
- companies += [company]*/
- }
- //patch = patch[:500]
- //fmt.println(len(patch))
- bts := json.indent(json.encode(patch), "", " ")
- fmt.println(string(bts))
|