feequery.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. fmt.Println(string(rbyte))
  30. if e = s.SyncSap(r, rp, rbyte); e == nil {
  31. if rp.Dest.Status == "S" {
  32. log.Infof("sap FeeQuery success sum=%d", len(rp.Data.Items))
  33. } else {
  34. log.Infof("sap FeeQuery fail", rp.Dest.MessText)
  35. pp.Print(r)
  36. }
  37. } else {
  38. log.Error(e)
  39. }
  40. for _, item := range rp.Data.Items {
  41. if item.FYLX == "" {
  42. continue
  43. }
  44. if strings.Index(item.HSL, "-") > 0 {
  45. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  46. }
  47. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  48. var n int64
  49. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  50. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  51. var dateStr string
  52. if poper > 10 {
  53. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  54. } else {
  55. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  56. }
  57. date, err := time.Parse("2006-01", dateStr)
  58. if err != nil {
  59. log.Error(err)
  60. return
  61. }
  62. //if item.FYLX != "" {
  63. if n == 0 {
  64. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  65. PastureId: int64(pastureID),
  66. RBUKRS: item.RBUKRS,
  67. WERKS: item.WERKS,
  68. FYLX: item.FYLX,
  69. GJAHR: item.GJAHR,
  70. POPER: poper,
  71. HSL: item.HSL,
  72. Date: date.Format("2006-01"),
  73. }).Error
  74. fmt.Println(err)
  75. } else {
  76. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  77. pastureID, item.FYLX, item.GJAHR, poper)
  78. }
  79. }
  80. } else {
  81. r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: "M017"}, http.TWERKS{WERKS: "M005"})
  82. r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
  83. http.TFYLX{FYLX: "电费"},
  84. http.TFYLX{FYLX: "燃动费"},
  85. http.TFYLX{FYLX: "柴油费"})
  86. rbyte, _ := json.Marshal(r)
  87. if e = s.SyncSap(r, rp, rbyte); e == nil {
  88. if rp.Dest.Status == "S" {
  89. log.Infof("sap FeeQuery success sum=%d", len(rp.Data.Items))
  90. } else {
  91. log.Infof("sap FeeQuery fail", rp.Dest.MessText)
  92. pp.Print(r)
  93. }
  94. } else {
  95. log.Error(e)
  96. }
  97. sapItems := make([]*http.SapITEMS, 0)
  98. for _, item := range rp.Data.Items {
  99. if item.FYLX == "" {
  100. continue
  101. }
  102. exist := false
  103. for _, sapItem := range sapItems {
  104. if sapItem.FYLX == item.FYLX && item.POPER == sapItem.POPER {
  105. hsl, _ := strconv.ParseFloat(sapItem.HSL, 64)
  106. hsl1, _ := strconv.ParseFloat(item.HSL, 64)
  107. sapItem.HSL = fmt.Sprintf("%f", hsl+hsl1)
  108. exist = true
  109. break
  110. }
  111. }
  112. if !exist {
  113. sapItems = append(sapItems, item)
  114. }
  115. }
  116. for _, item := range sapItems {
  117. fmt.Println(item)
  118. if item.FYLX == "" {
  119. continue
  120. }
  121. if strings.Index(item.HSL, "-") > 0 {
  122. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  123. }
  124. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  125. var n int64
  126. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  127. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  128. var dateStr string
  129. if poper > 10 {
  130. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  131. } else {
  132. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  133. }
  134. date, err := time.Parse("2006-01", dateStr)
  135. if err != nil {
  136. log.Error(err)
  137. return
  138. }
  139. if n == 0 {
  140. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  141. PastureId: int64(pastureID),
  142. RBUKRS: item.RBUKRS,
  143. WERKS: "M005",
  144. FYLX: item.FYLX,
  145. GJAHR: item.GJAHR,
  146. POPER: poper,
  147. HSL: item.HSL,
  148. Date: date.Format("2006-01"),
  149. }).Error
  150. fmt.Println(err)
  151. } else {
  152. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  153. pastureID, item.FYLX, item.GJAHR, poper)
  154. }
  155. }
  156. }
  157. }