作者 yangfu

1.测试用例修改 2.共创用户支持csv导入

... ... @@ -12,4 +12,5 @@ require (
github.com/google/uuid v1.1.1
github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d
github.com/stretchr/testify v1.7.0
golang.org/x/text v0.3.6
)
... ...
... ... @@ -11,6 +11,7 @@ type ImportDataCommand struct {
//操作人
Operator domain.Operator `json:"-"`
Reader io.Reader `json:"-"`
FileExt string `json:"-"`
// 业务编码
Code string `form:"code"`
}
... ...
... ... @@ -2,6 +2,7 @@ package service
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/converter"
"strings"
"github.com/linmadan/egglib-go/core/application"
... ... @@ -43,15 +44,12 @@ func (srv ExcelDataService) ImportCooperationUser(importDataCommand *command.Imp
{EnName: "phone", CnName: "*手机号"},
{EnName: "email", CnName: "邮箱"},
}
excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
excelData, err := converter.OpenImportFileFromIoReader(excelImport, importDataCommand.Reader, importDataCommand.FileExt) //excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
users := make([]allied_creation_user.BatchAddUserItem, 0)
for _, v := range excelData {
if srv.fieldValueAllEmpty(v) {
continue
}
item := allied_creation_user.BatchAddUserItem{
CompanyId: importDataCommand.Operator.CompanyId,
UserType: domain.UserTypeCooperation,
... ...
... ... @@ -118,12 +118,25 @@ func (controller *BaseController) GetExcelFile() (io.Reader, error) {
return nil, fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == ".xlsx" || ext == ".xls") {
return nil, fmt.Errorf("仅支持上传文件格式 xls/xlsx")
if !(ext == ".xlsx" || ext == ".xls" || ext == ".csv") {
return nil, fmt.Errorf("仅支持上传文件格式 xls/xlsx/csv")
}
return excelFile, nil
}
func (controller *BaseController) GetFileWithExt() (io.Reader, string, error) {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error(err.Error())
return nil, "", fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == ".xlsx" || ext == ".xls" || ext == ".csv") {
return nil, "", fmt.Errorf("仅支持上传文件格式 xls/xlsx/csv")
}
return excelFile, ext, nil
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
... ...
... ... @@ -189,9 +189,10 @@ func defaultImport(controller *ExcelDataController) {
data interface{}
err error
r io.Reader
ext string
)
excelService := service.NewExcelDataService(nil)
r, err = controller.GetExcelFile()
r, ext, err = controller.GetFileWithExt()
if err != nil {
controller.Response(nil, err)
return
... ... @@ -200,6 +201,7 @@ func defaultImport(controller *ExcelDataController) {
controller.ParseForm(cmd)
cmd.Operator = controller.GetOperator()
cmd.Reader = r
cmd.FileExt = ext
switch cmd.Code {
case domain.ImportCompanyUser:
data, err = excelService.ImportCompanyUser(cmd)
... ...
... ... @@ -296,11 +296,11 @@ func TestAdvancedQuerySql_PG(t *testing.T) {
col: columnNumber,
name: "range many item (80<n<220)",
ins: []Expr{
{OpChar: Range, Value: []interface{}{150, 180}, LeftOp: GreaterThanEqual, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{120, 220}, LeftOp: GreaterThanEqual, RightOp: LessThan},
{OpChar: Range, Value: []interface{}{100, 200}, LeftOp: GreaterThanEqual, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{80, 100}, LeftOp: GreaterThan, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{300, 500}, LeftOp: GreaterThan, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{"150", "180"}, LeftOp: GreaterThanEqual, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{"120", "220"}, LeftOp: GreaterThanEqual, RightOp: LessThan},
{OpChar: Range, Value: []interface{}{"100", "200"}, LeftOp: GreaterThanEqual, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{"80", "100"}, LeftOp: GreaterThan, RightOp: LessThanEqual},
{OpChar: Range, Value: []interface{}{"300", "500"}, LeftOp: GreaterThan, RightOp: LessThanEqual},
},
except: nil,
exceptSql: "(( age > 80 and age < 220 ) or ( age > 300 and age <= 500 ))",
... ...
package converter
import (
"bytes"
"encoding/csv"
"fmt"
"github.com/linmadan/egglib-go/utils/excel"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"strings"
"unicode/utf8"
)
type (
CSVReader struct {
Data *excel.ExcelImport
}
)
func (cr *CSVReader) ToMap(reader io.Reader) ([]map[string]string, error) {
var err error
if reader, err = cr.PrepareCheck(reader); err != nil {
return nil, err
}
r := csv.NewReader(reader)
rows := make([]map[string]string, 0)
var header = make([]string, 0)
var headerMap = make(map[string]string)
var index int = 0
for i := range cr.Data.DataFields {
item := cr.Data.DataFields[i]
headerMap[item.CnName] = item.EnName
}
for {
index++
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if index <= cr.Data.RowBegin-1 {
continue
}
if len(header) == 0 {
header = record
} else {
dict := map[string]string{}
for i := range header {
if column, ok := headerMap[header[i]]; ok {
dict[column] = record[i]
}
}
rows = append(rows, dict)
}
}
return rows, nil
}
func (cr *CSVReader) PrepareCheck(reader io.Reader) (io.Reader, error) {
return GBKToUtf8(reader)
}
func GBKToUtf8(readIn io.Reader) (io.Reader, error) {
var (
err error
fileByte []byte
)
fileByte, err = io.ReadAll(readIn)
if err != nil {
return nil, err
}
if utf8.Valid(fileByte) {
return bytes.NewReader(fileByte), nil
} else {
utf8Reader := transform.NewReader(bytes.NewReader(fileByte), simplifiedchinese.GBK.NewDecoder())
return utf8Reader, nil
}
}
func OpenImportFileFromIoReader(ex *excel.ExcelImport, reader io.Reader, ext string) ([]map[string]string, error) {
var tmp []map[string]string
var err error
if ext == "csv" || ext == ".csv" {
csvReader := &CSVReader{ex}
tmp, err = csvReader.ToMap(reader)
} else {
tmp, err = ex.OpenExcelFromIoReader(reader)
}
if err != nil {
return nil, err
}
var response []map[string]string
for i := range tmp {
if fieldValueAllEmpty(tmp[i]) {
continue
}
response = append(response, tmp[i])
}
return response, nil
}
func fieldValueAllEmpty(param map[string]string) bool {
isAllEmpty := true
for _, v := range param {
value := strings.TrimSpace(v)
if len(value) > 0 {
isAllEmpty = false
}
}
return isAllEmpty
}
// TODO: export csv
func MakeToCsv(sourData excel.ExcelMaker, w io.Writer) error {
_, err := w.Write([]byte("\xEF\xBB\xBF")) //写入UTF-8 BOM
if err != nil {
return err
}
csvWriter := csv.NewWriter(w)
fields := sourData.DataFieldList()
firstRow := []string{}
for i := range fields {
firstRow = append(firstRow, fields[i].CnName)
}
title := sourData.TableTitle()
if len(title) > 0 {
csvWriter.Write(title)
}
csvWriter.Write(firstRow)
dataLenght := sourData.DataListLen()
for i := 0; i < dataLenght; i++ {
rowData := []string{}
for ii := range fields {
cellValue := sourData.CellValue(i, fields[ii].EnName)
str := ""
if cellValue != nil {
str = fmt.Sprintf("%v", cellValue)
}
rowData = append(rowData, str)
}
csvWriter.Write(rowData)
}
csvWriter.Flush()
return csvWriter.Error()
}
... ...