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") 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", len(rp.Data.Items)) } else { log.Infof("sap FeeQuery fail", rp.Dest.MessText) pp.Print(r) } } else { log.Error(e) } for _, item := range rp.Data.Items { 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: item.WERKS, 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) } } }