123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package service
- import (
- "encoding/json"
- "fmt"
- "github.com/k0kubun/pp/v3"
- log "github.com/sirupsen/logrus"
- "kpt.xdmy/apiserver/config"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- "strconv"
- "strings"
- "time"
- )
- func AutoFeeQuery(werks string, pastureID int) {
- r := new(http.FeeQueryReq)
- rp := new(http.FeeQueryResp)
- var e error
- r.Url = fmt.Sprintf("%s/Common/FI009/FeeQuery", config.Conf.Http.Routing)
- r.Dest.DestID = "EQMAN"
- r.Dest.BussTp = "FI009"
- r.DATA.GJAHR = time.Now().Format("2006")
- if pastureID != 115 {
- r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: werks})
- r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
- http.TFYLX{FYLX: "电费"},
- http.TFYLX{FYLX: "燃动费"},
- http.TFYLX{FYLX: "柴油费"})
- rbyte, _ := json.Marshal(r)
- fmt.Println(string(rbyte))
- if e = s.SyncSap(r, rp, rbyte); e == nil {
- if rp.Dest.Status == "S" {
- log.Infof("sap FeeQuery success sum=%d")
- } else {
- log.Infof("sap FeeQuery fail", rp.Dest.MessText)
- pp.Print(r)
- }
- } else {
- log.Error(e)
- }
- for _, item := range rp.Data.Items {
- if item.FYLX == "" {
- continue
- }
- if strings.Index(item.HSL, "-") > 0 {
- item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
- }
- poper, _ := strconv.ParseInt(item.POPER, 10, 64)
- var n int64
- s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
- Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
- var dateStr string
- if poper >= 10 {
- dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
- } else {
- dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
- }
- date, err := time.Parse("2006-01", dateStr)
- if err != nil {
- log.Error(err)
- return
- }
- //if item.FYLX != "" {
- if n == 0 {
- err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
- PastureId: int64(pastureID),
- RBUKRS: item.RBUKRS,
- WERKS: item.WERKS,
- FYLX: item.FYLX,
- GJAHR: item.GJAHR,
- POPER: poper,
- HSL: item.HSL,
- Date: date.Format("2006-01"),
- }).Error
- fmt.Println(err)
- } else {
- err = s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
- pastureID, item.FYLX, item.GJAHR, poper).Error
- fmt.Println(err)
- }
- }
- } else {
- r.DATA.TWERKS = append(r.DATA.TWERKS, http.TWERKS{WERKS: "M017"}, http.TWERKS{WERKS: "M005"})
- r.DATA.TFYLX = append(r.DATA.TFYLX, http.TFYLX{FYLX: "水费"},
- http.TFYLX{FYLX: "电费"},
- http.TFYLX{FYLX: "燃动费"},
- http.TFYLX{FYLX: "柴油费"})
- rbyte, _ := json.Marshal(r)
- fmt.Println(string(rbyte))
- if e = s.SyncSap(r, rp, rbyte); e == nil {
- if rp.Dest.Status == "S" {
- log.Infof("sap FeeQuery success sum=")
- } else {
- log.Infof("sap FeeQuery fail", rp.Dest.MessText)
- pp.Print(r)
- }
- } else {
- log.Error(e)
- }
- sapItems := make([]*http.SapITEMS, 0)
- for _, item := range rp.Data.Items {
- if item.FYLX == "" {
- continue
- }
- exist := false
- for _, sapItem := range sapItems {
- if sapItem.FYLX == item.FYLX && item.POPER == sapItem.POPER {
- hsl, _ := strconv.ParseFloat(sapItem.HSL, 64)
- hsl1, _ := strconv.ParseFloat(item.HSL, 64)
- sapItem.HSL = fmt.Sprintf("%f", hsl+hsl1)
- exist = true
- break
- }
- }
- if !exist {
- sapItems = append(sapItems, item)
- }
- }
- for _, item := range sapItems {
- fmt.Println(item)
- if item.FYLX == "" {
- continue
- }
- if strings.Index(item.HSL, "-") > 0 {
- item.HSL = fmt.Sprintf("-%s", strings.ReplaceAll(item.HSL, "-", ""))
- }
- poper, _ := strconv.ParseInt(item.POPER, 10, 64)
- var n int64
- s.d.DB.Table(new(model.FeeQuery).TableName()).Where("pastureId = ? ", pastureID).Where("FYLX = ? ", item.FYLX).
- Where("GJAHR = ? ", item.GJAHR).Where("POPER = ? ", poper).Count(&n)
- var dateStr string
- if poper >= 10 {
- dateStr = fmt.Sprintf("%s-%d", item.GJAHR, poper)
- } else {
- dateStr = fmt.Sprintf("%s-0%d", item.GJAHR, poper)
- }
- date, err := time.Parse("2006-01", dateStr)
- if err != nil {
- log.Error(err)
- return
- }
- if n == 0 {
- err := s.d.DB.Table(new(model.FeeQuery).TableName()).Create(&model.FeeQuery{
- PastureId: int64(pastureID),
- RBUKRS: item.RBUKRS,
- WERKS: "M005",
- FYLX: item.FYLX,
- GJAHR: item.GJAHR,
- POPER: poper,
- HSL: item.HSL,
- Date: date.Format("2006-01"),
- }).Error
- fmt.Println(err)
- } else {
- s.d.DB.Exec(` update feequery set HSL = ? where pastureId = ? and FYLX = ? and GJAHR = ? and POPER = ? `, item.HSL,
- pastureID, item.FYLX, item.GJAHR, poper)
- }
- }
- }
- }
|