| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Mvc;
- using Abp.Application.Services.Dto;
- using Abp.Auditing;
- using Abp.Authorization;
- using Abp.Domain.Repositories;
- using Abp.Extensions;
- using Abp.Runtime.Caching;
- using Abp.Timing;
- using Abp.Web.Models.AbpUserConfiguration;
- using IwbZero.Auditing;
- using IwbZero.AppServiceBase;
- using IwbZero.IdentityFramework;
- using IwbZero.Setting;
- using NPOI.SS.Formula.Functions;
- using ShwasherSys.Authorization.Permissions;
- using ShwasherSys.BaseSysInfo;
- using ShwasherSys.BaseSysInfo.SysAttachFiles;
- using ShwasherSys.Common;
- using ShwasherSys.EntityFramework;
- using ShwasherSys.ProductInfo.Dto;
- using ShwasherSys.ProductInfo.Dto.FileUpload;
- namespace ShwasherSys.ProductInfo
- {
- [AbpAuthorize]
- public class ProductPropertyAppService : IwbZeroAsyncCrudAppService<ProductProperty, ProductPropertyDto, int, IwbPagedRequestDto, ProductPropertyCreateDto, ProductPropertyUpdateDto >, IProductPropertyAppService
- {
- public ProductPropertyAppService(
- ICacheManager cacheManager,
- IRepository<ProductProperty, int> repository, IRepository<Product, string> productRepository, IRepository<SemiProducts, string> semiProductRepository, IRepository<BusinessLog> logRepository) : base(repository, "Id")
- {
- LogRepository = logRepository;
- ProductRepository = productRepository;
- CacheManager = cacheManager;
- SemiProductRepository = semiProductRepository;
- }
- protected override bool KeyIsAuto { get; set; } = true;
- protected IRepository<BusinessLog> LogRepository;
- protected IRepository<Product, string> ProductRepository { get; }
- protected IRepository<SemiProducts, string> SemiProductRepository { get; }
- #region GetSelect
- [DisableAuditing]
- public override async Task<List<SelectListItem>> GetSelectList()
- {
- var list = await Repository.GetAllListAsync();
- var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
- foreach (var l in list)
- {
- //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
- }
- return sList;
- }
- [DisableAuditing]
- public override async Task<string> GetSelectStr()
- {
- var list = await Repository.GetAllListAsync();
- string str = "<option value=\"\" selected>请选择...</option>";
- foreach (var l in list)
- {
- //str += $"<option value=\"{l.Id}\">{l.}</option>";
- }
- return str;
- }
- #endregion
- #region 编号维护
- /// <summary>
- /// 十进制转32进制
- /// </summary>
- /// <param name="inputNum"></param>
- /// <param name="maxSize"></param>
- /// <returns></returns>
- public static string DecimalCalcTo32(int inputNum, int maxSize)
- {
- int max = 0;
- var result = new string[20];
- var displayArr = new string[]
- {
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M",
- "N", "P", "Q", "R", "T", "U", "V", "W", "X", "Y", "Z"
- };
- int ten = inputNum;
- int arrSize = displayArr.Length;
- string lResult = "";
- do
- {
- var sixteen = ten % arrSize;
- ten = ten / arrSize;
- result[max] = displayArr[sixteen];
- lResult = result[max] + lResult;
- max++;
- } while (ten != 0);
- lResult = lResult.PadLeft(maxSize, '0');
- return lResult;
- }
- /// <summary>
- /// 32进制转10进制
- /// </summary>
- /// <param name="inputNum"></param>
- /// <returns></returns>
- public static double Cal32ToDecimal(string inputNum)
- {
- if (string.IsNullOrEmpty(inputNum))
- {
- return -1;
- }
- var displayStr = "0123456789ABCDEFGHJKMNPQRTUVWXYZ";
- int disLength = displayStr.ToArray().Length;
- double numResult = 0;
- var inputArr = inputNum.ToArray().Reverse().ToList();
- for (int i = 0; i < inputArr.Count; i++)
- {
- int index = displayStr.IndexOf(inputArr[i]);
- if (index < 0)
- {
- return -1;
- }
- numResult += index * Math.Pow(disLength, i);
- }
- return numResult;
- }
- /// <summary>
- /// 上一个32位编码
- /// </summary>
- /// <param name="inputNum"></param>
- /// <returns></returns>
- private string GetNextNum(string inputNum,int maxSize)
- {
- var preNum = Cal32ToDecimal(inputNum);
- var newNum = preNum + 1;
- return DecimalCalcTo32((int) newNum, maxSize);
- }
- private int GetMaxSizeByType(string type)
- {
- int size = ProductProperty.ProductRigidityLength;
- if (int.TryParse(type, out int ts))
- {
- if (ts == ProductProperty.ProductModel)
- {
- size = ProductProperty.ProductModelLength;
- }
- else if (ts == ProductProperty.ProductSurfaceColor)
- {
- size = ProductProperty.ProductSurfaceColorLength;
- }
- }
- return size;
- }
- #endregion
- #region CURD
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyCreate)]
- public override async Task Create(ProductPropertyCreateDto input)
- {
- var checkEntity =
- (await Repository.GetAllListAsync(i =>
- i.PropertyType == input.PropertyType && i.PropertyValue == input.PropertyValue)).FirstOrDefault();
- if (checkEntity != null)
- {
- CheckErrors("该属性已经处在,不需要重复增加!");
- }
- var preEntity = (await Repository.GetAllListAsync(i => i.PropertyType == input.PropertyType))
- .OrderByDescending(i => i.CreationTime).FirstOrDefault();
- input.PropertyNo = GetNextNum(preEntity?.PropertyNo??"0", GetMaxSizeByType(input.PropertyType));
-
- await CreateEntity(input);
- }
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyUpdate)]
- public override async Task Update(ProductPropertyUpdateDto input)
- {
- string propertyNo = input.PropertyNo;
- string type = input.PropertyType;
- Product entity = await IsExistProduct(type, propertyNo);
- SemiProducts semiEntity = await IsExistSemiProduct(type, propertyNo);
- if (entity != null || semiEntity != null)
- {
- CheckErrors($"该属性已经被产品使用,不可进行修改!产品编号:[{entity?.Id}--{semiEntity.Id}]");
- }
- var checkEntity =
- (await Repository.GetAllListAsync(i =>
- i.PropertyType == type && i.PropertyValue == input.PropertyValue && i.PropertyNo != propertyNo)).FirstOrDefault();
- if (checkEntity != null)
- {
- CheckErrors("该属性已经处在,不需要重复增加!");
- }
-
- await UpdateEntity(input);
- }
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyDelete)]
- public override async Task Delete(EntityDto<int> input)
- {
- var checkEntity = Repository.Get(input.Id);
- Product entity = await IsExistProduct(checkEntity.PropertyType, checkEntity.PropertyNo);
- SemiProducts semiEntity = await IsExistSemiProduct(checkEntity.PropertyType, checkEntity.PropertyNo);
- if (entity != null || semiEntity != null)
- {
- CheckErrors($"该属性已经被产品使用,不可进行删除!产品编号:[{entity?.Id}--{semiEntity.Id}]");
- }
- await Repository.DeleteAsync(input.Id);
- }
- private async Task<Product> IsExistProduct(string type, string propertyNo)
- {
- Product entity = null;
- switch (type)
- {
- case "1":
- entity = await ProductRepository.FirstOrDefaultAsync(i => i.ModelNo == propertyNo);
- break;
- case "2":
- entity = await ProductRepository.FirstOrDefaultAsync(i => i.MaterialNo == propertyNo);
- break;
- case "3":
- entity = await ProductRepository.FirstOrDefaultAsync(i => i.RigidityNo == propertyNo);
- break;
- case "4":
- entity = await ProductRepository.FirstOrDefaultAsync(i => i.SurfaceColorNo == propertyNo);
- break;
- case "5":
- entity = await ProductRepository.FirstOrDefaultAsync(i => i.SpecialNo == propertyNo);
- break;
- }
- return entity;
- }
- private async Task<SemiProducts> IsExistSemiProduct(string type, string propertyNo)
- {
- SemiProducts entity = null;
- switch (type)
- {
- case "1":
- entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.ModelNo == propertyNo);
- break;
- case "2":
- entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.MaterialNo == propertyNo);
- break;
- case "3":
- entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.RigidityNo == propertyNo);
- break;
- case "4":
- entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.SurfaceColorNo == propertyNo);
- break;
- case "5":
- entity = await SemiProductRepository.FirstOrDefaultAsync(i => i.SpecialNo == propertyNo);
- break;
- }
- return entity;
- }
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<PagedResultDto<ProductPropertyDto>> GetAll(IwbPagedRequestDto input)
- {
- var query = CreateFilteredQuery(input);
- query = ApplyFilter(query, input);
- var totalCount = await AsyncQueryableExecuter.CountAsync(query);
- query = ApplySorting(query, input);
- query = ApplyPaging(query, input);
- var entities = await AsyncQueryableExecuter.ToListAsync(query);
- var dtoList = new PagedResultDto<ProductPropertyDto>(totalCount, entities.Select(MapToEntityDto).ToList());
- return dtoList;
- }
- #region GetEntity/Dto
- /// <summary>
- /// 查询实体Dto
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<ProductPropertyDto> GetDto(EntityDto<int> input)
- {
- var entity = await GetEntity(input);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体Dto
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<ProductPropertyDto> GetDtoById(int id)
- {
- var entity = await GetEntityById(id);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体Dto(需指明自定义字段)
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- [DisableAuditing]
- //[AbpAuthorize(PermissionNames.PagesMgProductPropertyMgQuery)]
- public override async Task<ProductPropertyDto> GetDtoByNo(string no)
- {
- var entity = await GetEntityByNo(no);
- return MapToEntityDto(entity);
- }
- /// <summary>
- /// 查询实体
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<ProductProperty> GetEntity(EntityDto<int> input)
- {
- var entity = await GetEntityById(input.Id);
- return entity;
- }
- /// <summary>
- /// 查询实体
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<ProductProperty> GetEntityById(int id)
- {
- return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
- }
- /// <summary>
- /// 查询实体(需指明自定义字段)
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)]
- public override async Task<ProductProperty> GetEntityByNo(string no)
- {
- //CheckGetPermission();
- if (string.IsNullOrEmpty(KeyFiledName))
- {
- ThrowError("NoKeyFieldName");
- }
- return await base.GetEntityByNo(no);
- }
- #endregion
- #region Hide
- ///// <summary>
- ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{ProductProperty}"/>过滤查询.
- ///// </summary>
- ///// <param name="input">The input.</param>
- //protected override IQueryable<ProductProperty> CreateFilteredQuery(IwbPagedRequestDto input)
- //{
- // var query = Repository.GetAll();
- // var pagedInput = input as IIwbPagedRequest;
- // if (pagedInput == null)
- // {
- // return query;
- // }
- // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
- // {
- // object keyWords = pagedInput.KeyWords;
- // LambdaObject obj = new LambdaObject()
- // {
- // FieldType = (LambdaFieldType)pagedInput.FieldType,
- // FieldName = pagedInput.KeyField,
- // FieldValue = keyWords,
- // ExpType = (LambdaExpType)pagedInput.ExpType
- // };
- // var exp = obj.GetExp<ProductProperty>();
- // query = exp != null ? query.Where(exp) : query;
- // }
- // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
- // {
- // List<LambdaObject> objList = new List<LambdaObject>();
- // foreach (var o in pagedInput.SearchList)
- // {
- // if (string.IsNullOrEmpty(o.KeyWords))
- // continue;
- // object keyWords = o.KeyWords;
- // objList.Add(new LambdaObject
- // {
- // FieldType = (LambdaFieldType)o.FieldType,
- // FieldName = o.KeyField,
- // FieldValue = keyWords,
- // ExpType = (LambdaExpType)o.ExpType
- // });
- // }
- // var exp = objList.GetExp<ProductProperty>();
- // query = exp != null ? query.Where(exp) : query;
- // }
- // return query;
- //}
- //protected override IQueryable<ProductProperty> ApplySorting(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
- //{
- // return query.OrderBy(a => a.No);
- //}
- //protected override IQueryable<ProductProperty> ApplyPaging(IQueryable<ProductProperty> query, IwbPagedRequestDto input)
- //{
- // if (input is IPagedResultRequest pagedInput)
- // {
- // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
- // }
- // return query;
- //}
- #endregion
- #endregion
- [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyImportExcel)]
- public async Task<bool> ImportExcel(FileUploadInfoDto input)
- {
- if (!string.IsNullOrEmpty(input.FileInfo))
- {
- string filePath =
- $"{SettingManager.GetSettingValue(SettingNames.DownloadPath)}/tmpUpload";
- var lcRetVal = SysAttachFileAppService.Base64ToFile(input.FileInfo,
- $"{input.FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}", input.FileExt,
- filePath);
- StringBuilder errStringBuilder = new StringBuilder();
- List<ProductPropertyCreateDto> resultEntityList = ExcelHelper.ExcelToEntityList<ProductPropertyCreateDto>(new Dictionary<string, string>() { { "PropertyType", "属性类别" }, { "PropertyValue", "属性值" }, { "DisplayValue", "显示值" }, { "ContentInfo", "内容描述" } }, $"{AppDomain.CurrentDomain.BaseDirectory}{lcRetVal}",
- out errStringBuilder);
- int indexCount = 1;
- foreach (var productProperty in resultEntityList)
- {
- if (productProperty.PropertyValue.IsNullOrEmpty()|| productProperty.PropertyType.IsNullOrEmpty())
- {
- continue;
- }
- var checkEntity =
- (await Repository.GetAllListAsync(i =>
- i.PropertyType == productProperty.PropertyType && i.PropertyValue == productProperty.PropertyValue)).FirstOrDefault();
- if (checkEntity != null)
- {
- //CheckErrors("该属性已经处在,不需要重复增加!");
- continue;
- }
- try
- {
- using (var uow = UnitOfWorkManager.Begin())
- {
- var preEntity = (await Repository.GetAllListAsync(i => i.PropertyType == productProperty.PropertyType))
- .OrderByDescending(i => i.CreationTime).FirstOrDefault();
- productProperty.PropertyNo = GetNextNum(preEntity?.PropertyNo ?? "0", GetMaxSizeByType(productProperty.PropertyType));
- await CreateEntity(productProperty);
- uow.Complete();
- }
- indexCount++;
- }
- catch (Exception e)
- {
- errStringBuilder.Append($"第{indexCount}条记录,上传失败!" + e.Message);
- // return false;
- CheckErrors(errStringBuilder.ToString());
- }
- }
- this.LogError(errStringBuilder.ToString());
- }
- else
- {
- CheckErrors(new IwbIdentityResult("请先上传文件!"));
- }
- return false;
- }
- }
- }
|