feequery.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: werks})
  23. r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
  24. http.TFYLX{FYLX: "电费"},
  25. http.TFYLX{FYLX: "燃动费"},
  26. http.TFYLX{FYLX: "柴油费"})
  27. rbyte, _ := json.Marshal(r)
  28. fmt.Println(string(rbyte))
  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 strings.Index(item.HSL, "-") > 0 {
  41. item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
  42. }
  43. poper, _ := strconv.ParseInt(item.POPER, 10, 64)
  44. var n int64
  45. s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
  46. Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
  47. var dateStr string
  48. if poper > 10 {
  49. dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
  50. } else {
  51. dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
  52. }
  53. date, err := time.Parse("2006-01", dateStr)
  54. if err != nil {
  55. log.Error(err)
  56. return
  57. }
  58. if n == 0 {
  59. err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
  60. PastureId: int64(pastureID),
  61. RBUKRS: item.RBUKRS,
  62. WERKS: item.WERKS,
  63. FYLX: item.FYLX,
  64. GJAHR: item.GJAHR,
  65. POPER: poper,
  66. HSL: item.HSL,
  67. Date: date.Format("2006-01"),
  68. }).Error
  69. fmt.Println(err)
  70. } else {
  71. s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
  72. pastureID, item.FYLX, item.GJAHR, poper)
  73. }
  74. }
  75. }