Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

182 lines
3.9KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/json"
  5. "errors"
  6. log "github.com/sirupsen/logrus"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. )
  12. type AiDecodeIncome struct {
  13. Id int64
  14. Input loan.Uploads
  15. ul uploadsOnDisk // internal data
  16. Mime string // mime actually detected.
  17. PayIn []loan.PayIn // generated PayIn Info
  18. DecodeIncomeDetails
  19. }
  20. type DecodeIncomeDetails struct {
  21. Lender loan.LenderType
  22. AAA []PayInAAARow
  23. Connective_ANZ []ConnectiveRow
  24. Connective_BOC []ConnectiveRow
  25. Connective_CHLAB []ConnectiveRow
  26. Connective_HSL []ConnectiveRow
  27. Connective_ING []ConnectiveRow
  28. Connective_MEB []ConnectiveRow
  29. Connective_SGB []ConnectiveRow
  30. Connective_WPC []ConnectiveRow
  31. }
  32. type AiDecodeJson struct {
  33. Version string
  34. V1 DecodeIncomeDetails
  35. }
  36. func (m *AiDecodeIncome) decodeUploadToPayIn(ulMeta loan.Uploads, allowCachedResult bool) (e error) {
  37. m.Id = ulMeta.Id
  38. m.Input = ulMeta
  39. m.ul.Upload = ulMeta
  40. m.PayIn = make([]loan.PayIn, 0, 100) // finalized payIns, generated
  41. if allowCachedResult {
  42. e = m.ReadJson()
  43. if e == nil {
  44. return // already decoded
  45. } else {
  46. log.Warn("trying to read existing json failed ", e.Error())
  47. e = nil
  48. }
  49. }
  50. m.PayIn = make([]loan.PayIn, 0, 10)
  51. switch m.getFileType() {
  52. case "pdf":
  53. e = m.decodePdf()
  54. break
  55. case "excel", "opensheet":
  56. e = m.decodeXls()
  57. break
  58. default:
  59. e = errors.New("unknown format")
  60. }
  61. if e == nil {
  62. eJson := m.WriteJson()
  63. if eJson != nil {
  64. log.Error("failed to write analysis to json", eJson.Error())
  65. }
  66. }
  67. return
  68. }
  69. func (m *AiDecodeIncome) ReadJson() (e error) {
  70. if !fileExists(m.ul.jsonPath()) {
  71. return errors.New(m.ul.jsonPath() + " not found")
  72. }
  73. f, e := os.Open(m.ul.jsonPath())
  74. if e != nil {
  75. return
  76. }
  77. decoder := json.NewDecoder(f)
  78. data := AiDecodeJson{}
  79. e = decoder.Decode(&data)
  80. if e != nil {
  81. log.Error("failed load existing decode json", e.Error())
  82. return
  83. }
  84. return
  85. }
  86. func (m *AiDecodeIncome) WriteJson() (e error) {
  87. b, e := json.Marshal(m.DecodeIncomeDetails)
  88. if e != nil {
  89. return
  90. }
  91. ioutil.WriteFile(m.ul.jsonPath(), b, 0644)
  92. return
  93. }
  94. func (m *AiDecodeIncome) getFileType() (ret string) {
  95. strMime, e := GetFileContentType(m.ul.filePath())
  96. if e != nil {
  97. return
  98. }
  99. m.Mime = strMime
  100. ret, e = m.ul.GetFileType()
  101. if e != nil {
  102. ret = ""
  103. }
  104. return
  105. }
  106. func (m *AiDecodeIncome) decodePdf() (e error) {
  107. cmd := exec.Command("pdftotext", "-layout", m.ul.filePath(), "-")
  108. out, e := cmd.Output()
  109. if e != nil {
  110. log.Error("cannot convert pdf to text ", e)
  111. }
  112. raw := string(out)
  113. switch m.detectFunder(raw) {
  114. case loan.Lender_AAA:
  115. m.Lender = loan.Lender_AAA
  116. e = m.decodeAAAPdf(raw)
  117. // regardless of error, we pump in all available row successed so far
  118. for _, row := range m.AAA {
  119. pi := loan.PayIn{}
  120. pi.Id = 0
  121. pi.Ts = row.Period
  122. pi.Amount = row.LoanAmount
  123. pi.Lender = loan.Lender_AAA
  124. pi.Settlement = row.Settlement
  125. pi.Balance = row.Balance
  126. pi.OffsetBalance = -1
  127. pi.IncomeAmount = row.InTrail
  128. pi.IncomeType = "Trail"
  129. m.PayIn = append(m.PayIn, pi)
  130. }
  131. log.Println("AAA final result", m.AAA)
  132. break
  133. case loan.Lender_Unknown:
  134. e = errors.New(loan.Lender_Unknown)
  135. break // not able to detect Funder
  136. }
  137. return
  138. }
  139. func (m *AiDecodeIncome) decodeXls() (e error) {
  140. e = errors.New("not implemented yet")
  141. return
  142. }
  143. func (m *AiDecodeIncome) detectFunder(raw string) loan.LenderType {
  144. if m.isAAA(raw) {
  145. return loan.Lender_AAA
  146. }
  147. return loan.Lender_Unknown
  148. }
  149. func (m *AiDecodeIncome) isAAA(raw string) bool {
  150. keyword := "AAA Financial Trail Report"
  151. lines := strings.Split(raw, "\n")
  152. return m.checkFunderKeyword(keyword, lines, 0, 3)
  153. }
  154. func (m *AiDecodeIncome) checkFunderKeyword(keyword string, lines []string, start int, end int) bool {
  155. for idx, line := range lines {
  156. // first 10 lines has Key word
  157. if strings.Contains(line, keyword) && idx >= start && idx <= 10 {
  158. return true
  159. }
  160. }
  161. return false
  162. }