ProductPropertysApplicationService.cs 19 KB

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