ProductPropertysApplicationService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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.PagesProductInfoProductPropertyUpdate)]
  190. public async Task UpdateExtend(ProductPropertyUpdateDto input)
  191. {
  192. var entity = await Repository.GetAsync(input.Id);
  193. if (entity == null)
  194. {
  195. CheckErrors("未查询到相应的属性!");
  196. }
  197. var dto = MapToEntityDto(entity);
  198. dto.HsCode = input.HsCode;
  199. await UpdateEntity(input);
  200. }
  201. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyDelete)]
  202. public override async Task Delete(EntityDto<int> input)
  203. {
  204. var checkEntity = Repository.Get(input.Id);
  205. Product entity = await IsExistProduct(checkEntity.PropertyType, checkEntity.PropertyNo);
  206. SemiProducts semiEntity = await IsExistSemiProduct(checkEntity.PropertyType, checkEntity.PropertyNo);
  207. if (entity != null || semiEntity != null)
  208. {
  209. CheckErrors($"该属性已经被产品使用,不可进行删除!产品编号:[{entity?.Id}--{semiEntity.Id}]");
  210. }
  211. await Repository.DeleteAsync(input.Id);
  212. }
  213. private async Task<Product> IsExistProduct(string type, string propertyNo)
  214. {
  215. Product entity = null;
  216. switch (type)
  217. {
  218. case "1":
  219. entity = await ProductRepository.FirstOrDefaultAsync(i => i.ModelNo == propertyNo);
  220. break;
  221. case "2":
  222. entity = await ProductRepository.FirstOrDefaultAsync(i => i.MaterialNo == propertyNo);
  223. break;
  224. case "3":
  225. entity = await ProductRepository.FirstOrDefaultAsync(i => i.RigidityNo == propertyNo);
  226. break;
  227. case "4":
  228. entity = await ProductRepository.FirstOrDefaultAsync(i => i.SurfaceColorNo == propertyNo);
  229. break;
  230. case "5":
  231. entity = await ProductRepository.FirstOrDefaultAsync(i => i.SpecialNo == propertyNo);
  232. break;
  233. }
  234. return entity;
  235. }
  236. private async Task<SemiProducts> IsExistSemiProduct(string type, string propertyNo)
  237. {
  238. SemiProducts entity = null;
  239. switch (type)
  240. {
  241. case "1":
  242. entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.ModelNo == propertyNo);
  243. break;
  244. case "2":
  245. entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.MaterialNo == propertyNo);
  246. break;
  247. case "3":
  248. entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.RigidityNo == propertyNo);
  249. break;
  250. case "4":
  251. entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.SurfaceColorNo == propertyNo);
  252. break;
  253. case "5":
  254. entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.SpecialNo == propertyNo);
  255. break;
  256. }
  257. return entity;
  258. }
  259. [DisableAuditing]
  260. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  261. public override async Task<PagedResultDto<ProductPropertyDto>> GetAll(IwbPagedRequestDto input)
  262. {
  263. var query = CreateFilteredQuery(input);
  264. query = ApplyFilter(query, input);
  265. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  266. query = ApplySorting(query, input);
  267. query = ApplyPaging(query, input);
  268. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  269. var dtoList = new PagedResultDto<ProductPropertyDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  270. return dtoList;
  271. }
  272. #region GetEntity/Dto
  273. /// <summary>
  274. /// 查询实体Dto
  275. /// </summary>
  276. /// <param name="input"></param>
  277. /// <returns></returns>
  278. [DisableAuditing]
  279. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  280. public override async Task<ProductPropertyDto> GetDto(EntityDto<int> input)
  281. {
  282. var entity = await GetEntity(input);
  283. return MapToEntityDto(entity);
  284. }
  285. /// <summary>
  286. /// 查询实体Dto
  287. /// </summary>
  288. /// <param name="id"></param>
  289. /// <returns></returns>
  290. [DisableAuditing]
  291. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  292. public override async Task<ProductPropertyDto> GetDtoById(int id)
  293. {
  294. var entity = await GetEntityById(id);
  295. return MapToEntityDto(entity);
  296. }
  297. /// <summary>
  298. /// 查询实体Dto(需指明自定义字段)
  299. /// </summary>
  300. /// <param name="no"></param>
  301. /// <returns></returns>
  302. [DisableAuditing]
  303. //[AbpAuthorize(PermissionNames.PagesMgProductPropertyMgQuery)]
  304. public override async Task<ProductPropertyDto> GetDtoByNo(string no)
  305. {
  306. var entity = await GetEntityByNo(no);
  307. return MapToEntityDto(entity);
  308. }
  309. /// <summary>
  310. /// 查询实体
  311. /// </summary>
  312. /// <param name="input"></param>
  313. /// <returns></returns>
  314. [DisableAuditing]
  315. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  316. public override async Task<ProductProperty> GetEntity(EntityDto<int> input)
  317. {
  318. var entity = await GetEntityById(input.Id);
  319. return entity;
  320. }
  321. /// <summary>
  322. /// 查询实体
  323. /// </summary>
  324. /// <param name="id"></param>
  325. /// <returns></returns>
  326. [DisableAuditing]
  327. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  328. public override async Task<ProductProperty> GetEntityById(int id)
  329. {
  330. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  331. }
  332. /// <summary>
  333. /// 查询实体(需指明自定义字段)
  334. /// </summary>
  335. /// <param name="no"></param>
  336. /// <returns></returns>
  337. [DisableAuditing]
  338. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
  339. public override async Task<ProductProperty> GetEntityByNo(string no)
  340. {
  341. //CheckGetPermission();
  342. if (string.IsNullOrEmpty(KeyFiledName))
  343. {
  344. ThrowError("NoKeyFieldName");
  345. }
  346. return await base.GetEntityByNo(no);
  347. }
  348. #endregion
  349. #region Hide
  350. ///// <summary>
  351. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{ProductProperty}"/>过滤查询.
  352. ///// </summary>
  353. ///// <param name="input">The input.</param>
  354. //protected override IQueryable<ProductProperty> CreateFilteredQuery(IwbPagedRequestDto input)
  355. //{
  356. // var query = Repository.GetAll();
  357. // var pagedInput = input as IIwbPagedRequest;
  358. // if (pagedInput == null)
  359. // {
  360. // return query;
  361. // }
  362. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  363. // {
  364. // object keyWords = pagedInput.KeyWords;
  365. // LambdaObject obj = new LambdaObject()
  366. // {
  367. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  368. // FieldName = pagedInput.KeyField,
  369. // FieldValue = keyWords,
  370. // ExpType = (LambdaExpType)pagedInput.ExpType
  371. // };
  372. // var exp = obj.GetExp<ProductProperty>();
  373. // query = exp != null ? query.Where(exp) : query;
  374. // }
  375. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  376. // {
  377. // List<LambdaObject> objList = new List<LambdaObject>();
  378. // foreach (var o in pagedInput.SearchList)
  379. // {
  380. // if (string.IsNullOrEmpty(o.KeyWords))
  381. // continue;
  382. // object keyWords = o.KeyWords;
  383. // objList.Add(new LambdaObject
  384. // {
  385. // FieldType = (LambdaFieldType)o.FieldType,
  386. // FieldName = o.KeyField,
  387. // FieldValue = keyWords,
  388. // ExpType = (LambdaExpType)o.ExpType
  389. // });
  390. // }
  391. // var exp = objList.GetExp<ProductProperty>();
  392. // query = exp != null ? query.Where(exp) : query;
  393. // }
  394. // return query;
  395. //}
  396. //protected override IQueryable<ProductProperty> ApplySorting(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
  397. //{
  398. // return query.OrderBy(a => a.No);
  399. //}
  400. //protected override IQueryable<ProductProperty> ApplyPaging(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
  401. //{
  402. // if (input is IPagedResultRequest pagedInput)
  403. // {
  404. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  405. // }
  406. // return query;
  407. //}
  408. #endregion
  409. #endregion
  410. [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyImportExcel)]
  411. public async Task<bool> ImportExcel(FileUploadInfoDto input)
  412. {
  413. if (!string.IsNullOrEmpty(input.FileInfo))
  414. {
  415. string filePath =
  416. $"{SettingManager.GetSettingValue(SettingNames.DownloadPath)}/tmpUpload";
  417. var lcRetVal = SysAttachFileAppService.Base64ToFile(input.FileInfo,
  418. $"{input.FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}", input.FileExt,
  419. filePath);
  420. StringBuilder errStringBuilder = new StringBuilder();
  421. List<ProductPropertyCreateDto> resultEntityList = ExcelHelper.ExcelToEntityList<ProductPropertyCreateDto>(new Dictionary<string, string>() { { "PropertyType", "属性类别" }, { "PropertyValue", "属性值" }, { "DisplayValue", "显示值" }, { "ContentInfo", "内容描述" } }, $"{AppDomain.CurrentDomain.BaseDirectory}{lcRetVal}",
  422. out errStringBuilder);
  423. int indexCount = 1;
  424. foreach (var productProperty in resultEntityList)
  425. {
  426. if (productProperty.PropertyValue.IsNullOrEmpty()|| productProperty.PropertyType.IsNullOrEmpty())
  427. {
  428. continue;
  429. }
  430. var checkEntity =
  431. (await Repository.GetAllListAsync(i =>
  432. i.PropertyType == productProperty.PropertyType && i.PropertyValue == productProperty.PropertyValue)).FirstOrDefault();
  433. if (checkEntity != null)
  434. {
  435. //CheckErrors("该属性已经处在,不需要重复增加!");
  436. continue;
  437. }
  438. try
  439. {
  440. using (var uow = UnitOfWorkManager.Begin())
  441. {
  442. var preEntity = (await Repository.GetAllListAsync(i => i.PropertyType == productProperty.PropertyType))
  443. .OrderByDescending(i => i.CreationTime).FirstOrDefault();
  444. productProperty.PropertyNo = GetNextNum(preEntity?.PropertyNo ?? "0", GetMaxSizeByType(productProperty.PropertyType));
  445. await CreateEntity(productProperty);
  446. uow.Complete();
  447. }
  448. indexCount++;
  449. }
  450. catch (Exception e)
  451. {
  452. errStringBuilder.Append($"第{indexCount}条记录,上传失败!" + e.Message);
  453. // return false;
  454. CheckErrors(errStringBuilder.ToString());
  455. }
  456. }
  457. this.LogError(errStringBuilder.ToString());
  458. }
  459. else
  460. {
  461. CheckErrors(new IwbIdentityResult("请先上传文件!"));
  462. }
  463. return false;
  464. }
  465. }
  466. }