EmergencyPlanApplicationService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Web.Mvc;
  7. using System.Web.WebPages;
  8. using Abp.Application.Services.Dto;
  9. using Abp.Auditing;
  10. using Abp.Authorization;
  11. using Abp.Domain.Repositories;
  12. using Abp.Runtime.Caching;
  13. using IwbZero.Auditing;
  14. using IwbZero.AppServiceBase;
  15. using WePlatform.Authorization;
  16. using WePlatform.Configuration;
  17. using WePlatform.WeLib.Knowledge.Plan.Dto;
  18. namespace WePlatform.WeLib.Knowledge.Plan
  19. {
  20. [AbpAuthorize, AuditLog("应急预案管理")]
  21. public class EmergencyPlanAppService : IwbAsyncCrudAppService<EmergencyPlanInfo, EmergencyPlanDto, string, IwbPagedRequestDto, EmergencyPlanCreateDto, EmergencyPlanUpdateDto >, IEmergencyPlanAppService
  22. {
  23. public EmergencyPlanAppService(
  24. ICacheManager cacheManager,
  25. IRepository<EmergencyPlanInfo, string> repository, IRepository<EmergencyPlanContentInfo> pcRepository) : base(repository, "Id")
  26. {
  27. PcRepository = pcRepository;
  28. CacheManager = cacheManager;
  29. }
  30. protected override bool KeyIsAuto { get; set; } = false;
  31. protected IRepository<EmergencyPlanContentInfo> PcRepository { get; }
  32. #region GetSelect
  33. [DisableAuditing]
  34. public override async Task<List<SelectListItem>> GetSelectList()
  35. {
  36. var list = await Repository.GetAllListAsync();
  37. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  38. foreach (var l in list)
  39. {
  40. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  41. }
  42. return sList;
  43. }
  44. [DisableAuditing]
  45. public override async Task<string> GetSelectStr()
  46. {
  47. var list = await Repository.GetAllListAsync();
  48. string str = "<option value=\"\" selected>请选择...</option>";
  49. foreach (var l in list)
  50. {
  51. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  52. }
  53. return str;
  54. }
  55. #endregion
  56. #region CURD
  57. /// <summary>
  58. /// 获取子预案
  59. /// </summary>
  60. /// <param name="input"></param>
  61. /// <returns></returns>
  62. [DisableAuditing]
  63. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  64. public async Task<List<JsTreeDto>> GetChildPlan(EntityDto<string> input)
  65. {
  66. if (input.Id.IsEmpty() || input.Id == "#" || input.Id == "0")
  67. {
  68. input.Id = null;
  69. }
  70. var list = new List<JsTreeDto>();
  71. var children = await Repository.GetAllListAsync(a => a.ParentNo == input.Id);
  72. if (children.Any())
  73. {
  74. foreach (var child in children)
  75. {
  76. list.Add(new JsTreeDto()
  77. {
  78. Id = child.Id,
  79. Text = child.Name,
  80. Children = (await Repository.CountAsync(a => a.ParentNo == child.Id)) > 0
  81. });
  82. }
  83. }
  84. return list;
  85. }
  86. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgCreate)]
  87. public override async Task Create(EmergencyPlanCreateDto input)
  88. {
  89. var parent = await GetEntityById(input.ParentNo);
  90. if (parent == null)
  91. {
  92. CheckErrors("未查询到父预案!");
  93. return;
  94. }
  95. input.Id = await AppGuidManager.GetNextRecordIdAsync(DataLibType.EmergencyPlan);
  96. input.Path = $"{parent.Path},{input.Id}";
  97. await CreateEntity(input);
  98. }
  99. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgUpdate)]
  100. public override async Task Update(EmergencyPlanUpdateDto input)
  101. {
  102. await UpdateEntity(input);
  103. }
  104. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgDelete)]
  105. public override async Task Delete(EntityDto<string> input)
  106. {
  107. var children = await Repository.CountAsync(a => a.ParentNo == input.Id);
  108. if (children > 0)
  109. {
  110. CheckErrors("还有子预案,不能删除!");
  111. }
  112. await DeleteEntity(input);
  113. }
  114. [DisableAuditing]
  115. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  116. public override async Task<PagedResultDto<EmergencyPlanDto>> GetAll(IwbPagedRequestDto input)
  117. {
  118. var query = CreateFilteredQuery(input);
  119. query = ApplyFilter(query, input);
  120. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  121. query = ApplySorting(query, input);
  122. query = ApplyPaging(query, input);
  123. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  124. var dtoList = new PagedResultDto<EmergencyPlanDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  125. return dtoList;
  126. }
  127. #region GetEntity/Dto
  128. /// <summary>
  129. /// 查询实体Dto
  130. /// </summary>
  131. /// <param name="input"></param>
  132. /// <returns></returns>
  133. [DisableAuditing]
  134. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  135. public override async Task<EmergencyPlanDto> GetDto(EntityDto<string> input)
  136. {
  137. var entity = await GetEntity(input);
  138. return MapToEntityDto(entity);
  139. }
  140. /// <summary>
  141. /// 查询实体Dto
  142. /// </summary>
  143. /// <param name="id"></param>
  144. /// <returns></returns>
  145. [DisableAuditing]
  146. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  147. public override async Task<EmergencyPlanDto> GetDtoById(string id)
  148. {
  149. var entity = await GetEntityById(id);
  150. return MapToEntityDto(entity);
  151. }
  152. /// <summary>
  153. /// 查询实体Dto(需指明自定义字段)
  154. /// </summary>
  155. /// <param name="no"></param>
  156. /// <returns></returns>
  157. [DisableAuditing]
  158. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  159. public override async Task<EmergencyPlanDto> GetDtoByNo(string no)
  160. {
  161. var entity = await GetEntityByNo(no);
  162. return MapToEntityDto(entity);
  163. }
  164. /// <summary>
  165. /// 查询实体
  166. /// </summary>
  167. /// <param name="input"></param>
  168. /// <returns></returns>
  169. [DisableAuditing]
  170. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  171. public override async Task<EmergencyPlanInfo> GetEntity(EntityDto<string> input)
  172. {
  173. var entity = await GetEntityById(input.Id);
  174. return entity;
  175. }
  176. /// <summary>
  177. /// 查询实体
  178. /// </summary>
  179. /// <param name="id"></param>
  180. /// <returns></returns>
  181. [DisableAuditing]
  182. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  183. public override async Task<EmergencyPlanInfo> GetEntityById(string id)
  184. {
  185. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  186. }
  187. /// <summary>
  188. /// 查询实体(需指明自定义字段)
  189. /// </summary>
  190. /// <param name="no"></param>
  191. /// <returns></returns>
  192. [DisableAuditing]
  193. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
  194. public override async Task<EmergencyPlanInfo> GetEntityByNo(string no)
  195. {
  196. //CheckGetPermission();
  197. if (string.IsNullOrEmpty(KeyFiledName))
  198. {
  199. ThrowError("NoKeyFieldName");
  200. }
  201. return await base.GetEntityByNo(no);
  202. }
  203. #endregion
  204. #region Hide
  205. ///// <summary>
  206. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{EmergencyPlanInfo}"/>过滤查询.
  207. ///// </summary>
  208. ///// <param name="input">The input.</param>
  209. //protected override IQueryable<EmergencyPlanInfo> CreateFilteredQuery(IwbPagedRequestDto input)
  210. //{
  211. // var query = Repository.GetAll();
  212. // var pagedInput = input as IIwbPagedRequest;
  213. // if (pagedInput == null)
  214. // {
  215. // return query;
  216. // }
  217. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  218. // {
  219. // object keyWords = pagedInput.KeyWords;
  220. // LambdaObject obj = new LambdaObject()
  221. // {
  222. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  223. // FieldName = pagedInput.KeyField,
  224. // FieldValue = keyWords,
  225. // ExpType = (LambdaExpType)pagedInput.ExpType
  226. // };
  227. // var exp = obj.GetExp<EmergencyPlanInfo>();
  228. // query = exp != null ? query.Where(exp) : query;
  229. // }
  230. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  231. // {
  232. // List<LambdaObject> objList = new List<LambdaObject>();
  233. // foreach (var o in pagedInput.SearchList)
  234. // {
  235. // if (string.IsNullOrEmpty(o.KeyWords))
  236. // continue;
  237. // object keyWords = o.KeyWords;
  238. // objList.Add(new LambdaObject
  239. // {
  240. // FieldType = (LambdaFieldType)o.FieldType,
  241. // FieldName = o.KeyField,
  242. // FieldValue = keyWords,
  243. // ExpType = (LambdaExpType)o.ExpType
  244. // });
  245. // }
  246. // var exp = objList.GetExp<EmergencyPlanInfo>();
  247. // query = exp != null ? query.Where(exp) : query;
  248. // }
  249. // return query;
  250. //}
  251. //protected override IQueryable<EmergencyPlanInfo> ApplySorting(IQueryable<EmergencyPlanInfo> query, IwbPagedRequestDto input)
  252. //{
  253. // return query.OrderBy(a => a.No);
  254. //}
  255. //protected override IQueryable<EmergencyPlanInfo> ApplyPaging(IQueryable<EmergencyPlanInfo> query, IwbPagedRequestDto input)
  256. //{
  257. // if (input is IPagedResultRequest pagedInput)
  258. // {
  259. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  260. // }
  261. // return query;
  262. //}
  263. #endregion
  264. #endregion
  265. /// <summary>
  266. /// 查下预案内容
  267. /// </summary>
  268. /// <param name="input"></param>
  269. /// <returns></returns>
  270. [DisableAuditing,AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgQuery)]
  271. public async Task<List<PlanContentDto>> GetPlanContent(QueryContentDto input)
  272. {
  273. var query = PcRepository.GetAllIncluding(a => a.PlanInfoInfo)
  274. .Where(a => string.IsNullOrEmpty(input.PlanNo) || a.PlanInfoInfo.Path.Contains(input.PlanNo)).Select(
  275. a => new PlanContentDto()
  276. {
  277. Id = a.Id,
  278. PlanNo = a.PlanNo,
  279. PlanName = a.PlanInfoInfo.Name,
  280. KeyWord = a.KeyWord,
  281. Version = a.Version,
  282. IsUse = a.IsUse,
  283. PlanContent = a.PlanContent
  284. });
  285. if (input.IsGlobal)
  286. {
  287. }
  288. else
  289. {
  290. if (input.KeyWords != null && input.KeyWords.Any())
  291. {
  292. foreach (var k in input.KeyWords)
  293. {
  294. var k1 = k.Trim();
  295. query = query.Where(a => (a.KeyWord + ",").Contains(k1 + ","));
  296. }
  297. }
  298. }
  299. query = query.OrderByDescending(a => a.IsUse).ThenBy(a => a.PlanNo).ThenByDescending(a => a.Id);
  300. int skip = input.Skip ?? 0, take = input.Take ?? IwbConsts.TakeCount;
  301. query = query.Skip(skip).Take(take);
  302. var list = await query.ToListAsync();
  303. return list;
  304. }
  305. /// <summary>
  306. /// 添加(更新)预案内容
  307. /// </summary>
  308. /// <param name="input"></param>
  309. /// <returns></returns>
  310. [AuditLog("添加(更新)预案内容")]
  311. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgCreate)]
  312. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgUpdate)]
  313. public async Task UpdatePlanContent(PlanContentDto input)
  314. {
  315. var curUse = await PcRepository.FirstOrDefaultAsync(a => a.IsUse&& a.PlanNo==input.PlanNo);
  316. if (curUse != null)
  317. {
  318. curUse.IsUse = false;
  319. await PcRepository.UpdateAsync(curUse);
  320. }
  321. var entity= new EmergencyPlanContentInfo()
  322. {
  323. PlanNo = input.PlanNo,
  324. KeyWord = input.KeyWord.Replace(",",","),
  325. PlanContent = input.PlanContent,
  326. Version = await GetVersion(input.PlanNo, input.BigVersion),
  327. IsUse = true
  328. };
  329. await PcRepository.InsertAndGetIdAsync(entity);
  330. }
  331. /// <summary>
  332. /// 删除内容
  333. /// </summary>
  334. /// <param name="input"></param>
  335. /// <returns></returns>
  336. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgDelete), AuditLog("删除内容")]
  337. public async Task DeletePlanContent(EntityDto input)
  338. {
  339. var content = await PcRepository.FirstOrDefaultAsync(a => a.Id == input.Id);
  340. if (content == null)
  341. {
  342. CheckErrors("未查询到预案内容信息!");
  343. return;
  344. }
  345. if (content.IsUse)
  346. {
  347. CheckErrors("当前版本正在使用不能删除!");
  348. }
  349. await PcRepository.DeleteAsync(input.Id);
  350. }
  351. /// <summary>
  352. /// 设为当前使用
  353. /// </summary>
  354. /// <param name="input"></param>
  355. /// <returns></returns>
  356. [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgDelete), AuditLog("设为当前使用")]
  357. public async Task SetUse(EntityDto input)
  358. {
  359. var entity = await PcRepository.FirstOrDefaultAsync(a => a.Id == input.Id);
  360. if (entity == null)
  361. {
  362. CheckErrors("未查询到预案内容信息!");
  363. return;
  364. }
  365. var curUse = await PcRepository.FirstOrDefaultAsync(a => a.IsUse && a.PlanNo == entity.PlanNo);
  366. if (curUse != null)
  367. {
  368. curUse.IsUse = false;
  369. await PcRepository.UpdateAsync(curUse);
  370. }
  371. entity.IsUse = true;
  372. await PcRepository.UpdateAsync(entity);
  373. }
  374. /// <summary>
  375. /// 获取新版本
  376. /// </summary>
  377. /// <param name="no"></param>
  378. /// <param name="big"></param>
  379. /// <returns></returns>
  380. private async Task<string> GetVersion(string no,bool? big)
  381. {
  382. var lastContent = await PcRepository.GetAll().Where(a => a.PlanNo == no)
  383. .OrderByDescending(a => a.Id).FirstOrDefaultAsync();
  384. decimal version = lastContent!=null? Convert.ToDecimal(lastContent.Version) : 1;
  385. if (big ?? false)
  386. {
  387. version = (Math.Floor(version) + 1);
  388. }
  389. else
  390. {
  391. version = version + (decimal)0.01;
  392. }
  393. var str = version.ToString("0.00");
  394. return str;
  395. }
  396. }
  397. }