jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
 
namespace HH.WMS.Utils
{
    public class SqlManager
    {
        #region 根据JSON字符串 获得表名、查询条件、排序
        /// <summary>
        /// 根据JSON字符串 获得表名、查询条件、排序 [分页调用]
        /// </summary>
        /// <param name="jsonString"></param>
        /// <param name="tabName">表名[分页调用]</param>
        /// <param name="strWhere">查询条件[分页调用]</param>
        /// <param name="orderBy">排序[分页调用]</param>
        /// <returns>不分页的SQL语句[不分页获得]</returns>
        public static string GetSqlString_Page(string jsonString, out string tabName, out string strWhere, out string orderBy)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Dictionary<string, string>[] dicArray = js.Deserialize<Dictionary<string, string>[]>(jsonString);
 
            //不分页查询
            StringBuilder strSql = new StringBuilder();
 
            //定义表名称
            string tableName = string.Empty;
            //定义查询条件
            StringBuilder sqlWhere = new StringBuilder();
            sqlWhere.Append(" WHERE 1=1 ");
            //定义排序
            string orderby = string.Empty;
 
            foreach (var item in dicArray)
            {
                Dictionary<string, string> dic = item;
 
                string name = dic["name"].ToString();
                //如果是表名称
                if (name.Equals("tableName"))
                {
                    tableName = dic["value"] + " ";
                }
                //如果是查询条件
                else if (name.Equals("select"))
                {
                    string colmn_id = dic["value"].ToString();
                    string colmn_value = dic["text"].ToString();
                    string colmn_key = dic["key"].ToString();
                    string colmn_type = dic["type"].ToString();
                    if (!string.IsNullOrEmpty(colmn_value))
                    {
                        sqlWhere.Append(GetWhereUnit(colmn_id, colmn_value, colmn_key, colmn_type));
                    }
                }
                //如果是排序
                else if (name.Equals("orderby"))
                {
                    string orderby_colmn = dic["value"].ToString();
                    string sort = dic["type"].ToString();
 
                    orderby = " order by " + orderby_colmn + " " + sort;
                }
            }
            tabName = tableName;
            strWhere = sqlWhere.ToString();
            orderBy = orderby;
 
 
            strSql.AppendLine("SELECT * FROM " + tableName + sqlWhere.ToString() + orderby);
 
            return strSql.ToString();
        }
        #endregion
 
        #region 获得查询条件
        /// <summary>
        /// 获得查询条件
        /// </summary>
        /// <param name="colmn_id">字段名</param>
        /// <param name="colmn_value">字段值</param>
        /// <param name="colmn_key">查询时的关键字( '=' 'like')</param>
        /// <param name="colmn_type">字段类型   char  varchar  int</param>
        /// <returns></returns>
        private static string GetWhereUnit(string colmn_id, string colmn_value, string colmn_key, string colmn_type)
        {
            string sqlString = string.Empty;
 
            switch (colmn_key)
            {
                case "=":
                    sqlString = " AND " + colmn_id + "=" + GetValue(colmn_value, colmn_type);
                    break;
                case "!=":
                    sqlString = " AND " + colmn_id + " != " + GetValue(colmn_value, colmn_type);
                    break;
                case "like":
                    sqlString = " AND " + colmn_id + " like '%" + colmn_value + "%' ";
                    break;
                default:
                    break;
            }
            return sqlString;
        }
        #endregion
 
        #region 判断是否需要加单引号
        /// <summary>
        /// 判断是否需要加单引号
        /// </summary>
        /// <param name="colmn_value"></param>
        /// <param name="colmn_type"></param>
        /// <returns></returns>
        private static string GetValue(string colmn_value, string colmn_type)
        {
            string value = string.Empty;
            switch (colmn_type.ToLower())
            {
                case "varchar":
                    value = " '" + colmn_value + "' ";
                    break;
                case "char":
                    value = " '" + colmn_value + "' ";
                    break;
                case "int":
                    value = colmn_value;
                    break;
                default:
                    break;
            }
            return value;
        }
        #endregion
    }
}