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, IProductPropertyAppService { public ProductPropertyAppService( ICacheManager cacheManager, IRepository repository, IRepository productRepository, IRepository semiProductRepository, IRepository logRepository) : base(repository, "Id") { LogRepository = logRepository; ProductRepository = productRepository; CacheManager = cacheManager; SemiProductRepository = semiProductRepository; } protected override bool KeyIsAuto { get; set; } = true; protected IRepository LogRepository; protected IRepository ProductRepository { get; } protected IRepository SemiProductRepository { get; } #region GetSelect [DisableAuditing] public override async Task> GetSelectList() { var list = await Repository.GetAllListAsync(); var sList = new List {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 GetSelectStr() { var list = await Repository.GetAllListAsync(); string str = ""; foreach (var l in list) { //str += $""; } return str; } #endregion #region 编号维护 /// /// 十进制转32进制 /// /// /// /// 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; } /// /// 32进制转10进制 /// /// /// 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; } /// /// 上一个32位编码 /// /// /// 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.PagesProductInfoProductPropertyUpdate)] public async Task UpdateExtend(ProductPropertyUpdateDto input) { var entity = await Repository.GetAsync(input.Id); if (entity == null) { CheckErrors("未查询到相应的属性!"); } var dto = MapToEntityDto(entity); dto.HsCode = input.HsCode; await UpdateEntity(input); } [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyDelete)] public override async Task Delete(EntityDto 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 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 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> 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(totalCount, entities.Select(MapToEntityDto).ToList()); return dtoList; } #region GetEntity/Dto /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)] public override async Task GetDto(EntityDto input) { var entity = await GetEntity(input); return MapToEntityDto(entity); } /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)] public override async Task GetDtoById(int id) { var entity = await GetEntityById(id); return MapToEntityDto(entity); } /// /// 查询实体Dto(需指明自定义字段) /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesMgProductPropertyMgQuery)] public override async Task GetDtoByNo(string no) { var entity = await GetEntityByNo(no); return MapToEntityDto(entity); } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)] public override async Task GetEntity(EntityDto input) { var entity = await GetEntityById(input.Id); return entity; } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)] public override async Task GetEntityById(int id) { return await Repository.FirstOrDefaultAsync(a=>a.Id==id); } /// /// 查询实体(需指明自定义字段) /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesProductInfoProductPropertyQuery)] public override async Task GetEntityByNo(string no) { //CheckGetPermission(); if (string.IsNullOrEmpty(KeyFiledName)) { ThrowError("NoKeyFieldName"); } return await base.GetEntityByNo(no); } #endregion #region Hide ///// ///// 根据给定的创建 过滤查询. ///// ///// The input. //protected override IQueryable 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(); // query = exp != null ? query.Where(exp) : query; // } // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0) // { // List objList = new List(); // 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(); // query = exp != null ? query.Where(exp) : query; // } // return query; //} //protected override IQueryable ApplySorting(IQueryable query, IwbPagedRequestDto input) //{ // return query.OrderBy(a => a.No); //} //protected override IQueryable ApplyPaging(IQueryable 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 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 resultEntityList = ExcelHelper.ExcelToEntityList(new Dictionary() { { "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; } } }