| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Linq.Dynamic.Core;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Web.Http;
- using System.Web.Mvc;
- using Abp.Application.Services.Dto;
- using Abp.Auditing;
- using Abp.Authorization;
- using Abp.Domain.Repositories;
- using Abp.Extensions;
- using Abp.Timing;
- using Abp.UI;
- using IwbZero.AppServiceBase;
- using IwbZero.IdentityFramework;
- using IwbZero.Setting;
- using Newtonsoft.Json;
- using ShwasherSys.Authorization.Permissions;
- using ShwasherSys.BaseSysInfo;
- using ShwasherSys.BaseSysInfo.States.Dto;
- using ShwasherSys.Common;
- using ShwasherSys.CustomerInfo;
- using ShwasherSys.Lambda;
- using ShwasherSys.Order;
- using ShwasherSys.ProductInfo.Dto;
- namespace ShwasherSys.ProductInfo
- {
- [AbpAuthorize]
- public class ProductsAppService : ShwasherAsyncCrudAppService<Product, ProductDto, string, PagedRequestDto, ProductCreateDto, ProductUpdateDto >, IProductsAppService
- {
- protected IRepository<CustomerDefaultProduct> CustomerDefaultProductRepository;
- protected IRepository<SemiProducts,string> SemiProductsRepository { get; set; }
- protected IRepository<OrderItem,int> OrderItemRepository { get; set; }
- protected IRepository<BusinessLog> BusinessLogRepository;
- public ProductsAppService(IRepository<Product, string> repository, IRepository<CustomerDefaultProduct> customerDefaultProductRepository, IIwbSettingManager settingManager, IRepository<SemiProducts, string> semiProductsRepository, IRepository<OrderItem, int> orderItemRepository, IRepository<BusinessLog> businessLogRepository) : base(repository,"Id")
- {
- CustomerDefaultProductRepository = customerDefaultProductRepository;
- SettingManager = settingManager;
- SemiProductsRepository = semiProductsRepository;
- OrderItemRepository = orderItemRepository;
- BusinessLogRepository = businessLogRepository;
- }
- protected override bool KeyIsAuto { get; set; } = false;
- protected override string GetPermissionName { get; set; }= PermissionNames.PagesProductInfoProducts;
- protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesProductInfoProducts;
- protected override string CreatePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsCreate;
- protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsUpdate;
- protected override string DeletePermissionName { get; set; } = PermissionNames.PagesProductInfoProductsDelete;
- [AbpAuthorize(PermissionNames.PagesProductInfoProductsCreate)]
- public override async Task<ProductDto> Create(ProductCreateDto input)
- {
- string sEntity = JsonConvert.SerializeObject(input);
- var entity = await SemiProductsRepository.FirstOrDefaultAsync(i => i.Id == input.Id);
- if (entity == null)
- {
- var semiProducts = JsonConvert.DeserializeObject<SemiProducts>(sEntity);
- semiProducts.IsLock = "N";
- semiProducts.SemiProductName = input.ProductName;
- semiProducts.TimeLastMod = Clock.Now;
- semiProducts.UserIDLastMod = AbpSession.UserName;
- semiProducts.TimeCreated = Clock.Now;
- SemiProductsRepository.Insert(semiProducts);
- }
- return await base.Create(input);
- }
- [AbpAuthorize(PermissionNames.PagesProductInfoProductsUpdate)]
- public override async Task<ProductDto> Update(ProductUpdateDto input)
- {
- var entity = await SemiProductsRepository.FirstOrDefaultAsync(i => i.Id == input.Id);
- string sEntity = JsonConvert.SerializeObject(input);
- var semiProducts = JsonConvert.DeserializeObject<SemiProducts>(sEntity);
- if (entity != null)
- {
- entity.SemiProductName = input.ProductName;
- entity.TimeLastMod = Clock.Now;
- entity.UserIDLastMod = AbpSession.UserName;
- entity.IsStandard = input.IsStandard;
- entity.ProductDesc = input.ProductDesc;
- entity.TranUnitValue = input.TranUnitValue;
- entity.PartNo = input.PartNo;
- SemiProductsRepository.Update(entity);
- }
- else
- {
- semiProducts.IsLock = "N";
- semiProducts.SemiProductName = input.ProductName;
- semiProducts.TimeLastMod = Clock.Now;
- semiProducts.UserIDLastMod = AbpSession.UserName;
- semiProducts.TimeCreated = Clock.Now;
- SemiProductsRepository.Insert(semiProducts);
- }
- return await base.Update(input);
- }
- [AbpAuthorize(PermissionNames.PagesProductInfoProductsDelete)]
- public override async Task Delete(EntityDto<string> input)
- {
- var entityItem = await OrderItemRepository.FirstOrDefaultAsync(i => i.ProductNo == input.Id);
- if (entityItem != null)
- {
- throw new UserFriendlyException("该产品已经在订单中使用!不可进行删除!");
- }
- BusinessLogTypeEnum.Product.WriteLog(BusinessLogRepository, "产品删除", $"产品删除成功!ProductNo:{input.Id},userName:{AbpSession.UserName}");
- await base.Delete(input);
- }
- [DisableAuditing]
- public List<SelectListItem> GetProductPropertyList(string pcPropertyName)
- {
- var objList = new List<SelectListItem>();
- if (pcPropertyName.IsNullOrEmpty())
- {
- return objList;
- }
- var entitys = Repository.GetAll().Where(i => i.IsLock == "N");
- var loPropertyInfo = typeof(Product).GetProperty(pcPropertyName,
- BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
- if (loPropertyInfo == null)
- {
- throw new ArgumentException($"{pcPropertyName} is not a property of type {typeof(Product)}.");
- }
- IQueryable<string> query = null;
- switch (pcPropertyName)
- {
- case "Material":
- query = entitys.Select(i => i.Material).Distinct();
- break;
- case "SurfaceColor":
- query = entitys.Select(i => i.SurfaceColor).Distinct();
- break;
- case "Rigidity":
- query = entitys.Select(i => i.Rigidity).Distinct();
- break;
- }
- //var r = entitys.Select(i => i.Material).Distinct();
- //var query = entitys.Distinct(new PropertyComparer<Product>(pcPropertyName));//entitys.Distinct(new PropertyComparer<Product>(pcPropertyName));
- if (query != null)
- {
- foreach (var product in query)
- {
- objList.Add(new SelectListItem()
- {
- Text = product,
- Value = product
- });
- }
- }
-
- return objList;
- }
-
- [DisableAuditing]
- public async Task<PagedResultDto<ProductDto>> GetQueryCustomerDefaultProduct(PagedRequestDto input)
- {
- CheckGetAllPermission();
- var query = CreateFilteredQuery(input);
- query = query.Where(i => i.IsLock == "N");
- string lcCustomerId = "";
- if (input.SearchList != null && input.SearchList.Count > 0)
- {
- List<LambdaObject> objList = new List<LambdaObject>();
- foreach (var o in input.SearchList)
- {
- if (o.KeyWords.IsNullOrEmpty())
- continue;
- object keyWords = o.KeyWords;
- if (o.KeyField == "CustomerId"|| o.KeyField == "customerId")
- {
- lcCustomerId = keyWords + "";
- continue;
- }
- objList.Add(new LambdaObject
- {
- FieldType = (LambdaFieldType)o.FieldType,
- FieldName = o.KeyField,
- FieldValue = keyWords,
- ExpType = (LambdaExpType)o.ExpType
- });
- }
- var exp = objList.GetExp<Product>();
- if (exp != null)
- {
- query = query.Where(exp);
- }
-
- }
- List<string> loNotContain = new List<string>();
- if (!lcCustomerId.IsNullOrEmpty())
- {
- var loDefualtProducts = CustomerDefaultProductRepository.GetAll().Where(i => i.CustomerId == lcCustomerId);
- foreach (CustomerDefaultProduct defaultProduct in loDefualtProducts)
- {
- loNotContain.Add(defaultProduct.ProductNo);
- }
- }
- if (loNotContain.Any())
- {
- query = query.Where(i => !loNotContain.Contains(i.Id));
- }
-
- var totalCount = await AsyncQueryableExecuter.CountAsync(query);
- query = ApplySorting(query, input);
- query = ApplyPaging(query, input);
- var entities = await AsyncQueryableExecuter.ToListAsync(query);
- var dtos = new PagedResultDto<ProductDto>(
- totalCount,
- entities.Select(MapToEntityDto).ToList()
- );
- return dtos;
- }
- [AbpAuthorize(PermissionNames.PagesProductInfoProductsExportExcel)]
- public async Task<string> ExportExcel(List<MultiSearchDto> input)
- {
- var query = Repository.GetAll();
- query = query.Where(i => i.IsLock == "N");
- if (input != null && input.Count > 0)
- {
- List<LambdaObject> objList = new List<LambdaObject>();
- foreach (var o in input)
- {
- if (o.KeyWords.IsNullOrEmpty())
- 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<Product>();
- if (exp != null)
- {
- query = query.Where(exp);
- }
- }
- query = query.OrderByDescending(i => i.Id);
- var entities = await AsyncQueryableExecuter.ToListAsync(query);
- string downloadUrl = await SettingManager.GetSettingValueAsync("SYSTEMDOWNLOADPATH");
- string lcFilePath = System.Web.HttpRuntime.AppDomainAppPath + "\\" +
- downloadUrl;
- List<ToExcelObj> columnsList = new List<ToExcelObj>()
- {
- new ToExcelObj(){MapColumn = "Id",ShowColumn = "成品编号"},
- new ToExcelObj(){MapColumn = "ProductName",ShowColumn = "名称"},
- new ToExcelObj(){MapColumn = "SurfaceColor",ShowColumn = "表色"},
- new ToExcelObj(){MapColumn = "Model",ShowColumn = "规格"},
- new ToExcelObj(){MapColumn = "Rigidity",ShowColumn = "硬度"},
- new ToExcelObj(){MapColumn = "Material",ShowColumn = "材质"},
- new ToExcelObj(){MapColumn = "Defprice",ShowColumn = "默认价格"},
- };
- string lcResultFileName = ExcelHelper.ToExcel2003(columnsList, entities, "sheet", lcFilePath);
- return Path.Combine(downloadUrl, lcResultFileName);
-
- }
- }
- }
|