11namespace APIJSON . NET
22{
3- using APIJSON . NET . Models ;
43 using APIJSON . NET . Services ;
54 using Microsoft . Extensions . Options ;
6- using Newtonsoft . Json ;
75 using Newtonsoft . Json . Linq ;
86 using SqlSugar ;
97 using System ;
108 using System . Collections . Generic ;
11- using System . IO ;
129 using System . Linq ;
13-
1410 public class SelectTable : DbContext
1511 {
16-
1712 private readonly IIdentityService _identitySvc ;
18- public SelectTable ( IOptions < DbOptions > options , IIdentityService identityService ) : base ( options )
13+ private readonly ITableMapper _tableMapper ;
14+ public SelectTable ( IOptions < DbOptions > options , IIdentityService identityService , ITableMapper tableMapper ) : base ( options )
1915 {
20-
2116 _identitySvc = identityService ;
17+ _tableMapper = tableMapper ;
2218 }
23- /// <summary>
24- /// 对应数据表
25- /// </summary>
26- static Dictionary < string , string > dict = new Dictionary < string , string >
27- {
28- { "user" , "apijson_user" } ,
29- } ;
30-
31- public ( bool , string ) GetSelectRole ( string table )
32- {
33- var role = _identitySvc . GetRole ( ) ;
34- if ( role == null || role . Select == null || role . Select . Table == null )
35- {
36- return ( false , $ "select.json权限配置不正确!") ;
37- }
38- string tablerole = role . Select . Table . FirstOrDefault ( it => it . Equals ( table , StringComparison . CurrentCultureIgnoreCase ) ) ;
39-
40- if ( string . IsNullOrEmpty ( tablerole ) )
41- {
42- return ( false , $ "表名{ table } 没权限查询!") ;
43- }
44- int index = Array . IndexOf ( role . Select . Table , tablerole ) ;
45- string selectrole = role . Select . Column [ index ] ;
46- return ( true , selectrole ) ;
47- }
48- public dynamic GetTableData ( string subtable , int page , int count , string json , JObject dd )
49- {
19+ public ( dynamic , int ) GetTableData ( string subtable , int page , int count , string json , JObject dd )
20+ {
5021 if ( ! subtable . IsTable ( ) )
5122 {
5223 throw new Exception ( $ "表名{ subtable } 不正确!") ;
5324 }
54- var role = GetSelectRole ( subtable ) ;
55- if ( ! role . Item1 )
25+ var role = _identitySvc . GetSelectRole ( subtable ) ;
26+ if ( ! role . Item1 ) //没有权限返回异常
5627 {
5728 throw new Exception ( role . Item2 ) ;
5829 }
5930 string selectrole = role . Item2 ;
60- if ( dict . ContainsKey ( subtable . ToLower ( ) ) )
61- {
62- subtable = dict . GetValueOrDefault ( subtable . ToLower ( ) ) ;
63- }
31+ subtable = _tableMapper . GetTableName ( subtable ) ;
6432 JObject values = JObject . Parse ( json ) ;
6533 var tb = Db . Queryable ( subtable , "tb" ) ;
6634 if ( values [ "@column" ] . IsValue ( ) )
@@ -71,20 +39,24 @@ public dynamic GetTableData(string subtable, int page, int count, string json, J
7139 string [ ] ziduan = item . Split ( ":" ) ;
7240 if ( ziduan . Length > 1 )
7341 {
74- if ( ziduan [ 0 ] . IsField ( ) && ziduan [ 1 ] . IsTable ( ) && ( selectrole == "*" || selectrole . Split ( ',' ) . Contains ( ziduan [ 0 ] , StringComparer . CurrentCultureIgnoreCase ) ) )
42+ if ( _identitySvc . ColIsRole ( ziduan [ 0 ] , selectrole . Split ( "," ) ) )
7543 {
7644
7745 str . Append ( ziduan [ 0 ] + " as " + ziduan [ 1 ] + "," ) ;
7846 }
7947 }
8048 else
8149 {
82- if ( item . IsField ( ) && ( selectrole == "*" || selectrole . Split ( ',' ) . Contains ( item , StringComparer . CurrentCultureIgnoreCase ) ) )
50+ if ( _identitySvc . ColIsRole ( item , selectrole . Split ( "," ) ) )
8351 {
8452 str . Append ( item + "," ) ;
8553 }
8654 }
8755 }
56+ if ( string . IsNullOrEmpty ( str . ToString ( ) ) )
57+ {
58+ throw new Exception ( $ "表名{ subtable } 没有可查询的字段!") ;
59+ }
8860 tb . Select ( str . ToString ( ) . TrimEnd ( ',' ) ) ;
8961 }
9062 else
@@ -103,12 +75,12 @@ public dynamic GetTableData(string subtable, int page, int count, string json, J
10375 {
10476 if ( vakey . TrimEnd ( '$' ) . IsTable ( ) )
10577 {
106- conModels . Add ( new ConditionalModel ( ) { FieldName = va . Key . TrimEnd ( '$' ) , ConditionalType = ConditionalType . Like , FieldValue = va . Value . ToString ( ) } ) ;
78+ conModels . Add ( new ConditionalModel ( ) { FieldName = vakey . TrimEnd ( '$' ) , ConditionalType = ConditionalType . Like , FieldValue = va . Value . ToString ( ) } ) ;
10779 }
10880 }
10981 else if ( vakey . EndsWith ( "{}" ) ) //逻辑运算
11082 {
111- string field = va . Key . TrimEnd ( "{}" . ToCharArray ( ) ) ;
83+ string field = vakey . TrimEnd ( "{}" . ToCharArray ( ) ) ;
11284 if ( va . Value . HasValues )
11385 {
11486 conModels . Add ( new ConditionalModel ( ) { FieldName = field , ConditionalType = field . EndsWith ( "!" ) ? ConditionalType . NotIn : ConditionalType . In , FieldValue = va . Value . ToString ( ) } ) ;
@@ -189,7 +161,10 @@ public dynamic GetTableData(string subtable, int page, int count, string json, J
189161 }
190162 else
191163 {
192- tb . OrderBy ( "id" ) ;
164+ if ( count > 0 )
165+ {
166+ tb . OrderBy ( "id" ) ;
167+ }
193168 }
194169 if ( values [ "@group" ] . IsValue ( ) )
195170 {
@@ -209,11 +184,12 @@ public dynamic GetTableData(string subtable, int page, int count, string json, J
209184 }
210185 if ( count > 0 )
211186 {
212- return tb . ToPageList ( page , count ) ;
187+ int total = 0 ;
188+ return ( tb . ToPageList ( page , count , ref total ) , total ) ;
213189 }
214190 else
215191 {
216- return tb . ToList ( ) ;
192+ return ( tb . ToList ( ) , tb . Count ( ) ) ;
217193 }
218194
219195 }
0 commit comments