de2bd2f9
“wangming”
项目初始化
|
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
|
using NCC.Common.Filter;
using NCC.Common.Util;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.System.Entitys.Permission;
using NCC.System.Entitys.System;
using NCC.VisualDev.Entitys;
using NCC.VisualDev.Entitys.Dto.Portal;
using NCC.VisualDev.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Mapster;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using NCC.Common.Core.Manager;
using NCC.FriendlyException;
using NCC.Common.Enum;
using System.Collections.Generic;
using NCC.Common.Extension;
using NCC.JsonSerialization;
using NCC.System.Interfaces.Common;
using Microsoft.AspNetCore.Http;
namespace NCC.VisualDev
{
/// <summary>
/// 业务实现:门户设计
/// </summary>
[ApiDescriptionSettings(Tag = "VisualDev", Name = "Portal", Order = 173)]
[Route("api/visualdev/[controller]")]
public class PortalService : IPortalService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<PortalEntity> _portalRepository;
private readonly ISqlSugarRepository<DictionaryDataEntity> _dictionaryDataRepository;// 数据字典表仓储
private readonly ISqlSugarRepository<AuthorizeEntity> _authorizeRepository; //权限操作表仓储
private readonly ISqlSugarRepository<RoleEntity> _roleRepository;
private readonly IUserManager _userManager;
private readonly IFileService _fileService;
/// <summary>
/// 初始化一个<see cref="PortalService"/>类型的新实例
/// </summary>
public PortalService(ISqlSugarRepository<PortalEntity> portalRepository, ISqlSugarRepository<RoleEntity> roleRepository, ISqlSugarRepository<DictionaryDataEntity> dictionaryDataRepository, IUserManager userManager, ISqlSugarRepository<AuthorizeEntity> authorizeRepository, IFileService fileService)
{
_portalRepository = portalRepository;
_dictionaryDataRepository = dictionaryDataRepository;
_userManager = userManager;
_authorizeRepository = authorizeRepository;
_roleRepository = roleRepository;
_fileService = fileService;
}
#region Get
/// <summary>
/// 列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns>返回列表</returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] KeywordInput input)
{
var portalList = await _portalRepository.Context.Queryable<PortalEntity, UserEntity, UserEntity>((a, b, c) => new JoinQueryInfos(JoinType.Left, b.Id == a.CreatorUserId, JoinType.Left, c.Id == a.LastModifyUserId))
.Select((a, b, c) => new PortalListOutput { id = a.Id, fullName = a.FullName, enCode = a.EnCode, deleteMark = SqlFunc.ToString(a.DeleteMark), description = a.Description, creatorTime = a.CreatorTime, creatorUser = SqlFunc.MergeString(b.RealName, "/", b.Account), parentId = a.Category, lastModifyUser = SqlFunc.MergeString(c.RealName, SqlFunc.IIF(c.RealName == null, "", "/"), c.Account), lastModifyTime = SqlFunc.ToDate(a.LastModifyTime), enabledMark = a.EnabledMark, sortCode = SqlFunc.ToString(a.SortCode) })
.MergeTable()
.WhereIF(!string.IsNullOrEmpty(input.keyword), p => p.fullName.Contains(input.keyword) || p.enCode.Contains(input.keyword))
.Where(p => p.deleteMark == null)
.OrderBy(p => p.sortCode).ToListAsync();
var parentIds = portalList.Select(x => x.parentId).ToList().Distinct();
var treeList = await _dictionaryDataRepository.Where(d => parentIds.Contains(d.Id) && d.DeleteMark == null && d.EnabledMark.Equals(1))
.Select(d => new { id = d.Id, parentId = "0", enCode = "", fullName = d.FullName, sortCode = d.SortCode })
.MergeTable().Select<PortalListOutput>().OrderBy(o => o.sortCode).ToListAsync();
return new { list = treeList.Union(portalList).ToList().ToTree("0") };
}
/// <summary>
/// 获取门户侧边框列表
/// </summary>
/// <returns></returns>
[HttpGet("Selector")]
public async Task<dynamic> GetSelector([FromQuery] string type)
{
var userInfo = await _userManager.GetUserInfo();
var data = new List<PortalSelectOutput>();
if ("1".Equals(type) && !userInfo.isAdministrator)
{
var roleId = await _roleRepository.Entities.In(r => r.Id, userInfo.roleIds).Where(r => r.EnabledMark.Equals(1) && r.DeleteMark == null).Select(r => r.Id).ToListAsync();
var items = await _authorizeRepository.Entities.In(a => a.ObjectId, roleId).Where(a => a.ItemType == "portal").GroupBy(it => new { it.ItemId }).Select(it => new { it.ItemId }).ToListAsync();
if (items.Count != 0)
data = await _portalRepository.Entities.In(p => p.Id, items.Select(it => it.ItemId).ToArray()).Select(s => new PortalSelectOutput { id = s.Id, fullName = s.FullName, parentId = s.Category, enabledMark = SqlFunc.ToInt32(s.EnabledMark), sortCode = SqlFunc.ToString(s.SortCode), deleteMark = SqlFunc.ToString(s.DeleteMark) })
.MergeTable()
.Where(p => p.enabledMark.Equals(1) && p.deleteMark == null).OrderBy(p => p.sortCode).ToListAsync();
}
else
{
data = await _portalRepository.Entities.Select(s => new PortalSelectOutput { id = s.Id, fullName = s.FullName, parentId = s.Category, enabledMark = SqlFunc.ToInt32(s.EnabledMark), sortCode = SqlFunc.ToString(s.SortCode), deleteMark = SqlFunc.ToString(s.DeleteMark) })
.MergeTable().Where(p => p.enabledMark.Equals(1) && p.deleteMark == null).OrderBy(o => o.sortCode).ToListAsync();
}
var parentIds = data.Select(it => it.parentId).Distinct().ToList();
var treeList = new List<PortalSelectOutput>();
if (parentIds.Count() != 0)
treeList = await _dictionaryDataRepository.Entities.In(it => it.Id, parentIds.ToArray()).Where(d => d.DeleteMark == null && d.EnabledMark.Equals(1)).Select(d => new PortalSelectOutput
{
id = d.Id,
parentId = "0",
fullName = d.FullName,
sortCode = SqlFunc.ToString(d.SortCode)
}).MergeTable().OrderBy(o => o.sortCode).ToListAsync();
return new { list = treeList.Union(data).ToList().ToTree("0") };
}
/// <summary>
/// 获取门户信息
/// </summary>
/// <param name="id">主键id</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
var data = await _portalRepository.SingleAsync(p => p.Id == id);
var output = data.Adapt<PortalInfoOutput>();
return output;
}
/// <summary>
/// 信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}/auth")]
public async Task<dynamic> GetInfoAuth(string id)
{
var userInfo = await _userManager.GetUserInfo();
if (userInfo.roleIds != null && !userInfo.isAdministrator)
{
var roleId = await _roleRepository.Entities.In(r => r.Id, userInfo.roleIds).Where(r => r.EnabledMark.Equals(1) && r.DeleteMark == null).Select(r => r.Id).ToListAsync();
var items = await _authorizeRepository.Entities.In(a => a.ObjectId, roleId).Where(a => a.ItemType == "portal").GroupBy(it => new { it.ItemId }).Select(it => new { it.ItemId }).ToListAsync();
if (items.Count == 0) return null;
var entity = await _portalRepository.Entities.In(p => p.Id, items.Select(it => it.ItemId).ToArray()).SingleAsync(p => p.Id == id && p.EnabledMark.Equals(1) && p.DeleteMark == null);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
return new { formData = entity.FormData };
}
if (userInfo.isAdministrator)
{
var entity = await _portalRepository.SingleAsync(p => p.Id == id && p.EnabledMark.Equals(1) && p.DeleteMark == null);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
return new { formData = entity.FormData };
}
throw NCCException.Oh(ErrorCode.D1900);
}
#endregion
#region Post
/// <summary>
/// 门户导出
/// </summary>
/// <param name="modelId"></param>
/// <returns></returns>
[HttpPost("{modelId}/Actions/ExportData")]
public async Task<dynamic> ActionsExportData(string modelId)
{
//模板实体
var templateEntity = await _portalRepository.SingleAsync(p => p.Id == modelId);
var jsonStr = templateEntity.Serialize();
return _fileService.Export(jsonStr, templateEntity.FullName);
}
/// <summary>
/// 门户导入
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost("Model/Actions/ImportData")]
public async Task ActionsImportData(IFormFile file)
{
var josn = _fileService.Import(file);
var templateEntity = josn.Deserialize<PortalEntity>();
if (templateEntity == null)
throw NCCException.Oh(ErrorCode.D3006);
if (templateEntity != null && templateEntity.FormData.IndexOf("layouyId") <= 0)
throw NCCException.Oh(ErrorCode.D3006);
if (!string.IsNullOrEmpty(templateEntity.Id) && await _portalRepository.AnyAsync(it => it.Id == templateEntity.Id && it.DeleteMark == null))
throw NCCException.Oh(ErrorCode.D1400);
if (await _portalRepository.AnyAsync(it => it.EnCode == templateEntity.EnCode && it.FullName == templateEntity.FullName && it.DeleteMark == null))
throw NCCException.Oh(ErrorCode.D1400);
await _portalRepository.Context.Insertable(templateEntity).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
}
/// <summary>
/// 新建功能信息
/// </summary>
/// <param name="input">实体对象</param>
/// <returns></returns>
[HttpPost("")]
public async Task Create([FromBody] PortalCrInput input)
{
var entity = input.Adapt<PortalEntity>();
await _portalRepository.Context.Insertable(entity).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
/// <summary>
/// 修改接口
/// </summary>
/// <param name="id">主键id</param>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] PortalUpInput input)
{
var entity = input.Adapt<PortalEntity>();
var isOk = await _portalRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
/// <summary>
/// 删除接口
/// </summary>
/// <param name="id">主键id</param>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _portalRepository.SingleAsync(p => p.Id == id && p.DeleteMark == null);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _portalRepository.Context.Updateable(entity).UpdateColumns(it => new { it.DeleteMark, it.DeleteUserId, it.DeleteTime }).CallEntityMethod(m => m.Delete()).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
/// <summary>
/// 复制
/// </summary>
/// <param name="id">主键值</param>
/// <returns></returns>
[HttpPost("{id}/Actions/Copy")]
public async Task ActionsCopy(string id)
{
var newEntity = new PortalEntity();
var entity = await _portalRepository.SingleAsync(p => p.Id == id && p.DeleteMark == null);
newEntity.FullName = entity.FullName + "_副本";
newEntity.EnCode = entity.EnCode + "_1";
newEntity.Category = entity.Category;
newEntity.FormData = entity.FormData;
newEntity.Description = entity.Description;
newEntity.EnabledMark = 0;
var isOk = await _portalRepository.Context.Insertable(newEntity).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
/// <summary>
/// 设置默认门户
/// </summary>
/// <returns></returns>
[HttpPut("{id}/Actions/SetDefault")]
public async Task SetDefault(string id)
{
var userEntity = _userManager.User;
_ = userEntity ?? throw NCCException.Oh(ErrorCode.D5002);
if (userEntity != null)
{
userEntity.PortalId = id;
var isOk = await _portalRepository.Context.Updateable<UserEntity>().SetColumns(it => new UserEntity()
{
PortalId = id,
LastModifyTime = SqlFunc.GetDate(),
LastModifyUserId = _userManager.UserId
}).Where(it => it.Id == userEntity.Id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.D5014);
}
}
#endregion
#region PublicMethod
/// <summary>
/// 获取默认
/// </summary>
/// <returns></returns>
[NonAction]
public async Task<string> GetDefault()
{
var user = _userManager.User;
if (!user.IsAdministrator.ToBool())
{
if (!string.IsNullOrEmpty(user.RoleId))
{
var roleIds = user.RoleId.Split(',');
var roleId = await _roleRepository.Entities.In(r => r.Id, roleIds).Where(r => r.EnabledMark.Equals(1) && r.DeleteMark == null).Select(r => r.Id).ToListAsync();
var items = await _authorizeRepository.Entities.In(a => a.ObjectId, roleId).Where(a => a.ItemType == "portal").GroupBy(it => new { it.ItemId }).Select(it => new { it.ItemId }).ToListAsync();
if (items.Count == 0) return null;
var portalList = await _portalRepository.Entities.In(p => p.Id, items.Select(it => it.ItemId).ToArray()).Where(p => p.EnabledMark.Equals(1) && p.DeleteMark == null).OrderBy(o => o.SortCode).Select(s => s.Id).ToListAsync();
return portalList.FirstOrDefault();
}
return null;
}
else
{
var portalList = await _portalRepository.Entities.Where(p => p.EnabledMark.Equals(1) && p.DeleteMark == null).OrderBy(o => o.SortCode).Select(s => s.Id).ToListAsync();
return portalList.FirstOrDefault();
}
}
#endregion
}
}
|