ProductPropertysApplicationService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web.Mvc;
  7. using Abp.Application.Services.Dto;
  8. using Abp.Auditing;
  9. using Abp.Authorization;
  10. using Abp.Domain.Repositories;
  11. using Abp.Extensions;
  12. using Abp.Runtime.Caching;
  13. using Abp.Timing;
  14. using Abp.Web.Models.AbpUserConfiguration;
  15. using IwbZero.Auditing;
  16. using IwbZero.AppServiceBase;
  17. using IwbZero.IdentityFramework;
  18. using IwbZero.Setting;
  19. using NPOI.SS.Formula.Functions;
  20. using ShwasherSys.Authorization.Permissions;
  21. using ShwasherSys.BaseSysInfo;
  22. using ShwasherSys.BaseSysInfo.SysAttachFiles;
  23. using ShwasherSys.Common;
  24. using ShwasherSys.EntityFramework;
  25. using ShwasherSys.ProductInfo.Dto;
  26. using ShwasherSys.ProductInfo.Dto.FileUpload;
  27. namespace ShwasherSys.ProductInfo
  28. {
  29. [AbpAuthorize]
  30. public class ProductPropertyAppService : IwbZeroAsyncCrudAppService<ProductProperty, ProductPropertyDto, int, IwbPagedRequestDto, ProductPropertyCreateDto, ProductPropertyUpdateDto >, IProductPropertyAppService
  31. {
  32. public ProductPropertyAppService(
  33. ICacheManager cacheManager,
  34. IRepository<ProductProperty, int> repository, IRepository<Product, string> productRepository, IRepository<BusinessLog> logRepository) : base(repository, "Id")
  35. {
  36. LogRepository = logRepository;
  37. ProductRepository = productRepository;
  38. CacheManager = cacheManager;
  39. }
  40. protected override bool KeyIsAuto { get; set; } = true;
  41. protected IRepository<BusinessLog> LogRepository;
  42. protected IRepository<Product, string> ProductRepository { get; }
  43. #region GetSelect
  44. [DisableAuditing]
  45. public override async Task<List<SelectListItem>> GetSelectList()
  46. {
  47. var list = await Repository.GetAllListAsync();
  48. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  49. foreach (var l in list)
  50. {
  51. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  52. }
  53. return sList;
  54. }
  55. [DisableAuditing]
  56. public override async Task<string> GetSelectStr()
  57. {
  58. var list = await Repository.GetAllListAsync();
  59. string str = "<option value=\"\" selected>请选择...</option>";
  60. foreach (var l in list)
  61. {
  62. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  63. }
  64. return str;
  65. }
  66. #endregion
  67. #region 编号维护
  68. /// <summary>
  69. /// 十进制转32进制
  70. /// </summary>
  71. /// <param name="inputNum"></param>
  72. /// <param name="maxSize"></param>
  73. /// <returns></returns>
  74. public static string DecimalCalcTo32(int inputNum, int maxSize)
  75. {
  76. int max = 0;
  77. var result = new string[20];
  78. var displayArr = new string[]
  79. {
  80. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M",
  81. "N", "P", "Q", "R", "T", "U", "V", "W", "X", "Y", "Z"
  82. };
  83. int ten = inputNum;
  84. int arrSize = displayArr.Length;
  85. string lResult = "";
  86. do
  87. {
  88. var sixteen = ten % arrSize;
  89. ten = ten / arrSize;
  90. result[max] = displayArr[sixteen];
  91. lResult = result[max] + lResult;
  92. max++;
  93. } while (ten != 0);
  94. lResult = lResult.PadLeft(maxSize, '0');
  95. return lResult;
  96. }
  97. /// <summary>
  98. /// 32进制转10进制
  99. /// </summary>
  100. /// <param name="inputNum"></param>
  101. /// <returns></returns>
  102. public static double Cal32ToDecimal(string inputNum)
  103. {
  104. if (string.IsNullOrEmpty(inputNum))
  105. {
  106. return -1;
  107. }
  108. var displayStr = "0123456789ABCDEFGHJKMNPQRTUVWXYZ";
  109. int disLength = displayStr.ToArray().Length;
  110. double numResult = 0;
  111. var inputArr = inputNum.ToArray().Reverse().ToList();
  112. for (int i = 0; i < inputArr.Count; i++)
  113. {
  114. int index = displayStr.IndexOf(inputArr[i]);
  115. if (index < 0)
  116. {
  117. return -1;
  118. }
  119. numResult += index * Math.Pow(disLength, i);
  120. }
  121. return numResult;
  122. }
  123. /// <summary>
  124. /// 上一个32位编码
  125. /// </summary>
  126. /// <param name="inputNum"></param>
  127. /// <returns></returns>
  128. private string GetNextNum(string inputNum,int maxSize)
  129. {
  130. var preNum = Cal32ToDecimal(inputNum);
  131. var newNum = preNum + 1;
  132. return DecimalCalcTo32((int) newNum, maxSize);
  133. }
  134. private int GetMaxSizeByType(string type)
  135. {
  136. int size = ProductProperty.ProductRigidityLength;
  137. if (int.TryParse(type, out int ts))
  138. {
  139. if (ts == ProductProperty.ProductModel)
  140. {
  141. size = ProductProperty.ProductModelLength;
  142. }
  143. else if (ts == ProductProperty.ProductSurfaceColor)
  144. {
  145. size = ProductProperty.ProductSurfaceColorLength;
  146. }
  147. }
  148. return size;
  149. }
  150. #endregion
  151. #region CURD
  152. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyCreate)]
  153. public override async Task Create(ProductPropertyCreateDto input)
  154. {
  155. var checkEntity =
  156. (await Repository.GetAllListAsync(i =>
  157. i.PropertyType == input.PropertyType && i.PropertyValue == input.PropertyValue)).FirstOrDefault();
  158. if (checkEntity != null)
  159. {
  160. CheckErrors("该属性已经处在,不需要重复增加!");
  161. }
  162. var preEntity = (await Repository.GetAllListAsync(i => i.PropertyType == input.PropertyType))
  163. .OrderByDescending(i => i.CreationTime).FirstOrDefault();
  164. input.PropertyNo = GetNextNum(preEntity?.PropertyNo??"0", GetMaxSizeByType(input.PropertyType));
  165. await CreateEntity(input);
  166. }
  167. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyUpdate)]
  168. public override async Task Update(ProductPropertyUpdateDto input)
  169. {
  170. string propertyNo = input.PropertyNo;
  171. string type = input.PropertyType;
  172. Product entity = await IsExistProduct(type, propertyNo);
  173. if (entity != null)
  174. {
  175. CheckErrors($"该属性已经被产品使用,不可进行修改!产品编号:[{entity.Id}]");
  176. }
  177. var checkEntity =
  178. (await Repository.GetAllListAsync(i =>
  179. i.PropertyType == type && i.PropertyValue == input.PropertyValue && i.PropertyNo != propertyNo)).FirstOrDefault();
  180. if (checkEntity != null)
  181. {
  182. CheckErrors("该属性已经处在,不需要重复增加!");
  183. }
  184. await UpdateEntity(input);
  185. }
  186. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyDelete)]
  187. public override async Task Delete(EntityDto<int> input)
  188. {
  189. var checkEntity = Repository.Get(input.Id);
  190. Product entity = await IsExistProduct(checkEntity.PropertyType, checkEntity.PropertyNo);
  191. if (entity != null)
  192. {
  193. CheckErrors($"该属性已经被产品使用,不可进行删除!产品编号:[{entity.Id}]");
  194. }
  195. await Repository.DeleteAsync(input.Id);
  196. }
  197. private async Task<Product> IsExistProduct(string type, string propertyNo)
  198. {
  199. Product entity = null;
  200. switch (type)
  201. {
  202. case "1":
  203. entity = await ProductRepository.FirstOrDefaultAsync(i => i.ModelNo == propertyNo);
  204. break;
  205. case "2":
  206. entity = await ProductRepository.FirstOrDefaultAsync(i => i.MaterialNo == propertyNo);
  207. break;
  208. case "3":
  209. entity = await ProductRepository.FirstOrDefaultAsync(i => i.RigidityNo == propertyNo);
  210. break;
  211. case "4":
  212. entity = await ProductRepository.FirstOrDefaultAsync(i => i.SurfaceColorNo == propertyNo);
  213. break;
  214. case "5":
  215. entity = await ProductRepository.FirstOrDefaultAsync(i => i.SpecialNo == propertyNo);
  216. break;
  217. }
  218. return entity;
  219. }
  220. [DisableAuditing]
  221. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  222. public override async Task<PagedResultDto<ProductPropertyDto>> GetAll(IwbPagedRequestDto input)
  223. {
  224. var query = CreateFilteredQuery(input);
  225. query = ApplyFilter(query, input);
  226. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  227. query = ApplySorting(query, input);
  228. query = ApplyPaging(query, input);
  229. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  230. var dtoList = new PagedResultDto<ProductPropertyDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  231. return dtoList;
  232. }
  233. #region GetEntity/Dto
  234. /// <summary>
  235. /// 查询实体Dto
  236. /// </summary>
  237. /// <param name="input"></param>
  238. /// <returns></returns>
  239. [DisableAuditing]
  240. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  241. public override async Task<ProductPropertyDto> GetDto(EntityDto<int> input)
  242. {
  243. var entity = await GetEntity(input);
  244. return MapToEntityDto(entity);
  245. }
  246. /// <summary>
  247. /// 查询实体Dto
  248. /// </summary>
  249. /// <param name="id"></param>
  250. /// <returns></returns>
  251. [DisableAuditing]
  252. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  253. public override async Task<ProductPropertyDto> GetDtoById(int id)
  254. {
  255. var entity = await GetEntityById(id);
  256. return MapToEntityDto(entity);
  257. }
  258. /// <summary>
  259. /// 查询实体Dto(需指明自定义字段)
  260. /// </summary>
  261. /// <param name="no"></param>
  262. /// <returns></returns>
  263. [DisableAuditing]
  264. //[AbpAuthorize(PermissionNames.PagesMgProductPropertyMgQuery)]
  265. public override async Task<ProductPropertyDto> GetDtoByNo(string no)
  266. {
  267. var entity = await GetEntityByNo(no);
  268. return MapToEntityDto(entity);
  269. }
  270. /// <summary>
  271. /// 查询实体
  272. /// </summary>
  273. /// <param name="input"></param>
  274. /// <returns></returns>
  275. [DisableAuditing]
  276. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  277. public override async Task<ProductProperty> GetEntity(EntityDto<int> input)
  278. {
  279. var entity = await GetEntityById(input.Id);
  280. return entity;
  281. }
  282. /// <summary>
  283. /// 查询实体
  284. /// </summary>
  285. /// <param name="id"></param>
  286. /// <returns></returns>
  287. [DisableAuditing]
  288. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  289. public override async Task<ProductProperty> GetEntityById(int id)
  290. {
  291. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  292. }
  293. /// <summary>
  294. /// 查询实体(需指明自定义字段)
  295. /// </summary>
  296. /// <param name="no"></param>
  297. /// <returns></returns>
  298. [DisableAuditing]
  299. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  300. public override async Task<ProductProperty> GetEntityByNo(string no)
  301. {
  302. //CheckGetPermission();
  303. if (string.IsNullOrEmpty(KeyFiledName))
  304. {
  305. ThrowError("NoKeyFieldName");
  306. }
  307. return await base.GetEntityByNo(no);
  308. }
  309. #endregion
  310. #region Hide
  311. ///// <summary>
  312. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{ProductProperty}"/>过滤查询.
  313. ///// </summary>
  314. ///// <param name="input">The input.</param>
  315. //protected override IQueryable<ProductProperty> CreateFilteredQuery(IwbPagedRequestDto input)
  316. //{
  317. // var query = Repository.GetAll();
  318. // var pagedInput = input as IIwbPagedRequest;
  319. // if (pagedInput == null)
  320. // {
  321. // return query;
  322. // }
  323. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  324. // {
  325. // object keyWords = pagedInput.KeyWords;
  326. // LambdaObject obj = new LambdaObject()
  327. // {
  328. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  329. // FieldName = pagedInput.KeyField,
  330. // FieldValue = keyWords,
  331. // ExpType = (LambdaExpType)pagedInput.ExpType
  332. // };
  333. // var exp = obj.GetExp<ProductProperty>();
  334. // query = exp != null ? query.Where(exp) : query;
  335. // }
  336. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  337. // {
  338. // List<LambdaObject> objList = new List<LambdaObject>();
  339. // foreach (var o in pagedInput.SearchList)
  340. // {
  341. // if (string.IsNullOrEmpty(o.KeyWords))
  342. // continue;
  343. // object keyWords = o.KeyWords;
  344. // objList.Add(new LambdaObject
  345. // {
  346. // FieldType = (LambdaFieldType)o.FieldType,
  347. // FieldName = o.KeyField,
  348. // FieldValue = keyWords,
  349. // ExpType = (LambdaExpType)o.ExpType
  350. // });
  351. // }
  352. // var exp = objList.GetExp<ProductProperty>();
  353. // query = exp != null ? query.Where(exp) : query;
  354. // }
  355. // return query;
  356. //}
  357. //protected override IQueryable<ProductProperty> ApplySorting(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
  358. //{
  359. // return query.OrderBy(a => a.No);
  360. //}
  361. //protected override IQueryable<ProductProperty> ApplyPaging(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
  362. //{
  363. // if (input is IPagedResultRequest pagedInput)
  364. // {
  365. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  366. // }
  367. // return query;
  368. //}
  369. #endregion
  370. #endregion
  371. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyImportExcel)]
  372. public async Task<bool> ImportExcel(FileUploadInfoDto input)
  373. {
  374. if (!string.IsNullOrEmpty(input.FileInfo))
  375. {
  376. string filePath =
  377. $"{SettingManager.GetSettingValue(SettingNames.DownloadPath)}/tmpUpload";
  378. var lcRetVal = SysAttachFileAppService.Base64ToFile(input.FileInfo,
  379. $"{input.FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}", input.FileExt,
  380. filePath);
  381. StringBuilder errStringBuilder = new StringBuilder();
  382. List<ProductPropertyCreateDto> resultEntityList = ExcelHelper.ExcelToEntityList<ProductPropertyCreateDto>(new Dictionary<string, string>() { { "PropertyType", "属性类别" }, { "PropertyValue", "属性值" }, { "DisplayValue", "显示值" }, { "ContentInfo", "内容描述" } }, $"{AppDomain.CurrentDomain.BaseDirectory}{lcRetVal}",
  383. out errStringBuilder);
  384. int indexCount = 1;
  385. foreach (var productProperty in resultEntityList)
  386. {
  387. if (productProperty.PropertyValue.IsNullOrEmpty()|| productProperty.PropertyType.IsNullOrEmpty())
  388. {
  389. continue;
  390. }
  391. var checkEntity =
  392. (await Repository.GetAllListAsync(i =>
  393. i.PropertyType == productProperty.PropertyType && i.PropertyValue == productProperty.PropertyValue)).FirstOrDefault();
  394. if (checkEntity != null)
  395. {
  396. //CheckErrors("该属性已经处在,不需要重复增加!");
  397. continue;
  398. }
  399. try
  400. {
  401. using (var uow = UnitOfWorkManager.Begin())
  402. {
  403. var preEntity = (await Repository.GetAllListAsync(i => i.PropertyType == productProperty.PropertyType))
  404. .OrderByDescending(i => i.CreationTime).FirstOrDefault();
  405. productProperty.PropertyNo = GetNextNum(preEntity?.PropertyNo ?? "0", GetMaxSizeByType(productProperty.PropertyType));
  406. await CreateEntity(productProperty);
  407. uow.Complete();
  408. }
  409. indexCount++;
  410. }
  411. catch (Exception e)
  412. {
  413. errStringBuilder.Append($"第{indexCount}条记录,上传失败!" + e.Message);
  414. // return false;
  415. CheckErrors(errStringBuilder.ToString());
  416. }
  417. }
  418. this.LogError(errStringBuilder.ToString());
  419. }
  420. else
  421. {
  422. CheckErrors(new IwbIdentityResult("请先上传文件!"));
  423. }
  424. return false;
  425. }
  426. }
  427. }