11namespace APIJSON . NET . Controllers
22{
3- using APIJSON . NET . Services ;
3+ using ApiJson . Common ;
4+ using ApiJson . Common . Services ;
45 using Microsoft . AspNetCore . Cors ;
56 using Microsoft . AspNetCore . Mvc ;
67 using Newtonsoft . Json . Linq ;
2021 public class JsonController : ControllerBase
2122 {
2223
23- private SelectTable selectTable ;
24- private DbContext db ;
24+ private SelectTable _selectTable ;
25+ private DbContext _db ;
2526 private readonly IIdentityService _identitySvc ;
26- public JsonController ( SelectTable _selectTable , DbContext _db , IIdentityService identityService )
27+ private ITableMapper _tableMapper ;
28+ public JsonController ( ITableMapper tableMapper , DbContext db , IIdentityService identityService )
2729 {
28-
29- selectTable = _selectTable ;
30- db = _db ;
30+ _db = db ;
3131 _identitySvc = identityService ;
32+ _tableMapper = tableMapper ;
33+ _selectTable = new SelectTable ( _identitySvc , _tableMapper , _db . Db ) ;
3234 }
3335
3436 /// <summary>
@@ -39,7 +41,7 @@ public ActionResult Test()
3941 {
4042 string str = "{\" page\" :1,\" count\" :3,\" query\" :2,\" Org\" :{\" @column\" :\" Id,Name\" }}" ;
4143 var content = new StringContent ( str ) ;
42-
44+
4345 HttpClient hc = new HttpClient ( ) ;
4446 var response = hc . PostAsync ( "http://localhost:89/api/json/org" , content ) . Result ;
4547 string result = ( response . Content . ReadAsStringAsync ( ) . Result ) ; //result就是返回的结果。
@@ -50,11 +52,10 @@ public ActionResult Test()
5052
5153 public async Task < ActionResult > Query1 ( [ FromRoute ] string table )
5254 {
53-
5455 string json = string . Empty ;
5556 using ( StreamReader reader = new StreamReader ( Request . Body , Encoding . UTF8 ) )
5657 {
57- json = await reader . ReadToEndAsync ( ) ;
58+ json = await reader . ReadToEndAsync ( ) ;
5859 }
5960
6061 json = HttpUtility . UrlDecode ( json ) ;
@@ -75,11 +76,13 @@ public async Task<ActionResult> Query1([FromRoute]string table)
7576 }
7677 if ( ! hasTableKey )
7778 {
78- jobject . Add ( table , new JObject ( ) ) ;
79+ jobject . Add ( table , new JObject ( ) ) ;
7980 }
80- var newJson = Newtonsoft . Json . JsonConvert . SerializeObject ( ht ) ;
81- return Query ( newJson ) ;
81+
82+ JObject resultJobj = new SelectTable ( _identitySvc , _tableMapper , _db . Db ) . Query ( ht ) ;
83+ return Ok ( resultJobj ) ;
8284 }
85+
8386 /// <summary>
8487 /// 查询
8588 /// </summary>
@@ -90,142 +93,8 @@ public async Task<ActionResult> Query1([FromRoute]string table)
9093 public ActionResult Query ( [ FromBody ] string json )
9194 {
9295 json = HttpUtility . UrlDecode ( json ) ;
93- JObject ht = new JObject ( ) ;
94- ht . Add ( "code" , "200" ) ;
95- ht . Add ( "msg" , "success" ) ;
96- try
97- {
98- JObject jobject = JObject . Parse ( json ) ;
99- int page = 0 , count = 0 , query = 0 , total = 0 ;
100- foreach ( var item in jobject )
101- {
102- string key = item . Key . Trim ( ) ;
103- JObject jb ;
104- if ( key . Equals ( "[]" ) )
105- {
106- jb = JObject . Parse ( item . Value . ToString ( ) ) ;
107- page = jb [ "page" ] == null ? 0 : int . Parse ( jb [ "page" ] . ToString ( ) ) ;
108- count = jb [ "count" ] == null ? 0 : int . Parse ( jb [ "count" ] . ToString ( ) ) ;
109- query = jb [ "query" ] == null ? 0 : int . Parse ( jb [ "query" ] . ToString ( ) ) ;
110- jb . Remove ( "page" ) ; jb . Remove ( "count" ) ; jb . Remove ( "query" ) ;
111- var htt = new JArray ( ) ;
112- List < string > tables = new List < string > ( ) , where = new List < string > ( ) ;
113- foreach ( var t in jb )
114- {
115- tables . Add ( t . Key ) ; where . Add ( t . Value . ToString ( ) ) ;
116- }
117- if ( tables . Count > 0 )
118- {
119- string table = tables [ 0 ] ;
120- var temp = selectTable . GetTableData ( table , page , count , where [ 0 ] , null ) ;
121- if ( query > 0 )
122- {
123- total = temp . Item2 ;
124- }
125-
126- foreach ( var dd in temp . Item1 )
127- {
128- var zht = new JObject ( ) ;
129- zht . Add ( table , JToken . FromObject ( dd ) ) ;
130- for ( int i = 1 ; i < tables . Count ; i ++ )
131- {
132- string subtable = tables [ i ] ;
133- if ( subtable . EndsWith ( "[]" ) )
134- {
135- subtable = subtable . TrimEnd ( "[]" . ToCharArray ( ) ) ;
136- var jbb = JObject . Parse ( where [ i ] ) ;
137- page = jbb [ "page" ] == null ? 0 : int . Parse ( jbb [ "page" ] . ToString ( ) ) ;
138- count = jbb [ "count" ] == null ? 0 : int . Parse ( jbb [ "count" ] . ToString ( ) ) ;
139-
140- var lt = new JArray ( ) ;
141- foreach ( var d in selectTable . GetTableData ( subtable , page , count , jbb [ subtable ] . ToString ( ) , zht ) . Item1 )
142- {
143- lt . Add ( JToken . FromObject ( d ) ) ;
144- }
145- zht . Add ( tables [ i ] , lt ) ;
146- }
147- else
148- {
149- var ddf = selectTable . GetFirstData ( subtable , where [ i ] . ToString ( ) , zht ) ;
150- if ( ddf != null )
151- {
152- zht . Add ( subtable , JToken . FromObject ( ddf ) ) ;
153-
154- }
155- }
156- }
157- htt . Add ( zht ) ;
158- }
159-
160- }
161- if ( query != 1 )
162- {
163- ht . Add ( "[]" , htt ) ;
164- }
165- }
166- else if ( key . EndsWith ( "[]" ) )
167- {
168- jb = JObject . Parse ( item . Value . ToString ( ) ) ;
169- page = jb [ "page" ] == null ? 0 : int . Parse ( jb [ "page" ] . ToString ( ) ) ;
170- count = jb [ "count" ] == null ? 0 : int . Parse ( jb [ "count" ] . ToString ( ) ) ;
171- query = jb [ "query" ] == null ? 0 : int . Parse ( jb [ "query" ] . ToString ( ) ) ;
172- jb . Remove ( "page" ) ; jb . Remove ( "count" ) ; jb . Remove ( "query" ) ;
173- var htt = new JArray ( ) ;
174- foreach ( var t in jb )
175- {
176- var temp = selectTable . GetTableData ( t . Key , page , count , t . Value . ToString ( ) , null ) ;
177- if ( query > 0 )
178- {
179- total = temp . Item2 ;
180- }
181- foreach ( var d in temp . Item1 )
182- {
183- htt . Add ( JToken . FromObject ( d ) ) ;
184- }
185- }
186- ht . Add ( key , htt ) ;
187- }
188- else if ( key . Equals ( "func" ) )
189- {
190- jb = JObject . Parse ( item . Value . ToString ( ) ) ;
191- Type type = typeof ( FuncList ) ;
192- Object obj = Activator . CreateInstance ( type ) ;
193- var bb = new JObject ( ) ;
194- foreach ( var f in jb )
195- {
196- var types = new List < Type > ( ) ;
197- var param = new List < object > ( ) ;
198- foreach ( var va in JArray . Parse ( f . Value . ToString ( ) ) )
199- {
200- types . Add ( typeof ( object ) ) ;
201- param . Add ( va ) ;
202- }
203- bb . Add ( f . Key , JToken . FromObject ( selectTable . ExecFunc ( f . Key , param . ToArray ( ) , types . ToArray ( ) ) ) ) ;
204- }
205- ht . Add ( "func" , bb ) ;
206- }
207- else if ( key . Equals ( "total@" ) )
208- {
209- ht . Add ( "total" , total ) ;
210- }
211- else
212- {
213- var template = selectTable . GetFirstData ( key , item . Value . ToString ( ) , ht ) ;
214- if ( template != null )
215- {
216- ht . Add ( key , JToken . FromObject ( template ) ) ;
217- }
218- }
219-
220- }
221- }
222- catch ( Exception ex )
223- {
224- ht [ "code" ] = "500" ;
225- ht [ "msg" ] = ex . Message ;
226-
227- }
228- return Ok ( ht ) ;
96+ JObject resultJobj = new SelectTable ( _identitySvc , _tableMapper , _db . Db ) . Query ( json ) ;
97+ return Ok ( resultJobj ) ;
22998 }
23099 /// <summary>
231100 /// 新增
@@ -257,10 +126,10 @@ public ActionResult Add([FromBody]string json)
257126 var dt = new Dictionary < string , object > ( ) ;
258127 foreach ( var f in JObject . Parse ( item . Value . ToString ( ) ) )
259128 {
260- if ( f . Key . ToLower ( ) != "id" && selectTable . IsCol ( key , f . Key ) && ( role . Insert . Column . Contains ( "*" ) || role . Insert . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) ) )
129+ if ( f . Key . ToLower ( ) != "id" && _selectTable . IsCol ( key , f . Key ) && ( role . Insert . Column . Contains ( "*" ) || role . Insert . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) ) )
261130 dt . Add ( f . Key , f . Value ) ;
262131 }
263- int id = db . Db . Insertable ( dt ) . AS ( key ) . ExecuteReturnIdentity ( ) ;
132+ int id = _db . Db . Insertable ( dt ) . AS ( key ) . ExecuteReturnIdentity ( ) ;
264133 ht . Add ( key , JToken . FromObject ( new { code = 200 , msg = "success" , id } ) ) ;
265134 }
266135 }
@@ -309,12 +178,12 @@ public ActionResult Edit([FromBody]string json)
309178 dt . Add ( "id" , value [ "id" ] . ToString ( ) ) ;
310179 foreach ( var f in value )
311180 {
312- if ( f . Key . ToLower ( ) != "id" && selectTable . IsCol ( key , f . Key ) && ( role . Update . Column . Contains ( "*" ) || role . Update . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) ) )
181+ if ( f . Key . ToLower ( ) != "id" && _selectTable . IsCol ( key , f . Key ) && ( role . Update . Column . Contains ( "*" ) || role . Update . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) ) )
313182 {
314183 dt . Add ( f . Key , f . Value ) ;
315184 }
316185 }
317- db . Db . Updateable ( dt ) . AS ( key ) . ExecuteCommand ( ) ;
186+ _db . Db . Updateable ( dt ) . AS ( key ) . ExecuteCommand ( ) ;
318187 ht . Add ( key , JToken . FromObject ( new { code = 200 , msg = "success" , id = value [ "id" ] . ToString ( ) } ) ) ;
319188 }
320189 }
@@ -373,7 +242,7 @@ public ActionResult Remove([FromBody]string json)
373242 p . Add ( new SugarParameter ( $ "@{ f . Key } ", f . Value . ToString ( ) ) ) ;
374243 }
375244 string sql = sb . ToString ( ) . TrimEnd ( ',' ) ;
376- db . Db . Ado . ExecuteCommand ( sql , p ) ;
245+ _db . Db . Ado . ExecuteCommand ( sql , p ) ;
377246 ht . Add ( key , JToken . FromObject ( new { code = 200 , msg = "success" , id = value [ "id" ] . ToString ( ) } ) ) ;
378247
379248 }
0 commit comments