代码生成新增单选按钮动态绑定数据

This commit is contained in:
不做码农 2021-11-27 18:43:14 +08:00
parent 250a9a9847
commit 6433861fe7
8 changed files with 88 additions and 62 deletions

View File

@ -33,11 +33,17 @@ namespace ZR.Admin.WebApi.Controllers
private CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService();
private IGenTableService GenTableService;
private IGenTableColumnService GenTableColumnService;
private readonly ISysDictDataService SysDictDataService;
private IWebHostEnvironment WebHostEnvironment;
public CodeGeneratorController(IGenTableService genTableService, IGenTableColumnService genTableColumnService, IWebHostEnvironment webHostEnvironment)
public CodeGeneratorController(
IGenTableService genTableService,
IGenTableColumnService genTableColumnService,
ISysDictDataService dictDataService,
IWebHostEnvironment webHostEnvironment)
{
GenTableService = genTableService;
GenTableColumnService = genTableColumnService;
SysDictDataService = dictDataService;
WebHostEnvironment = webHostEnvironment;
}
@ -71,40 +77,6 @@ namespace ZR.Admin.WebApi.Controllers
return SUCCESS(vm);
}
/// <summary>
/// 代码生成器
/// </summary>
/// <param name="dto">数据传输对象</param>
/// <returns></returns>
[HttpPost("genCode")]
[Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)]
[ActionPermissionFilter(Permission = "tool:gen:code")]
public IActionResult Generate([FromBody] GenerateDto dto)
{
if (dto.TableId <= 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode");
dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd"));
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
if (!string.IsNullOrEmpty(genTableInfo.Options))
{
Dictionary<string, object> options = JsonConvert.DeserializeObject<Dictionary<string, object>>(genTableInfo.Options);
dto.ParentMenuId = (long)options.GetValueOrDefault("parentMenuId", 0);
}
dto.GenTable = genTableInfo;
//生成代码
CodeGeneratorTool.Generate(genTableInfo, dto);
//下载文件
FileHelper.ZipGenCode(dto);
//HttpContext.Response.Headers.Add("Content-disposition", $"attachment; filename={zipFileName}");
return SUCCESS(new { zipPath = "/Generatecode/" + dto.ZipFileName, fileName = dto.ZipFileName });
}
/// <summary>
/// 获取代码生成表列表
/// </summary>
@ -241,6 +213,12 @@ namespace ZR.Admin.WebApi.Controllers
}
var genTableInfo = GenTableService.GetGenTableInfo(tableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(tableId);
//var dictList = genTableInfo.Columns.FindAll(x => !string.IsNullOrEmpty(x.DictType));
//foreach (var item in dictList)
//{
// item.DictDatas = SysDictDataService.SelectDictDataByType(item.DictType);
//}
GenerateDto dto = new();
dto.GenTable = genTableInfo;
dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode");
@ -252,5 +230,41 @@ namespace ZR.Admin.WebApi.Controllers
return SUCCESS(dto.GenCodes);
}
/// <summary>
/// 生成代码
/// </summary>
/// <param name="dto">数据传输对象</param>
/// <returns></returns>
[HttpPost("genCode")]
[Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)]
[ActionPermissionFilter(Permission = "tool:gen:code")]
public IActionResult Generate([FromBody] GenerateDto dto)
{
if (dto.TableId <= 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode");
dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd"));
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
//附加参数keyvalue格式
if (!string.IsNullOrEmpty(genTableInfo.Options))
{
Dictionary<string, object> options = JsonConvert.DeserializeObject<Dictionary<string, object>>(genTableInfo.Options);
dto.ParentMenuId = (long)options.GetValueOrDefault("parentMenuId", 0);
}
dto.GenTable = genTableInfo;
//生成代码
CodeGeneratorTool.Generate(genTableInfo, dto);
//下载文件
FileHelper.ZipGenCode(dto);
//HttpContext.Response.Headers.Add("Content-disposition", $"attachment; filename={zipFileName}");
return SUCCESS(new { zipPath = "/Generatecode/" + dto.ZipFileName, fileName = dto.ZipFileName });
}
}
}

View File

