DeviceMgPlansApplicationService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.Helper;
  14. using ShwasherSys.Authorization.Permissions;
  15. using ShwasherSys.CompanyInfo.DeviceInfo.Dto;
  16. namespace ShwasherSys.CompanyInfo.DeviceInfo
  17. {
  18. [AbpAuthorize, AuditLog("设备维护计划")]
  19. public class DeviceMgPlanAppService : IwbZeroAsyncCrudAppService<DeviceMgPlan, DeviceMgPlanDto, int, IwbPagedRequestDto, DeviceMgPlanCreateDto, DeviceMgPlanUpdateDto >, IDeviceMgPlanAppService
  20. {
  21. public DeviceMgPlanAppService(
  22. ICacheManager cacheManager,
  23. IRepository<DeviceMgPlan, int> repository, IRepository<MaintenanceRecord, string> mrRepository, IRepository<Mold> moldRepository, IRepository<FixedAsset> faRepository) : base(repository)
  24. {
  25. MrRepository = mrRepository;
  26. MoldRepository = moldRepository;
  27. FaRepository = faRepository;
  28. CacheManager = cacheManager;
  29. FixedAssetList = null;
  30. MoldList = null;
  31. }
  32. private List<FixedAsset> FixedAssetList { get; set; }
  33. private List<Mold> MoldList { get; set; }
  34. protected IRepository<MaintenanceRecord,string> MrRepository { get; }
  35. protected IRepository<Mold> MoldRepository { get; }
  36. protected IRepository<FixedAsset> FaRepository { get; }
  37. protected override bool KeyIsAuto { get; set; } = false;
  38. #region GetSelect
  39. [DisableAuditing]
  40. public override async Task<List<SelectListItem>> GetSelectList()
  41. {
  42. var list = await Repository.GetAllListAsync();
  43. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  44. foreach (var l in list)
  45. {
  46. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  47. }
  48. return sList;
  49. }
  50. [DisableAuditing]
  51. public override async Task<string> GetSelectStr()
  52. {
  53. var list = await Repository.GetAllListAsync();
  54. string str = "<option value=\"\" selected>请选择...</option>";
  55. foreach (var l in list)
  56. {
  57. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  58. }
  59. return str;
  60. }
  61. #endregion
  62. #region CURD
  63. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlan)]
  64. public override async Task Create(DeviceMgPlanCreateDto input)
  65. {
  66. input.No = await MaintainTypeDefinition.GetDeviceMgPlanNo(Repository, input.PlanType);
  67. input.NextMaintenanceDate = input.MaintenanceDate.AddDays(input.MaintenanceCycle);
  68. await CreateEntity(input);
  69. }
  70. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanUpdate)]
  71. public override async Task Update(DeviceMgPlanUpdateDto input)
  72. {
  73. var entity = await GetEntity(input);
  74. if (entity == null)
  75. {
  76. CheckErrors("未查询到记录");
  77. return;
  78. }
  79. entity.NextMaintenanceDate = entity.MaintenanceDate.AddDays(input.MaintenanceCycle);
  80. MapToEntity(input,entity);
  81. await Repository.UpdateAsync(entity);
  82. await CurrentUnitOfWork.SaveChangesAsync();
  83. }
  84. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanDelete)]
  85. public override Task Delete(EntityDto<int> input)
  86. {
  87. return Repository.DeleteAsync(input.Id);
  88. }
  89. [DisableAuditing]
  90. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  91. public override async Task<PagedResultDto<DeviceMgPlanDto>> GetAll(IwbPagedRequestDto input)
  92. {
  93. var query = CreateFilteredQuery(input);
  94. query = ApplyFilter(query, input);
  95. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  96. query = ApplySorting(query, input).OrderByDescending(a=>a.NextMaintenanceDate);
  97. query = ApplyPaging(query, input);
  98. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  99. var dtoList = new PagedResultDto<DeviceMgPlanDto>(totalCount, entities.Select(MapDto).ToList());
  100. return dtoList;
  101. }
  102. private DeviceMgPlanDto MapDto(DeviceMgPlan input)
  103. {
  104. var dto = MapToEntityDto(input);
  105. if (dto.PlanType == MaintainTypeDefinition.Mold)
  106. {
  107. MoldList = MoldList ?? MoldRepository.GetAllList();
  108. dto.Name = MoldList?.FirstOrDefault(a => a.No == dto.DeviceNo)?.Name ?? input.Name;
  109. }else if (dto.PlanType == MaintainTypeDefinition.Device)
  110. {
  111. FixedAssetList = FixedAssetList ?? FaRepository.GetAllList();
  112. dto.Name = FixedAssetList?.FirstOrDefault(a => a.No == dto.DeviceNo)?.Name ?? input.Name;
  113. }
  114. return dto;
  115. }
  116. #region GetEntity/Dto
  117. /// <summary>
  118. /// 查询实体Dto
  119. /// </summary>
  120. /// <param name="input"></param>
  121. /// <returns></returns>
  122. [DisableAuditing]
  123. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  124. public override async Task<DeviceMgPlanDto> GetDto(EntityDto<int> input)
  125. {
  126. var entity = await GetEntity(input);
  127. return MapToEntityDto(entity);
  128. }
  129. /// <summary>
  130. /// 查询实体Dto
  131. /// </summary>
  132. /// <param name="id"></param>
  133. /// <returns></returns>
  134. [DisableAuditing]
  135. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  136. public override async Task<DeviceMgPlanDto> GetDtoById(int id)
  137. {
  138. var entity = await GetEntityById(id);
  139. return MapToEntityDto(entity);
  140. }
  141. /// <summary>
  142. /// 查询实体Dto(需指明自定义字段)
  143. /// </summary>
  144. /// <param name="no"></param>
  145. /// <returns></returns>
  146. [DisableAuditing]
  147. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  148. public override async Task<DeviceMgPlanDto> GetDtoByNo(string no)
  149. {
  150. var entity = await GetEntityByNo(no);
  151. return MapToEntityDto(entity);
  152. }
  153. /// <summary>
  154. /// 查询实体
  155. /// </summary>
  156. /// <param name="input"></param>
  157. /// <returns></returns>
  158. [DisableAuditing]
  159. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  160. public override async Task<DeviceMgPlan> GetEntity(EntityDto<int> input)
  161. {
  162. var entity = await GetEntityById(input.Id);
  163. return entity;
  164. }
  165. /// <summary>
  166. /// 查询实体
  167. /// </summary>
  168. /// <param name="id"></param>
  169. /// <returns></returns>
  170. [DisableAuditing]
  171. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  172. public override async Task<DeviceMgPlan> GetEntityById(int id)
  173. {
  174. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  175. }
  176. /// <summary>
  177. /// 查询实体(需指明自定义字段)
  178. /// </summary>
  179. /// <param name="no"></param>
  180. /// <returns></returns>
  181. [DisableAuditing]
  182. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanQuery)]
  183. public override async Task<DeviceMgPlan> GetEntityByNo(string no)
  184. {
  185. //CheckGetPermission();
  186. if (string.IsNullOrEmpty(KeyFiledName))
  187. {
  188. ThrowError("NoKeyFieldName");
  189. }
  190. return await base.GetEntityByNo(no);
  191. }
  192. #endregion
  193. #region Hide
  194. ///// <summary>
  195. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{Device}"/>过滤查询.
  196. ///// </summary>
  197. ///// <param name="input">The input.</param>
  198. //protected override IQueryable<Device> CreateFilteredQuery(IwbPagedRequestDto input)
  199. //{
  200. // var query = Repository.GetAll();
  201. // var pagedInput = input as IIwbPagedRequest;
  202. // if (pagedInput == null)
  203. // {
  204. // return query;
  205. // }
  206. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  207. // {
  208. // object keyWords = pagedInput.KeyWords;
  209. // LambdaObject obj = new LambdaObject()
  210. // {
  211. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  212. // FieldName = pagedInput.KeyField,
  213. // FieldValue = keyWords,
  214. // ExpType = (LambdaExpType)pagedInput.ExpType
  215. // };
  216. // var exp = obj.GetExp<Device>();
  217. // query = exp != null ? query.Where(exp) : query;
  218. // }
  219. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  220. // {
  221. // List<LambdaObject> objList = new List<LambdaObject>();
  222. // foreach (var o in pagedInput.SearchList)
  223. // {
  224. // if (string.IsNullOrEmpty(o.KeyWords))
  225. // continue;
  226. // object keyWords = o.KeyWords;
  227. // objList.Add(new LambdaObject
  228. // {
  229. // FieldType = (LambdaFieldType)o.FieldType,
  230. // FieldName = o.KeyField,
  231. // FieldValue = keyWords,
  232. // ExpType = (LambdaExpType)o.ExpType
  233. // });
  234. // }
  235. // var exp = objList.GetExp<Device>();
  236. // query = exp != null ? query.Where(exp) : query;
  237. // }
  238. // return query;
  239. //}
  240. //protected override IQueryable<Device> ApplySorting(IQueryable<Device> query, IwbPagedRequestDto input)
  241. //{
  242. // return query.OrderBy(a => a.No);
  243. //}
  244. //protected override IQueryable<Device> ApplyPaging(IQueryable<Device> query, IwbPagedRequestDto input)
  245. //{
  246. // if (input is IPagedResultRequest pagedInput)
  247. // {
  248. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  249. // }
  250. // return query;
  251. //}
  252. #endregion
  253. #endregion
  254. [AbpAuthorize(PermissionNames.PagesCompanyDieMaintenanceDeviceMgPlanMaintain)]
  255. public async Task Maintain(MaintainDto input)
  256. {
  257. var entity = await GetEntity(input);
  258. if (entity == null)
  259. {
  260. CheckErrors("未查询到记录");
  261. return;
  262. }
  263. await MrRepository.InsertAsync(new MaintenanceRecord()
  264. {
  265. Id =await MaintainTypeDefinition.GetMaintainRecordNo(MrRepository,entity.PlanType),
  266. DeviceMgPlanNo = entity.No,
  267. DeviceNo = entity.DeviceNo,
  268. DeviceName = entity.Name,
  269. MgType = entity.PlanType,
  270. CompleteState = MaintainStateDefinition.New,
  271. CompleteDate = null,
  272. Address = input.Address,
  273. Description = input.Description,
  274. PlanDate = input.PlanDate ??
  275. entity.NextMaintenanceDate ?? entity.ExpireDate.AddDays(entity.MaintenanceCycle),
  276. });
  277. }
  278. }
  279. }