Beispiel #1
0
        //导出代码
        private void OutputCode()
        {
            string tableName = lb_Tables.Text;
            List <CompleteField> comFields   = SqlHelper.GetColumnCompleteField(conn, tableName);
            CompleteField        parentField = null;

            foreach (CompleteField field in comFields)
            {
                //经过约定,存在Id=>对应ParentId即为树形结构
                if (field.name == "ParentId")
                {
                    parentField = field;
                    continue;
                }
            }
            //经过约定,存在Id=>对应ParentId即为树形结构,并且人工启用的树选择
            if (parentField != null && parentField.name == "ParentId" && cb_tree.Enabled)
            {
                txt_Controller.Text   = GetTreeController(tableName);
                txt_Index.Text        = GetTreeIndex(tableName);
                txt_PartialBLL.Text   = GetTreePartialBLL(tableName);
                txt_PartialModel.Text = GetTreePartialModel(tableName);
            }
            else
            {
                txt_Controller.Text   = GetController(tableName);
                txt_Index.Text        = GetIndex(tableName);
                txt_PartialBLL.Text   = GetPartialBLL(tableName);
                txt_PartialModel.Text = GetPartialModel(tableName);
            }

            //获取IBLL
            tbIBLL.Text = GetIBLL(tableName);

            //获取Model
            txt_Create.Text = GetCreate(tableName);
            //获取Model
            txt_Edit.Text = GetEdit(tableName);

            //默认值
            txt_CreateParent.Text = "//只有当启用父表时且开启可在子表中编辑父表时会生成";
            txt_EditParent.Text   = "//只有当启用父表时且开启可在子表中编辑父表时会生成";

            //没有启用连表编辑
            if (cb_EnableParent.Checked)
            {
                //启用在字表中编辑父表信息,对应的视图
                if (cb_MulView.Checked)
                {
                    //获取Model
                    txt_CreateParent.Text = GetCreateParent(txt_TableName1.Text);
                    //获取Model
                    txt_EditParent.Text = GetEditParent(txt_TableName1.Text);
                }
            }
        }
Beispiel #2
0
        //处理注解
        public string SetValid(CompleteField field)//0不能为空
        {
            string validStr = "";
            string xtype    = SqlHelper.GetType(field.xType);

            if (xtype == "string")
            {
                //不能为空
                if (field.isNullAble == "0" && field.name.ToLower() != "id")//一般ID为主键
                {
                    validStr = validStr + "        [NotNullExpression]\r\n";
                }
                validStr = validStr + "        [MaxWordsExpression(" + field.length + ")]\r\n";
            }
            else if (xtype == "int" && field.isNullAble == "0")//冲突处理,不能自定义注解
            {
                validStr = validStr + "        [Required(ErrorMessage= \"{0}只能填写数字\")]\r\n";
            }
            else if (field.xType == "int")
            {
                validStr = validStr + "        [IsNumberExpression]\r\n";
            }
            return(validStr);
        }
