CustomerDefaultProductsApplicationService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. Sequence = d.Sequence,
  73. TimeLastMod = d.TimeLastMod,
  74. PartNo=d.PartNo
  75. };
  76. loCustomerDefaultProductDto = loCustomerDefaultProductDto.OrderBy(i => i.Sequence);
  77. //query = ApplySorting(query, input);
  78. loCustomerDefaultProductDto = loCustomerDefaultProductDto.Skip(input.SkipCount).Take(input.MaxResultCount);
  79. var entities = await AsyncQueryableExecuter.ToListAsync(loCustomerDefaultProductDto);
  80. var dtos = new PagedResultDto<CustomerDefaultProductDto>(
  81. totalCount,
  82. entities
  83. );
  84. return dtos;
  85. }
  86. public override async Task<CustomerDefaultProductDto> Create(CustomerDefaultProductCreateDto input)
  87. {
  88. CheckCreatePermission();
  89. string lcProductNos = input?.ProductNo;
  90. string lcCustomerId = input?.CustomerId;
  91. if (!lcProductNos.IsNullOrEmpty() && lcProductNos.EndsWith(","))
  92. {
  93. lcProductNos = lcProductNos.Substring(0, lcProductNos.Length - 1);
  94. }
  95. if (lcProductNos.IsNullOrEmpty()|| lcCustomerId.IsNullOrEmpty())
  96. {
  97. throw new UserFriendlyException("传入参数有误!");
  98. }
  99. var loExistProducts = Repository.GetAll().Where(i => i.CustomerId == lcCustomerId).OrderByDescending(i => i.Sequence);
  100. var obj = loExistProducts.FirstOrDefault();
  101. string[] pNos = lcProductNos?.Split(',');
  102. int index = obj == null ? 1 : obj.Sequence;
  103. if (pNos != null)
  104. foreach (var s in pNos)
  105. {
  106. var exist = loExistProducts.FirstOrDefault(i => i.ProductNo == s);
  107. if (exist != null)
  108. {
  109. continue;
  110. }
  111. CustomerDefaultProduct loCustomerDefaultProduct = new CustomerDefaultProduct()
  112. {
  113. CustomerId = lcCustomerId,
  114. ProductNo = s,
  115. Sequence = index,
  116. TimeLastMod = Clock.Now,
  117. };
  118. await Repository.InsertAsync(loCustomerDefaultProduct);
  119. index++;
  120. }
  121. return new CustomerDefaultProductDto();
  122. }
  123. public override async Task<CustomerDefaultProductDto> Update(CustomerDefaultProductUpdateDto input)
  124. {
  125. var loExistProducts =await Repository.FirstOrDefaultAsync(i => i.CustomerId == input.CustomerId &&i.PartNo==input.PartNo);
  126. if (loExistProducts != null && loExistProducts.Id != input.Id)
  127. {
  128. throw new UserFriendlyException($"零件号重复!该客户维护的产品{loExistProducts.ProductNo },已经使用了零件号{loExistProducts.PartNo}!");
  129. }
  130. return await base.Update(input);
  131. }
  132. /*public string GetDefualtProductByOrderItemNo(int orderItemNo)
  133. {
  134. var orderItem = OrderItemRepository.Get(orderItemNo);
  135. return GetDefualtProductByOrderNo(orderItem.OrderNo);
  136. }
  137. public string GetDefualtProductByOrderNo(string orderNo)
  138. {
  139. var orderHeader = OrderHeaderRepository.Get(orderNo);
  140. return GetDefualtProductByCustomerId(orderHeader.CustomerId);
  141. }
  142. public string GetDefualtProductByCustomerId(string customerId)
  143. {
  144. var defualtProducts = Repository.GetAll().Where(i => i.CustomerId == customerId);
  145. string lcRetval = "";
  146. foreach (var item in defualtProducts)
  147. {
  148. lcRetval += $"<option value=\"{item.ProductNo}\">{item.ProductNo}</option>";
  149. }
  150. return lcRetval;
  151. }*/
  152. }
  153. }