using System; using System.Linq; using System.Text; using ZR.Model.System.Generate; namespace ZR.CodeGenerator { /// /// 代码生成模板 /// public class CodeGenerateTemplate { #region vue 模板 /// /// Vue 添加修改表单 /// /// /// public static string TplVueFormContent(GenTableColumn dbFieldInfo, GenTable genTable) { string columnName = dbFieldInfo.CsharpFieldFl; string labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName); string labelDisabled = dbFieldInfo.IsPk ? ":disabled=\"true\"" : ""; StringBuilder sb = new(); string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue"; if (GenConstants.inputDtoNoField.Any(f => f.Contains(dbFieldInfo.CsharpField, StringComparison.OrdinalIgnoreCase))) { return sb.ToString(); } if (!dbFieldInfo.IsInsert && !dbFieldInfo.IsEdit) { sb.AppendLine(" "); sb.AppendLine($" {{{{form.{columnName}}}}}"); sb.AppendLine(" "); return sb.ToString(); } //树 if (genTable.TplCategory.Equals("tree", StringComparison.OrdinalIgnoreCase) && genTable.TreeParentCode != null && dbFieldInfo.CsharpField.Equals(genTable.TreeParentCode)) { sb.AppendLine(@" "); sb.AppendLine($@" "); sb.AppendLine($@" "); sb.AppendLine(@" "); sb.AppendLine(@" "); return sb.ToString(); } //主键、非自增要插入,不能编辑 if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement) { sb.AppendLine(" "); sb.AppendLine($" "); //主键非自增 显示input if (dbFieldInfo.IsPk && !dbFieldInfo.IsIncrement) { sb.AppendLine($" "); } else if (dbFieldInfo.IsIncrement) //只有是 自增 就显示label { sb.AppendLine($" "); } sb.AppendLine(" "); sb.AppendLine(" "); return sb.ToString(); } if (dbFieldInfo.HtmlType == GenConstants.HTML_INPUT_NUMBER) { //数字框 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME) { //时间 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_IMAGE_UPLOAD) { //图片 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($@" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_FILE_UPLOAD) { //文件 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($@" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) { //单选按钮 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine($" {{{{item.dictLabel}}}}"); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_TEXTAREA) { //文本域 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_EDITOR) { //编辑器 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT) { //下拉框 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_CHECKBOX) { //多选框 sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine($" {{{{item.dictLabel}}}}"); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" "); } else { string inputNumTxt = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? ".number" : ""; sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } return sb.ToString(); } ///// ///// Vue 查询表单 ///// ///// ///// //public static string TplQueryFormHtml(GenTableColumn dbFieldInfo) //{ // StringBuilder sb = new(); // string columnName = dbFieldInfo.CsharpFieldFl; // string labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, dbFieldInfo.CsharpField); // if (!dbFieldInfo.IsQuery) return sb.ToString(); // if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME) // { // sb.AppendLine($" "); // sb.AppendLine($" "); // sb.AppendLine(" "); // } // else if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT || dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) // { // sb.AppendLine($" "); // sb.AppendLine($" "); // sb.AppendLine($" "); // sb.AppendLine(" "); // sb.AppendLine(" "); // } // else // { // string inputNumTxt = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? ".number" : ""; // sb.AppendLine($" "); // sb.AppendLine($" "); // sb.AppendLine(" "); // } // return sb.ToString(); //} /// /// Vue 查询列表 /// /// /// /// public static string TplTableColumn(GenTableColumn dbFieldInfo, GenTable genTable) { string columnName = dbFieldInfo.CsharpFieldFl; string label = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName); string showToolTip = ShowToolTip(dbFieldInfo); string formatter = GetFormatter(dbFieldInfo.HtmlType, columnName); StringBuilder sb = new StringBuilder(); //自定义排序字段 if (GenConstants.HTML_CUSTOM_INPUT.Equals(dbFieldInfo.HtmlType) && !dbFieldInfo.IsPk) { sb.AppendLine($@" "); sb.AppendLine(@" "); sb.AppendLine(@" "); } else if (dbFieldInfo.IsList && dbFieldInfo.HtmlType.Equals(GenConstants.HTML_IMAGE_UPLOAD)) { sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); } else if (dbFieldInfo.IsList && !string.IsNullOrEmpty(formatter)) { sb.AppendLine($@" "); sb.AppendLine(@" "); sb.AppendLine(@" "); } else if (dbFieldInfo.IsList) { sb.AppendLine($" "); } return sb.ToString(); } #endregion //模板调用 public static string QueryExp(string propertyName, string queryType) { if (queryType.Equals("EQ")) { return $"it => it.{ propertyName} == parm.{propertyName})"; } if (queryType.Equals("GTE")) { return $"it => it.{ propertyName} >= parm.{propertyName})"; } if (queryType.Equals("GT")) { return $"it => it.{ propertyName} > parm.{propertyName})"; } if (queryType.Equals("LT")) { return $"it => it.{ propertyName} < parm.{propertyName})"; } if (queryType.Equals("LTE")) { return $"it => it.{ propertyName} <= parm.{propertyName})"; } if (queryType.Equals("NE")) { return $"it => it.{ propertyName} != parm.{propertyName})"; } if (queryType.Equals("LIKE")) { return $"it => it.{ propertyName}.Contains(parm.{propertyName}))"; } return $"it => it.{ propertyName} == parm.{propertyName})"; } /// /// 格式化字典数据显示到table /// /// /// /// public static string GetFormatter(string htmlType, string columnName) { if (htmlType.Equals(GenConstants.HTML_CHECKBOX) || htmlType.Equals(GenConstants.HTML_SELECT) || htmlType.Equals(GenConstants.HTML_RADIO)) { return $" :formatter=\"{columnName}Format\""; } return ""; } /// /// 超出隐藏 /// /// /// public static string ShowToolTip(GenTableColumn column) { if (column.CsharpType.Equals("string") || column.HtmlType.Equals(GenConstants.HTML_DATETIME)) { return $" :show-overflow-tooltip=\"true\""; } return ""; } } }