Beispiel #3
0
        public string GetPartialBLL(string tableName)
        {
            string        leftStr = GetLeftStr(tableName);
            List <string> fields  = SqlHelper.GetColumnField(conn, tableName);

            StringBuilder sb = new StringBuilder();

            sb.Append("using Apps.Common;\r\n");
            sb.Append("using Apps.Models;\r\n");
            sb.Append("using System.Linq;\r\n");
            sb.Append("using System.Collections.Generic;\r\n");
            sb.Append("using System.Linq;\r\n");
            sb.Append("using System;\r\n");
            sb.Append("using " + txt_prefix.Text + ".Models." + leftStr.Replace(".", "") + ";\r\n");
            sb.Append("\r\n");
            sb.Append("namespace " + txt_prefix.Text + ".BLL" + (leftStr == ".Sys" ? "" : "." + leftStr) + "\r\n");
            sb.Append("{\r\n");
            sb.Append("    public  partial class " + tableName + "BLL\r\n");
            sb.Append("    {\r\n");
            sb.Append("\r\n");
            if (cb_EnableParent.Checked)
            {
                List <CompleteField> comFields   = SqlHelper.GetColumnCompleteField(conn, tableName);
                CompleteField        parentField = null;
                foreach (CompleteField field in comFields)
                {
                    if (field.name == txt_TableKey1.Text.Trim())
                    {
                        parentField = field;
                    }
                }

                if (parentField == null)
                {
                    MessageBox.Show("关键父表的外键" + txt_TableKey1.Text + "不正确,请重新确认!不然生成数据需要手动更改");
                    return("");
                }

                sb.Append("    public override List<" + tableName + "Model> GetListByParentId(ref GridPager pager, string queryStr, object parentId)\r\n");
                sb.Append("    {\r\n");
                sb.Append("        IQueryable<" + tableName + "> queryData = null;\r\n");
                if (parentField.xType != "56" && parentField.xType != "127")//非int型主键
                {
                    sb.Append("        string pid = parentId.ToString();\r\n");
                }
                else
                {
                    sb.Append("        int pid = Convert.ToInt32(parentId);\r\n");
                }

                sb.Append("        if (pid != " + (parentField.xType != "56" && parentField.xType != "127" ? "\"0\"" : "0") + ")\r\n");
                sb.Append("        {\r\n");
                sb.Append("        queryData = m_Rep.GetList(a => a." + txt_TableKey1.Text + " == pid);\r\n");
                sb.Append("        }\r\n");
                sb.Append("        else\r\n");
                sb.Append("        {\r\n");
                sb.Append("        queryData = m_Rep.GetList();\r\n");
                sb.Append("        }\r\n");

                sb.Append("        if (!string.IsNullOrWhiteSpace(queryStr))\r\n");
                sb.Append("        {\r\n");
                sb.Append("            queryData = m_Rep.GetList(\r\n");
                sb.Append("                        a => (\r\n");
                bool orEnable = false;
                foreach (CompleteField field in comFields)
                {
                    if (field.xType == "167" || field.xType == "35" || field.xType == "231" || field.xType == "239")
                    {
                        sb.Append("                               " + (orEnable ? "||" : "") + " a." + field.name + ".Contains(queryStr)\r\n");
                        orEnable = true;
                    }
                }
                sb.Append("                             )\r\n");
                sb.Append("                        );\r\n");
                sb.Append("        }\r\n");
                sb.Append("        pager.totalRows = queryData.Count();\r\n");
                sb.Append("        queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);\r\n");
                sb.Append("        return CreateModelList(ref queryData);\r\n");
                sb.Append("    }\r\n");
            }

            sb.Append("        public override List<" + tableName + "Model> CreateModelList(ref IQueryable<" + tableName + "> queryData)\r\n");
            sb.Append("        {\r\n");
            sb.Append("\r\n");
            sb.Append("            List<" + tableName + "Model> modelList = (from r in queryData\r\n");
            sb.Append("                                              select new " + tableName + "Model\r\n");
            sb.Append("                                              {\r\n");
            foreach (string field in fields)
            {
                sb.Append("                                                  " + field + " = r." + field + ",\r\n");
            }
            //启用表关联
            if (cb_EnableParent.Checked)
            {
                //表1
                if (!string.IsNullOrWhiteSpace(txt_TableName1.Text))
                {
                    sb.Append("                                                  " + txt_TableName1.Text.Replace(leftStr + "_", "") + "Name".Trim() + " = r." + txt_TableName1.Text.Trim() + ".Name,\r\n");
                }
            }
            sb.Append("                                              }).ToList();\r\n");
            sb.Append("            return modelList;\r\n");
            sb.Append("        }\r\n");
            sb.Append("    }\r\n");
            sb.Append(" }\r\n");
            return(sb.ToString());
        }