提交 88c9cec2 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

close task#7670,7671,7674

上级 f964a1d6
......@@ -36,4 +36,7 @@ fields:
use: small{2}
postfix: "\t"
- from: custom.test.number.v1.yaml
use: large{3}
\ No newline at end of file
use: large{3}
- field: field_blank
range: [user-1,]
\ No newline at end of file
......@@ -118,7 +118,7 @@ fields:
postfix: "}"
- field: field_use_excel # 从excel数据源里面取数据。
from: system.address.v1.china # 从data/system/address/v1.xlsx文件中读取名为china的工作簿
from: address.cn.v1.china # 从data/address/cn.v1.xlsx文件中读取名为china的工作表
select: city # 查询city字段。
where: state like '%山东%' # 条件是省份包含山东。
limit: 10
......
......@@ -15,20 +15,19 @@ import (
"time"
)
func GenerateFieldValuesFromExcel(path string, field *model.DefField) (map[string][]string, string) {
func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (map[string][]string, string) {
values := map[string][]string{}
idx := strings.LastIndex(field.From, ".")
tableName := field.From[idx + 1:]
arr := strings.Split(field.From, ".")
dbName := arr[len(arr) - 3] + "_" + arr[len(arr) - 2]
dbName := getDbName(path)
list := make([]string, 0)
selectCol := ""
ConvertExcelToSQLiteIfNeeded(dbName, path)
firstSheet := ConvertExcelToSQLiteIfNeeded(dbName, path)
if sheet == "" {
sheet = firstSheet
}
list, selectCol = ReadDataFromSQLite(*field, dbName, tableName)
list, selectCol = ReadDataFromSQLite(*field, dbName, sheet)
// get index for data retrieve
numbs := GenerateIntItems(0, (int64)(len(list)-1), 1, false, 1)
// get data by index
......@@ -50,13 +49,22 @@ func GenerateFieldValuesFromExcel(path string, field *model.DefField) (map[strin
return values, dbName
}
func ConvertExcelToSQLiteIfNeeded(dbName string, path string) {
func getDbName(path string) (dbName string) {
dbName = strings.Replace(path, vari.WorkDir + constant.ResDir, "", -1)
dbName = strings.Replace(dbName, constant.PthSep, "_", -1)
dbName = strings.Replace(dbName, ".", "_", -1)
return
}
func ConvertExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet string) {
excel, err := excelize.OpenFile(path)
if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path))
return
}
firstSheet = excel.GetSheetList()[0]
if !isExcelChanged(path) {
return
}
......@@ -148,6 +156,8 @@ func ConvertExcelToSQLiteIfNeeded(dbName string, path string) {
return
}
}
return
}
func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ([]string, string) {
......
......@@ -47,19 +47,17 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
loadResField(&child, res)
}
} else if field.From != "" {
resFile, resType := getResProp(field.From)
values, _ := getResValue(resFile, resType, field)
resFile, resType, sheet := getResProp(field.From)
values, _ := getResValue(resFile, resType, sheet, field)
(*res)[field.From] = values
} else if field.Config != "" {
resFile, resType := getResProp(field.Config)
values, _ := getResValue(resFile, resType, field)
resFile, resType, _ := getResProp(field.Config)
values, _ := getResValue(resFile, resType, "", field)
(*res)[field.Config] = values
}
}
func getResProp(from string) (string, string) { // from resource
resFile := ""
resType := ""
func getResProp(from string) (resFile, resType, sheet string) { // from resource
index := strings.LastIndex(from, ".yaml")
if index > -1 { // yaml, system.ip.v1.yaml
......@@ -68,13 +66,8 @@ func getResProp(from string) (string, string) { // from resource
resFile = left + ".yaml"
resType = "yaml"
} else { // excel, system.address.v1.city
index = strings.LastIndex(from, ".")
left := from[:index]
left = strings.ReplaceAll(left, ".", constant.PthSep)
resFile = left + ".xlsx"
} else { // excel, like address.cn.v1.china
resFile, sheet = convertExcelPath(from)
resType = "excel"
}
......@@ -101,24 +94,60 @@ func getResProp(from string) (string, string) { // from resource
resFile = resPath
}
return resFile, resType
return
}
func convertExcelPath(from string) (ret, sheet string) {
path1 := from // address.cn.v1
index := strings.LastIndex(from, ".")
path2 := from[:index] // address.cn.v1.china
paths := [2]string{path1, path2}
for index, path := range paths {
arr := strings.Split(path, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".") + ".xlsx"
if dir != "" {
ret = dir + constant.PthSep + file
} else {
ret = file
}
realPth := vari.WorkDir + constant.ResDir + ret
if fileUtils.FileExist(realPth) {
if index == 1 {
sheet = from[strings.LastIndex(from, ".")+1:]
}
ret = realPth
return
}
}
}
return
}
func getResValue(resFile string, resType string, field *model.DefField) (map[string][]string, string) {
func getResValue(resFile, resType, sheet string, field *model.DefField) (map[string][]string, string) {
resName := ""
groupedValues := map[string][]string{}
if resType == "yaml" {
groupedValues, resName = getResForYaml(resFile)
} else if resType == "excel" {
groupedValues, resName = getResForExcel(resFile, field)
groupedValues, resName = getResForExcel(resFile, sheet, field)
}
return groupedValues, resName
}
func getResForExcel(resFile string, field *model.DefField) (map[string][]string, string) {
valueMap, resName := GenerateFieldValuesFromExcel(resFile, field)
func getResForExcel(resFile, sheet string, field *model.DefField) (map[string][]string, string) {
valueMap, resName := GenerateFieldValuesFromExcel(resFile, sheet, field)
return valueMap, resName
}
......@@ -199,7 +228,7 @@ func getResForInstances(insts model.ResInsts) map[string][]string {
}
func getRootRangeOrInstant(inst model.DefField) (parentRanges model.ResRanges, parentInsts model.ResInsts) {
resFile, _ := getResProp(inst.From)
resFile, _, _ := getResProp(inst.From)
yamlContent, err := ioutil.ReadFile(resFile)
if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册