inn-check.xgo 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. os := import("os")
  2. fmt := import("fmt")
  3. json := import("json")
  4. args := os.args()[2:]
  5. file_path := args[0]
  6. contact_field_id := 231711
  7. company_field_id := 1300614
  8. lead_field_id := 686597
  9. holders := json.decode(os.read_file(file_path))
  10. //fmt.println(len(holders))
  11. //os.exit(0)
  12. patch := []
  13. find_by_field_id := func(id, fields){
  14. for _, field in fields {
  15. if field.field_id == id {
  16. return field
  17. }
  18. }
  19. }
  20. is_field_correct := func(field){
  21. if !int(field.values[0].value) {
  22. return false
  23. }
  24. str := string(field.values[0].value)
  25. if len(str) <9 || len(str) > 12 {
  26. return false
  27. }
  28. return true
  29. }
  30. //holders = holders[:2]
  31. for i, h in holders {
  32. company := {}
  33. if !h.company_id {
  34. continue
  35. }
  36. company.id = h.company_id
  37. company_inn := find_by_field_id(company_field_id, h.company_inns)
  38. //fmt.println(i, " reachd", company_inn)
  39. if company_inn {
  40. continue
  41. }
  42. contact_inn := find_by_field_id(contact_field_id, h.contact_inns)
  43. lead_inn := find_by_field_id(lead_field_id, h.lead_inns)
  44. if !contact_inn {
  45. if !lead_inn || !is_field_correct(lead_inn) {
  46. continue
  47. }
  48. company.__reason = "Взято из лида (в контакте не было)"
  49. company.custom_fields_values = [{
  50. field_id: company_field_id,
  51. values: [{
  52. value: string(lead_inn.values[0].value)
  53. }]
  54. }]
  55. patch += [company]
  56. continue
  57. }
  58. if !lead_inn {
  59. if !is_field_correct(contact_inn) {
  60. continue
  61. }
  62. company.__reason = "Взято из контакта (в лиде не было)"
  63. company.custom_fields_values =[{
  64. field_id: company_field_id,
  65. values: [{
  66. value: string(contact_inn.values[0].value)
  67. }]
  68. }]
  69. } else {
  70. if int(contact_inn.values[0].value) != int(lead_inn.values[0].value) {
  71. continue
  72. }
  73. if !is_field_correct(contact_inn) {
  74. continue
  75. }
  76. company.__reason = "Взято из лида и контакта"
  77. company.custom_fields_values =[{
  78. field_id: company_field_id,
  79. values: [{
  80. value: string(contact_inn.values[0].value)
  81. }]
  82. }]
  83. }
  84. patch += [company]
  85. /*if h.company_inns {
  86. for _, field in h.company_inns {
  87. company.custom_fields_values += [{
  88. field_id: field.field_id,
  89. values: [{
  90. value: field.values[0].value
  91. }]
  92. }]
  93. }
  94. }
  95. companies += [company]*/
  96. }
  97. //patch = patch[:500]
  98. //fmt.println(len(patch))
  99. bts := json.indent(json.encode(patch), "", " ")
  100. fmt.println(string(bts))