package util
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
)
type EASLoginInfo struct {
UserName string `json:"userName"`
Password string `json:"password"`
SlnName string `json:"slnName"`
DcName string `json:"dcName"`
Language string `json:"language"`
DbType string `json:"dbType"`
Url string `json:"dbType"`
}
var (
LoginStr = `
userName?
password?
slnName?
dcName?
language?
dbType?
`
LogoutStr = `
userName?
slnName?
dcName?
language?
`
InsertPartsStr = `
SessionId?
strJson?
`
QureyPartsStr = `
SessionId?
{"number":"strJson?"}
`
InsertPartsuselaidStr = `
SessionId?
billType?
strJson?
2
`
QureyBill = `
SessionId?
billType?
{"number":"strJson?"}
`
)
var user EASLoginInfo
type BillLaid struct { // 销售出库&其它出库 &采购入库 & 其他入库
BizType BillNumber `json:"bizType"` //业务类型
BizDate string `json:"bizDate"` //业务日期
Description string `json:"description"` //摘要
PaymentType BillNumber `json:"paymentType"` // 付款方式
TransactionType BillNumber `json:"transactionType"` // 事务类型
Number string `json:"number"` // 单据编号
Entry []BillEntry `json:"entry"` //
StorageOrgUnit BillNumber `json:"storageOrgUnit"` //库存组织
Supplier BillNumber `json:"supplier"` //供应商
Currency BillNumber `json:"currency"` // 币别
}
type BillNumber struct {
Number string `json:"number"`
}
type BillEntry struct {
ActualPrice string `json:"actualPrice"` //实际单价
ActualTaxPrice string `json:"actualTaxPrice"` //实际含税单价
UnitActualCost string `json:"unitActualCost"` //单位实际成本
UnitPurchaseCost string `json:"unitPurchaseCost"` //单位采购成本
TaxPrice string `json:"taxPrice"` //含税单价
BalanceSupplier BillNumber `json:"balanceSupplier"` //结算供应商
Price string `json:"price"` //单价
ActualCost string `json:"actualCost"` //实际成本
PurchaseCost string `json:"purchaseCost"` //采购成本
Amount string `json:"amount"` // 金额
TaxAmount string `json:"taxAmount"` // 价税合计
ContractNumber string `json:"contractNumber"` // 合同号
Warehouse BillNumber `json:"warehouse"` // 仓库
Unit BillNumber `json:"unit"` // 单位
Material BillNumber `json:"material"` //物料
Qty string `json:"qty"` //数量
}
type BillUse struct { //领料出库&生产入库
Head BillUseHead `json:"head"`
Entrys []BillUseEntrys `json:"entrys"`
}
type BillUseHead struct {
BillNumber string `json:"billNumber"` //单据编号
BizDate string `json:"bizDate"` //业务日期
BizTypeNumber string `json:"bizTypeNumber"` //业务类型
TransactionType string `json:"transactionType"` //事务类型
Department string `json:"department"` //部门
SupplyStorageOrg string `json:"supplyStorageOrg"` //供方库存组织
StorageOrg string `json:"storageOrg"` //需方库存组织
CostCenter string `json:"costCenter"` //成本中心
Remark string `json:"remark"` //摘要
Creator string `json:"creator"` //创建者
}
type BillUseEntrys struct {
MaterielNumber string `json:"materielNumber"` // 物料
Lot string `json:"lot"` //批次
Amount string `json:"amount"` //数量
MeteredUnit string `json:"meteredUnit"` //单位
WareHouse string `json:"wareHouse"` //需方仓库
Location string `json:"location"` //需方库位
Picker string `json:"picker"` // 领料员
EntryRemark string `json:"entryRemark"` //备注
MasterDepartment string `json:"masterDepartment"` //主制部门
EntryCostCenter string `json:"entryCostCenter"` //成本中心
}
// 备件
type Parts struct {
MaterialNumber string `json:"materialNumber"`
MaterialName string `json:"materialName"`
MaterialGroup string `json:"materialGroup"`
Model string `json:"model"`
BaseUnit string `json:"baseUnit"`
Status int `json:"status"`
//kaClass int32 `json:"kaClass"`
//planningMode int32 `json:"planningMode"`
}
func InitWebS(use, pass, url string) {
user.Password = pass
user.UserName = use
user.DbType = "1"
user.DcName = "MASM"
user.Language = "L2"
user.SlnName = "eas"
user.Url = url
}
func eastest() {
sessionid, _ := EASWebServiceLogin()
//EASWebServicInsertParts(sessionid,"{}")
EASWebServicInsertPartsuselaid(sessionid, "{}", "007")
//EASWebServiceLogout()
}
type Response struct {
Body rspBody `xml:"Body" json:"body"`
}
type rspBody struct {
Record []rqRecord `xml:"multiRef" json:"multiRef"`
SynMaterialResponse SMReturn `xml:"synMaterialResponse" json:"SynMaterialResponse"`
ImportBillResponse ImportBillResponse `xml:"importBillResponse" json:"importBillResponse"`
QueryMaterialDataResponse QureyParts `xml:"queryMaterialDataResponse" json:"queryMaterialDataResponse"`
QueryScmBillDataResponse QureyBills `xml:"queryScmBillDataResponse" json:"queryScmBillDataResponse"`
}
type ImportBillResponse struct {
ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
}
type QureyParts struct {
QueryMaterialDataReturn string `xml:"queryMaterialDataReturn" json:"queryMaterialDataReturn"`
}
type QureyBills struct {
QueryScmBillDataReturn string `xml:"queryScmBillDataReturn" json:"queryScmBillDataReturn"`
}
type SMReturn struct {
SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
}
type rqRecord struct {
SessionId string `xml:"sessionId" json:"sessionId"`
}
func EASWebServiceLogin() (string, error) {
client := &http.Client{}
resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/EASLogin?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return "", err
}
defer resp.Body.Close()
result_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return "", err
}
v := Response{}
err = xml.Unmarshal([]byte(string(result_body)), &v)
if err != nil {
fmt.Println(err.Error())
}
if len(v.Body.Record) > 0 {
return v.Body.Record[0].SessionId, nil
}
return "", err
}
func EASWebServiceLogout() error {
client := &http.Client{}
resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/EASLogin?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
// handle error
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
//fmt.Println(string(result_body))
}
func EASWebServiceQureyParts(SessionId, PartCode string) error {
client := &http.Client{}
resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
// handle error
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
v := Response{}
err = xml.Unmarshal(result_body, &v)
if err != nil {
fmt.Println(err.Error())
}
resmap := make(map[string]interface{}, 0)
err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
//fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
switch resmap["data"].(type) {
case []interface{}:
if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
return errors.New("单据未查询到")
}
default:
return errors.New("单据未查询到")
}
return nil
}
func EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
client := &http.Client{}
resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result_body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(result_body))
if err != nil {
fmt.Println(err)
return err
}
v := Response{}
err = xml.Unmarshal(result_body, &v)
if err != nil {
fmt.Println(err.Error())
}
resmap := make(map[string]interface{}, 0)
err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
//fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
switch resmap["data"].(type) {
case []interface{}:
if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
return errors.New("单据未查询到")
}
default:
return errors.New("单据未查询到")
}
return nil
}
func EASWebServicInsertParts(SessionId string, parts Parts) error {
client := &http.Client{}
soapencJson, err := json.Marshal(parts)
if err != nil {
return err
}
println(string(soapencJson))
resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
// handle error
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
result_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return err
}
v := Response{}
err = xml.Unmarshal(result_body, &v)
if err != nil {
fmt.Println(err.Error())
}
resmap := make(map[string]string, 0)
err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
if resmap["success"] == "0" {
fmt.Println("resmap", resmap["message"])
return errors.New(resmap["message"])
}
fmt.Println("resmap success", resmap["message"], resmap["success"])
return err
}
func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
client := &http.Client{}
resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
body := bytes.NewBuffer([]byte(resstr))
req, err := http.NewRequest("POST",
user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
body,
)
req.Header.Set("Content-Type", "text/xml")
req.Header.Set("SOAPAction", "application/soap+xml")
if err != nil {
// handle error
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
result_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return err
}
v := Response{}
err = xml.Unmarshal(result_body, &v)
if err != nil {
fmt.Println(err.Error())
}
println(string(result_body))
resmap := make(map[string]interface{}, 0)
if v.Body.ImportBillResponse.ImportBillReturn != "" {
err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
//if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
//if resmap["status"] != "1" {
// return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
//}
}
return nil
}
func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
res := ""
switch typeIn {
case "login":
res = LoginStr
res = strings.ReplaceAll(res, "userName?", userName)
res = strings.ReplaceAll(res, "password?", password)
res = strings.ReplaceAll(res, "slnName?", slnName)
res = strings.ReplaceAll(res, "dcName?", dcName)
res = strings.ReplaceAll(res, "language?", language)
res = strings.ReplaceAll(res, "dbType?", dbType)
case "logout":
res = LogoutStr
res = strings.ReplaceAll(res, "userName?", userName)
res = strings.ReplaceAll(res, "slnName?", slnName)
res = strings.ReplaceAll(res, "dcName?", dcName)
res = strings.ReplaceAll(res, "language?", language)
case "InsertParts":
res = InsertPartsStr
res = strings.ReplaceAll(res, "SessionId?", SessionId)
res = strings.ReplaceAll(res, "strJson?", strJson)
case "QureyPartsStr":
res = QureyPartsStr
res = strings.ReplaceAll(res, "SessionId?", SessionId)
res = strings.ReplaceAll(res, "strJson?", strJson)
case "InsertPartsuselaidStr":
res = InsertPartsuselaidStr
res = strings.ReplaceAll(res, "SessionId?", SessionId)
res = strings.ReplaceAll(res, "strJson?", strJson)
res = strings.ReplaceAll(res, "billType?", billType)
case "QureyBill":
res = QureyBill
res = strings.ReplaceAll(res, "SessionId?", SessionId)
res = strings.ReplaceAll(res, "strJson?", strJson)
res = strings.ReplaceAll(res, "billType?", billType)
}
return res
}