Exemple #1
0
        public IActionResult CodeGenerate([FromQuery] CodeGeneratorInput input)
        {
            var template   = new SingleTableTemplate();
            var fileConfig = template.GetFileConfig(_env.ContentRootPath, input);
            var result     = new Dictionary <string, string>();

            if (input.WriteModel)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"SELECT COLUMN_NAME TableColumn,
		                           DATA_TYPE Datatype,
		                           (CASE COLUMN_KEY WHEN 'PRI' THEN COLUMN_NAME ELSE '' END) TableIdentity,
		                           REPLACE(REPLACE(SUBSTRING(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)),'(',''),')','') FieldLength,
	                               (CASE IS_NULLABLE WHEN 'NO' THEN 'N' ELSE 'Y' END) IsNullable,
                                   IFNULL(COLUMN_DEFAULT,'') FieldDefault,
                                   COLUMN_COMMENT Remark
                             FROM information_schema.columns WHERE table_schema='" + GetDatabase() + "' AND table_name='" + fileConfig.TableName + "'");
                var fieldList = _sqlExecuterRepository.SqlQuery <TableFieldInfo>(strSql.ToString()).ToList();
                if (fieldList.Count < 1)
                {
                    result.Add("实体类", $"生成失败!未在{GetDatabase()}中找到表{fileConfig.TableName}");
                }
                else
                {
                    DataTable dt         = DataTableHelper.ListToDataTable(fieldList);
                    var       codeEntity = template.BuildEntity(fileConfig, dt);
                    result.Add("实体类", "生成成功!");
                    FileHelper.OutCode2File(fileConfig.OutputEntity, fileConfig.EntityName, codeEntity);
                }
            }
            if (input.WriteCotroller)
            {
                var codeCotroller = template.BuildController(fileConfig);
                result.Add("控制类", "生成成功!");
                FileHelper.OutCode2File(fileConfig.OutputController, fileConfig.ControllerName, codeCotroller);
            }
            if (input.WriteService)
            {
                var codeService = template.BuildService(fileConfig);
                result.Add("服务类", "生成成功!");
                FileHelper.OutCode2File(fileConfig.OutputService, fileConfig.ServiceName, codeService);
                var codeIService = template.BuildIService(fileConfig);
                result.Add("服务接口", "生成成功!");
                FileHelper.OutCode2File(fileConfig.OutputService, fileConfig.IServiceName, codeIService);
            }
            if (input.WriteDto)
            {
                var codeDtoGetInputName = template.BuildDto(fileConfig, $"{fileConfig.DtoGetInputName} : PageRequest");
                FileHelper.OutCode2File(fileConfig.OutputDto, fileConfig.DtoGetInputName, codeDtoGetInputName);
                var codeDtoGetOutputName = template.BuildDto(fileConfig, fileConfig.DtoGetOutputName);
                FileHelper.OutCode2File(fileConfig.OutputDto, fileConfig.DtoGetOutputName, codeDtoGetOutputName);
                var codeDtoUpdateInputName = template.BuildDto(fileConfig, fileConfig.DtoUpdateInputName);
                FileHelper.OutCode2File(fileConfig.OutputDto, fileConfig.DtoUpdateInputName, codeDtoUpdateInputName);
                result.Add("DTO", "生成成功!");
            }
            return(Ok(result));
        }
        public async Task <TData <object> > CodePreviewJson([FromForm] BaseConfigModel baseConfig)
        {
            var obj = new TData <object>();

            if (string.IsNullOrEmpty(baseConfig.OutputConfig.OutputModule))
            {
                obj.Message = "请选择输出到的模块";
                return(obj);
            }

            var objTable = await _databaseTableBLL.GetTableFieldList(baseConfig.TableName);

            var dt = DataTableHelper.ListToDataTable(objTable.Data); // 用DataTable类型,避免依赖

            var codeEntity      = _singleTableTemplate.BuildEntity(baseConfig, dt);
            var codeEntityParam = _singleTableTemplate.BuildEntityParam(baseConfig, dt);
            var codeService     = _singleTableTemplate.BuildService(baseConfig, dt);
            var codeBusiness    = _singleTableTemplate.BuildBusiness(baseConfig);
            var codeIService    = _singleTableTemplate.BuildIService(baseConfig, dt);
            var codeIBusiness   = _singleTableTemplate.BuildIBusiness(baseConfig);

            var codeController = _singleTableTemplate.BuildController(baseConfig);
            var codeIndex      = _singleTableTemplate.BuildIndex(baseConfig, dt);
            var codeForm       = _singleTableTemplate.BuildForm(baseConfig, dt);

            var json = new
            {
                CodeEntity      = HttpUtility.HtmlEncode(codeEntity),
                CodeEntityParam = HttpUtility.HtmlEncode(codeEntityParam),
                CodeService     = HttpUtility.HtmlEncode(codeService),
                CodeBusiness    = HttpUtility.HtmlEncode(codeBusiness),
                CodeIService    = HttpUtility.HtmlEncode(codeIService),
                CodeIBusiness   = HttpUtility.HtmlEncode(codeIBusiness),
                CodeController  = HttpUtility.HtmlEncode(codeController),
                CodeIndex       = HttpUtility.HtmlEncode(codeIndex),
                CodeForm        = HttpUtility.HtmlEncode(codeForm)
            };

            obj.Data = json;
            obj.Tag  = 1;

            return(obj);
        }