package asn
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ** 打头
|
|
type ASN_SEP struct {
|
|
TradePartnerId ASN_FieldContent
|
|
DocType ASN_FieldContent
|
|
SenderAppId ASN_FieldContent
|
|
RecieverAppId ASN_FieldContent
|
|
GrpSenderId ASN_FieldContent
|
|
GrpRecieverId ASN_FieldContent
|
|
IntChgCtrlNr ASN_FieldContent
|
|
GrpCtrlNr ASN_FieldContent
|
|
DocCtrlNr ASN_FieldContent
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
// 05 打头,头信息
|
|
type ASN_HEAD struct {
|
|
AsnNr ASN_FieldContent
|
|
AsnDate ASN_FieldContent
|
|
AsnTime ASN_FieldContent
|
|
ShipDate ASN_FieldContent
|
|
ShipTime ASN_FieldContent
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
// 10 打头,发运信息
|
|
type ASN_SHIP struct {
|
|
SupplierId ASN_FieldContent
|
|
ShipFromId ASN_FieldContent
|
|
ShipToId ASN_FieldContent
|
|
DockCode ASN_FieldContent
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
// 30 打头,订单信息
|
|
type ASN_ORDER struct {
|
|
CustPartId ASN_FieldContent
|
|
PONr ASN_FieldContent
|
|
ShipQty ASN_FieldContent
|
|
QtyUom ASN_FieldContent
|
|
AsnItem ASN_ITEM
|
|
AsnPackLabellst []ASN_PACKLABEL
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
// 40 打头,零件信息
|
|
type ASN_ITEM struct {
|
|
ShipQty ASN_FieldContent
|
|
QtyUom ASN_FieldContent
|
|
PartDescr ASN_FieldContent
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
// 56 打头,包装标签信息
|
|
type ASN_PACKLABEL struct {
|
|
PalleteNr ASN_FieldContent
|
|
PackageNr ASN_FieldContent
|
|
PartFamily ASN_FieldContent
|
|
SupplyGroup ASN_FieldContent
|
|
OrderNr ASN_FieldContent
|
|
ShipQty ASN_FieldContent
|
|
OemOrderNr string
|
|
SwetYear string
|
|
ParseString string // 原始消息数据串
|
|
}
|
|
|
|
type ASN_FieldContent struct {
|
|
Content string
|
|
StartPos int
|
|
EndPos int
|
|
Width int
|
|
}
|
|
|
|
type ASN struct {
|
|
Separator ASN_SEP
|
|
Header ASN_HEAD
|
|
Shipment ASN_SHIP
|
|
OrderList []ASN_ORDER
|
|
}
|
|
|
|
func (afc *ASN_FieldContent) Read(data string) {
|
|
if afc.EndPos > len(data) {
|
|
return
|
|
}
|
|
afc.Content = strings.TrimSpace(data[afc.StartPos-1 : afc.EndPos])
|
|
return
|
|
}
|
|
|
|
func (as *ASN_SEP) Init() {
|
|
as.TradePartnerId.Content = ""
|
|
as.TradePartnerId.StartPos = 3
|
|
as.TradePartnerId.EndPos = 22
|
|
as.TradePartnerId.Width = 20
|
|
|
|
as.DocType.Content = ""
|
|
as.DocType.StartPos = 23
|
|
as.DocType.EndPos = 32
|
|
as.DocType.Width = 10
|
|
|
|
as.SenderAppId.Content = ""
|
|
as.SenderAppId.StartPos = 33
|
|
as.SenderAppId.EndPos = 52
|
|
as.SenderAppId.Width = 20
|
|
|
|
as.RecieverAppId.Content = ""
|
|
as.RecieverAppId.StartPos = 53
|
|
as.RecieverAppId.EndPos = 72
|
|
as.RecieverAppId.Width = 20
|
|
|
|
as.GrpSenderId.Content = ""
|
|
as.GrpSenderId.StartPos = 73
|
|
as.GrpSenderId.EndPos = 92
|
|
as.GrpSenderId.Width = 20
|
|
|
|
as.GrpRecieverId.Content = ""
|
|
as.GrpRecieverId.StartPos = 93
|
|
as.GrpRecieverId.EndPos = 112
|
|
as.GrpRecieverId.Width = 20
|
|
|
|
as.IntChgCtrlNr.Content = ""
|
|
as.IntChgCtrlNr.StartPos = 113
|
|
as.IntChgCtrlNr.EndPos = 122
|
|
as.IntChgCtrlNr.Width = 10
|
|
|
|
as.GrpCtrlNr.Content = ""
|
|
as.GrpCtrlNr.StartPos = 123
|
|
as.GrpCtrlNr.EndPos = 132
|
|
as.GrpCtrlNr.Width = 10
|
|
|
|
as.DocCtrlNr.Content = ""
|
|
as.DocCtrlNr.StartPos = 133
|
|
as.DocCtrlNr.EndPos = 142
|
|
as.DocCtrlNr.Width = 10
|
|
}
|
|
|
|
func (as *ASN_SEP) Parse(parsedata string) (parseresult string) {
|
|
as.Init()
|
|
as.TradePartnerId.Read(parsedata)
|
|
as.DocType.Read(parsedata)
|
|
as.SenderAppId.Read(parsedata)
|
|
as.RecieverAppId.Read(parsedata)
|
|
as.GrpSenderId.Read(parsedata)
|
|
as.GrpRecieverId.Read(parsedata)
|
|
as.IntChgCtrlNr.Read(parsedata)
|
|
as.GrpCtrlNr.Read(parsedata)
|
|
as.DocCtrlNr.Read(parsedata)
|
|
as.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (as *ASN_SEP) Print() {
|
|
fmt.Println(as.ParseString)
|
|
fmt.Println("TradePartnerId = ", as.TradePartnerId.Content)
|
|
fmt.Println("DocType = ", as.DocType.Content)
|
|
fmt.Println("SenderAppId = ", as.SenderAppId.Content)
|
|
fmt.Println("RecieverAppId = ", as.RecieverAppId.Content)
|
|
fmt.Println("GrpSenderId = ", as.GrpSenderId.Content)
|
|
fmt.Println("GrpRecieverId = ", as.GrpRecieverId.Content)
|
|
fmt.Println("IntChgCtrlNr = ", as.IntChgCtrlNr.Content)
|
|
fmt.Println("GrpCtrlNr = ", as.GrpCtrlNr.Content)
|
|
fmt.Println("DocCtrlNr = ", as.DocCtrlNr.Content)
|
|
fmt.Println("TradePartnerId = ", as.TradePartnerId.Content)
|
|
return
|
|
}
|
|
|
|
func (ah *ASN_HEAD) Init() {
|
|
ah.AsnNr.Content = ""
|
|
ah.AsnNr.StartPos = 5
|
|
ah.AsnNr.EndPos = 24
|
|
ah.AsnNr.Width = 20
|
|
|
|
ah.AsnDate.Content = ""
|
|
ah.AsnDate.StartPos = 25
|
|
ah.AsnDate.EndPos = 32
|
|
ah.AsnDate.Width = 8
|
|
|
|
ah.AsnTime.Content = ""
|
|
ah.AsnTime.StartPos = 33
|
|
ah.AsnTime.EndPos = 38
|
|
ah.AsnTime.Width = 6
|
|
|
|
ah.ShipDate.Content = ""
|
|
ah.ShipDate.StartPos = 39
|
|
ah.ShipDate.EndPos = 46
|
|
ah.ShipDate.Width = 8
|
|
|
|
ah.ShipTime.Content = ""
|
|
ah.ShipTime.StartPos = 47
|
|
ah.ShipTime.EndPos = 52
|
|
ah.ShipTime.Width = 6
|
|
|
|
}
|
|
|
|
func (ah *ASN_HEAD) Parse(parsedata string) (parseresult string) {
|
|
ah.Init()
|
|
ah.AsnNr.Read(parsedata)
|
|
ah.AsnDate.Read(parsedata)
|
|
ah.AsnTime.Read(parsedata)
|
|
ah.ShipDate.Read(parsedata)
|
|
ah.ShipTime.Read(parsedata)
|
|
ah.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (ah *ASN_HEAD) Print() {
|
|
fmt.Println(ah.ParseString)
|
|
fmt.Println("AsnNr = ", ah.AsnNr.Content)
|
|
fmt.Println("AsnDate = ", ah.AsnDate.Content)
|
|
fmt.Println("AsnTime = ", ah.AsnTime.Content)
|
|
fmt.Println("ShipDate = ", ah.ShipDate.Content)
|
|
fmt.Println("ShipTime = ", ah.ShipTime.Content)
|
|
return
|
|
}
|
|
|
|
func (as *ASN_SHIP) Init() {
|
|
as.SupplierId.Content = ""
|
|
as.SupplierId.StartPos = 19
|
|
as.SupplierId.EndPos = 35
|
|
as.SupplierId.Width = 17
|
|
|
|
as.ShipFromId.Content = ""
|
|
as.ShipFromId.StartPos = 36
|
|
as.ShipFromId.EndPos = 52
|
|
as.ShipFromId.Width = 17
|
|
|
|
as.ShipToId.Content = ""
|
|
as.ShipToId.StartPos = 53
|
|
as.ShipToId.EndPos = 69
|
|
as.ShipToId.Width = 17
|
|
|
|
as.DockCode.Content = ""
|
|
as.DockCode.StartPos = 87
|
|
as.DockCode.EndPos = 106
|
|
as.DockCode.Width = 20
|
|
}
|
|
|
|
func (as *ASN_SHIP) Parse(parsedata string) (parseresult string) {
|
|
as.Init()
|
|
as.SupplierId.Read(parsedata)
|
|
as.ShipFromId.Read(parsedata)
|
|
as.ShipToId.Read(parsedata)
|
|
as.DockCode.Read(parsedata)
|
|
as.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (as *ASN_SHIP) Print() {
|
|
fmt.Println(as.ParseString)
|
|
fmt.Println("SupplierId = ", as.SupplierId.Content)
|
|
fmt.Println("ShipFromId = ", as.ShipFromId.Content)
|
|
fmt.Println("ShipToId = ", as.ShipToId.Content)
|
|
fmt.Println("DockCode = ", as.DockCode.Content)
|
|
return
|
|
}
|
|
|
|
func (ao *ASN_ORDER) Init() {
|
|
ao.CustPartId.Content = ""
|
|
ao.CustPartId.StartPos = 26
|
|
ao.CustPartId.EndPos = 55
|
|
ao.CustPartId.Width = 30
|
|
|
|
ao.PONr.Content = ""
|
|
ao.PONr.StartPos = 56
|
|
ao.PONr.EndPos = 77
|
|
ao.PONr.Width = 22
|
|
|
|
ao.ShipQty.Content = ""
|
|
ao.ShipQty.StartPos = 88
|
|
ao.ShipQty.EndPos = 97
|
|
ao.ShipQty.Width = 10
|
|
|
|
ao.QtyUom.Content = ""
|
|
ao.QtyUom.StartPos = 98
|
|
ao.QtyUom.EndPos = 101
|
|
ao.QtyUom.Width = 4
|
|
}
|
|
|
|
func (ao *ASN_ORDER) Parse(parsedata string) (parseresult string) {
|
|
ao.Init()
|
|
ao.CustPartId.Read(parsedata)
|
|
ao.PONr.Read(parsedata)
|
|
ao.ShipQty.Read(parsedata)
|
|
ao.QtyUom.Read(parsedata)
|
|
ao.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (ao *ASN_ORDER) Print() {
|
|
fmt.Println(ao.ParseString)
|
|
fmt.Println("CustPartId = ", ao.CustPartId.Content)
|
|
fmt.Println("PONr = ", ao.PONr.Content)
|
|
fmt.Println("ShipQty = ", ao.ShipQty.Content)
|
|
fmt.Println("QtyUom = ", ao.QtyUom.Content)
|
|
return
|
|
}
|
|
|
|
func (ai *ASN_ITEM) Init() {
|
|
ai.ShipQty.Content = ""
|
|
ai.ShipQty.StartPos = 69
|
|
ai.ShipQty.EndPos = 78
|
|
ai.ShipQty.Width = 10
|
|
|
|
ai.QtyUom.Content = ""
|
|
ai.QtyUom.StartPos = 79
|
|
ai.QtyUom.EndPos = 82
|
|
ai.QtyUom.Width = 4
|
|
|
|
ai.PartDescr.Content = ""
|
|
ai.PartDescr.StartPos = 355
|
|
ai.PartDescr.EndPos = 384
|
|
ai.PartDescr.Width = 30
|
|
}
|
|
|
|
func (ai *ASN_ITEM) Parse(parsedata string) (parseresult string) {
|
|
ai.Init()
|
|
ai.ShipQty.Read(parsedata)
|
|
ai.QtyUom.Read(parsedata)
|
|
ai.PartDescr.Read(parsedata)
|
|
ai.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (ai *ASN_ITEM) Print() {
|
|
fmt.Println(ai.ParseString)
|
|
fmt.Println("ShipQty = ", ai.ShipQty.Content)
|
|
fmt.Println("QtyUom = ", ai.QtyUom.Content)
|
|
fmt.Println("PartDescr = ", ai.PartDescr.Content)
|
|
return
|
|
}
|
|
|
|
func (ap *ASN_PACKLABEL) Init() {
|
|
ap.PalleteNr.Content = ""
|
|
ap.PalleteNr.StartPos = 3
|
|
ap.PalleteNr.EndPos = 22
|
|
ap.PalleteNr.Width = 20
|
|
|
|
ap.PackageNr.Content = ""
|
|
ap.PackageNr.StartPos = 23
|
|
ap.PackageNr.EndPos = 42
|
|
ap.PackageNr.Width = 20
|
|
|
|
ap.PartFamily.Content = ""
|
|
ap.PartFamily.StartPos = 43
|
|
ap.PartFamily.EndPos = 62
|
|
ap.PartFamily.Width = 20
|
|
|
|
ap.SupplyGroup.Content = ""
|
|
ap.SupplyGroup.StartPos = 63
|
|
ap.SupplyGroup.EndPos = 82
|
|
ap.SupplyGroup.Width = 20
|
|
|
|
ap.OrderNr.Content = ""
|
|
ap.OrderNr.StartPos = 83
|
|
ap.OrderNr.EndPos = 107
|
|
ap.OrderNr.Width = 25
|
|
|
|
ap.ShipQty.Content = ""
|
|
ap.ShipQty.StartPos = 128
|
|
ap.ShipQty.EndPos = 137
|
|
ap.ShipQty.Width = 10
|
|
}
|
|
|
|
func (ap *ASN_PACKLABEL) Parse(parsedata string) (parseresult string) {
|
|
var reslst []string
|
|
|
|
ap.Init()
|
|
ap.PalleteNr.Read(parsedata)
|
|
ap.PackageNr.Read(parsedata)
|
|
ap.PartFamily.Read(parsedata)
|
|
ap.SupplyGroup.Read(parsedata)
|
|
ap.OrderNr.Read(parsedata)
|
|
ap.ShipQty.Read(parsedata)
|
|
reslst = strings.Split(ap.OrderNr.Content, "-")
|
|
if len(reslst) >= 1 {
|
|
ap.OemOrderNr = reslst[0]
|
|
}
|
|
if len(reslst) >= 3 {
|
|
ap.SwetYear = reslst[2]
|
|
}
|
|
ap.ParseString = parsedata
|
|
return
|
|
}
|
|
|
|
func (ap *ASN_PACKLABEL) Print() {
|
|
fmt.Println(ap.ParseString)
|
|
fmt.Println("PalleteNr = ", ap.PalleteNr.Content)
|
|
fmt.Println("PackageNr = ", ap.PackageNr.Content)
|
|
fmt.Println("PartFamily = ", ap.PartFamily.Content)
|
|
fmt.Println("SupplyGroup = ", ap.SupplyGroup.Content)
|
|
fmt.Println("OrderNr = ", ap.OrderNr.Content)
|
|
fmt.Println("ShipQty = ", ap.ShipQty.Content)
|
|
fmt.Println("OemOrderNr = ", ap.OemOrderNr)
|
|
return
|
|
}
|
|
|
|
// 解析Grammer ASN文件
|
|
func ParseGrammerAsn(asnFile string, asn *ASN) (err error) {
|
|
var (
|
|
f *os.File
|
|
scanner *bufio.Scanner
|
|
rowdata string
|
|
ao ASN_ORDER
|
|
ai ASN_ITEM
|
|
ap ASN_PACKLABEL
|
|
ilen int
|
|
)
|
|
|
|
if f, err = os.Open(asnFile); err != nil {
|
|
goto ERR
|
|
}
|
|
defer f.Close()
|
|
|
|
// 逐行读取文件
|
|
scanner = bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
rowdata = scanner.Text()
|
|
if len(rowdata) < 2 {
|
|
continue
|
|
}
|
|
switch rowdata[:2] {
|
|
case "**":
|
|
asn.Separator.Parse(rowdata)
|
|
case "05":
|
|
asn.Header.Parse(rowdata)
|
|
case "10":
|
|
asn.Shipment.Parse(rowdata)
|
|
case "30":
|
|
ao = ASN_ORDER{}
|
|
ao.Parse(rowdata)
|
|
asn.OrderList = append(asn.OrderList, ao)
|
|
case "40":
|
|
ai = ASN_ITEM{}
|
|
ai.Parse(rowdata)
|
|
ilen = len(asn.OrderList)
|
|
if ilen < 1 {
|
|
err = errors.New("40 ITEM is existed before 30 ORDER!")
|
|
goto ERR
|
|
}
|
|
asn.OrderList[ilen-1].AsnItem = ai
|
|
case "56":
|
|
ap = ASN_PACKLABEL{}
|
|
ap.Parse(rowdata)
|
|
ilen = len(asn.OrderList)
|
|
if ilen < 1 {
|
|
err = errors.New("56 PACKLABEL is existed before 30 ORDER!")
|
|
goto ERR
|
|
}
|
|
asn.OrderList[ilen-1].AsnPackLabellst = append(asn.OrderList[ilen-1].AsnPackLabellst, ap)
|
|
}
|
|
}
|
|
|
|
ERR:
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
// 打印解析的ASN对象
|
|
func PrintGrammerAsn(asn *ASN) {
|
|
var (
|
|
ao ASN_ORDER
|
|
ap ASN_PACKLABEL
|
|
)
|
|
|
|
asn.Separator.Print()
|
|
asn.Header.Print()
|
|
asn.Shipment.Print()
|
|
for _, ao = range asn.OrderList {
|
|
ao.Print()
|
|
ao.AsnItem.Print()
|
|
for _, ap = range ao.AsnPackLabellst {
|
|
ap.Print()
|
|
}
|
|
}
|
|
}
|