package reorder
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
/** REORDER文件解析器,将REORDER文件解析成预定义的数据格式以便于后续的数据处理 **/
|
|
// 解析REORDER文件
|
|
func ParseReorderEdi(ediFile string, reorder *REORDER)(err error){
|
|
var(
|
|
f *os.File
|
|
fd []byte
|
|
datalist []string
|
|
row string
|
|
rowObj string
|
|
ilen, jlen int
|
|
seq SEQ
|
|
gir GIR
|
|
lin LIN
|
|
pia PIA
|
|
qty QTY
|
|
)
|
|
// 打开文件
|
|
if f, err = os.Open(ediFile); err != nil{
|
|
goto ERR
|
|
}
|
|
defer f.Close()
|
|
|
|
// 读取文件数据
|
|
if fd, err = ioutil.ReadAll(f); err != nil{
|
|
goto ERR
|
|
}
|
|
|
|
// 切割数据
|
|
datalist = strings.Split(string(fd), "'")
|
|
|
|
// 遍历数据,生成TOD对象
|
|
reorder.DataRows = 0
|
|
fmt.Println("Total data row = ",len(datalist))
|
|
for _, row = range datalist {
|
|
if strings.TrimSpace(row) == "" {
|
|
continue
|
|
}
|
|
rowObj = getReorderDataRowObj(row)
|
|
switch rowObj{
|
|
case "UNB":
|
|
if err = reorder.Unb.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
case "UNH":
|
|
if err = reorder.Unh.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "BGM":
|
|
if err = reorder.Bgm.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "DTM":
|
|
if err = reorder.Dtm.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "RFF":
|
|
if err = reorder.Rff.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "NAD":
|
|
if err = reorder.Nad.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "FTX":
|
|
if err = reorder.Ftx.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "SEQ":
|
|
seq = SEQ{}
|
|
if err = seq.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.SeqList = append(reorder.SeqList, seq)
|
|
reorder.DataRows++
|
|
case "GIR":
|
|
gir = GIR{}
|
|
if err = gir.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(reorder.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ gir is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
reorder.SeqList[ilen-1].Gir = gir
|
|
reorder.DataRows++
|
|
case "LIN":
|
|
lin = LIN{}
|
|
if err = lin.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(reorder.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ lin is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
reorder.SeqList[ilen-1].LinList = append(reorder.SeqList[ilen-1].LinList, lin)
|
|
reorder.DataRows++
|
|
case "PIA":
|
|
pia = PIA{}
|
|
if err = pia.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(reorder.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ pia is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
jlen = len(reorder.SeqList[ilen-1].LinList)
|
|
if jlen < 1 {
|
|
err = errors.New("SEQ pia is existed before the 1st LIN!")
|
|
goto ERR
|
|
}
|
|
reorder.SeqList[ilen-1].LinList[jlen-1].Pia = pia
|
|
reorder.DataRows++
|
|
case "QTY":
|
|
qty = QTY{}
|
|
if err = qty.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(reorder.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ qty is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
jlen = len(reorder.SeqList[ilen-1].LinList)
|
|
if jlen < 1 {
|
|
err = errors.New("SEQ qty is existed before the 1st LIN!")
|
|
goto ERR
|
|
}
|
|
reorder.SeqList[ilen-1].LinList[jlen-1].Qty = qty
|
|
reorder.DataRows++
|
|
case "UNT":
|
|
reorder.Unt = UNT{}
|
|
if err = reorder.Unt.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
reorder.DataRows++
|
|
case "UNZ":
|
|
reorder.Unz = UNZ{}
|
|
if err = reorder.Unz.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
break
|
|
default:
|
|
err = errors.New("Unknown TOD object : "+rowObj)
|
|
goto ERR
|
|
}
|
|
}
|
|
|
|
if reorder.Unt.DataRows != reorder.DataRows {
|
|
err = errors.New(fmt.Sprintf("Unt datarows: %d is not matching the actual datarows: %d", reorder.Unt.DataRows, reorder.DataRows))
|
|
goto ERR
|
|
}
|
|
|
|
ERR:
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
func getReorderDataRowObj(row string)( obj string){
|
|
if len(row) < 3 {
|
|
return ""
|
|
}
|
|
return strings.ToUpper(row[:3])
|
|
}
|
|
|
|
// 打印解析的REORDER对象
|
|
func PrintReorderEdi(reorder *REORDER){
|
|
var(
|
|
seq SEQ
|
|
lin LIN
|
|
)
|
|
|
|
reorder.Unb.Print()
|
|
reorder.Unh.Print()
|
|
reorder.Bgm.Print()
|
|
reorder.Dtm.Print()
|
|
reorder.Rff.Print()
|
|
reorder.Nad.Print()
|
|
reorder.Ftx.Print()
|
|
for _, seq = range reorder.SeqList {
|
|
seq.Print()
|
|
seq.Gir.Print()
|
|
for _, lin = range seq.LinList {
|
|
lin.Print()
|
|
}
|
|
}
|
|
reorder.Unt.Print()
|
|
reorder.Unz.Print()
|
|
}
|