| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Web.Mvc;
- using System.Web.WebPages;
- using Abp.Application.Services.Dto;
- using Abp.Auditing;
- using Abp.Authorization;
- using Abp.Domain.Repositories;
- using Abp.Runtime.Caching;
- using IwbZero.Auditing;
- using IwbZero.AppServiceBase;
- using WePlatform.Authorization;
- using WePlatform.Configuration;
- using WePlatform.WeLib.Knowledge.Plan.Dto;
- namespace WePlatform.WeLib.Knowledge.Plan
- {
- [AbpAuthorize, AuditLog("应急预案管理")]
- public class EmergencyPlanAppService : IwbAsyncCrudAppService<EmergencyPlanInfo, EmergencyPlanDto, string, IwbPagedRequestDto, EmergencyPlanCreateDto, EmergencyPlanUpdateDto >, IEmergencyPlanAppService
- {
- public EmergencyPlanAppService(
- ICacheManager cacheManager,
- IRepository<EmergencyPlanInfo, string> repository, IRepository<EmergencyPlanContentInfo> pcRepository) : base(repository, "Id")
- {
- PcRepository = pcRepository;
- CacheManager = cacheManager;
- }
- protected override bool KeyIsAuto { get; set; } = false;
- protected IRepository<EmergencyPlanContentInfo> PcRepository { get; }
- #region GetSelect
- [DisableAuditing]
- public override async Task<List<SelectListItem>> GetSelectList()
- {
- var list = await Repository.GetAllListAsync();
- var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
- foreach (var l in list)
- {
- //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
- }
- return sList;
- }
- [DisableAuditing]
- public override async Task<string> GetSelectStr()
- {
- var list = await Repository.GetAllListAsync();
- string str = "<option value=\"\" selected>请选择...</option>";
- foreach (var l in list)
- {
- //str += $"<option value=\"{l.Id}\">{l.}</option>";
- }
- return str;
- }
- #endregion
- #region CURD
- /// <summary>
- /// 获取子预案
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public async Task<List<JsTreeDto>> GetChildPlan(EntityDto<string> input)
- {
- if (input.Id.IsEmpty() || input.Id == "#" || input.Id == "0")
- {
- input.Id = null;
- }
- var list = new List<JsTreeDto>();
- var children = await Repository.GetAllListAsync(a => a.ParentNo == input.Id);
- if (children.Any())
- {
- foreach (var child in children)
- {
- list.Add(new JsTreeDto()
- {
- Id = child.Id,
- Text = child.Name,
- Children = (await Repository.CountAsync(a => a.ParentNo == child.Id)) > 0
- });
- }
- }
- return list;
- }
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgCreate)]
- public override async Task Create(EmergencyPlanCreateDto input)
- {
- var parent = await GetEntityById(input.ParentNo);
- if (parent == null)
- {
- CheckErrors("未查询到父预案!");
- return;
- }
- input.Id = await AppGuidManager.GetNextRecordIdAsync(DataLibType.EmergencyPlan);
- input.Path = $"{parent.Path},{input.Id}";
- await CreateEntity(input);
- }
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgUpdate)]
- public override async Task Update(EmergencyPlanUpdateDto input)
- {
- await UpdateEntity(input);
- }
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgDelete)]
- public override async Task Delete(EntityDto<string> input)
- {
- var children = await Repository.CountAsync(a => a.ParentNo == input.Id);
- if (children > 0)
- {
- CheckErrors("还有子预案,不能删除!");
- }
- await DeleteEntity(input);
- }
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<PagedResultDto<EmergencyPlanDto>> GetAll(IwbPagedRequestDto input)
- {
- var query = CreateFilteredQuery(input);
- query = ApplyFilter(query, input);
- var totalCount = await AsyncQueryableExecuter.CountAsync(query);
- query = ApplySorting(query, input);
- query = ApplyPaging(query, input);
- var entities = await AsyncQueryableExecuter.ToListAsync(query);
- var dtoList = new PagedResultDto<EmergencyPlanDto>(totalCount, entities.Select(MapToEntityDto).ToList());
- return dtoList;
- }
- #region GetEntity/Dto
- /// <summary>
- /// 查询实体Dto
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanDto> GetDto(EntityDto<string> input)
- {
- var entity = await GetEntity(input);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体Dto
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanDto> GetDtoById(string id)
- {
- var entity = await GetEntityById(id);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体Dto(需指明自定义字段)
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanDto> GetDtoByNo(string no)
- {
- var entity = await GetEntityByNo(no);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanInfo> GetEntity(EntityDto<string> input)
- {
- var entity = await GetEntityById(input.Id);
- return entity;
- }
- /// <summary>
- /// 查询实体
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanInfo> GetEntityById(string id)
- {
- return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
- }
- /// <summary>
- /// 查询实体(需指明自定义字段)
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanMgQuery)]
- public override async Task<EmergencyPlanInfo> GetEntityByNo(string no)
- {
- //CheckGetPermission();
- if (string.IsNullOrEmpty(KeyFiledName))
- {
- ThrowError("NoKeyFieldName");
- }
- return await base.GetEntityByNo(no);
- }
- #endregion
- #region Hide
-
- ///// <summary>
- ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{EmergencyPlanInfo}"/>过滤查询.
- ///// </summary>
- ///// <param name="input">The input.</param>
- //protected override IQueryable<EmergencyPlanInfo> CreateFilteredQuery(IwbPagedRequestDto input)
- //{
- // var query = Repository.GetAll();
- // var pagedInput = input as IIwbPagedRequest;
- // if (pagedInput == null)
- // {
- // return query;
- // }
- // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
- // {
- // object keyWords = pagedInput.KeyWords;
- // LambdaObject obj = new LambdaObject()
- // {
- // FieldType = (LambdaFieldType)pagedInput.FieldType,
- // FieldName = pagedInput.KeyField,
- // FieldValue = keyWords,
- // ExpType = (LambdaExpType)pagedInput.ExpType
- // };
- // var exp = obj.GetExp<EmergencyPlanInfo>();
- // query = exp != null ? query.Where(exp) : query;
- // }
- // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
- // {
- // List<LambdaObject> objList = new List<LambdaObject>();
- // foreach (var o in pagedInput.SearchList)
- // {
- // if (string.IsNullOrEmpty(o.KeyWords))
- // continue;
- // object keyWords = o.KeyWords;
- // objList.Add(new LambdaObject
- // {
- // FieldType = (LambdaFieldType)o.FieldType,
- // FieldName = o.KeyField,
- // FieldValue = keyWords,
- // ExpType = (LambdaExpType)o.ExpType
- // });
- // }
- // var exp = objList.GetExp<EmergencyPlanInfo>();
- // query = exp != null ? query.Where(exp) : query;
- // }
- // return query;
- //}
- //protected override IQueryable<EmergencyPlanInfo> ApplySorting(IQueryable<EmergencyPlanInfo> query, IwbPagedRequestDto input)
- //{
- // return query.OrderBy(a => a.No);
- //}
- //protected override IQueryable<EmergencyPlanInfo> ApplyPaging(IQueryable<EmergencyPlanInfo> query, IwbPagedRequestDto input)
- //{
- // if (input is IPagedResultRequest pagedInput)
- // {
- // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
- // }
- // return query;
- //}
- #endregion
- #endregion
- /// <summary>
- /// 查下预案内容
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing,AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgQuery)]
- public async Task<List<PlanContentDto>> GetPlanContent(QueryContentDto input)
- {
- var query = PcRepository.GetAllIncluding(a => a.PlanInfoInfo)
- .Where(a => string.IsNullOrEmpty(input.PlanNo) || a.PlanInfoInfo.Path.Contains(input.PlanNo)).Select(
- a => new PlanContentDto()
- {
- Id = a.Id,
- PlanNo = a.PlanNo,
- PlanName = a.PlanInfoInfo.Name,
- KeyWord = a.KeyWord,
- Version = a.Version,
- IsUse = a.IsUse,
- PlanContent = a.PlanContent
- });
- if (input.IsGlobal)
- {
- }
- else
- {
- if (input.KeyWords != null && input.KeyWords.Any())
- {
- foreach (var k in input.KeyWords)
- {
- var k1 = k.Trim();
- query = query.Where(a => (a.KeyWord + ",").Contains(k1 + ","));
- }
- }
- }
- query = query.OrderByDescending(a => a.IsUse).ThenBy(a => a.PlanNo).ThenByDescending(a => a.Id);
- int skip = input.Skip ?? 0, take = input.Take ?? IwbConsts.TakeCount;
- query = query.Skip(skip).Take(take);
- var list = await query.ToListAsync();
- return list;
- }
- /// <summary>
- /// 添加(更新)预案内容
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [AuditLog("添加(更新)预案内容")]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgCreate)]
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgUpdate)]
- public async Task UpdatePlanContent(PlanContentDto input)
- {
- var curUse = await PcRepository.FirstOrDefaultAsync(a => a.IsUse&& a.PlanNo==input.PlanNo);
- if (curUse != null)
- {
- curUse.IsUse = false;
- await PcRepository.UpdateAsync(curUse);
- }
- var entity= new EmergencyPlanContentInfo()
- {
- PlanNo = input.PlanNo,
- KeyWord = input.KeyWord.Replace(",",","),
- PlanContent = input.PlanContent,
- Version = await GetVersion(input.PlanNo, input.BigVersion),
- IsUse = true
- };
- await PcRepository.InsertAndGetIdAsync(entity);
- }
- /// <summary>
- /// 删除内容
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgDelete), AuditLog("删除内容")]
- public async Task DeletePlanContent(EntityDto input)
- {
- var content = await PcRepository.FirstOrDefaultAsync(a => a.Id == input.Id);
- if (content == null)
- {
- CheckErrors("未查询到预案内容信息!");
- return;
- }
- if (content.IsUse)
- {
- CheckErrors("当前版本正在使用不能删除!");
- }
- await PcRepository.DeleteAsync(input.Id);
- }
- /// <summary>
- /// 设为当前使用
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [AbpAuthorize(PermissionNames.PagesResourceMgGuideMgPlanContentMgDelete), AuditLog("设为当前使用")]
- public async Task SetUse(EntityDto input)
- {
- var entity = await PcRepository.FirstOrDefaultAsync(a => a.Id == input.Id);
- if (entity == null)
- {
- CheckErrors("未查询到预案内容信息!");
- return;
- }
- var curUse = await PcRepository.FirstOrDefaultAsync(a => a.IsUse && a.PlanNo == entity.PlanNo);
- if (curUse != null)
- {
- curUse.IsUse = false;
- await PcRepository.UpdateAsync(curUse);
- }
- entity.IsUse = true;
- await PcRepository.UpdateAsync(entity);
- }
- /// <summary>
- /// 获取新版本
- /// </summary>
- /// <param name="no"></param>
- /// <param name="big"></param>
- /// <returns></returns>
- private async Task<string> GetVersion(string no,bool? big)
- {
- var lastContent = await PcRepository.GetAll().Where(a => a.PlanNo == no)
- .OrderByDescending(a => a.Id).FirstOrDefaultAsync();
- decimal version = lastContent!=null? Convert.ToDecimal(lastContent.Version) : 1;
- if (big ?? false)
- {
- version = (Math.Floor(version) + 1);
- }
- else
- {
- version = version + (decimal)0.01;
- }
- var str = version.ToString("0.00");
- return str;
- }
- }
- }
|