DeviceMgPlansApplicationService.cs 14 KB

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