@ -152,20 +152,23 @@ namespace ZR.CodeGenerator
{
return sb.ToString();
}
sb.AppendLine("<el-row>");
sb.AppendLine("<el-col :span=\"24\">");
sb.AppendLine(" <el-row>");
if (dbFieldInfo.HtmlType == GenConstants.HTML_INPUT_NUMBER)
{
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{CodeGeneratorTool.FirstLowerCase(columnName)}\">");
sb.AppendLine($" <el-input-number v-model.number=\"form.{CodeGeneratorTool.FirstLowerCase(columnName)}\" placeholder=\"{placeHolder}\" {labelDisabled}/>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME)
{
//时间
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-date-picker v-model=\"form.{columnName}\" format=\"yyyy-MM-dd HH:mm:ss\" value-format=\"yyyy-MM-dd HH:mm:ss\" type=\"datetime\" placeholder=\"选择日期时间\"> </el-date-picker>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_IMAGE_UPLOAD)
{
@ -177,54 +180,68 @@ namespace ZR.CodeGenerator
sb.AppendLine(" </el-upload>");
sb.AppendLine($" <el-input v-model=\"form.{columnName}\" placeholder=\"请上传文件或手动输入文件地址\"></el-input>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue";
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-radio-group v-model=\"form.{columnName}\">");
sb.AppendLine($" <el-radio v-for=\"item in {columnName}Options\" :key=\"item.dictValue\" :label=\"{value}\">{{item.dictLabel}}</el-radio>");
sb.AppendLine(" </el-radio-group>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO)
{
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-radio-group v-model=\"form.{columnName}\">");
sb.AppendLine(" <el-radio :key=\"1\" :label=\"1\">是</el-radio>");
sb.AppendLine(" <el-radio :key=\"0\" :label=\"0\">否</el-radio>");
sb.AppendLine(" </el-radio-group>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
//else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO && !string.IsNullOrEmpty(dbFieldInfo.DictType))
//{
// sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
// sb.AppendLine($" <el-radio-group v-model=\"form.{columnName}\">");
// //TODO 根据字典类型循环
// sb.AppendLine(" </el-radio-group>");
// sb.AppendLine(" </el-form-item>");
//}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_TEXTAREA)
{
sb.AppendLine(" <el-col :span=\"24\">");
sb.AppendLine($" <el-form-item label=\"{ labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-input type=\"textarea\" v-model=\"form.{columnName}\" placeholder=\"请输入内容\"/>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_EDITOR)
{
sb.AppendLine(" <el-col :span=\"24\">");
sb.AppendLine($" <el-form-item label=\"{ labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <editor v-model=\"form.{columnName}\" :min-height=\"200\" />");
sb.AppendLine($" <editor v-model=\"form.{columnName}\" :min-height=\"200\" />");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue";
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{ labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-select v-model=\"form.{columnName}\">");
sb.AppendLine($" <el-option v-for=\"item in {columnName}Options\" :key=\"item.dictValue\" :label=\"item.dictLabel\" :value=\"{value}\"></el-option>");
sb.AppendLine(" </el-select>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
else
{
string inputNumTxt = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? ".number" : "";
sb.AppendLine(" <el-col :span=\"12\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{CodeGeneratorTool.FirstLowerCase(columnName)}\">");
sb.AppendLine($" <el-input v-model{inputNumTxt}=\"form.{CodeGeneratorTool.FirstLowerCase(columnName)}\" placeholder=\"{placeHolder}\" {labelDisabled}/>");
sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>");
}
sb.AppendLine("</el-col>");
sb.AppendLine("</el-row>");
sb.AppendLine(" </el-row>");
return sb.ToString();
}

View File

@ -59,7 +59,6 @@ namespace ZR.CodeGenerator
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
//循环表字段信息
foreach (GenTableColumn dbFieldInfo in listField)
@ -85,12 +84,6 @@ namespace ZR.CodeGenerator
{
replaceDto.InsertColumn += $" it.{dbFieldInfo.CsharpField}, \n";
}
//TODO 查询
//if (dbFieldInfo.IsQuery)
//{
// replaceDto.Querycondition += $"predicate = predicate.And(m => m.{dbFieldInfo.CsharpField}.Contains(parm.Name));";
//}
if ((dbFieldInfo.HtmlType == GenConstants.HTML_SELECT || dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) && !string.IsNullOrEmpty(dbFieldInfo.DictType))
{
sb1.AppendLine($" // {dbFieldInfo.ColumnComment}选项列表");

View File

@ -29,10 +29,7 @@ namespace ZR.CodeGenerator
if (!string.IsNullOrEmpty(dbName))
{
string replaceStr = GetValue(connStr, "database=", ";");
connStr = connStr.Replace(replaceStr, dbName);
//connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.ConnAdmin);
//dbType = ConfigUtils.Instance.GetAppConfig<int>(OptionsSetting.ConnDbType);
}
var db = new SqlSugarScope(new List<ConnectionConfig>()
{

View File

@ -63,5 +63,10 @@ namespace ZR.Model.System.Generate
/// 字典类型
/// </summary>
public string DictType { get; set; }
/// <summary>
/// 字典集合
/// </summary>
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<SysDictData> DictDatas { get; set; }
}
}

View File

@ -17,6 +17,7 @@
<ItemGroup>
<Folder Include="Dto\" />
<Folder Include="Enum\" />
</ItemGroup>
</Project>

View File

@ -184,7 +184,7 @@ export default {
// treeParentCode: genTable.treeParentCode,
// parentMenuId: genTable.parentMenuId,
// };
console.log(JSON.stringify(genTable));
// console.log(JSON.stringify(genTable));
updateGenTable(genTable).then((res) => {
this.msgSuccess(res.msg);
if (res.code === 200) {

View File

@ -166,7 +166,6 @@ export default {
},
//
handlePreview(row) {
// this.msgError("");
previewTable(row.tableId).then((res) => {
if (res.code === 200) {
this.preview.open = true;