feequery.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/k0kubun/pp/v3"
  6. log "github.com/sirupsen/logrus"
  7. "kpt.xdmy/apiserver/config"
  8. "kpt.xdmy/apiserver/model"
  9. "kpt.xdmy/apiserver/model/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func AutoFeeQuery(werks string, pastureID int) {
  15. r := new(http.FeeQueryReq)
  16. rp := new(http.FeeQueryResp)
  17. var e error
  18. r.Url = fmt.Sprintf("%s/Common/FI009/FeeQuery", config.Conf.Http.Routing)
  19. r.Dest.DestID = "EQMAN"
  20. r.Dest.BussTp = "FI009"
  21. r.DATA.GJAHR = time.Now().Format("2006")
  22. if pastureID != 115 {
  23. r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: werks})
  24. r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
  25. http.TFYLX{FYLX: "电费"},
  26. http.TFYLX{FYLX: "燃动费"},
  27. http.TFYLX{FYLX: "柴油费"})
  28. rbyte, _ := json.Marshal(r)
  29. if e = s.SyncSap(r, rp, rbyte); e == nil {
  30. if rp.Dest.Status == "S" {
  31. log.Infof("sap FeeQuery success sum=%d", len(rp.Data.Items))
  32. } else {
  33. log.Infof("sap FeeQuery fail", rp.Dest.MessText)
  34. pp.Print(r)
  35. }
  36. } else {
  37. log.Error(e)
  38. }
  39. for _, item := range rp.Data.Items {
  40. if item.FYLX == "" {
  41. continue
  42. }
  43. if strings.Index(item.HSL, "-") > 0 {
  44. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  45. }
  46. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  47. var n int64
  48. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  49. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  50. var dateStr string
  51. if poper > 10 {
  52. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  53. } else {
  54. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  55. }
  56. date, err := time.Parse("2006-01", dateStr)
  57. if err != nil {
  58. log.Error(err)
  59. return
  60. }
  61. //if item.FYLX != "" {
  62. if n == 0 {
  63. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  64. PastureId: int64(pastureID),
  65. RBUKRS: item.RBUKRS,
  66. WERKS: item.WERKS,
  67. FYLX: item.FYLX,
  68. GJAHR: item.GJAHR,
  69. POPER: poper,
  70. HSL: item.HSL,
  71. Date: date.Format("2006-01"),
  72. }).Error
  73. fmt.Println(err)
  74. } else {
  75. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  76. pastureID, item.FYLX, item.GJAHR, poper)
  77. }
  78. }
  79. } else {
  80. r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: "M017"}, http.TWERKS{WERKS: "M005"})
  81. r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
  82. http.TFYLX{FYLX: "电费"},
  83. http.TFYLX{FYLX: "燃动费"},
  84. http.TFYLX{FYLX: "柴油费"})
  85. rbyte, _ := json.Marshal(r)
  86. if e = s.SyncSap(r, rp, rbyte); e == nil {
  87. if rp.Dest.Status == "S" {
  88. log.Infof("sap FeeQuery success sum=%d", len(rp.Data.Items))
  89. } else {
  90. log.Infof("sap FeeQuery fail", rp.Dest.MessText)
  91. pp.Print(r)
  92. }
  93. } else {
  94. log.Error(e)
  95. }
  96. sapItems := make([]*http.SapITEMS, 0)
  97. for _, item := range rp.Data.Items {
  98. if item.FYLX == "" {
  99. continue
  100. }
  101. exist := false
  102. for _, sapItem := range sapItems {
  103. if sapItem.FYLX == item.FYLX && item.POPER == sapItem.POPER {
  104. hsl, _ := strconv.ParseFloat(sapItem.HSL, 64)
  105. hsl1, _ := strconv.ParseFloat(item.HSL, 64)
  106. sapItem.HSL = fmt.Sprintf("%f", hsl+hsl1)
  107. exist = true
  108. break
  109. }
  110. }
  111. if !exist {
  112. sapItems = append(sapItems, item)
  113. }
  114. }
  115. for _, item := range sapItems {
  116. fmt.Println(item)
  117. if item.FYLX == "" {
  118. continue
  119. }
  120. if strings.Index(item.HSL, "-") > 0 {
  121. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  122. }
  123. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  124. var n int64
  125. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  126. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  127. var dateStr string
  128. if poper > 10 {
  129. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  130. } else {
  131. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  132. }
  133. date, err := time.Parse("2006-01", dateStr)
  134. if err != nil {
  135. log.Error(err)
  136. return
  137. }
  138. if n == 0 {
  139. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  140. PastureId: int64(pastureID),
  141. RBUKRS: item.RBUKRS,
  142. WERKS: "M005",
  143. FYLX: item.FYLX,
  144. GJAHR: item.GJAHR,
  145. POPER: poper,
  146. HSL: item.HSL,
  147. Date: date.Format("2006-01"),
  148. }).Error
  149. fmt.Println(err)
  150. } else {
  151. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  152. pastureID, item.FYLX, item.GJAHR, poper)
  153. }
  154. }
  155. }
  156. }