inn2company.xgo 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. os := import("os")
  2. fmt := import("fmt")
  3. json := import("json")
  4. cjson := import("cjson")
  5. log := import("log")
  6. contact_field_id := 231711
  7. company_field_id := 1300614
  8. lead_field_id := 686597
  9. dec := cjson.new_decoder("<stdin>")
  10. patch := []
  11. add_patch := func(company_id, inn, reason) {
  12. log.printf("%d %q %q\n", company_id, string(inn), reason)
  13. company := {}
  14. company.id = company_id
  15. company.__reason = reason
  16. company.custom_fields_values = [{
  17. field_id: company_field_id,
  18. values: [{
  19. value: inn
  20. }]
  21. }]
  22. patch += [company]
  23. }
  24. find_by_field_id := func(id, fields){
  25. for _, field in fields {
  26. if field.field_id == id {
  27. return field.values[0].value
  28. }
  29. }
  30. }
  31. is_field_correct := func(value){
  32. if !int(value) {
  33. return false
  34. }
  35. str := string(value)
  36. if len(str) <9 || len(str) > 12 {
  37. return false
  38. }
  39. return true
  40. }
  41. format_empty := func(...values){
  42. ret := ""
  43. for _, value in values {
  44. if !value {
  45. ret += "<empty> "
  46. } else {
  47. ret += string(value) + " "
  48. }
  49. }
  50. return ret
  51. }
  52. i := 0
  53. for {
  54. i++
  55. //log.println("Checking", i)
  56. h := dec.decode()
  57. if !h {
  58. break
  59. }
  60. //fmt.println(i, " reachd", company_inn)
  61. company_inn := find_by_field_id(
  62. company_field_id,
  63. h.company.custom_fields_values)
  64. contact_inn := find_by_field_id(
  65. contact_field_id,
  66. h.main_contact.custom_fields_values
  67. )
  68. lead_inn := find_by_field_id(
  69. lead_field_id,
  70. h.lead.custom_fields_values
  71. )
  72. //log.println(format_empty(i, company_inn, contact_inn, lead_inn))
  73. if !h.company || int(company_inn) {
  74. continue
  75. }
  76. // Adding lead INN if no contact INN.
  77. if !int(contact_inn) {
  78. if !lead_inn || !is_field_correct(lead_inn) {
  79. continue
  80. }
  81. add_patch(
  82. h.company.id,
  83. lead_inn,
  84. fmt.sprintf("Взято из лида (%d)", h.lead.id)
  85. )
  86. continue
  87. }
  88. // Adding contact INN if no lead INN.
  89. if !int(lead_inn) {
  90. if !is_field_correct(contact_inn) {
  91. continue
  92. }
  93. add_patch(
  94. h.company.id,
  95. contact_inn,
  96. fmt.sprintf(
  97. "Взято из контакта (%d)",
  98. h.main_contact.id
  99. )
  100. )
  101. } else { // Here we have both.
  102. if int(contact_inn) != int(lead_inn) {
  103. continue
  104. }
  105. if !is_field_correct(contact_inn) {
  106. continue
  107. }
  108. add_patch(
  109. h.company.id,
  110. contact_inn,
  111. fmt.sprintf("Взято из контакта и лида (%d, %d)",
  112. h.main_contact.id, h.lead.id)
  113. )
  114. }
  115. }
  116. //patch = patch[:500]
  117. //fmt.println(len(patch))
  118. log.println("Summarized got", len(patch), "companies")
  119. bts := json.indent(json.encode(patch), "", " ")
  120. fmt.println(string(bts))