mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-26 16:46:14 +08:00
修复树表上级关系绑定验证,优化CURD代码生成
This commit is contained in:
@@ -23,6 +23,13 @@ var defaultFormModeMap = map[string]string{
|
||||
TsTypeAny: FormModeInput,
|
||||
}
|
||||
|
||||
var defaultEditFields = map[string]bool{
|
||||
"id": false,
|
||||
"created_at": false,
|
||||
"updated_at": false,
|
||||
"deleted_at": false,
|
||||
}
|
||||
|
||||
var defaultEditSwitch = map[string]bool{
|
||||
"id": false,
|
||||
"level": false,
|
||||
@@ -33,6 +40,7 @@ var defaultEditSwitch = map[string]bool{
|
||||
"updated_at": false,
|
||||
"deleted_at": false,
|
||||
}
|
||||
|
||||
var defaultListSwitch = map[string]bool{
|
||||
"level": false,
|
||||
"tree": false,
|
||||
@@ -135,6 +143,10 @@ func setDefaultEdit(field *sysin.GenCodesColumnListModel) {
|
||||
return
|
||||
}
|
||||
|
||||
if df, ok := defaultEditFields[field.Name]; ok {
|
||||
field.IsEdit = df
|
||||
}
|
||||
|
||||
if df, ok := defaultEditSwitch[field.Name]; ok {
|
||||
field.IsEdit = df
|
||||
}
|
||||
|
@@ -25,7 +25,8 @@ const (
|
||||
InputTypeListModel = 2 // 列表输出
|
||||
InputTypeExportModel = 3 // 列表导出
|
||||
InputTypeEditInpValidator = 4 // 添加&编辑验证器
|
||||
|
||||
InputTypeUpdateFields = 5 // 编辑修改过滤字段
|
||||
InputTypeInsertFields = 6 // 编辑新增过滤字段
|
||||
EditInpValidatorGenerally = "if err := g.Validator().Rules(\"%s\").Data(in.%s).Messages(\"%s\").Run(ctx); err != nil {\n\t\treturn err.Current()\n\t}\n"
|
||||
)
|
||||
|
||||
@@ -35,6 +36,8 @@ func (l *gCurd) inputTplData(ctx context.Context, in *CurdPreviewInput) (data g.
|
||||
data["listModelColumns"] = l.generateInputListColumns(ctx, in, InputTypeListModel)
|
||||
data["exportModelColumns"] = l.generateInputListColumns(ctx, in, InputTypeExportModel)
|
||||
data["editInpValidator"] = l.generateInputListColumns(ctx, in, InputTypeEditInpValidator)
|
||||
data["updateFieldsColumns"] = l.generateInputListColumns(ctx, in, InputTypeUpdateFields)
|
||||
data["insertFieldsColumns"] = l.generateInputListColumns(ctx, in, InputTypeInsertFields)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -132,7 +135,7 @@ func (l *gCurd) generateStructFieldDefinition(field *sysin.GenCodesColumnListMod
|
||||
if !field.Required && (field.FormRole == "none" || field.FormRole == "") {
|
||||
return nil
|
||||
}
|
||||
rule := "// 验证" + field.GoName + "\n"
|
||||
rule := "// 验证" + field.Dc + "\n"
|
||||
if field.Required && (field.FormRole == FormRoleNone || field.FormRole == "") {
|
||||
field.FormRole = "required"
|
||||
}
|
||||
@@ -142,6 +145,22 @@ func (l *gCurd) generateStructFieldDefinition(field *sysin.GenCodesColumnListMod
|
||||
rule += s
|
||||
}
|
||||
result = []string{rule}
|
||||
case InputTypeUpdateFields:
|
||||
if !field.IsEdit && field.GoName != "UpdatedBy" {
|
||||
return nil
|
||||
}
|
||||
|
||||
result = append(result, " #"+field.GoType)
|
||||
result = append(result, " #"+fmt.Sprintf(tagKey+`json:"%s"`, field.TsName))
|
||||
result = append(result, " #"+fmt.Sprintf(`dc:"%s"`+tagKey, descriptionTag))
|
||||
case InputTypeInsertFields:
|
||||
if !field.IsEdit && field.GoName != "CreatedBy" {
|
||||
return nil
|
||||
}
|
||||
|
||||
result = append(result, " #"+field.GoType)
|
||||
result = append(result, " #"+fmt.Sprintf(tagKey+`json:"%s"`, field.TsName))
|
||||
result = append(result, " #"+fmt.Sprintf(`dc:"%s"`+tagKey, descriptionTag))
|
||||
default:
|
||||
panic("inputType is invalid")
|
||||
}
|
||||
|
@@ -21,8 +21,8 @@ const (
|
||||
LogicListSimpleSelect = "\tfields, err := hgorm.GenSelect(ctx, sysin.%sListModel{}, dao.%s)\n\tif err != nil {\n\t\treturn\n\t}"
|
||||
LogicListJoinSelect = "\t//关联表select\n\tfields, err := hgorm.GenJoinSelect(ctx, %sin.%sListModel{}, dao.%s, []*hgorm.Join{\n%v\t})"
|
||||
LogicListJoinOnRelation = "\t// 关联表%s\n\tmod = mod.%s(hgorm.GenJoinOnRelation(\n\t\tdao.%s.Table(), dao.%s.Columns().%s, // 主表表名,关联字段\n\t\tdao.%s.Table(), \"%s\", dao.%s.Columns().%s, // 关联表表名,别名,关联字段\n\t)...)\n\n"
|
||||
LogicEditUpdate = "\t\t_, err = s.Model(ctx%s).\n\t\t\tFields(\n%s\t\t\t).\n\t\t\tWhere(dao.%s.Columns().%s, in.%s).Data(in).Update()\n\t\treturn "
|
||||
LogicEditInsert = "\t_, err = s.Model(ctx, &handler.Option{FilterAuth: false}).\n\t\tFields(\n%s\t\t).\n\t\tData(in).Insert()"
|
||||
LogicEditUpdate = "\t\t_, err = s.Model(ctx%s).\n\t\t\tFields(%sin.%sUpdateFields{}).\n\t\t\tWherePri(in.%s).Data(in).Update()\n\t\treturn "
|
||||
LogicEditInsert = "\t_, err = s.Model(ctx, &handler.Option{FilterAuth: false}).\n\t\tFields(%sin.%sInsertFields{}).\n\t\tData(in).Insert()"
|
||||
LogicEditUnique = "\t// 验证'%s'唯一\n\tif err = hgorm.IsUnique(ctx, dao.%s, g.Map{dao.%s.Columns().%s: in.%s}, \"%s已存在\", in.Id); err != nil {\n\t\treturn\n\t}\n"
|
||||
LogicSwitchUpdate = "g.Map{\n\t\tin.Key: in.Value,\n%s}"
|
||||
LogicStatusUpdate = "g.Map{\n\t\tdao.%s.Columns().Status: in.Status,\n%s}"
|
||||
@@ -69,7 +69,7 @@ func (l *gCurd) generateLogicSwitchFields(ctx context.Context, in *CurdPreviewIn
|
||||
if in.options.Step.HasSwitch {
|
||||
for _, field := range in.masterFields {
|
||||
if field.FormMode == "Switch" {
|
||||
buffer.WriteString("\t\t\"" + field.TsName + "\",\n")
|
||||
buffer.WriteString("\t\tdao." + in.In.DaoName + ".Columns()." + field.GoName + ",\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,9 +79,7 @@ func (l *gCurd) generateLogicSwitchFields(ctx context.Context, in *CurdPreviewIn
|
||||
func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.Map {
|
||||
var (
|
||||
data = make(g.Map)
|
||||
updateFields = ""
|
||||
updateBuffer = bytes.NewBuffer(nil)
|
||||
insertFields = ""
|
||||
insertBuffer = bytes.NewBuffer(nil)
|
||||
uniqueBuffer = bytes.NewBuffer(nil)
|
||||
)
|
||||
@@ -95,14 +93,6 @@ func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.M
|
||||
insertBuffer.WriteString("\tin.CreatedBy = contexts.GetUserId(ctx)\n")
|
||||
}
|
||||
|
||||
if field.Index != consts.GenCodesIndexPK && (field.IsEdit == true || field.GoName == "UpdatedAt" || field.GoName == "UpdatedBy") {
|
||||
updateFields = updateFields + "\t\t\t\tdao." + in.In.DaoName + ".Columns()." + field.GoName + ",\n"
|
||||
}
|
||||
|
||||
if field.Index != consts.GenCodesIndexPK && (field.IsEdit == true || field.Required == true || field.GoName == "CreatedAt" || field.GoName == "CreatedBy") {
|
||||
insertFields = insertFields + "\t\t\t\tdao." + in.In.DaoName + ".Columns()." + field.GoName + ",\n"
|
||||
}
|
||||
|
||||
if field.Unique {
|
||||
uniqueBuffer.WriteString(fmt.Sprintf(LogicEditUnique, field.GoName, in.In.DaoName, in.In.DaoName, field.GoName, field.GoName, field.Dc))
|
||||
}
|
||||
@@ -113,8 +103,8 @@ func (l *gCurd) generateLogicEdit(ctx context.Context, in *CurdPreviewInput) g.M
|
||||
notFilterAuth = ", &handler.Option{FilterAuth: false}"
|
||||
}
|
||||
|
||||
updateBuffer.WriteString(fmt.Sprintf(LogicEditUpdate, notFilterAuth, updateFields, in.In.DaoName, in.pk.GoName, in.pk.GoName))
|
||||
insertBuffer.WriteString(fmt.Sprintf(LogicEditInsert, insertFields))
|
||||
updateBuffer.WriteString(fmt.Sprintf(LogicEditUpdate, notFilterAuth, in.options.TemplateGroup, in.In.VarName, in.pk.GoName))
|
||||
insertBuffer.WriteString(fmt.Sprintf(LogicEditInsert, in.options.TemplateGroup, in.In.VarName))
|
||||
|
||||
data["update"] = updateBuffer.String()
|
||||
data["insert"] = insertBuffer.String()
|
||||
|
Reference in New Issue
Block a user