feequery.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. fmt.Println(string(rbyte))
  88. if e = s.SyncSap(r, rp, rbyte); e == nil {
  89. if rp.Dest.Status == "S" {
  90. log.Infof("sap FeeQuery success sum=%d", len(rp.Data.Items))
  91. } else {
  92. log.Infof("sap FeeQuery fail", rp.Dest.MessText)
  93. pp.Print(r)
  94. }
  95. } else {
  96. log.Error(e)
  97. }
  98. sapItems := make([]*http.SapITEMS, 0)
  99. for _, item := range rp.Data.Items {
  100. if item.FYLX == "" {
  101. continue
  102. }
  103. exist := false
  104. for _, sapItem := range sapItems {
  105. if sapItem.FYLX == item.FYLX && item.POPER == sapItem.POPER {
  106. hsl, _ := strconv.ParseFloat(sapItem.HSL, 64)
  107. hsl1, _ := strconv.ParseFloat(item.HSL, 64)
  108. sapItem.HSL = fmt.Sprintf("%f", hsl+hsl1)
  109. exist = true
  110. break
  111. }
  112. }
  113. if !exist {
  114. sapItems = append(sapItems, item)
  115. }
  116. }
  117. for _, item := range sapItems {
  118. fmt.Println(item)
  119. if item.FYLX == "" {
  120. continue
  121. }
  122. if strings.Index(item.HSL, "-") > 0 {
  123. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  124. }
  125. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  126. var n int64
  127. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  128. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  129. var dateStr string
  130. if poper > 10 {
  131. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  132. } else {
  133. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  134. }
  135. date, err := time.Parse("2006-01", dateStr)
  136. if err != nil {
  137. log.Error(err)
  138. return
  139. }
  140. if n == 0 {
  141. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  142. PastureId: int64(pastureID),
  143. RBUKRS: item.RBUKRS,
  144. WERKS: "M005",
  145. FYLX: item.FYLX,
  146. GJAHR: item.GJAHR,
  147. POPER: poper,
  148. HSL: item.HSL,
  149. Date: date.Format("2006-01"),
  150. }).Error
  151. fmt.Println(err)
  152. } else {
  153. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  154. pastureID, item.FYLX, item.GJAHR, poper)
  155. }
  156. }
  157. }
  158. }