package service import ( "encoding/json" "fmt" "github.com/jmoiron/sqlx" "github.com/pkg/errors" "github.com/xormplus/xorm/schemas" "kpt.xdmy/apiserver/config" "reflect" "strconv" "strings" "time" _ "github.com/go-sql-driver/mysql" "github.com/k0kubun/pp/v3" log "github.com/sirupsen/logrus" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/pkg/util" ) // sap入库凭证接口,业务触发 func (s *Service) LaidProof(p interface{}, tx *sqlx.Tx) error { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := make([]*model.PartLaidStr, 0) //buyDetail := []model.BuyDetail{} //pc := new(model.Bigpartpurchase) //dp := new(model.Department) //prs := []model.BigPartLaid{} if e := util.Unmarshal(p, pr); e != nil { log.Error(e, p) return errors.New("转码失败!!!") } bp := new(model.BigPartLaid) bbd := new(model.BigBuyDetailStr) pa := new(model.Pasture) bigpartlaidByte, err := MapStr(tx, `select * from bigpartlaid where laidCode = ? `, pr.LaidCode) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if bigpartlaidByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartlaidByte, &data) if err != nil { log.Error(err) return errors.New("入库信息转码出错!!!") } bp.ID, _ = strconv.Atoi(data["id"].(string)) bp.PurchaseID, _ = data["purchaseId"].(string) bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string)) bp.ProofCode, _ = data["ProofCode"].(string) } if len(bp.ProofCode) > 1 { return errors.New(fmt.Sprintf("errors")) } partlaidByte, err := GetDataList(tx, `select * from partlaid where bigid = ? `, []interface{}{bp.ID}) if err != nil { log.Error(err) return errors.New("无入库详情信息!!!") } if partlaidByte != nil { err := json.Unmarshal(partlaidByte, &parts) if err != nil { log.Error(err) return errors.New("入库信息转码出错!!!") } } bigbuydetailByte, err := MapStr(tx, `select * from bigbuydetail where buyeCode = ? `, bp.PurchaseID) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if bigbuydetailByte != nil { err := json.Unmarshal(bigbuydetailByte, &bbd) if err != nil { log.Error(err) return errors.New("入库信息转码出错!!!") } } if e := s.d.DB.Raw(` select * from pasture where id = ? `, bp.PastureID).First(pa).Error; e != nil { log.Error(e) return errors.New("查询牧场详情出错!!!") } proofDest(r) r.Dest.MESSIDOS = pr.LaidCode r.Dest.BUSSDOCOS = pr.LaidCode r.Data.EqNumber = pr.LaidCode r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.StorageTime[:10]); e != nil { log.Errorf("parse time error:err=%v,param=%v", e, pr.StorageTime) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { if v.StorageAmount == "0" { continue } sdetail := http.ProofDetail{MoveType: "101"} c := new(model.Contract) bc := new(model.BigContract) bd := new(model.BuyDetailStr) pro := new(model.Provider) //w := new(model.Warehouse) contractID, _ := v.ContractID.(float64) if e := s.d.DB.Raw(` select * from contract where id = ?`, contractID).First(c).Error; e != nil { log.Errorf("contract query error:%v", e) //return } if e := s.d.DB.Raw(`select * from bigcontract where id = ? `, c.BigID).First(bc).Error; e != nil { log.Errorf("bigcontract query error:%v", e) //return } //if e := s.d.DB.Where("id = ?", v.LocationID).First(w).Error; e != nil { // log.Error(e) // return errors.New("查询库位信息出错!!!") //} if e := s.d.DB.Raw(`select * from provider where id = ? `, v.ProviderID).First(pro).Error; e != nil { log.Error(e) return errors.New("查询供应商信息出错!!!") } if contractID > 0 { buydetailByte, err := MapStr(tx, `select * from buydetail where bigid = ? and contractId = ? `, bbd.ID, v.ContractID) if err != nil { log.Error(err) //return errors.New("查询供应商信息出错!!!") } if buydetailByte != nil { err := json.Unmarshal(buydetailByte, &bd) if err != nil { return errors.New("查询BuyDetail转码出错!!!") } } sdetail.OrderRowNum = bd.RowNumber } else { buydetailByte, err := MapStr(tx, `select * from buydetail where bigid = ? and partCode = ? `, bbd.ID, v.PartCode) if err != nil { log.Error(err) //return errors.New("查询供应商信息出错!!!") } if buydetailByte != nil { err := json.Unmarshal(buydetailByte, &bd) if err != nil { return errors.New("查询BuyDetail转码出错!!!") } } sdetail.OrderRowNum = bd.RowNumber } //rowNumber, _ := if v.RowNumber.(float64) > 0 { sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber) } else { sdetail.RowNumber = fmt.Sprintf("%d", (i + 1)) } sdetail.Quantity = fmt.Sprintf("%v", v.StorageAmount) sdetail.Unit = v.Unit.(string) sdetail.Code = v.PartCode.(string) sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") //if dp.Factory == "" { sdetail.Factory = pa.FactoryCode //} else { // sdetail.Factory = dp.Factory //} sdetail.Location = "1004" sdetail.OrderNumber = bbd.SapNumber sdetail.Provider = pro.SapCode r.Data.Detail = append(r.Data.Detail, sdetail) //if e := s.d.DB.Model(&model.PartLaid{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil { // log.Errorln(e, v.ID) //} _, err = tx.Exec(` update partlaid set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID) if err != nil { return err } } ///* rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e := s.SyncSap(r, rp, rbyte); e != nil { log.Error(e) fmt.Println(e) return errors.New(fmt.Sprintf("传输sap errors:%s", r.MessText)) } else { if rp.Dest.Status == "S" { log.Printf("laid proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber) tx.Exec(` update bigpartlaid set ProofCode = ? where laidcode = ? `, rp.Data.SapNumber, rp.Data.EqNumber) } else { log.Warnf("%s laid proof failed:causetext=%s", rp.Dest.MessText) pp.Print(r) fmt.Println(e) return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText)) } } //*/ return nil } // 出库单推送sap,包括了报废出库 func (s *Service) UseProof(p interface{}, tx *sqlx.Tx) error { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := make([]*model.PartUseStr, 0) bp := new(model.BigPartUse) pa := new(model.Pasture) dp := new(model.Department) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) return errors.New(fmt.Sprintf("数据转码 errors:%v", e)) } bigpartUseByte, err := MapStr(tx, `select * from bigpartUse where useForm = ? `, pr.UseForm) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("查询不到出库单 errors:%v", err)) } if bigpartUseByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartUseByte, &data) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("查询出库单 errors:%v", err)) } bp.ID, _ = strconv.Atoi(data["id"].(string)) bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string)) bp.ProofCode, _ = data["ProofCode"].(string) bp.DepartmentID, _ = strconv.Atoi(data["departmentId"].(string)) if _, ok := data["oddCode"]; !ok { return errors.New(fmt.Sprintf("查询出库单 OddCode 出错!!!")) } bp.OddCode = data["oddCode"].(string) if bp.OddCode == "" { return errors.New(fmt.Sprintf("查询出库单 OddCode 出错!!!")) } } if len(bp.ProofCode) > 1 { return errors.New("出库单已传输Sap!!!") } partusebyte, err := GetDataList(tx, ` select * from partuse where bigid = ?`, []interface{}{bp.ID}) if err != nil { log.Error(err) return errors.New("查询入库详情出错!!!") } if partusebyte != nil { err := json.Unmarshal(partusebyte, &parts) if err != nil { log.Error(err) return errors.New("查询入库详情出错!!!") } } pp.Print(bp) if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return errors.New(fmt.Sprintf("查询牧场信息 errors:%v", e)) } //if e := s.d.DB.First(dp, model.big{ID: bp.DepartmentID, PastureID: bp.PastureID}).Error; e != nil { // log.Error(e) // return errors.New("查询部门信息出错!!!") //} if e := s.d.DB.Raw(` select * from Department where id = (select departmentId from bigpartapply where applyCode = ? ) `, bp.OddCode). First(dp).Error; e != nil { log.Error(e) return errors.New("查询部门信息出错!!!") } // pp.Println(bp, d) proofDest(r) //b, _ := json.Marshal(pr) //restful.Dbs.DB.Exec(` insert into equipmentlingshi(text)values(?)`, string(b)) r.Dest.MESSIDOS = pr.UseForm r.Dest.BUSSDOCOS = pr.UseForm r.Data.EqNumber = pr.UseForm r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.ReceiveTime[:10]); e != nil { log.Error(e, pr.ReceiveTime) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { bc := new(model.BigContract) c := new(model.Contract) //w := new(model.Warehouse) pv := new(model.Provider) eq := new(model.Equipment) storage := new(model.PartRepertory) dict := new(model.Dictlist) contractID, _ := strconv.Atoi(v.ContractID.(string)) if contractID < 0 { if e := s.d.DB.First(storage, model.PartRepertory{ContractID: contractID}).Error; e != nil { log.Errorln(e, v.ContractID) return errors.New(fmt.Sprintf("查询库存信息出错 errors:%v", e)) } if e := s.d.DB.First(pv, model.Provider{ID: storage.ProviderID}).Error; e != nil { log.Errorln(e, storage.ProviderID) return errors.New(fmt.Sprintf("查询供应商信息出错 errors:%v", e)) } } else { if e := s.d.DB.Where("id = ? ", v.ContractID).First(c).Error; e != nil { log.Errorf("contract query error:%v", e) return errors.New(fmt.Sprintf("查询合同信息 errors:%v", e)) } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return errors.New(fmt.Sprintf("查询合同信息 errors:%v", e)) } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return errors.New(fmt.Sprintf("无供应商信息请检查合同%s对应的供应商", bc.ContractCode)) } } s.d.DB.First(dict, model.Dictlist{Pid: 21, Value: v.UseTypeV.(string)}) //if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil { // log.Errorln(e, v.LocationID) // return errors.New(fmt.Sprintf("errors:%v", e)) //} if e := s.d.DB.Where("eqCode = ? and pastureid = ? ", v.EqCode, pr.PastureID).First(eq).Error; e != nil { log.Errorln(e, v.EqID) return errors.New(fmt.Sprintf("errors:%v", e)) } sdetail := http.ProofDetail{} sdetail.EXT01 = dict.Label // 报废出库 if pr.UseType == 5 { sdetail.MoveType = "Z19" if contractID > 0 && bc.IsZeroStock != 0 { log.Info("寄售不能报废=======") return errors.New(fmt.Sprintf("寄售不能报废======= errors")) } } // 领用出库、手动出库 //if pr.UseType == 0 || pr.UseType == 1 { sdetail.MoveType = "201" sdetail.CostCenter = dp.CostCenterCode //if dp.Factory == "" { sdetail.Factory = pa.FactoryCode //} else { // sdetail.Factory = dp.Factory //} if sdetail.Factory == "M005" && strings.Contains(dp.Name, "二期") { sdetail.Factory = "M017" } if v.RowNumber.(float64) > 0 { sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber) } else { sdetail.RowNumber = fmt.Sprintf("%d", (i + 1)) } sdetail.Quantity = fmt.Sprintf("%v", v.CheckoutNumber) sdetail.Unit = v.Unit.(string) sdetail.Code = v.PartCode.(string) sdetail.AssetCode = eq.SapCode sdetail.AssetSon = eq.SonCode sdetail.Location = "1004" if contractID > 0 && bc.IsZeroStock == 1 { sdetail.SaveType = "K" } if sdetail.SaveType == "K" { sdetail.Provider = pv.SapCode } r.Data.Detail = append(r.Data.Detail, sdetail) //if e := s.d.DB.Model(&model.PartUse{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil { // log.Errorln(e, v.ID) //} _, err = tx.Exec(` update partuse set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID) if err != nil { return err } } //} ///* pp.Print(r) log.Infof("bp.pastureId:%d,pa.name:%s,pa.companycode:%s", bp.PastureID, pa.Name, pa.CompanyCode) rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e := s.SyncSap(r, rp, rbyte); e != nil { log.Errorf("laid proof error:%v", e) return errors.New(fmt.Sprintf("errors:%s", r.MessText)) } else { if rp.Dest.Status == "S" { log.Printf("use proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber) //if e := s.d.DB.Model(&model.BigPartUse{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { // log.Errorln(e) //} tx.Exec(` update bigpartuse set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber) } else { log.Warnf(" use proof :%s", rp.Dest.MessText) return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText)) } } return nil } // 退库单推送sap func (s *Service) QuitProof(p interface{}, tx *sqlx.Tx) error { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := make([]*model.PartquitStr, 0) bp := new(model.BigPartquit) pa := new(model.Pasture) d := new(model.Department) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) return errors.New(fmt.Sprintf("errors:%v", e)) } //defer func() { // if msg := recover(); msg != nil { // fmt.Println("panic信息:", msg, "---recover恢复---") // } //}() //if e := s.d.DB.First(bp, model.BigPartquit{UseForm: pr.UseForm}).Error; e != nil { // log.Error(e) // return errors.New(fmt.Sprintf("errors:%v", e)) //} bigpartquitByte, err := MapStr(tx, `select * from bigpartquit where useForm = ? `, pr.UseForm) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err)) } if bigpartquitByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartquitByte, &data) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err)) } bp.ID, _ = strconv.Atoi(data["id"].(string)) bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string)) bp.ProofCode, _ = data["ProofCode"].(string) bp.DeptID, _ = strconv.Atoi(data["deptId"].(string)) } if len(bp.ProofCode) > 1 { return errors.New(fmt.Sprintf("errors")) } partquitByte, err := GetDataList(tx, `select * from partquit where bigid = ? `, []interface{}{bp.ID}) if err != nil { log.Error(err) return errors.New("无退货详情信息!!!") } if partquitByte != nil { err := json.Unmarshal(partquitByte, &parts) if err != nil { log.Error(err) return errors.New("退货详情信息转码出错!!!") } } if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return errors.New(fmt.Sprintf("无牧场信息 errors:%v", err)) } if e := s.d.DB.First(d, model.Department{ID: bp.DeptID}).Error; e != nil { log.Error(e) return errors.New(fmt.Sprintf("无部门信息 errors:%v", err)) } var useFormCount int64 if e := s.d.DB.Table("bigpartquit").Where(" useForm = ? ", pr.UseForm).Count(&useFormCount).Error; e != nil { log.Error(e) } useForm := fmt.Sprintf("%stk%d", pr.UseForm, useFormCount+1) proofDest(r) r.Dest.MESSIDOS = useForm r.Dest.BUSSDOCOS = useForm r.Data.EqNumber = useForm r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.QuitDate); e != nil { log.Error(e, pr.QuitDate) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { c := new(model.Contract) bc := new(model.BigContract) pv := new(model.Provider) // w := new(model.Warehouse) // /* if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return errors.New(fmt.Sprintf("errors:%v", e)) } */ contractID, _ := strconv.Atoi(v.ContractID.(string)) if contractID > 0 { if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return errors.New(fmt.Sprintf("合同不存在 errors:%v", err)) } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return errors.New(fmt.Sprintf("合同不存在 errors:%v", err)) } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err)) } } else { if e := s.d.DB.Where("id = ?", v.ProviderID).First(pv).Error; e != nil { log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e) return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err)) } } sdetail := http.ProofDetail{MoveType: "202"} if v.RowNumber.(float64) > 0 { sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber) } else { sdetail.RowNumber = fmt.Sprintf("%d", i+1) } sdetail.Quantity = fmt.Sprintf("%v", v.QuitNumber) sdetail.Unit = c.Unit sdetail.Factory = pa.FactoryCode sdetail.CostCenter = d.CostCenterCode sdetail.Location = "1004" sdetail.Provider = pv.SapCode sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") r.Data.Detail = append(r.Data.Detail, sdetail) _, err = tx.Exec(` update partquit set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID) if err != nil { log.Error(err) return err } } rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e := s.SyncSap(r, rp, rbyte); e != nil { log.Errorf("laid proof error:%v", e) return errors.New(fmt.Sprintf("errors:%v", e)) } else { if rp.Dest.Status == "S" { log.Printf("quit proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber) //if e := s.d.DB.Model(&model.BigPartquit{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { // log.Errorln(e) // return err //} tx.Exec(` update bigpartquit set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber) } else { log.Warnf("%s quit proof failed:causetext=%s", rp.Dest.MessText) pp.Print(r) return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText)) } } return nil } // 退货单推送sap func (s *Service) RefundProof(p interface{}, tx *sqlx.Tx) error { pr := new(model.RefundParam) r := new(http.ProofReq) rp := new(http.ProofResp) parts := make([]*model.PartRefundStr, 0) bp := new(model.BigPartRefund) pa := new(model.Pasture) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) log.Info("%v", pr) return errors.New(fmt.Sprintf("errors:%v", e)) } pid := pr.ID bigpartrefundByte, err := MapStr(tx, `select * from bigpartrefund where id = ? `, pid) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("查询退货 errors:%v", err)) } if bigpartrefundByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartrefundByte, &data) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("查询退货 errors:%v", err)) } bp.ID, _ = strconv.Atoi(data["id"].(string)) bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string)) bp.ProofCode, _ = data["ProofCode"].(string) bp.UseForm = data["useForm"].(string) bp.CreateTime, _ = data["createTime"].(time.Time) t, err := time.Parse("2006-01-02T15:04:05", data["createTime"].(string)[:len("2006-01-02 15:04:05")]) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("查询退货 errors:%v", err)) } fmt.Println(t) bp.CreateTime = t bp.ProviderID, _ = strconv.Atoi(data["providerId"].(string)) if _, ok := data["purchaseId"]; ok { bp.PurchaseId = data["purchaseId"].(string) } } if len(bp.ProofCode) > 1 { return errors.New(fmt.Sprintf("errors")) } //if e := s.d.DB.Find(&parts, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(parts) == 0 { // log.Error(e) // return errors.New("无退货详情信息!!!") //} partquitByte, err := GetDataList(tx, `select * from partrefund where bigid = ? `, []interface{}{bp.ID}) if err != nil { log.Error(err) return errors.New("无退货详情信息!!!") } if partquitByte != nil { err := json.Unmarshal(partquitByte, &parts) if err != nil { log.Error(err) return errors.New("退货详情信息转码出错!!!") } } if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return errors.New(fmt.Sprintf("查询退货 errors:%v", err)) } proofDest(r) r.Dest.MESSIDOS = bp.UseForm r.Dest.BUSSDOCOS = bp.UseForm r.Data.EqNumber = bp.UseForm r.Data.PostDate = bp.CreateTime.Format("20060102") r.Data.CreateTime = time.Now().Format("20060102") for i, v := range parts { sdetail := http.ProofDetail{} c := new(model.Contract) bc := new(model.BigContract) //bd := new(model.BuyDetail) //bbd := new(model.BigBuyDetail) pv := new(model.Provider) /* if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.Order("id desc").First(bd, model.BuyDetail{ContractID: v.ContractID}).Error; e != nil { log.Error(e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.First(bbd, model.BigBuyDetail{ID: bd.BigID}).Error; e != nil { log.Errorf("refund proof bigbuydetail:id=%d,%v", bd.BigID, e) return errors.New(fmt.Sprintf("errors:%v", e)) } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e) return errors.New(fmt.Sprintf("errors:%v", e)) } */ contractID, _ := strconv.Atoi(v.ContractID.(string)) if contractID > 0 { if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return errors.New(fmt.Sprintf("合同不存在 errors:%v", err)) } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return errors.New(fmt.Sprintf("合同不存在 errors:%v", err)) } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err)) } //return nil } else { if e := s.d.DB.First(pv, model.Provider{ID: bp.ProviderID}).Error; e != nil { log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e) return errors.New(fmt.Sprintf("errors:%v", e)) } } refunddetail := new(model.BigRefunddetail) s.d.DB.Raw(` select * from bigrefunddetail where buyeCode = ? `, bp.PurchaseId).First(&refunddetail) if bc.IsZeroStock == 0 { sdetail.MoveType = "161" } else { sdetail.MoveType = "502" } if v.RowNumber.(float64) > 0 { sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber) } else { sdetail.RowNumber = fmt.Sprintf("%d", (i+1)*10) } sdetail.Quantity = fmt.Sprintf("%v", v.RefundNumber) if v.PartCode == "" { sdetail.Unit = c.Unit sdetail.Code = c.PartCode } else { sdetail.Unit = v.Unit.(string) sdetail.Code = v.PartCode.(string) } sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") sdetail.Factory = pa.FactoryCode sdetail.Location = "1004" sdetail.OrderNumber = refunddetail.SapNumber sdetail.OrderRowNum = fmt.Sprintf("%s", sdetail.RowNumber) sdetail.Provider = pv.SapCode r.Data.Detail = append(r.Data.Detail, sdetail) //if e := s.d.DB.Model(&model.PartRefund{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil { // log.Errorln(e, v.ID) //} _, err = tx.Exec(` update partrefund set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID) if err != nil { log.Error(err) return err } } rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e := s.SyncSap(r, rp, rbyte); e != nil { log.Error(e) return errors.New(fmt.Sprintf("errors:%v", e)) } else { if rp.Dest.Status == "S" { log.Printf("refund proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber) //if e := s.d.DB.Model(&model.BigPartRefund{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { // log.Errorln(e) //} tx.Exec(` update bigpartrefund set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber) } else { log.Warnf("return good fail: %s", rp.Dest.MessText) return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText)) } } return nil } func proofDest(p *http.ProofReq) { p.Dest.DestID = "EQMAN" p.Dest.BussTp = "MM001" //p.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM001/ModifyMblnr" p.Dest.Url = fmt.Sprintf("%s/Common/MM001/ModifyMblnr", config.Conf.Http.Routing) //p.Dest.Url = "http://192.168.61.117/SAPQ0/Common/MM001/ModifyMblnr" } func MapStr(tx *sqlx.Tx, sql string, param ...interface{}) ([]byte, error) { m := make(map[string]interface{}) rows, err := tx.Queryx(sql, param...) if err != nil { return nil, err } col, err := rows.Columns() if err != nil { return nil, err } if rows.Next() { rs, err := rows.SliceScan() if err != nil { return nil, err } for i, r := range rs { m[col[i]] = r switch r.(type) { case []uint8: m[col[i]] = string(r.([]uint8)) case int64: m[col[i]] = strconv.FormatInt(r.(int64), 10) } } } defer func() { rows.Close() }() b, err := json.Marshal(m) if err != nil { return nil, err } return b, nil } func GetDataList(tx *sqlx.Tx, sqlstr string, args []interface{}) ([]byte, error) { row, err := tx.Queryx(sqlstr, args...) if err != nil { return nil, err } defer row.Close() results, err := rows2mapObjects(row) if err != nil { return nil, err } b, err := json.Marshal(results) return b, err } func rows2mapObjects(rows *sqlx.Rows) (resultsSlice []map[string]interface{}, err error) { fields, err := rows.Columns() if err != nil { return nil, err } defer rows.Close() for rows.Next() { result, err := rows2mapObject(rows, fields) if err != nil { return nil, err } resultsSlice = append(resultsSlice, result) } return resultsSlice, nil } func rows2mapObject(rows *sqlx.Rows, fields []string) (resultsMap map[string]interface{}, err error) { result := make(map[string]interface{}) scanResultContainers := make([]interface{}, len(fields)) for i := 0; i < len(fields); i++ { var scanResultContainer interface{} scanResultContainers[i] = &scanResultContainer } if err := rows.Scan(scanResultContainers...); err != nil { return nil, err } for ii, key := range fields { rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii])) //if row is null then ignore if rawValue.Interface() == nil { continue } if data, err := value2Object(&rawValue); err == nil { result[key] = data } else { return nil, err // !nashtsai! REVIEW, should return err or just error log? } } return result, nil } func value2Object(rawValue *reflect.Value) (data interface{}, err error) { data, err = reflect2object(rawValue) if err != nil { return } return } func reflect2object(rawValue *reflect.Value) (value interface{}, err error) { aa := reflect.TypeOf((*rawValue).Interface()) vv := reflect.ValueOf((*rawValue).Interface()) switch aa.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: value = vv.Int() case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: value = vv.Uint() case reflect.Float32, reflect.Float64: value = vv.Float() case reflect.String: value = vv.String() case reflect.Array, reflect.Slice: switch aa.Elem().Kind() { case reflect.Uint8: data := rawValue.Interface().([]byte) value = string(data) default: err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } // time type case reflect.Struct: if aa.ConvertibleTo(schemas.TimeType) { value = vv.Convert(schemas.TimeType).Interface().(time.Time) } else { err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } case reflect.Bool: value = vv.Bool() case reflect.Complex128, reflect.Complex64: value = vv.Complex() /* TODO: unsupported types below case reflect.Map: case reflect.Ptr: case reflect.Uintptr: case reflect.UnsafePointer: case reflect.Chan, reflect.Func, reflect.Interface: */ default: err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } return }