CustomerDefaultProductsApplicationService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Dynamic.Core;
  4. using System.Threading.Tasks;
  5. using Abp.Application.Services.Dto;
  6. using Abp.Authorization;
  7. using Abp.Domain.Repositories;
  8. using Abp.Extensions;
  9. using Abp.Timing;
  10. using Abp.UI;
  11. using IwbZero.AppServiceBase;
  12. using ShwasherSys.Authorization.Permissions;
  13. using ShwasherSys.CustomerInfo.Dto;
  14. using ShwasherSys.EntityFramework.Repositories;
  15. using ShwasherSys.Lambda;
  16. using ShwasherSys.Order;
  17. using ShwasherSys.ProductInfo;
  18. using ShwasherSys.ProductInfo.Dto;
  19. namespace ShwasherSys.CustomerInfo
  20. {
  21. [AbpAuthorize]
  22. public class CustomerDefaultProductAppService :ShwasherAsyncCrudAppService<CustomerDefaultProduct, CustomerDefaultProductDto, int, PagedRequestDto, CustomerDefaultProductCreateDto, CustomerDefaultProductUpdateDto >, ICustomerDefaultProductAppService
  23. {
  24. protected IRepository<Product,string> ProductRepository;
  25. protected IRepository<OrderItem> OrderItemRepository;
  26. protected IRepository<OrderHeader,string> OrderHeaderRepository;
  27. public CustomerDefaultProductAppService(IRepository<CustomerDefaultProduct, int> repository, IRepository<Product, string> productRepository, IRepository<OrderItem> orderItemRepository, IRepository<OrderHeader, string> orderHeaderRepository) : base(repository)
  28. {
  29. ProductRepository = productRepository;
  30. OrderItemRepository = orderItemRepository;
  31. OrderHeaderRepository = orderHeaderRepository;
  32. }
  33. protected override string GetPermissionName { get; set; } = PermissionNames.PagesCustomerInfoCustomers;
  34. protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesCustomerInfoCustomers;
  35. protected override string CreatePermissionName { get; set; } = PermissionNames.PagesCustomerInfoCustomersCreateDefaultProduct;
  36. protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesCustomerInfoCustomersUpdateDefaultProduct;
  37. protected override string DeletePermissionName { get; set; } = PermissionNames.PagesCustomerInfoCustomersDeleteDefaultProduct;
  38. public override async Task<PagedResultDto<CustomerDefaultProductDto>> GetAll(PagedRequestDto input)
  39. {
  40. CheckGetAllPermission();
  41. var query = CreateFilteredQuery(input);
  42. if (input.SearchList != null && input.SearchList.Count > 0)
  43. {
  44. List<LambdaObject> objList = new List<LambdaObject>();
  45. foreach (var o in input.SearchList)
  46. {
  47. if (o.KeyWords.IsNullOrEmpty())
  48. continue;
  49. object keyWords = o.KeyWords;
  50. objList.Add(new LambdaObject
  51. {
  52. FieldType = (LambdaFieldType)o.FieldType,
  53. FieldName = o.KeyField,
  54. FieldValue = keyWords,
  55. ExpType = (LambdaExpType)o.ExpType
  56. });
  57. }
  58. var exp = objList.GetExp<CustomerDefaultProduct>();
  59. query = query.Where(exp);
  60. }
  61. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  62. var loProducts = ProductRepository.GetAll().Where(i=>i.IsLock=="N");
  63. var loCustomerDefaultProductDto = from d in query join p in loProducts on d.ProductNo equals p.Id
  64. orderby d.TimeLastMod descending
  65. select new CustomerDefaultProductDto()
  66. {
  67. CustomerId = d.CustomerId,
  68. CustomerProductName = d.CustomerProductName,
  69. Id = d.Id,
  70. ProductNo = d.ProductNo,
  71. ProductName = p.ProductName,
  72. Material = p.Material,
  73. Model = p.Model,
  74. Rigidity = p.Rigidity,
  75. SurfaceColor = p.SurfaceColor,
  76. Sequence = d.Sequence,
  77. TimeLastMod = d.TimeLastMod,
  78. PartNo=d.PartNo
  79. };
  80. loCustomerDefaultProductDto = loCustomerDefaultProductDto.OrderBy(i => i.Sequence);
  81. //query = ApplySorting(query, input);
  82. loCustomerDefaultProductDto = loCustomerDefaultProductDto.Skip(input.SkipCount).Take(input.MaxResultCount);
  83. var entities = await AsyncQueryableExecuter.ToListAsync(loCustomerDefaultProductDto);
  84. var dtos = new PagedResultDto<CustomerDefaultProductDto>(
  85. totalCount,
  86. entities
  87. );
  88. return dtos;
  89. }
  90. public override async Task<CustomerDefaultProductDto> Create(CustomerDefaultProductCreateDto input)
  91. {
  92. CheckCreatePermission();
  93. string lcProductNos = input?.ProductNo;
  94. string lcCustomerId = input?.CustomerId;
  95. if (!lcProductNos.IsNullOrEmpty() && lcProductNos.EndsWith(","))
  96. {
  97. lcProductNos = lcProductNos.Substring(0, lcProductNos.Length - 1);
  98. }
  99. if (lcProductNos.IsNullOrEmpty()|| lcCustomerId.IsNullOrEmpty())
  100. {
  101. throw new UserFriendlyException("传入参数有误!");
  102. }
  103. var loExistProducts = Repository.GetAll().Where(i => i.CustomerId == lcCustomerId).OrderByDescending(i => i.Sequence);
  104. var obj = loExistProducts.FirstOrDefault();
  105. string[] pNos = lcProductNos?.Split(',');
  106. int index = obj == null ? 1 : obj.Sequence;
  107. if (pNos != null)
  108. foreach (var s in pNos)
  109. {
  110. var exist = loExistProducts.FirstOrDefault(i => i.ProductNo == s);
  111. if (exist != null)
  112. {
  113. continue;
  114. }
  115. CustomerDefaultProduct loCustomerDefaultProduct = new CustomerDefaultProduct()
  116. {
  117. CustomerId = lcCustomerId,
  118. ProductNo = s,
  119. Sequence = index,
  120. TimeLastMod = Clock.Now,
  121. };
  122. await Repository.InsertAsync(loCustomerDefaultProduct);
  123. index++;
  124. }
  125. return new CustomerDefaultProductDto();
  126. }
  127. public override async Task<CustomerDefaultProductDto> Update(CustomerDefaultProductUpdateDto input)
  128. {
  129. var loExistProducts =await Repository.FirstOrDefaultAsync(i => i.CustomerId == input.CustomerId &&i.PartNo==input.PartNo);
  130. if (loExistProducts != null && loExistProducts.Id != input.Id)
  131. {
  132. throw new UserFriendlyException($"零件号重复!该客户维护的产品{loExistProducts.ProductNo },已经使用了零件号{loExistProducts.PartNo}!");
  133. }
  134. return await base.Update(input);
  135. }
  136. /*public string GetDefualtProductByOrderItemNo(int orderItemNo)
  137. {
  138. var orderItem = OrderItemRepository.Get(orderItemNo);
  139. return GetDefualtProductByOrderNo(orderItem.OrderNo);
  140. }
  141. public string GetDefualtProductByOrderNo(string orderNo)
  142. {
  143. var orderHeader = OrderHeaderRepository.Get(orderNo);
  144. return GetDefualtProductByCustomerId(orderHeader.CustomerId);
  145. }
  146. public string GetDefualtProductByCustomerId(string customerId)
  147. {
  148. var defualtProducts = Repository.GetAll().Where(i => i.CustomerId == customerId);
  149. string lcRetval = "";
  150. foreach (var item in defualtProducts)
  151. {
  152. lcRetval += $"<option value=\"{item.ProductNo}\">{item.ProductNo}</option>";
  153. }
  154. return lcRetval;
  155. }*/
  156. }
  157. }