using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Mvc; 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 IwbZero.Helper; using ShwasherSys.Authorization.Permissions; using ShwasherSys.CompanyInfo.DeviceInfo.Dto; using ShwasherSys.ProductStoreInfo; using System.Linq.Dynamic.Core; using Abp.Extensions; using Abp.Web.Security.AntiForgery; namespace ShwasherSys.CompanyInfo.DeviceInfo { [AbpAuthorize, AuditLog("设备维护计划")] public class DeviceMgPlanAppService : IwbZeroAsyncCrudAppService, IDeviceMgPlanAppService { public DeviceMgPlanAppService( ICacheManager cacheManager, IRepository repository, IRepository mrRepository, IRepository moldRepository, IRepository faRepository) : base(repository) { MrRepository = mrRepository; MoldRepository = moldRepository; FaRepository = faRepository; CacheManager = cacheManager; FixedAssetList = null; MoldList = null; } private List FixedAssetList { get; set; } private List MoldList { get; set; } protected IRepository MrRepository { get; } protected IRepository MoldRepository { get; } protected IRepository FaRepository { get; } protected override bool KeyIsAuto { get; set; } = false; #region GetSelect [DisableAuditing] public override async Task> GetSelectList() { var list = await Repository.GetAllListAsync(); var sList = new List {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 GetSelectStr() { var list = await Repository.GetAllListAsync(); string str = ""; foreach (var l in list) { //str += $""; } return str; } #endregion #region CURD [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlan)] public override async Task Create(DeviceMgPlanCreateDto input) { var entity = await Repository.FirstOrDefaultAsync(i=>i.DeviceNo==input.DeviceNo && i.PlanType==input.PlanType); if (entity != null) { CheckErrors("设备计划已经存在!请留意修改计划有效期!!"); } input.No = await MaintainTypeDefinition.GetDeviceMgPlanNo(Repository, input.PlanType); input.NextMaintenanceDate = input.PlanType != 1 ? input.MaintenanceDate.AddDays(input.MaintenanceCycle) : input.MaintenanceDate; await CreateEntity(input); } [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanUpdate)] public override async Task Update(DeviceMgPlanUpdateDto input) { var entity = await GetEntity(input); if (entity == null) { CheckErrors("未查询到记录"); return; } entity.NextMaintenanceDate = input.PlanType != 1 ? entity.MaintenanceDate.AddDays(input.MaintenanceCycle): entity.MaintenanceDate; MapToEntity(input,entity); await Repository.UpdateAsync(entity); await CurrentUnitOfWork.SaveChangesAsync(); } [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanDelete)] public override Task Delete(EntityDto input) { return Repository.DeleteAsync(input.Id); } [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task> GetAll(IwbPagedRequestDto input) { var query =Repository.GetAll(); //query = ApplyFilter(query, input); string mpTypeVal = ""; if (input.SearchList != null && input.SearchList.Count > 0) { List objList = new List(); foreach (var o in input.SearchList) { if (o.KeyWords.IsNullOrEmpty()) continue; if (o.KeyField == "mpType") { mpTypeVal = o.KeyWords.ToString(); continue; } object keyWords = o.KeyWords; objList.Add(new LambdaObject { FieldType = (LambdaFieldType)o.FieldType, FieldName = o.KeyField, FieldValue = keyWords, ExpType = (LambdaExpType)o.ExpType }); } if (objList.Any()) { var exp = objList.GetExp(); query = query.Where(exp); } } //过滤出模具需要维护和即将要维护的记录 if (mpTypeVal == "1") { query = query.Where(i => i.PlanType == 1 && ( i.NumberOfUsers >= i.MaintenanceCycle || ( i.MaintenanceCycle > i.NumberOfUsers && ((i.MaintenanceCycle - i.NumberOfUsers) <= 10000)) )); } var totalCount = await AsyncQueryableExecuter.CountAsync(query); query = ApplySorting(query, input).OrderByDescending(a=>a.NextMaintenanceDate); query = ApplyPaging(query, input); var entities = await AsyncQueryableExecuter.ToListAsync(query); var dtoList = new PagedResultDto(totalCount, entities.Select(MapDto).ToList()); return dtoList; } private DeviceMgPlanDto MapDto(DeviceMgPlan input) { var dto = MapToEntityDto(input); if (dto.PlanType == MaintainTypeDefinition.Mold) { MoldList = MoldList ?? MoldRepository.GetAllList(); dto.Name = MoldList?.FirstOrDefault(a => a.No == dto.DeviceNo)?.Name ?? input.Name; }else if (dto.PlanType == MaintainTypeDefinition.Device) { FixedAssetList = FixedAssetList ?? FaRepository.GetAllList(); dto.Name = FixedAssetList?.FirstOrDefault(a => a.No == dto.DeviceNo)?.Name ?? input.Name; } return dto; } #region GetEntity/Dto /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetDto(EntityDto input) { var entity = await GetEntity(input); return MapToEntityDto(entity); } /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetDtoById(int id) { var entity = await GetEntityById(id); return MapToEntityDto(entity); } /// /// 查询实体Dto(需指明自定义字段) /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetDtoByNo(string no) { var entity = await GetEntityByNo(no); return MapToEntityDto(entity); } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetEntity(EntityDto input) { var entity = await GetEntityById(input.Id); return entity; } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetEntityById(int id) { return await Repository.FirstOrDefaultAsync(a=>a.Id==id); } /// /// 查询实体(需指明自定义字段) /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)] public override async Task GetEntityByNo(string no) { //CheckGetPermission(); if (string.IsNullOrEmpty(KeyFiledName)) { ThrowError("NoKeyFieldName"); } return await base.GetEntityByNo(no); } #endregion #region Hide ///// ///// 根据给定的创建 过滤查询. ///// ///// The input. //protected override IQueryable 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(); // query = exp != null ? query.Where(exp) : query; // } // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0) // { // List objList = new List(); // 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(); // query = exp != null ? query.Where(exp) : query; // } // return query; //} //protected override IQueryable ApplySorting(IQueryable query, IwbPagedRequestDto input) //{ // return query.OrderBy(a => a.No); //} //protected override IQueryable ApplyPaging(IQueryable query, IwbPagedRequestDto input) //{ // if (input is IPagedResultRequest pagedInput) // { // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount); // } // return query; //} #endregion #endregion [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanMaintain)] public async Task Maintain(MaintainDto input) { var entity = await GetEntity(input); if (entity == null) { CheckErrors("未查询到记录"); return; } await MrRepository.InsertAsync(new MaintenanceRecord() { Id =await MaintainTypeDefinition.GetMaintainRecordNo(MrRepository,entity.PlanType), DeviceMgPlanNo = entity.No, DeviceNo = entity.DeviceNo, DeviceName = entity.Name, MgType = entity.PlanType, CompleteState = MaintainStateDefinition.New, CompleteDate = null, Address = input.Address, Description = input.Description, PlanDate = input.PlanDate ?? entity.NextMaintenanceDate ?? entity.ExpireDate.AddDays(entity.MaintenanceCycle), }); } } }