提交 5ef701b0 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

generate article content

上级 5d8685b3
......@@ -107,7 +107,7 @@ func mergerDefine(defaultDef, configDef *model.DefData, fieldsToExport *[]string
CreatePathToFieldMap(&defaultDef.Fields[i], defaultFieldMap, nil)
}
for i, field := range configDef.Fields {
vari.TopFiledMap[field.Field] = field
vari.TopFieldMap[field.Field] = field
if !isSetFieldsToExport {
if !stringUtils.StrInArr(field.Field, *fieldsToExport) {
*fieldsToExport = append(*fieldsToExport, field.Field)
......@@ -138,7 +138,7 @@ func mergerDefine(defaultDef, configDef *model.DefData, fieldsToExport *[]string
}
for _, field := range defaultDef.Fields {
vari.TopFiledMap[field.Field] = field
vari.TopFieldMap[field.Field] = field
}
}
......
......@@ -228,7 +228,7 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) (
return list, ""
}
selectCol := strings.Replace(field.Select, "-", "_", -1)
selectCol := field.Select
from := dbName
if tableName != "" {
from += "_" + tableName
......@@ -309,6 +309,10 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) (
}
}
if field.Select == "xingrongci_waimao_nvxing" {
log.Println(field.Select)
}
return list, selectCol
}
......
......@@ -156,8 +156,14 @@ func GenerateForField(field *model.DefField, withFix bool) (values []string) {
}
} else if field.Select != "" { // refer to excel
groupValues := vari.Res[field.From]
slct := field.Select
values = append(values, groupValues[slct]...)
resKey := field.Select
// deal with the key
if vari.Def.Type == constant.ConfigTypeArticle {
resKey = resKey + "_" + field.Field
}
values = append(values, groupValues[resKey]...)
}
values = loopFieldValues(field, values, vari.Total, true)
......
......@@ -32,7 +32,7 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool,
for j, col := range cols {
col = replacePlaceholder(col)
field := vari.TopFiledMap[fields[j]]
field := vari.TopFieldMap[fields[j]]
if field.Width > runewidth.StringWidth(col) {
col = stringUtils.AddPad(col, field)
}
......
......@@ -2,6 +2,7 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
......@@ -10,6 +11,7 @@ import (
"github.com/jinzhu/copier"
"gopkg.in/yaml.v3"
"io/ioutil"
"strings"
)
func LoadResDef(fieldsToExport []string) map[string]map[string][]string {
......@@ -28,7 +30,7 @@ func LoadResDef(fieldsToExport []string) map[string]map[string][]string {
}
func loadResField(field *model.DefField, res *map[string]map[string][]string) {
if len(field.Fields) > 0 {
if len(field.Fields) > 0 { // sub fields
for _, child := range field.Fields {
if child.Use != "" && child.From == "" {
child.From = field.From
......@@ -36,7 +38,7 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
loadResField(&child, res)
}
} else if len(field.Froms) > 0 {
} else if len(field.Froms) > 0 { // multiple from
for _, child := range field.Froms {
if child.Use != "" && child.From == "" {
child.From = field.From
......@@ -44,24 +46,59 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
loadResField(&child, res)
}
} else if field.From != "" {
resFile, resType, sheet := fileUtils.GetResProp(field.From)
valueMap, _ := getResValue(resFile, resType, sheet, field)
} else if field.From != "" { // refer to res
var valueMap map[string][]string
if vari.Def.Type == constant.ConfigTypeArticle && field.UseLastSameValue { // use last
valueMap = getLastDuplicateVal((*res)[field.From], field.Select)
}
if valueMap == nil {
resFile, resType, sheet := fileUtils.GetResProp(field.From)
valueMap, _ = getResValue(resFile, resType, sheet, field)
}
if (*res)[field.From] == nil {
(*res)[field.From] = map[string][]string{}
}
for key, val := range valueMap {
(*res)[field.From][key] = val
resKey := key
// avoid article key to be duplicate
if vari.Def.Type == constant.ConfigTypeArticle {
resKey = resKey + "_" + field.Field
}
(*res)[field.From][resKey] = val
}
} else if field.Config != "" {
} else if field.Config != "" { // refer to config
resFile, resType, _ := fileUtils.GetResProp(field.Config)
values, _ := getResValue(resFile, resType, "", field)
(*res)[field.Config] = values
}
}
func getLastDuplicateVal(preMap map[string][]string, key string) (valMap map[string][]string) {
lastKey := ""
for k := range preMap {
if key == removeKeyNumber(k) {
lastKey = k
break
}
}
if lastKey == "" || preMap[lastKey] == nil {
return nil
}
valMap = map[string][]string{}
valMap[key] = preMap[lastKey]
return
}
func removeKeyNumber(key string) string {
arr := strings.Split(key, "_")
ret := strings.Join(arr[:len(arr) - 1], "_")
return ret
}
func getResValue(resFile, resType, sheet string, field *model.DefField) (map[string][]string, string) {
resName := ""
groupedValues := map[string][]string{}
......
......@@ -85,7 +85,7 @@ type FieldSimple struct {
LoopIndex int `yaml:"-"`
IsRand bool `yaml:"-"`
IsReferYaml bool `yaml:"-"`
UseLastSameValue bool `yaml:"-"`
UseLastSameValue bool `yaml:"useLastSameValue"`
}
type FieldWithValues struct {
......
......@@ -32,10 +32,10 @@ var (
Ip string
Port int
Def = model.DefData{}
Res = map[string]map[string][]string{}
Def = model.DefData{}
Res = map[string]map[string][]string{}
RandFieldNameToValuesMap = map[string]map[string]interface{}{}
TopFiledMap = map[string]model.DefField{}
TopFieldMap = map[string]model.DefField{}
CacheResFileToMap = map[string] map[string][]string {}
CacheResFileToName = map[string]string{}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册