60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package renew
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchinput/text"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
func ParseInputRows(ctx context.Context, input string) ([]batchimport.Row, error) {
|
|
lines, err := text.ParseWhitespaceFields(ctx, input)
|
|
if err != nil {
|
|
if errors.Is(err, text.ErrEmptyInput) {
|
|
return nil, gerror.New("批量输入续费数据为空")
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
renewRows := make([]*Row, 0, len(lines))
|
|
for _, line := range lines {
|
|
renewRows = append(renewRows, parseRenewInputLine(line))
|
|
}
|
|
|
|
rows := make([]batchimport.Row, 0, len(renewRows))
|
|
for _, row := range renewRows {
|
|
rows = append(rows, row)
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func parseRenewInputLine(line text.FieldLine) *Row {
|
|
row := &Row{RowNo: line.RowNo}
|
|
switch len(line.Fields) {
|
|
case 1:
|
|
row.InputCode = strings.TrimSpace(line.Fields[0])
|
|
row.Iccid = row.InputCode
|
|
case 3, 4:
|
|
row.Iccid = strings.TrimSpace(line.Fields[0])
|
|
row.Msisdn = strings.TrimSpace(line.Fields[1])
|
|
row.Imsi = strings.TrimSpace(line.Fields[2])
|
|
row.InputCode = firstNonEmpty(row.Iccid, row.Msisdn, row.Imsi)
|
|
default:
|
|
row.AddReason("格式错误,应为 ICCID/MSISDN/IMSI 任一号码,或 ICCID MSISDN IMSI")
|
|
}
|
|
return row
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, value := range values {
|
|
if strings.TrimSpace(value) != "" {
|
|
return strings.TrimSpace(value)
|
|
}
|
|
}
|
|
return ""
|
|
}
|