新框架PC后端代码(祈禧6月初版本)
南骏 池
3 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.Text;
using Gs.Sys.Models;
using Gs.Toolbox;
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Mvc;
using Gs.Toolbox.ApiCore.Group;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using static Gs.Toolbox.UtilityHelper;
 
namespace Gs.Sys.Services;
 
[ApiGroup(ApiGroupNames.Sys)]
public class RoleController : Repository<SysRole>, IRomteService
{
    private readonly IHttpContextAccessor _http;
    private readonly string _userCode, _userGuid, _orgFids;
 
    public RoleController(IHttpContextAccessor httpContextAccessor)
    {
        _http = httpContextAccessor;
        (_userCode, _userGuid, _orgFids) =
            GetUserGuidAndOrgGuid(_http);
    }
 
    /// <summary>
    ///     查询列表,支持分页
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<PageList<SysRole>> GetListPage(PageQuery query)
    {
        var pageList = new PageList<SysRole>();
        try
        {
            var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere);
            var _sbBy =
                new StringBuilder(query.sortName + " " + query.sortOrder);
            var totalCount = 0;
            var itemsList = Db.Queryable<SysRole>("")
                .Where(_sbWhere.ToString())
                .OrderBy(_sbBy.ToString())
                .ToPageList(query.currentPage, query.everyPageSize,
                    ref totalCount);
 
            pageList = new PageList<SysRole>(itemsList, totalCount,
                query.everyPageSize);
            return ReturnDto<PageList<SysRole>>.QuickReturn(pageList,
                ReturnCode.Success, "读取成功");
        }
        catch (Exception ex)
        {
            return ReturnDto<PageList<SysRole>>.QuickReturn(pageList,
                ReturnCode.Default, ex.Message);
        }
    }
 
 
    /// <summary>
    ///     删除
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> DeleteModel([FromBody] JArray guidList)
    {
        var intArray = guidList.ToObject<string[]>();
        var guid = intArray[0];
        var rtnInt = (int)ReturnCode.Default;
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (var cmd = new SqlCommand("prc_sys_role_delete", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@outMsg", SqlDbType.NVarChar, 300),
                        new("@roleGuid", guid),
                        new("@edtUserGuid", _userGuid)
                    };
                    parameters[0].Direction = ParameterDirection.Output;
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    rtnInt = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    LogHelper.Debug(ToString(),
                        "DeleteModel error:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
 
            if (rtnInt > 0)
                return ReturnDto<int>.QuickReturn(default(int?),
                    ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!");
            return ReturnDto<int>.QuickReturn(default(int?),
                ReturnCode.Exception, "删除失败,请重试!");
        }
    }
 
    /// <summary>
    ///     增加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<string?> EditModel([FromBody] SysRole model)
    {
        var _bl = false;
        try
        {
            if (!CheckGuid(model.Guid))
            {
                model.Guid = Guid.NewGuid();
                model.CreateBy = _userCode;
                model.CreateTime = DateTime.Now;
                _bl = base.Insert(model);
            }
            else
            {
                model.LastupdateBy = _userCode;
                model.LastupdateTime = DateTime.Now;
                // _bl = base.Update(model);
                _bl = Db.Updateable(model).IgnoreColumns(true)
                    .ExecuteCommand() > 0
                    ? true
                    : false;
            }
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "EditModel error:" + ex.Message);
            return ReturnDto<string>.QuickReturn("", ReturnCode.Exception,
                ex.Message);
        }
 
        if (_bl)
            return ReturnDto<string>.QuickReturn(model.Guid.ToString(),
                ReturnCode.Success, "操作成功!");
        return ReturnDto<string>.QuickReturn("", ReturnCode.Exception,
            "增加失败,请重试!");
    }
 
    /// <summary>
    ///     读取
    /// </summary>
    /// <param name="guid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    [AllowAnonymous]
    public ReturnDto<SysRole> GetModel([FromBody] SysRole model)
    {
        var m = base.GetById(model.Guid);
        if (m != null)
            return ReturnDto<SysRole>.QuickReturn(m, ReturnCode.Success,
                "读取成功!");
        return ReturnDto<SysRole>.QuickReturn(m, ReturnCode.Default, "读取失败!");
    }
 
 
    /// <summary>
    ///     根据角色roleGuid读取权限
    /// </summary>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<PageList<dynamic>> GetListByRole([FromBody] dynamic model)
    {
        string roleGuid = model.roleGuid;
        System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
        sbSql.Append("select ma.guid,ma.up_Guid as upGuid ,ma.name,ma.is_Status as isStatus,ma.idx");
        sbSql.Append(",(select count(1) from  [dbo].[sys_Role_Menu_Action]  act where  act.role_Guid='" + roleGuid + "'  and act.MENU_ACTION_GUID=ma.guid) as isCK");
        sbSql.Append(" from sys_Menu_Action ma where ma.is_Status=1   order by ma.IDX asc");
        var dset = new DataSet();
        try
        {
            dset = DbHelperSQL.Query(sbSql.ToString());
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "GetListByRole error:" + ex.Message);
            return ReturnDto<PageList<dynamic>>.QuickReturn(default(PageList<dynamic>), ReturnCode.Exception, "读取失败");
        }
        var _pglist = new PageList<dynamic>
        {
            total = 0,
            everyPageSize = 0,
            pages = 0,
            list = new List<dynamic>()
        };
        if (dset != null && dset.Tables.Count > 0 &&
            dset.Tables[0].Rows.Count > 0) //有数据
        {
            var intTotal = 999;
            var pages = 1;
            _pglist.total = intTotal;
            _pglist.everyPageSize = 1;
            _pglist.pages = pages;
            var _dy = dset.Tables[0].TableToDynamicList();
            _pglist.list = _dy;
        }
        return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist,
            ReturnCode.Success, "读取成功");
    }
 
 
    /// <summary>
    ///     根据角色设置权限
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> SetRoleMenuAction([FromBody] dynamic model)
    {
        string roleGuid = model.roleGuid.ToString();
        string actionLst = model.actionLst.ToString();
        dynamic m = new ExpandoObject();
        m.outMsg = "";
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (var cmd =
                   new SqlCommand("[prc_sys_role_set_menuOrAction]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@outMsg", SqlDbType.NVarChar, 300),
                        new("@roleGuid", roleGuid),
                        new("@actionLst", actionLst),
                        new("@edtUserGuid", _userGuid)
                    };
                    parameters[0].Direction = ParameterDirection.Output;
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    cmd.ExecuteNonQuery();
                    m.outMsg = parameters[0].Value.ToString();
                }
                catch (Exception ex)
                {
                    LogHelper.Debug(ToString(),
                        "SetMenuActionByRole error:" + ex.Message);
                    m.outMsg = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
 
        return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "操作成功!");
    }
 
    /// <summary>
    ///     根据用户设置角色
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> SetUserRole([FromBody] dynamic model)
    {
        string userGuid = model.userGuid.ToString();
        string roleLst = model.roleLst.ToString();
        dynamic m = new ExpandoObject();
        m.outMsg = "";
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (var cmd = new SqlCommand("[prc_sys_user_set_role]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@outMsg", SqlDbType.NVarChar, 300),
                        new("@userGuid", userGuid),
                        new("@roleLst", roleLst),
                        new("@edtUserGuid", _userGuid)
                    };
                    parameters[0].Direction = ParameterDirection.Output;
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    cmd.ExecuteNonQuery();
                    m.outMsg = parameters[0].Value.ToString();
                }
                catch (Exception ex)
                {
                    LogHelper.Debug(ToString(),
                        "SetRoleByUser error:" + ex.Message);
                    m.outMsg = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
 
        return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "操作成功!");
    }
 
 
    /// <summary>
    ///     根据用户读取角色
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<List<dynamic>> GetUserRole([FromBody] dynamic model)
    {
        var lst = new List<dynamic>();
        string userGuid = model.userGuid;
        var sbSql = new StringBuilder();
        sbSql.Append(
            "select a.*,u.guid as isCk from sys_Role a left join sys_User_Role u on (a.guid=u.role_Guid and u.user_Guid='" +
            userGuid +
            "') where 1=1 and a.[is_Status]=1 order by a.ROLE_NAME asc");
        var dset = new DataSet();
        try
        {
            dset = DbHelperSQL.Query(sbSql.ToString());
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "GetUserRole error:" + ex.Message);
            return ReturnDto<List<dynamic>>.QuickReturn(lst,
                ReturnCode.Exception, "读取失败!");
        }
 
        if (dset != null && dset.Tables.Count > 0 &&
            dset.Tables[0].Rows.Count > 0) //有数据
            foreach (DataRow dr in dset.Tables[0].Rows)
                lst.Add(new
                {
                    guid = Guid.Parse(dr["guid"].ToString()),
                    roleName = dr["role_Name"].ToString(),
                    rolRemark = dr["role_Remark"].ToString(),
                    isCk = string.IsNullOrEmpty(dr["isCk"].ToString())
                        ? false
                        : true
                });
        return ReturnDto<List<dynamic>>.QuickReturn(lst, ReturnCode.Success,
            "读取成功!");
    }
 
    /// <summary>
    ///     根据用户读取权限
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<PageList<dynamic>> GetUserMenuAction(
        [FromBody] dynamic model)
    {
        string userGuid = model.userGuid;
        var sbSql = new StringBuilder();
 
        sbSql.Append(" ; with cet as (");
        sbSql.Append(" select ma.guid,ma.up_Guid,ma.name,ma.is_Status as isStatus,ma.idx");
        sbSql.Append(" , (select count(1) from[dbo].[sys_Role_Menu_Action]  act left join sys_User_Role u on act.role_Guid = u.role_Guid");
        sbSql.Append(" where u.user_Guid = '"+ userGuid + "' and act.menu_Action_Guid = ma.guid) as isCK");
        sbSql.Append(" from sys_Menu_Action ma where ma.[is_Status] = 1");
 
        sbSql.Append(" ),cet2 as (");
        sbSql.Append(" select ma.guid,ma.up_Guid,ma.name,ma.is_Status as isStatus,ma.idx");
        sbSql.Append(" , (select count(1) from[dbo].[sys_Role_Menu_Action]  act left join sys_User  u on act.role_Guid = u.GUID");
        sbSql.Append("    where u.GUID = '"+ userGuid + "' and act.menu_Action_Guid = ma.guid) as isCK");
        sbSql.Append(" from sys_Menu_Action ma where ma.[is_Status] = 1");
        sbSql.Append(" ),cet3 as (");
        sbSql.Append("  select * from cet2 where isCK > 0");
        sbSql.Append(" ),cet4 as (");
        sbSql.Append(" select * from cet");
        sbSql.Append(" union all");
        sbSql.Append("  select * from cet3");
        sbSql.Append("  )");
        sbSql.Append(" ,cet5 as (");
        sbSql.Append("      select distinct * from cet4");
        sbSql.Append("),cet6 as (");
        sbSql.Append("   SELECT");
        sbSql.Append("    *,");
        sbSql.Append("    ROW_NUMBER() OVER(PARTITION BY guid ORDER BY isck DESC) AS rn");
        sbSql.Append(" FROM");
        sbSql.Append("    cet5");
        sbSql.Append(")");
        sbSql.Append(" select * from cet6 where rn = 1");
        var dset = new DataSet();
        try
        {
            dset = DbHelperSQL.Query(sbSql.ToString());
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "GetUserMenuAction error:" + ex.Message);
            return ReturnDto<PageList<dynamic>>.QuickReturn(default(PageList<dynamic>), ReturnCode.Exception, "读取失败");
        }
        var _pglist = new PageList<dynamic>
        {
            total = 0,
            everyPageSize = 0,
            pages = 0,
            list = new List<dynamic>()
        };
        if (dset != null && dset.Tables.Count > 0 &&
            dset.Tables[0].Rows.Count > 0) //有数据
        {
            var intTotal = 999;
            var pages = 1;
            _pglist.total = intTotal;
            _pglist.everyPageSize = 1;
            _pglist.pages = pages;
            var _dy = dset.Tables[0].TableToDynamicList();
            _pglist.list = _dy;
        }
        return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist,
            ReturnCode.Success, "读取成功");
    }
 
 
    /// <summary>
    ///     根据用户,组织,窗体命名空间读工具条权限
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<List<dynamic>> GetListByUserOrgSpace(
        [FromBody] dynamic model)
    {
        var lst = new List<dynamic>();
        string userGuid = model.userGuid;
        string formNamespace = model.formNamespace;
        var dset = new DataSet();
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (var cmd = new SqlCommand("[prc_sys_toolbar_action]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@userGuid", userGuid),
                        new("@formNamespace", formNamespace)
                    };
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    using (var dt = new SqlDataAdapter(cmd))
                    {
                        dt.Fill(dset, "0");
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Debug(ToString(),
                        "GetListByUserOrgSpace error:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
 
        if (dset != null && dset.Tables.Count > 0 &&
            dset.Tables[0].Rows.Count > 0) //有数据
            foreach (DataRow dr in dset.Tables[0].Rows)
                lst.Add(new
                {
                    guid = Guid.Parse(dr["guid"].ToString()),
                    name = dr["name"].ToString()
                });
        return ReturnDto<List<dynamic>>.QuickReturn(lst, ReturnCode.Success,
            "读取成功!");
    }
}