package tod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"leit.com/leit_seat_aps/common"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
/** TOD文件解析器,将TOD文件解析成预定义的数据格式以便于后续的数据处理 **/
|
|
// 解析TOD文件
|
|
func ParseTodEdi(ediFile string, tod *TOD) (err error) {
|
|
var (
|
|
f *os.File
|
|
fd []byte
|
|
datalist []string
|
|
row string
|
|
rowObj string
|
|
ilen, jlen int
|
|
dtm DTM
|
|
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对象
|
|
tod.DataRows = 0
|
|
fmt.Println("Total data row = ", len(datalist))
|
|
for _, row = range datalist {
|
|
if strings.TrimSpace(row) == "" {
|
|
continue
|
|
}
|
|
rowObj = getTodDataRowObj(row)
|
|
switch rowObj {
|
|
case "UNB":
|
|
if err = tod.Unb.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
case "UNH":
|
|
if err = tod.Unh.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
tod.DataRows++
|
|
case "BGM":
|
|
if err = tod.Bgm.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
tod.DataRows++
|
|
case "DTM":
|
|
dtm = DTM{}
|
|
if err = dtm.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
// 基于日期时间类型判定是TOD还是SEQ订单时间
|
|
// 137 是TOD消息时间;194是订单开始时间
|
|
switch dtm.DateTimeType {
|
|
case 137:
|
|
tod.Dtm = dtm
|
|
case 194:
|
|
ilen = len(tod.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ dtm is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
tod.SeqList[ilen-1].Dtm = dtm
|
|
default:
|
|
err = errors.New("Unknown datetime type in DTM : " + common.ValueToString(dtm.DateTimeType, ""))
|
|
goto ERR
|
|
}
|
|
tod.DataRows++
|
|
case "NAD":
|
|
if err = tod.Nad.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
tod.DataRows++
|
|
case "SEQ":
|
|
seq = SEQ{}
|
|
if err = seq.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
tod.SeqList = append(tod.SeqList, seq)
|
|
tod.DataRows++
|
|
case "GIR":
|
|
gir = GIR{}
|
|
if err = gir.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(tod.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ gir is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
tod.SeqList[ilen-1].Gir = gir
|
|
tod.DataRows++
|
|
case "LIN":
|
|
lin = LIN{}
|
|
if err = lin.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(tod.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ lin is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
tod.SeqList[ilen-1].LinList = append(tod.SeqList[ilen-1].LinList, lin)
|
|
tod.DataRows++
|
|
case "PIA":
|
|
pia = PIA{}
|
|
if err = pia.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(tod.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ pia is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
jlen = len(tod.SeqList[ilen-1].LinList)
|
|
if jlen < 1 {
|
|
err = errors.New("SEQ pia is existed before the 1st LIN!")
|
|
goto ERR
|
|
}
|
|
tod.SeqList[ilen-1].LinList[jlen-1].Pia = pia
|
|
tod.DataRows++
|
|
case "QTY":
|
|
qty = QTY{}
|
|
if err = qty.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
ilen = len(tod.SeqList)
|
|
if ilen < 1 {
|
|
err = errors.New("SEQ qty is existed before the 1st SEQ!")
|
|
goto ERR
|
|
}
|
|
jlen = len(tod.SeqList[ilen-1].LinList)
|
|
if jlen < 1 {
|
|
err = errors.New("SEQ qty is existed before the 1st LIN!")
|
|
goto ERR
|
|
}
|
|
tod.SeqList[ilen-1].LinList[jlen-1].Qty = qty
|
|
tod.DataRows++
|
|
case "UNT":
|
|
tod.Unt = UNT{}
|
|
if err = tod.Unt.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
tod.DataRows++
|
|
case "UNZ":
|
|
tod.Unz = UNZ{}
|
|
if err = tod.Unz.Parse(row); err != nil {
|
|
goto ERR
|
|
}
|
|
break
|
|
default:
|
|
err = errors.New("Unknown TOD object : " + rowObj)
|
|
goto ERR
|
|
}
|
|
}
|
|
|
|
if tod.Unt.DataRows != tod.DataRows {
|
|
err = errors.New(fmt.Sprintf("Unt datarows: %d is not matching the actual datarows: %d", tod.Unt.DataRows, tod.DataRows))
|
|
goto ERR
|
|
}
|
|
|
|
ERR:
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
func getTodDataRowObj(row string) (obj string) {
|
|
if len(row) < 3 {
|
|
return ""
|
|
}
|
|
return strings.ToUpper(row[:3])
|
|
}
|
|
|
|
// 打印解析的TOD对象
|
|
func PrintTodEdi(tod *TOD) {
|
|
var (
|
|
seq SEQ
|
|
lin LIN
|
|
)
|
|
|
|
tod.Unb.Print()
|
|
tod.Unh.Print()
|
|
tod.Bgm.Print()
|
|
tod.Dtm.Print()
|
|
tod.Nad.Print()
|
|
for _, seq = range tod.SeqList {
|
|
seq.Print()
|
|
seq.Gir.Print()
|
|
for _, lin = range seq.LinList {
|
|
lin.Print()
|
|
}
|
|
}
|
|
tod.Unt.Print()
|
|
tod.Unz.Print()
|
|
}
|