LicenseDocumentsApplicationService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Web.Mvc;
  6. using Abp.Application.Services.Dto;
  7. using Abp.Auditing;
  8. using Abp.Authorization;
  9. using Abp.Domain.Repositories;
  10. using Abp.Runtime.Caching;
  11. using IwbZero.Auditing;
  12. using IwbZero.AppServiceBase;
  13. using IwbZero.IdentityFramework;
  14. using IwbZero.Setting;
  15. using ShwasherSys.Authorization.Permissions;
  16. using ShwasherSys.BaseSysInfo;
  17. using ShwasherSys.BaseSysInfo.SysAttachFiles;
  18. using ShwasherSys.BaseSysInfo.SysAttachFiles.Dto;
  19. using ShwasherSys.Common;
  20. using ShwasherSys.CompanyInfo.LicenseInfo.Dto;
  21. namespace ShwasherSys.CompanyInfo.LicenseInfo
  22. {
  23. [AbpAuthorize, AuditLog("证照信息维护")]
  24. public class LicenseDocumentAppService : IwbZeroAsyncCrudAppService<LicenseDocument, LicenseDocumentDto, int, IwbPagedRequestDto, LicenseDocumentCreateDto, LicenseDocumentUpdateDto >, ILicenseDocumentAppService
  25. {
  26. public LicenseDocumentAppService(
  27. ICacheManager cacheManager,
  28. IRepository<LicenseDocument, int> repository, IRepository<SysAttachFile> attachRepository, IRepository<BusinessLog> logRepository) : base(repository)
  29. {
  30. AttachRepository = attachRepository;
  31. LogRepository = logRepository;
  32. CacheManager = cacheManager;
  33. }
  34. public IRepository<SysAttachFile> AttachRepository { get; }
  35. public IRepository<BusinessLog> LogRepository { get; }
  36. protected override bool KeyIsAuto { get; set; } = false;
  37. #region GetSelect
  38. [DisableAuditing]
  39. public override async Task<List<SelectListItem>> GetSelectList()
  40. {
  41. var list = await Repository.GetAllListAsync();
  42. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  43. foreach (var l in list)
  44. {
  45. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  46. }
  47. return sList;
  48. }
  49. [DisableAuditing]
  50. public override async Task<string> GetSelectStr()
  51. {
  52. var list = await Repository.GetAllListAsync();
  53. string str = "<option value=\"\" selected>请选择...</option>";
  54. foreach (var l in list)
  55. {
  56. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  57. }
  58. return str;
  59. }
  60. #endregion
  61. #region CURD
  62. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentCreate)]
  63. public override async Task Create(LicenseDocumentCreateDto input)
  64. {
  65. var entity =
  66. await Repository.FirstOrDefaultAsync(a => a.No == input.No && a.LicenseType == input.LicenseType);
  67. if (entity != null)
  68. {
  69. CheckErrors("证照编码不能重复,请检查后再试!");
  70. }
  71. var dto = await CreateEntity(input);
  72. if (input.AttachFiles != null && input.AttachFiles.Any())
  73. {
  74. foreach (var attach in input.AttachFiles)
  75. {
  76. attach.AttachNo = Guid.NewGuid().ToString("N");
  77. attach.TableName = "License";
  78. attach.ColumnName = "License";
  79. attach.SourceKey = dto.No;
  80. await CreateAttach(attach);
  81. }
  82. }
  83. }
  84. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentUpdate)]
  85. public override async Task Update(LicenseDocumentUpdateDto input)
  86. {
  87. var dto = await UpdateEntity(input);
  88. if (input.AttachFiles != null && input.AttachFiles.Any())
  89. {
  90. await AttachRepository.DeleteAsync( a => a.ColumnName == "License" && a.TableName == "License" && a.SourceKey == dto.No);
  91. foreach (var attach in input.AttachFiles)
  92. {
  93. attach.AttachNo = Guid.NewGuid().ToString("N");
  94. attach.TableName = "License";
  95. attach.ColumnName = "License";
  96. attach.SourceKey = dto.No;
  97. await CreateAttach(attach);
  98. }
  99. }
  100. }
  101. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentDelete)]
  102. public override Task Delete(EntityDto<int> input)
  103. {
  104. return Repository.DeleteAsync(input.Id);
  105. }
  106. [DisableAuditing]
  107. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  108. public override async Task<PagedResultDto<LicenseDocumentDto>> GetAll(IwbPagedRequestDto input)
  109. {
  110. var query = CreateFilteredQuery(input);
  111. query = ApplyFilter(query, input);
  112. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  113. query = ApplySorting(query, input);
  114. query = ApplyPaging(query, input);
  115. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  116. var dtoList = new PagedResultDto<LicenseDocumentDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  117. return dtoList;
  118. }
  119. #region GetEntity/Dto
  120. /// <summary>
  121. /// 查询实体Dto
  122. /// </summary>
  123. /// <param name="input"></param>
  124. /// <returns></returns>
  125. [DisableAuditing]
  126. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  127. public override async Task<LicenseDocumentDto> GetDto(EntityDto<int> input)
  128. {
  129. var entity = await GetEntity(input);
  130. return MapToEntityDto(entity);
  131. }
  132. /// <summary>
  133. /// 查询实体Dto
  134. /// </summary>
  135. /// <param name="id"></param>
  136. /// <returns></returns>
  137. [DisableAuditing]
  138. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  139. public override async Task<LicenseDocumentDto> GetDtoById(int id)
  140. {
  141. var entity = await GetEntityById(id);
  142. return MapToEntityDto(entity);
  143. }
  144. /// <summary>
  145. /// 查询实体Dto(需指明自定义字段)
  146. /// </summary>
  147. /// <param name="no"></param>
  148. /// <returns></returns>
  149. [DisableAuditing]
  150. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  151. public override async Task<LicenseDocumentDto> GetDtoByNo(string no)
  152. {
  153. var entity = await GetEntityByNo(no);
  154. return MapToEntityDto(entity);
  155. }
  156. /// <summary>
  157. /// 查询实体
  158. /// </summary>
  159. /// <param name="input"></param>
  160. /// <returns></returns>
  161. [DisableAuditing]
  162. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  163. public override async Task<LicenseDocument> GetEntity(EntityDto<int> input)
  164. {
  165. var entity = await GetEntityById(input.Id);
  166. return entity;
  167. }
  168. /// <summary>
  169. /// 查询实体
  170. /// </summary>
  171. /// <param name="id"></param>
  172. /// <returns></returns>
  173. [DisableAuditing]
  174. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  175. public override async Task<LicenseDocument> GetEntityById(int id)
  176. {
  177. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  178. }
  179. /// <summary>
  180. /// 查询实体(需指明自定义字段)
  181. /// </summary>
  182. /// <param name="no"></param>
  183. /// <returns></returns>
  184. [DisableAuditing]
  185. [AbpAuthorize(PermissionNames.PagesCompanyLicenseDocumentQuery)]
  186. public override async Task<LicenseDocument> GetEntityByNo(string no)
  187. {
  188. //CheckGetPermission();
  189. if (string.IsNullOrEmpty(KeyFiledName))
  190. {
  191. ThrowError("NoKeyFieldName");
  192. }
  193. return await base.GetEntityByNo(no);
  194. }
  195. #endregion
  196. #region Hide
  197. ///// <summary>
  198. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{LicenseDocument}"/>过滤查询.
  199. ///// </summary>
  200. ///// <param name="input">The input.</param>
  201. //protected override IQueryable<LicenseDocument> CreateFilteredQuery(IwbPagedRequestDto input)
  202. //{
  203. // var query = Repository.GetAll();
  204. // var pagedInput = input as IIwbPagedRequest;
  205. // if (pagedInput == null)
  206. // {
  207. // return query;
  208. // }
  209. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  210. // {
  211. // object keyWords = pagedInput.KeyWords;
  212. // LambdaObject obj = new LambdaObject()
  213. // {
  214. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  215. // FieldName = pagedInput.KeyField,
  216. // FieldValue = keyWords,
  217. // ExpType = (LambdaExpType)pagedInput.ExpType
  218. // };
  219. // var exp = obj.GetExp<LicenseDocument>();
  220. // query = exp != null ? query.Where(exp) : query;
  221. // }
  222. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  223. // {
  224. // List<LambdaObject> objList = new List<LambdaObject>();
  225. // foreach (var o in pagedInput.SearchList)
  226. // {
  227. // if (string.IsNullOrEmpty(o.KeyWords))
  228. // continue;
  229. // object keyWords = o.KeyWords;
  230. // objList.Add(new LambdaObject
  231. // {
  232. // FieldType = (LambdaFieldType)o.FieldType,
  233. // FieldName = o.KeyField,
  234. // FieldValue = keyWords,
  235. // ExpType = (LambdaExpType)o.ExpType
  236. // });
  237. // }
  238. // var exp = objList.GetExp<LicenseDocument>();
  239. // query = exp != null ? query.Where(exp) : query;
  240. // }
  241. // return query;
  242. //}
  243. //protected override IQueryable<LicenseDocument> ApplySorting(IQueryable<LicenseDocument> query, IwbPagedRequestDto input)
  244. //{
  245. // return query.OrderBy(a => a.No);
  246. //}
  247. //protected override IQueryable<LicenseDocument> ApplyPaging(IQueryable<LicenseDocument> query, IwbPagedRequestDto input)
  248. //{
  249. // if (input is IPagedResultRequest pagedInput)
  250. // {
  251. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  252. // }
  253. // return query;
  254. //}
  255. #endregion
  256. #endregion
  257. private async Task CreateAttach(SysAttachFileCreateDto input)
  258. {
  259. if (await IsValidFileType(input.FileExt))
  260. {
  261. string filePath =
  262. $"{SettingManager.GetSettingValue(SettingNames.DownloadPath)}/{input.TableName}/{input.ColumnName}";
  263. var lcRetVal = SysAttachFileAppService.Base64ToFile(input.FileInfo,
  264. $"{input.FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}", input.FileExt,
  265. filePath);
  266. if (lcRetVal.StartsWith("error@"))
  267. {
  268. CheckErrors(
  269. IwbIdentityResult.Failed(lcRetVal.Split(new[] {'@'},
  270. StringSplitOptions.RemoveEmptyEntries)[1]));
  271. return;
  272. }
  273. input.FilePath = lcRetVal;
  274. var entity = ObjectMapper.Map<SysAttachFile>(input);
  275. entity = await AttachRepository.InsertAsync(entity);
  276. BusinessLogTypeEnum.License.WriteLog(LogRepository, "证照上传",
  277. $"添加附件:[{entity.AttachNo}]-{entity.FileTitle}-{entity.FileName}.{entity.FileExt}",
  278. entity.SourceKey, entity.AttachNo);
  279. return;
  280. }
  281. CheckErrors(IwbIdentityResult.Failed("文件类型不合法,请上传合法文件。"));
  282. }
  283. private async Task<bool> IsValidFileType(string fileName ,bool isName=false)
  284. {
  285. string ext = isName ? GetFileExt(fileName) : fileName;
  286. string lcExts = await SettingManager.GetSettingValueAsync(SettingNames.UploadFileExt);
  287. string[] loList = lcExts.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  288. foreach (var lcExt in loList)
  289. {
  290. if (ext.ToLower() == lcExt.ToLower())
  291. return true;
  292. }
  293. this.LogError("上传的文件非法:" + fileName);
  294. return false;
  295. }
  296. private string GetFileExt(string fileName)
  297. {
  298. string fileExt = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1, fileName.Length - fileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
  299. return fileExt.ToLower();
  300. }
  301. }
  302. }