feequery.go 4.7 KB

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