SJA APS后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
3.5 KiB

package calloff
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
// 解析CALLOFF文件
func ParseCalloffEdi(ediFile string, calloff *CALLOFF) (err error) {
var (
f *os.File
fd []byte
datalist []string
row string
rowObj string
ilen int
dtm DTM
rff RFF
seq SEQ
gir GIR
)
// 打开文件
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), "'")
// 遍历数据,生成SEQ对象
calloff.DataRows = 0
fmt.Println("Total data row = ", len(datalist))
for _, row = range datalist {
if strings.TrimSpace(row) == "" {
continue
}
rowObj = getCallOffDataRowObj(row)
switch rowObj {
case "UNB":
if err = calloff.Unb.Parse(row); err != nil {
goto ERR
}
case "UNH":
if err = calloff.Unh.Parse(row); err != nil {
goto ERR
}
calloff.DataRows++
case "BGM":
if err = calloff.Bgm.Parse(row); err != nil {
goto ERR
}
calloff.DataRows++
case "DTM":
dtm = DTM{}
if err = dtm.Parse(row); err != nil {
goto ERR
}
// 基于日期时间类型判定是TOD还是SEQ订单时间
// 137 是TOD消息时间;2是客户要求的发运时间
switch dtm.DateTimeType {
case 137:
calloff.DtmMsg = dtm
case 2:
calloff.DtmDelivery = dtm
default:
err = errors.New("Unknown datetime type in DTM : " + string(dtm.DateTimeType))
goto ERR
}
calloff.DataRows++
case "RFF":
rff = RFF{}
if err = rff.Parse(row); err != nil {
goto ERR
}
switch rff.RefType {
case "CALLOFF":
calloff.RffCF = rff
case "PARTFAMILY":
calloff.RffPF = rff
}
calloff.DataRows++
case "NAD":
if err = calloff.Nad.Parse(row); err != nil {
goto ERR
}
calloff.DataRows++
case "LOC":
if err = calloff.Loc.Parse(row); err != nil {
goto ERR
}
calloff.DataRows++
case "SEQ":
seq = SEQ{}
if err = seq.Parse(row); err != nil {
goto ERR
}
calloff.SeqList = append(calloff.SeqList, seq)
calloff.DataRows++
case "GIR":
gir = GIR{}
if err = gir.Parse(row); err != nil {
goto ERR
}
ilen = len(calloff.SeqList)
if ilen < 1 {
err = errors.New("SEQ gir is existed before the 1st SEQ!")
goto ERR
}
calloff.SeqList[ilen-1].Gir = gir
calloff.DataRows++
case "UNT":
calloff.Unt = UNT{}
if err = calloff.Unt.Parse(row); err != nil {
goto ERR
}
calloff.DataRows++
case "UNZ":
calloff.Unz = UNZ{}
if err = calloff.Unz.Parse(row); err != nil {
goto ERR
}
break
default:
err = errors.New("Unknown SEQ object : " + rowObj)
goto ERR
}
}
if calloff.Unt.DataRows != calloff.DataRows {
err = errors.New(fmt.Sprintf("Unt datarows: %d is not matching the actual datarows: %d", calloff.Unt.DataRows, calloff.DataRows))
goto ERR
}
ERR:
fmt.Println(err)
return
}
func getCallOffDataRowObj(row string) (obj string) {
if len(row) < 3 {
return ""
}
return strings.ToUpper(row[:3])
}
// 打印解析的Calloff对象
func PrintCalloffEdi(calloff *CALLOFF) {
var (
seq SEQ
)
calloff.Unb.Print()
calloff.Unh.Print()
calloff.Bgm.Print()
calloff.DtmMsg.Print()
calloff.DtmDelivery.Print()
calloff.RffCF.Print()
calloff.RffPF.Print()
calloff.Nad.Print()
calloff.Loc.Print()
for _, seq = range calloff.SeqList {
seq.Print()
seq.Gir.Print()
}
calloff.Unt.Print()
calloff.Unz.Print()
}