public override void Generate(StringBuilder sb) { var entityName = Helper.GetClassName(this.Model.Name); var varEntityName = Helper.GetVarName(this.Model.Name); var dtoName = entityName + "Dto"; var dtoVariable = Helper.GetVarName(dtoName); var viewModel = Helper.GetViewMode(this.Model.Name); var varViewModel = Helper.GetVarName(viewModel); var builder = new IndentStringBuilder(); builder.Increase(); builder.AppendFormatLine("public ActionResult Edit{0}(Edit{0}Model model)",entityName); builder.IncreaseIndentLine("{"); builder.AppendLine(); builder.AppendLine("//model.UserId = Company.ZCH49.OaSystem.WebSite.Models.User.GetCurrentUser(this).Id;"); builder.AppendLine("model.Action();"); builder.AppendLine("return View(model);"); builder.AppendLine(); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendLine("/*[HttpPost]"); builder.AppendFormatLine("public ActionResult Delete{0}(Guid id)", entityName); builder.IncreaseIndentLine("{"); builder.AppendLine("var model = new ReturnInfo();"); builder.AppendLine(); builder.AppendLine("if (id == default(Guid))"); builder.IncreaseIndentLine("{"); builder.AppendLine("model.Message = \"no id\";"); builder.DecreaseIndentLine("}"); builder.AppendLine("else"); builder.IncreaseIndentLine("{"); builder.AppendFormatLine(" model = Edit{0}Model.Delete{0}(id);", entityName); builder.DecreaseIndentLine("}"); builder.AppendLine("return Json(model, JsonRequestBehavior.AllowGet);"); builder.AppendLine(""); builder.DecreaseIndentLine("}*/"); builder.AppendLine(); builder.AppendLine("[HttpPost]"); builder.AppendFormatLine("public ActionResult Delete{0}(List<Guid> idList)", entityName); builder.IncreaseIndentLine("{"); builder.AppendLine("var model = new ReturnInfo();"); builder.AppendLine(); builder.AppendLine("if (idList == null || idList.Count == 0)"); builder.IncreaseIndentLine("{"); builder.AppendLine("model.Message = \"no id\";"); builder.DecreaseIndentLine("}"); builder.AppendLine("else"); builder.IncreaseIndentLine("{"); builder.AppendFormatLine(" model = Edit{0}Model.Delete{0}(idList);",entityName); builder.DecreaseIndentLine("}"); builder.AppendLine("return Json(model, JsonRequestBehavior.AllowGet);"); builder.AppendLine(""); builder.DecreaseIndentLine("}"); sb.Append(builder.ToString()); base.Generate(sb); }
private void btnGenActions_Click(object sender, EventArgs e) { List<ParseErrorInfo> errors; ContextMenu contextMenu; DataViewDB dvDB; SqlParser.Instance().Parse(this.textEditorControl.Document.TextContent, 0, out dvDB, out errors, out contextMenu); textEditorControl_Output.Document.TextContent = null; if (dvDB == null) { OnError(SqlParser.Instance().Errors.ToList()); } if (dvDB != null) { OnError(dvDB.Errors); SelectContext.Root = dvDB.Context; dvDB.InitOption(); dvDB.Context.Wise(null); var builder = new IndentStringBuilder(); var query = Helper.GetClassName(this.txtName.Text.Trim()); var query_header = query.EndsWith("_query", StringComparison.OrdinalIgnoreCase) ? query.Substring(0, query.Length - 6) : query; builder.AppendFormatLine("public ActionResult {0}({1}Model model)", query_header, query); builder.IncreaseIndentLine("{"); builder.AppendLine("return View(model);"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("public ActionResult {0}_List({1}Model model)", query_header, query); builder.IncreaseIndentLine("{"); builder.AppendLine("model.Action();"); builder.AppendLine("return Json(model, JsonRequestBehavior.AllowGet);"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("public ActionResult {0}_Export({1}Model model)", query_header, query); builder.IncreaseIndentLine("{"); builder.AppendLine("model.PageSize = int.MaxValue;"); builder.AppendLine("model.PageIndex = 1;"); builder.AppendLine("model.Action();"); builder.AppendLine("var rdlc = new Company.ZCH49.OASystem.Crosscutting.Report.ReportGenerator();"); builder.AppendLine("var error = \"\";"); builder.AppendFormatLine("if (model.{0}Infos.Count > 0)", query_header); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("var buff = rdlc.GenerateReport(model.{0}Infos, \"{0}_Export\", out error);", query_header); builder.AppendFormatLine("return this.File(buff, \"application/vnd.ms-excel\", \"{0} Export.xls\");", query_header.Replace("_", " ").Trim()); builder.DecreaseIndentLine("}"); builder.AppendLine("else"); builder.IncreaseIndentLine("{"); builder.AppendLine("return new EmptyResult();"); builder.DecreaseIndentLine("}"); builder.DecreaseIndentLine("}"); builder.AppendLine(); textEditorControl_Output.Document.TextContent = builder.ToString();// dvDB.Context.Render(); var frm = new ShowCodeFrm(); frm.SetText(textEditorControl_Output.Document.TextContent); frm.Show(); textEditorControl_Output.Refresh(); } }
private void btnGenCCFlowService_Click(object sender, EventArgs e) { List<ParseErrorInfo> errors; ContextMenu contextMenu; DataViewDB dvDB; SqlParser.Instance().Parse(this.textEditorControl.Document.TextContent, 0, out dvDB, out errors, out contextMenu); textEditorControl_Output.Document.TextContent = null; if (dvDB == null) { OnError(SqlParser.Instance().Errors.ToList()); } if (dvDB != null) { SelectContext.Root = dvDB.Context; OnError(dvDB.Errors); dvDB.InitOption(); dvDB.Context.Wise(null); var search = GeneratorUtil.ClassName(this.txtName.Text.Trim()); var dtoName = string.Format("{0}_ConditionDto", GeneratorUtil.ClassName(this.txtName.Text.Trim())); var dtoName_variable = GeneratorUtil.VariableName(dtoName); var domainName = string.Format("{0}_Condition", GeneratorUtil.ClassName(this.txtName.Text.Trim())); var domainName_variable = GeneratorUtil.VariableName(domainName); var action = GeneratorUtil.ClassName(this.txtName.Text.Trim()); var retrunInfoDto_class = "ReturnInfoDto"; if (dvDB.IsPager) { retrunInfoDto_class = "PageOfReturnDto"; } this.ModelInfo = dvDB.Context.Unions[0].Context.TableJoinInfos[0].RightModelInfo; var orders = dvDB.OrderPairs; //获得对应的model var modelName = "unknown"; var paras = dvDB.Context.Parameters; modelName = GeneratorUtil.ClassName(this.txtResultModel.Text.Trim()); var varModelName = Helper.GetVarName(modelName); var serviceDict = new Dictionary<string, string>(); var result_list = false; var result_wraper = true; foreach (var op in dvDB.Options) { if (op.Name.StartsWith(":")) { serviceDict.Add(op.Name.Substring(1), op.Value); } else if (op.Name.Equals("result_list", StringComparison.OrdinalIgnoreCase)) { bool.TryParse(op.Value, out result_list); } else if (op.Name.Equals("result_wraper", StringComparison.OrdinalIgnoreCase)) { bool.TryParse(op.Value, out result_wraper); } } if (serviceDict.Count == 0) return; var modelFields = new Dictionary<string, FieldInfo>(); foreach (var field in dvDB.Context.Unions[0].Context.ReturnFields.OrderByName()) { if (field is MutiField) { var muti_fieds = field as MutiField; foreach (var f in muti_fieds.AllFields.OrderByName()) { var field_name = string.IsNullOrWhiteSpace(f.Alias) ? f.FullName : f.Alias; field_name = GeneratorUtil.ClassName(field_name); if (field_name.LastIndexOf(".") > -1) { field_name = field_name.Substring(field_name.LastIndexOf(".") + 1); } modelFields.Add(field_name, f); } } else { var field_name = string.IsNullOrWhiteSpace(field.Alias) ? field.FullName : field.Alias; field_name = GeneratorUtil.ClassName(field_name); if (field_name.LastIndexOf(".") > -1) { field_name = field_name.Substring(field_name.LastIndexOf(".") + 1); } var nullAble = field.NullAble ? "?" : ""; if (field.SystemType.Equals("string", StringComparison.OrdinalIgnoreCase)) { nullAble = ""; } modelFields.Add(field_name, field); } } //验证返回字段是否合法 foreach (var srv in serviceDict) { var ss = srv.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var s in ss) { if (!modelFields.ContainsKey(s)) { MessageBox.Show(string.Format("结果集中没有该字段:{0} {1}", srv.Key, s)); return; } } } var builder = new IndentStringBuilder(); foreach (var srv in serviceDict) { builder.AppendFormatLine("[OperationContract]"); builder.AppendFormatLine("[WithoutAuthorization]"); builder.AppendFormatLine("[FaultContractAttribute(typeof(UnAuthorization))]"); builder.AppendFormat("List<KeyValuePair<string, string>> {0}(", srv.Key); var fields = srv.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (var index = 0; index < paras.Count(); index++) { var para = paras[index]; builder.AppendFormat("string {0}", para.VariableName); if (index != paras.Count() - 1) { builder.AppendFormat(", ", para); } } builder.AppendFormatLine(");"); builder.AppendLine(); } foreach (var srv in serviceDict) { builder.AppendFormat("public List<KeyValuePair<string, string>> {0}(", srv.Key); var fields = srv.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (var index = 0; index < paras.Count(); index++) { var para = paras[index]; builder.AppendFormat("string {0}", para.VariableName); if (index != paras.Count() - 1) { builder.AppendFormat(", "); } } builder.AppendFormatLine(")"); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("try"); builder.IncreaseIndentLine("{"); builder.AppendFormat("var ret = _appService.{0}(", action); for (var index = 0; index < paras.Count(); index++) { var para = paras[index]; var parseString = para.VariableName; if (para.DbType == CodeHelper.Core.Parse.ParseResults.DataViews.DbType.Guid) { parseString = string.Format("Guid.Parse({0})", para.VariableName); } else if (para.DbType == CodeHelper.Core.Parse.ParseResults.DataViews.DbType.Int) { parseString = string.Format("Int.Parse({0})", para.VariableName); } builder.AppendFormat("{0}", parseString); if (index != paras.Count() - 1) { builder.AppendFormat(", "); } } builder.AppendFormatLine(");"); if (!result_list) { if (result_wraper) { builder.AppendLine("if (ret == null || ret.Data == null)"); builder.AppendLine("\treturn null;"); } else { builder.AppendLine("if (ret == null)"); builder.AppendLine("\treturn null;"); } } else { if (result_wraper) { builder.AppendLine("if (ret == null || ret.Data == null || ret.Data.Count() < 1)"); builder.AppendLine("\treturn null;"); } else { builder.AppendLine("if (ret == null || ret.Data.Count() < 1)"); builder.AppendLine("\treturn null;"); } } builder.AppendLine(); builder.AppendLine("List<KeyValuePair<string, string>> kvs = new List<KeyValuePair<string, string>>();"); if (!result_list) { builder.Append("kvs.Add(new KeyValuePair<string, string>("); for (var index = 0; index < fields.Count(); index++) { var field = fields[index]; if (modelFields[field].DbType == CodeHelper.Core.Parse.ParseResults.DataViews.DbType.String) { if (result_wraper) { builder.AppendFormat("ret.Data.{0}", field); } else { builder.AppendFormat("ret.{0}", field); } } else { if (result_wraper) { builder.AppendFormat("ret.Data.{0}.ToString()", field); } else { builder.AppendFormat("ret.{0}.ToString()", field); } } if (index != fields.Count() - 1) { builder.AppendFormat(", "); } } builder.AppendLine("));"); } else { if (result_wraper) { builder.Append("kvs.AddRange(ret.Data.Select(o => new KeyValuePair<string, string>("); } else { builder.Append("kvs.AddRange(ret.Select(o => new KeyValuePair<string, string>("); } for (var index = 0; index < fields.Count(); index++) { var field = fields[index]; if (modelFields[field].DbType == CodeHelper.Core.Parse.ParseResults.DataViews.DbType.String) { builder.AppendFormat("o.{0}", field); } else { builder.AppendFormat("o.{0}.ToString()", field); } if (index != fields.Count() - 1) { builder.AppendFormat(", "); } } builder.AppendLine(")));"); } builder.AppendLine("return kvs.AddDefaultItem();"); builder.DecreaseIndentLine("}"); builder.AppendFormatLine("catch( Exception ex)"); builder.IncreaseIndentLine("{"); builder.AppendLine("LogHelper.Error(ex);"); builder.AppendLine("return null;"); builder.DecreaseIndentLine("}"); builder.DecreaseIndentLine("}"); builder.AppendLine(); } textEditorControl_Output.Document.TextContent = builder.ToString();// dvDB.Context.Render(); var frm = new ShowCodeFrm(); frm.SetText(textEditorControl_Output.Document.TextContent); frm.Show(); textEditorControl_Output.Refresh(); } }
//private string public override void Generate(StringBuilder sb) { var entityName = Helper.GetClassName(this.Model.Name); var varEntityName = Helper.GetVarName(this.Model.Name); var dtoName = entityName + "Dto"; var dtoVariable = Helper.GetVarName(dtoName); var viewModel = Helper.GetViewMode(this.Model.Name); var varViewModel = Helper.GetVarName(viewModel); var serivce = Helper.GetClassName(Helper.GetModuleName(this.Model.Name) + "Service"); var varSerivce = "_" + Helper.GetVarName(serivce); var builder = new IndentStringBuilder(); builder.AppendLine(@"using System; using System.Collections.Generic; using System.Linq; using System.Web; using Company.ZCH49.OaSystem.WebSite.ServiceLayer;"); builder.AppendLine(); builder.AppendLine("namespace xxx"); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("public class Edit{0}Model", entityName); builder.IncreaseIndentLine("{"); builder.AppendLine("#region property"); builder.AppendLine(); foreach (var f in this.Model.Fields) { if (f.SystemType == "String") { builder.AppendFormatLine("public {0} {1} ", f.SystemType, f.Name); } else { builder.AppendFormatLine("public {0}? {1} ", f.SystemType, f.Name); } builder.AppendLine("{get;set;}"); builder.AppendLine(); } builder.AppendLine("public string ErrorMsg"); builder.AppendLine("{get;set;}"); builder.AppendLine(); builder.AppendLine("public bool IsSave"); builder.AppendLine("{get;set;}"); builder.AppendLine(); builder.AppendLine("public bool IsPostBack"); builder.AppendLine("{get;set;}"); builder.AppendLine(); builder.AppendLine("#endregion"); builder.AppendLine(); builder.AppendFormatLine("public {0} {1} = new {0}();", serivce, varSerivce); builder.AppendLine(); builder.AppendLine("public bool IsNew"); builder.AppendLine("{"); builder.AppendLine("\tget"); builder.AppendLine("\t{"); builder.AppendLine("\t\treturn !this.Id.HasValue;"); builder.AppendLine("\t}"); builder.AppendLine("}"); builder.AppendLine(); builder.AppendLine("public void Action()"); builder.IncreaseIndentLine("{"); builder.AppendLine("Init();"); builder.AppendLine(); builder.AppendLine("if (this.IsSave)"); builder.AppendLine("{"); builder.AppendLine("\tthis.Save();"); builder.AppendLine("}"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendLine("private void Init()"); builder.IncreaseIndentLine("{"); builder.AppendLine("if (this.Id.HasValue && this.IsPostBack == false)"); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("var rslt = {0}.Get{1}(Id.Value);", varSerivce,entityName); builder.AppendFormatLine("if (rslt == null || !rslt.IsSuccess || rslt.Data == null)"); builder.IncreaseIndentLine("{"); builder.AppendLine("this.ErrorMsg = rslt == null ? \"no this data\" : rslt.Message;"); builder.AppendLine("return;"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendLine("var info = rslt.Data;"); foreach (var f in this.Model.Fields) { builder.AppendFormatLine("this.{0} = info.{0};",Helper.GetClassName(f.Name)); } builder.DecreaseIndentLine("}"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("private bool Validate({0} info)",viewModel); builder.IncreaseIndentLine("{"); foreach (var f in this.Model.Fields) { if (!f.NullAble) { if (string.Compare(f.SystemType, "string", true) == 0) { builder.AppendFormatLine("if (string.IsNullOrWhiteSpace(info.{0}))", Helper.GetClassName(f.Name)); } else { builder.AppendFormatLine("if (info.{0} == default({1}))", Helper.GetClassName(f.Name),f.SystemType); } builder.IncreaseIndentLine("{"); builder.AppendFormatLine("this.ErrorMsg = \"{0} Required\";", Helper.GetClassName(f.Name)); builder.AppendLine("return false;"); builder.DecreaseIndentLine("}"); } } builder.AppendLine("return true;"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendLine("private void Save()"); builder.IncreaseIndentLine("{"); //builder.AppendFormatLine("var {0} = new {1}();", varSerivce, serivce); builder.AppendFormatLine("var info = this.To{0}();", viewModel); builder.AppendFormatLine("if (!this.Validate(info))"); builder.AppendLine("\treturn;"); builder.AppendLine(); builder.AppendFormatLine("var rslt = new ReturnInfo();", viewModel); builder.AppendLine(); builder.AppendLine("if (this.IsNew)"); builder.AppendLine("{"); builder.AppendFormatLine("\trslt = {0}.Add{1}(info);",varSerivce, entityName); builder.AppendLine("}"); builder.AppendLine("else"); builder.AppendLine("{"); builder.AppendFormatLine("\trslt = {0}.Update{1}(info);",varSerivce, entityName); builder.AppendLine("}"); builder.AppendLine(); builder.AppendLine("this.ErrorMsg = rslt.Message;"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("private {0} To{0}()", viewModel); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("var info = new {0}()",viewModel); builder.IncreaseIndentLine("{"); foreach (var f in this.Model.Fields) { if (f.NullAble) { builder.AppendFormatLine("{0} = this.{0},", f.Name); } else { if (f.SystemType == "Guid") { if (f.Name.Equals("id", StringComparison.OrdinalIgnoreCase)) { builder.AppendFormatLine("{0} = this.{0}??Guid.NewGuid(),", f.Name); } else { builder.AppendFormatLine("{0} = this.{0}??Guid.Empty,", f.Name); } } else if (f.SystemType == "DateTime") { builder.AppendFormatLine("{0} = this.{0}??DateTime.Now,", f.Name); } else { builder.AppendFormatLine("{0} = this.{0}??default({1}),", f.Name,f.SystemType); } } } builder.DecreaseIndentLine("};"); builder.AppendLine(); builder.AppendLine("return info;"); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("public static ReturnInfo Delete{0}(Guid id)",entityName); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("var {0} = new {1}();",varSerivce,serivce); builder.AppendFormatLine("return {0}.Delete{1}(id);",varSerivce,entityName); builder.DecreaseIndentLine("}"); builder.AppendLine(); builder.AppendFormatLine("public static ReturnInfo Delete{0}(List<Guid> id_list)", entityName); builder.IncreaseIndentLine("{"); builder.AppendFormatLine("var {0} = new {1}();", varSerivce, serivce); builder.AppendFormatLine("return {0}.Delete{1}(id_list);", varSerivce, entityName); builder.DecreaseIndentLine("}"); builder.DecreaseIndentLine("}"); builder.DecreaseIndentLine("}"); sb.Append(builder.ToString()); }