using System;
using System.Linq;
using System.Text;
using ZR.CodeGenerator.Model;
using ZR.Model.System.Generate;
namespace ZR.CodeGenerator
{
///
/// 代码生成模板
///
public class CodeGenerateTemplate
{
///
/// 查询Dto属性
///
///
/// 替换字符对象
///
public static void GetQueryDtoProperty(GenTableColumn tbColumn, ReplaceDto replaceDto)
{
if (tbColumn.IsQuery)
{
//字符串类型表达式
if (tbColumn.CsharpType == GenConstants.TYPE_STRING)
{
replaceDto.QueryCondition += $" predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.{tbColumn.CsharpField}), {QueryExp(tbColumn.CsharpField, tbColumn.QueryType)};\n";
}
//int类型表达式
if (CodeGeneratorTool.IsNumber(tbColumn.CsharpType))
{
replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.{tbColumn.CsharpField} > 0, {QueryExp(tbColumn.CsharpField, tbColumn.QueryType)};\n";
}
//时间类型
if (tbColumn.CsharpType == GenConstants.TYPE_DATE)
{
replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.BeginTime != null, it => it.{tbColumn.CsharpField} >= parm.BeginTime);\n";
replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.EndTime != null, it => it.{tbColumn.CsharpField} <= parm.EndTime);\n";
}
}
}
#region vue 模板
///
/// Vue rules
///
///
///
public static string TplFormRules(GenTableColumn dbFieldInfo)
{
StringBuilder sbRule = new StringBuilder();
//Rule 规则验证
if (!dbFieldInfo.IsPk && !dbFieldInfo.IsIncrement && dbFieldInfo.IsRequired)
{
sbRule.AppendLine($" {dbFieldInfo.ColumnName}: [{{ required: true, message: '请输入{dbFieldInfo.ColumnComment}', trigger: \"blur\"}}],");
}
else if (CodeGeneratorTool.IsNumber(dbFieldInfo.ColumnType) && dbFieldInfo.IsRequired)
{
sbRule.AppendLine($" {dbFieldInfo.ColumnName}: [{{ type: 'number', message: '{dbFieldInfo.ColumnName}必须为数字值', trigger: \"blur\"}}],");
}
return sbRule.ToString();
}
///
/// Vue 添加修改表单
///
///
///
public static string TplVueFormContent(GenTableColumn dbFieldInfo)
{
string columnName = dbFieldInfo.ColumnName;
string labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName);
string labelDisabled = dbFieldInfo.IsPk ? ":disabled=\"true\"" : "";
string placeHolder = dbFieldInfo.IsIncrement ? "" : $"请输入{labelName}";
StringBuilder sb = new StringBuilder();
if (GenConstants.inputDtoNoField.Any(f => f.ToLower().Contains(dbFieldInfo.CsharpField.ToLower())))
{
return sb.ToString();
}
if (!dbFieldInfo.IsInsert || !dbFieldInfo.IsEdit)
{
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(" ");
//sb.AppendLine(" ");
//sb.AppendLine($" ");
sb.AppendLine($@" ");
sb.AppendLine(" ");
sb.AppendLine(" ");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue";
sb.AppendLine(" ");
sb.AppendLine($" ");
sb.AppendLine($" ");
sb.AppendLine($" {{{{item.dictLabel}}}}");
sb.AppendLine(" ");
sb.AppendLine(" ");
sb.AppendLine(" ");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO)
{
sb.AppendLine(" ");
sb.AppendLine($" ");
sb.AppendLine($" ");
sb.AppendLine(" 是");
sb.AppendLine(" 否");
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 && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue";
sb.AppendLine(" ");
sb.AppendLine($" ");
sb.AppendLine($" ");
sb.AppendLine($" ");
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 labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, dbFieldInfo.ColumnName);
if (!dbFieldInfo.IsQuery || dbFieldInfo.HtmlType == GenConstants.HTML_FILE_UPLOAD) return sb.ToString();
if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME)
{
sb.AppendLine(" ");
sb.AppendLine(" ");
sb.AppendLine(" ");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
//string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue";
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.ColumnName;
string label = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName);
string showToolTip = dbFieldInfo.CsharpType == "string" ? ":show-overflow-tooltip=\"true\"" : "";
string formatter = !string.IsNullOrEmpty(dbFieldInfo.DictType) ? $" :formatter=\"{columnName}Format\"" : "";
StringBuilder sb = new StringBuilder();
//有排序字段
if (!string.IsNullOrEmpty(genTable?.SortField.ToString()) && genTable?.SortField.ToString() == dbFieldInfo.CsharpField)
{
sb.AppendLine($@" ");
sb.AppendLine(@" ");
sb.AppendLine($@" ");
sb.AppendLine($" {{{{scope.row.{columnName}}}}}");
sb.AppendLine(@" ");
sb.AppendLine(@" ");
}
else if (dbFieldInfo.IsList && dbFieldInfo.HtmlType.Equals(GenConstants.HTML_IMAGE_UPLOAD))
{
sb.AppendLine($" ");
sb.AppendLine(" ");
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 $"m => m.{ propertyName} == parm.{propertyName})";
}
if (queryType.Equals("GTE"))
{
return $"m => m.{ propertyName} >= parm.{propertyName})";
}
if (queryType.Equals("GT"))
{
return $"m => m.{ propertyName} > parm.{propertyName})";
}
if (queryType.Equals("LT"))
{
return $"m => m.{ propertyName} < parm.{propertyName})";
}
if (queryType.Equals("LTE"))
{
return $"m => m.{ propertyName} <= parm.{propertyName})";
}
if (queryType.Equals("NE"))
{
return $"m => m.{ propertyName} != parm.{propertyName})";
}
if (queryType.Equals("LIKE"))
{
return $"m => m.{ propertyName}.Contains(parm.{propertyName}))";
}
return "";
}
}
}