ProductsApplicationService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using System.Web.Http;
  10. using System.Web.Mvc;
  11. using Abp.Application.Services.Dto;
  12. using Abp.Auditing;
  13. using Abp.Authorization;
  14. using Abp.Domain.Repositories;
  15. using Abp.Extensions;
  16. using Abp.Timing;
  17. using Abp.UI;
  18. using IwbZero.AppServiceBase;
  19. using IwbZero.IdentityFramework;
  20. using IwbZero.Setting;
  21. using Newtonsoft.Json;
  22. using ShwasherSys.Authorization.Permissions;
  23. using ShwasherSys.BaseSysInfo;
  24. using ShwasherSys.BaseSysInfo.States.Dto;
  25. using ShwasherSys.Common;
  26. using ShwasherSys.CustomerInfo;
  27. using ShwasherSys.Lambda;
  28. using ShwasherSys.Order;
  29. using ShwasherSys.ProductInfo.Dto;
  30. namespace ShwasherSys.ProductInfo
  31. {
  32. [AbpAuthorize]
  33. public class ProductsAppService : ShwasherAsyncCrudAppService<Product, ProductDto, string, PagedRequestDto, ProductCreateDto, ProductUpdateDto >, IProductsAppService
  34. {
  35. protected IRepository<CustomerDefaultProduct> CustomerDefaultProductRepository;
  36. protected IRepository<SemiProducts,string> SemiProductsRepository { get; set; }
  37. protected IRepository<OrderItem,int> OrderItemRepository { get; set; }
  38. protected IRepository<BusinessLog> BusinessLogRepository;
  39. public ProductsAppService(IRepository<Product, string> repository, IRepository<CustomerDefaultProduct> customerDefaultProductRepository, IIwbSettingManager settingManager, IRepository<SemiProducts, string> semiProductsRepository, IRepository<OrderItem, int> orderItemRepository, IRepository<BusinessLog> businessLogRepository) : base(repository,"Id")
  40. {
  41. CustomerDefaultProductRepository = customerDefaultProductRepository;
  42. SettingManager = settingManager;
  43. SemiProductsRepository = semiProductsRepository;
  44. OrderItemRepository = orderItemRepository;
  45. BusinessLogRepository = businessLogRepository;
  46. }
  47. protected override bool KeyIsAuto { get; set; } = false;
  48. protected override string GetPermissionName { get; set; }= PermissionNames.PagesProductInfoProducts;
  49. protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesProductInfoProducts;
  50. protected override string CreatePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsCreate;
  51. protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsUpdate;
  52. protected override string DeletePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsDelete;
  53. [AbpAuthorize(PermissionNames.PagesProductInfoProductsCreate)]
  54. public override async Task<ProductDto> Create(ProductCreateDto input)
  55. {
  56. string sEntity = JsonConvert.SerializeObject(input);
  57. var entity = await SemiProductsRepository.FirstOrDefaultAsync(i => i.Id == input.Id);
  58. if (entity == null)
  59. {
  60. var semiProducts = JsonConvert.DeserializeObject<SemiProducts>(sEntity);
  61. semiProducts.IsLock = "N";
  62. semiProducts.SemiProductName = input.ProductName;
  63. semiProducts.TimeLastMod = Clock.Now;
  64. semiProducts.UserIDLastMod = AbpSession.UserName;
  65. semiProducts.TimeCreated = Clock.Now;
  66. SemiProductsRepository.Insert(semiProducts);
  67. }
  68. return await base.Create(input);
  69. }
  70. [AbpAuthorize(PermissionNames.PagesProductInfoProductsUpdate)]
  71. public override async Task<ProductDto> Update(ProductUpdateDto input)
  72. {
  73. var entity = await SemiProductsRepository.FirstOrDefaultAsync(i => i.Id == input.Id);
  74. string sEntity = JsonConvert.SerializeObject(input);
  75. var semiProducts = JsonConvert.DeserializeObject<SemiProducts>(sEntity);
  76. if (entity != null)
  77. {
  78. entity.SemiProductName = input.ProductName;
  79. entity.TimeLastMod = Clock.Now;
  80. entity.UserIDLastMod = AbpSession.UserName;
  81. entity.IsStandard = input.IsStandard;
  82. entity.ProductDesc = input.ProductDesc;
  83. entity.TranUnitValue = input.TranUnitValue;
  84. entity.PartNo = input.PartNo;
  85. SemiProductsRepository.Update(entity);
  86. }
  87. else
  88. {
  89. semiProducts.IsLock = "N";
  90. semiProducts.SemiProductName = input.ProductName;
  91. semiProducts.TimeLastMod = Clock.Now;
  92. semiProducts.UserIDLastMod = AbpSession.UserName;
  93. semiProducts.TimeCreated = Clock.Now;
  94. SemiProductsRepository.Insert(semiProducts);
  95. }
  96. return await base.Update(input);
  97. }
  98. [AbpAuthorize(PermissionNames.PagesProductInfoProductsDelete)]
  99. public override async Task Delete(EntityDto<string> input)
  100. {
  101. var entityItem = await OrderItemRepository.FirstOrDefaultAsync(i => i.ProductNo == input.Id);
  102. if (entityItem != null)
  103. {
  104. throw new UserFriendlyException("该产品已经在订单中使用!不可进行删除!");
  105. }
  106. BusinessLogTypeEnum.Product.WriteLog(BusinessLogRepository, "产品删除", $"产品删除成功!ProductNo:{input.Id},userName:{AbpSession.UserName}");
  107. await base.Delete(input);
  108. }
  109. [DisableAuditing]
  110. public List<SelectListItem> GetProductPropertyList(string pcPropertyName)
  111. {
  112. var objList = new List<SelectListItem>();
  113. if (pcPropertyName.IsNullOrEmpty())
  114. {
  115. return objList;
  116. }
  117. var entitys = Repository.GetAll().Where(i => i.IsLock == "N");
  118. var loPropertyInfo = typeof(Product).GetProperty(pcPropertyName,
  119. BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
  120. if (loPropertyInfo == null)
  121. {
  122. throw new ArgumentException($"{pcPropertyName} is not a property of type {typeof(Product)}.");
  123. }
  124. IQueryable<string> query = null;
  125. switch (pcPropertyName)
  126. {
  127. case "Material":
  128. query = entitys.Select(i => i.Material).Distinct();
  129. break;
  130. case "SurfaceColor":
  131. query = entitys.Select(i => i.SurfaceColor).Distinct();
  132. break;
  133. case "Rigidity":
  134. query = entitys.Select(i => i.Rigidity).Distinct();
  135. break;
  136. }
  137. //var r = entitys.Select(i => i.Material).Distinct();
  138. //var query = entitys.Distinct(new PropertyComparer<Product>(pcPropertyName));//entitys.Distinct(new PropertyComparer<Product>(pcPropertyName));
  139. if (query != null)
  140. {
  141. foreach (var product in query)
  142. {
  143. objList.Add(new SelectListItem()
  144. {
  145. Text = product,
  146. Value = product
  147. });
  148. }
  149. }
  150. return objList;
  151. }
  152. [DisableAuditing]
  153. public async Task<PagedResultDto<ProductDto>> GetQueryCustomerDefaultProduct(PagedRequestDto input)
  154. {
  155. CheckGetAllPermission();
  156. var query = CreateFilteredQuery(input);
  157. query = query.Where(i => i.IsLock == "N");
  158. string lcCustomerId = "";
  159. if (input.SearchList != null && input.SearchList.Count > 0)
  160. {
  161. List<LambdaObject> objList = new List<LambdaObject>();
  162. foreach (var o in input.SearchList)
  163. {
  164. if (o.KeyWords.IsNullOrEmpty())
  165. continue;
  166. object keyWords = o.KeyWords;
  167. if (o.KeyField == "CustomerId"|| o.KeyField == "customerId")
  168. {
  169. lcCustomerId = keyWords + "";
  170. continue;
  171. }
  172. objList.Add(new LambdaObject
  173. {
  174. FieldType = (LambdaFieldType)o.FieldType,
  175. FieldName = o.KeyField,
  176. FieldValue = keyWords,
  177. ExpType = (LambdaExpType)o.ExpType
  178. });
  179. }
  180. var exp = objList.GetExp<Product>();
  181. if (exp != null)
  182. {
  183. query = query.Where(exp);
  184. }
  185. }
  186. List<string> loNotContain = new List<string>();
  187. if (!lcCustomerId.IsNullOrEmpty())
  188. {
  189. var loDefualtProducts = CustomerDefaultProductRepository.GetAll().Where(i => i.CustomerId == lcCustomerId);
  190. foreach (CustomerDefaultProduct defaultProduct in loDefualtProducts)
  191. {
  192. loNotContain.Add(defaultProduct.ProductNo);
  193. }
  194. }
  195. if (loNotContain.Any())
  196. {
  197. query = query.Where(i => !loNotContain.Contains(i.Id));
  198. }
  199. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  200. query = ApplySorting(query, input);
  201. query = ApplyPaging(query, input);
  202. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  203. var dtos = new PagedResultDto<ProductDto>(
  204. totalCount,
  205. entities.Select(MapToEntityDto).ToList()
  206. );
  207. return dtos;
  208. }
  209. [AbpAuthorize(PermissionNames.PagesProductInfoProductsExportExcel)]
  210. public async Task<string> ExportExcel(List<MultiSearchDto> input)
  211. {
  212. var query = Repository.GetAll();
  213. query = query.Where(i => i.IsLock == "N");
  214. if (input != null && input.Count > 0)
  215. {
  216. List<LambdaObject> objList = new List<LambdaObject>();
  217. foreach (var o in input)
  218. {
  219. if (o.KeyWords.IsNullOrEmpty())
  220. continue;
  221. object keyWords = o.KeyWords;
  222. objList.Add(new LambdaObject
  223. {
  224. FieldType = (LambdaFieldType)o.FieldType,
  225. FieldName = o.KeyField,
  226. FieldValue = keyWords,
  227. ExpType = (LambdaExpType)o.ExpType
  228. });
  229. }
  230. var exp = objList.GetExp<Product>();
  231. if (exp != null)
  232. {
  233. query = query.Where(exp);
  234. }
  235. }
  236. query = query.OrderByDescending(i => i.Id);
  237. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  238. string downloadUrl = await SettingManager.GetSettingValueAsync("SYSTEMDOWNLOADPATH");
  239. string lcFilePath = System.Web.HttpRuntime.AppDomainAppPath + "\\" +
  240. downloadUrl;
  241. List<ToExcelObj> columnsList = new List<ToExcelObj>()
  242. {
  243. new ToExcelObj(){MapColumn = "Id",ShowColumn = "成品编号"},
  244. new ToExcelObj(){MapColumn = "ProductName",ShowColumn = "名称"},
  245. new ToExcelObj(){MapColumn = "SurfaceColor",ShowColumn = "表色"},
  246. new ToExcelObj(){MapColumn = "Model",ShowColumn = "规格"},
  247. new ToExcelObj(){MapColumn = "Rigidity",ShowColumn = "硬度"},
  248. new ToExcelObj(){MapColumn = "Material",ShowColumn = "材质"},
  249. new ToExcelObj(){MapColumn = "Defprice",ShowColumn = "默认价格"},
  250. };
  251. string lcResultFileName = ExcelHelper.ToExcel2003(columnsList, entities, "sheet", lcFilePath);
  252. return Path.Combine(downloadUrl, lcResultFileName);
  253. }
  254. }
  255. }