DeviceMgPlansApplicationService.cs 14 KB

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