using System; using Microsoft.Extensions.Configuration; using Zxd.Core.Domain.Dto; using Zxd.Core.Domain.Response; using Zxd.Domain.Impl; using Zxd.Entity.Crm; namespace Zxd.Domain { /// /// 成品产品 /// public class FinishedProductDomain : IFinishedProductDomain { private readonly IBaseRepository _crmRepository; private readonly IBaseRepository _zxdRepository; private readonly IRedisManager _redisManager; private readonly IConfiguration _configuration; private readonly IDeptmentDomain _deptmentDomain; private readonly IHttpClient _httpClient; private readonly IMapper _mapper; private readonly SystemConfig _systemConfig; public FinishedProductDomain(IBaseRepository crmRepository, IBaseRepository zxdRepository, IRedisManager redisManager, IConfiguration configuration, IDeptmentDomain deptmentDomain, IMapper mapper, IHttpClient httpClient) { _crmRepository = crmRepository; _zxdRepository = zxdRepository; _redisManager = redisManager; _deptmentDomain = deptmentDomain; _mapper = mapper; _httpClient = httpClient; _configuration = configuration; _systemConfig = _configuration.GetSection("SystemConfig").Get(); } private async Task> GetProductTypeList() { var key = CacheKeys.ProductTypeList; var data = new List(); if (!await _redisManager.ExistsAsync(key)) { data = await _zxdRepository.GetRepository().QueryListAsync(); await _redisManager.SetAsync(key, data, TimeSpan.FromHours(1)); } else { data = await _redisManager.GetListAsync(key); } return data; } public async Task> GetProductSeriesSelect() { var list = await GetProductTypeList(); var data = new List(); list = list.Where(x => x.Level == 1).ToList(); foreach (var item in list) { data.Add(new SelectItem(item.ProductId, item.ProductName ?? "")); } return data; } public async Task> GetProductCategorySelect(int? productSeriesId) { var list = await GetProductTypeList(); var data = new List(); list = list.Where(x => x.Level == 2).If(productSeriesId != null, x => x.Where(x => x.Parentid == productSeriesId)).ToList(); foreach (var item in list) { data.Add(new SelectItem(item.ProductId, item.ProductName ?? "")); } return data; } /// /// 成品产品明细 /// /// /// /// public async Task GetFinishedProductDetail(int productid) { var finishProductDetail = await _zxdRepository.GetRepository().Query() .Where(x => x.Id == productid) .Select(x => new FinishedProductDetailDto { Id = x.Id, StandardProductId = x.StandardProductId, ProductSeriesId = x.ProductSeriesId, ProductCategoryId = x.ProductCategoryId, Day = x.Day, Deptid = x.Deptid, Discount = x.Discount, AutoOpen = x.AutoOpen, BuyOnline = x.BuyOnline, Coefficient = x.Coefficient, Custom = x.Custom, Give = x.Give, OpenCondition = x.OpenCondition, OtherClassify = x.OtherClassify, ProductCode = x.ProductCode, ProductName = x.ProductName, ProductPrice = x.ProducPrice, ProductServerType = x.ProductServerType, Remark = x.Remark, Upgrade = x.Upgrade, Status = x.Status, DiscounSections = x.DiscounSections, GiveProducts = x.GiveProducts, CustomPrices = x.CustomPrices }) .FirstOrDefaultAsync(); if (finishProductDetail == null) { throw new ApiException("成品产品不存在或已删除!"); } var standardProduct = await _zxdRepository.GetRepository().Query() .Where(x => x.Id == finishProductDetail.StandardProductId) .FirstOrDefaultAsync(); if (standardProduct == null) { throw new ApiException("基准产品不存在或已删除!"); } var productTypeList = await GetProductTypeList(); finishProductDetail.ProductSeries = productTypeList.Where(x => x.ProductId == finishProductDetail.ProductSeriesId).Select(x => x.ProductName).FirstOrDefault(); finishProductDetail.ProductCategory = productTypeList.Where(x => x.ProductId == finishProductDetail.ProductCategoryId).Select(x => x.ProductName).FirstOrDefault(); var deptments = await _deptmentDomain.GetDeptments(); finishProductDetail.Department = deptments.First(y => y.Id == finishProductDetail.Deptid).Title; if (!string.IsNullOrEmpty(standardProduct.ProductCode)) { var baseProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Code == standardProduct.ProductCode); finishProductDetail.StandardProductDay = baseProduct.Day.ToString(); finishProductDetail.StandardProductName = baseProduct.Name; finishProductDetail.StandardProductPrice = (decimal)baseProduct.Price; finishProductDetail.StandardProductType = standardProduct.StandardType.GetDescription(); finishProductDetail.StandardProductModules = new List { $"{baseProduct.Name}【{baseProduct.Moduleid}】【{baseProduct.Day}】" }; } if (!string.IsNullOrEmpty(standardProduct.ProductPackageCode)) { var baseProductPackage = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Code == standardProduct.ProductPackageCode); finishProductDetail.StandardProductDay = string.Join("/", baseProductPackage.BaseProductPackageRelations.Select(x => x.Day).ToList()); finishProductDetail.StandardProductName = baseProductPackage.Name; finishProductDetail.StandardProductPrice = (decimal)baseProductPackage.Price; finishProductDetail.StandardProductType = standardProduct.StandardType.GetDescription(); finishProductDetail.StandardProductModules = baseProductPackage.BaseProductPackageRelations.Select(x => $"{x.ProductName}【{x.Moduleid}】【{x.Day}】").ToList(); } return finishProductDetail; } /// /// 基准产品信息 /// /// /// /// public async Task GetStandardProduct(int productid) { var standardProduct = await _zxdRepository.GetRepository().Query() .Where(x => x.Id == productid) .FirstOrDefaultAsync(); if (standardProduct == null) { throw new ApiException("基准产品不存在或已删除!"); } if (!string.IsNullOrEmpty(standardProduct.ProductCode)) { var baseProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Code == standardProduct.ProductCode); return new StandardProductInfoDto { Price = $"{baseProduct.Price:0.00}", Day = baseProduct.Day.ToString(), ProductName = baseProduct.Name, ProductCode = baseProduct.Code, ProductType = standardProduct.StandardType.GetDescription(), Module = new List { $"{baseProduct.Name}【{baseProduct.Moduleid}】【{baseProduct.Day}】" } }; } if (!string.IsNullOrEmpty(standardProduct.ProductPackageCode)) { var baseProductPackage = await _zxdRepository.GetRepository().Query().Include(x => x.BaseProductPackageRelations) .FirstOrDefaultAsync(x => x.Code == standardProduct.ProductPackageCode); return new StandardProductInfoDto { Price = $"{baseProductPackage.Price:0.00}", Day = string.Join("/", baseProductPackage.BaseProductPackageRelations.Select(x => x.Day).ToList()), ProductName = baseProductPackage.Name, ProductCode = baseProductPackage.Code, ProductType = standardProduct.StandardType.GetDescription(), Module = baseProductPackage.BaseProductPackageRelations.Select(x => $"{x.ProductName}【{x.Moduleid}】【{x.Day}】").ToList() }; } return new StandardProductInfoDto(); } /// /// 修改成品产品 /// /// /// /// public async Task EditFinishedProduct(EditFinishedProductDto dto) { var finishedProduct = await _zxdRepository.GetRepository().Query() .Include(x => x.StandardProduct) .FirstOrDefaultAsync(x => x.Id == dto.Id); if (finishedProduct == null) { throw new ApiException("成品产品不存在或已删除!"); } //finishedProduct.ProductTypeId = dto.ProductTypeId; finishedProduct.ProductCategoryId = dto.ProductCategoryId; finishedProduct.ProductSeriesId = dto.ProductSeriesId; finishedProduct.ProductServerType = dto.ProductServerType; finishedProduct.AutoOpen = dto.AutoOpen; finishedProduct.BuyOnline = dto.BuyOnline; finishedProduct.Custom = dto.Custom; finishedProduct.Give = dto.Give; finishedProduct.Discount = dto.Discount; finishedProduct.OpenCondition = dto.OpenCondition; finishedProduct.OtherClassify = dto.OtherClassify; finishedProduct.Remark = dto.Remark; finishedProduct.Status = dto.Status; finishedProduct.Upgrade = dto.Upgrade; finishedProduct.CustomPrices = dto.CustomPrices; finishedProduct.DiscounSections = dto.DiscounSections; finishedProduct.GiveProducts = dto.GiveProducts; var productCode = !string.IsNullOrEmpty(finishedProduct.StandardProduct.ProductCode) ? finishedProduct.StandardProduct.ProductCode : finishedProduct.StandardProduct.ProductPackageCode; var addVirtualProduct = new AddVirtualProductDto { OutProductCode = productCode, Day = finishedProduct.Day, Price = finishedProduct.ProducPrice, Num = finishedProduct.Coefficient, ProductName = finishedProduct.ProductName, BasicPorductCode = productCode, ProductCode = finishedProduct.ProductCode }; if (!string.IsNullOrEmpty(finishedProduct.StandardProduct.ProductCode)) { var result = await _httpClient.PostAsync(_systemConfig.GetAddVirtualProduct(), addVirtualProduct); if (result.Result) { finishedProduct.ProductCode = result.ProductCode; } else { throw new ApiException($"优品接口请求错误:{result.Retmsg}"); } } else { var result = await _httpClient.PostAsync(_systemConfig.GetAddVirtualPackage(), addVirtualProduct); if (result.Result) { finishedProduct.ProductCode = result.ProductCode; } else { throw new ApiException($"优品接口请求错误:{result.Retmsg}"); } } await _zxdRepository.GetRepository().UpdateAsync(finishedProduct); } /// /// 创建成品产品 /// /// /// /// public async Task CreateFinishedProduct(CreateFinishedProductDto dto) { if (!dto.FinishedProductDetails.Any()) { throw new ApiException("请正确填写产品信息!"); } var product = await _crmRepository.GetRepository().Query() .Where(x => x.Id == dto.ProductCode) .FirstOrDefaultAsync(); var productPackage = await _zxdRepository.GetRepository().Query() .Where(x => x.Code == dto.ProductCode) .Include(x => x.BaseProductPackageRelations) .FirstOrDefaultAsync(); using var transaction = await _zxdRepository.BeginTransactionAsync(); try { foreach (var finishedProductDetail in dto.FinishedProductDetails) { var finishedProduct = new FinishedProduct { Deptid = finishedProductDetail.Deptid, Day = finishedProductDetail.Day, ProducPrice = finishedProductDetail.Price, ProductName = finishedProductDetail.ProductName, Coefficient = finishedProductDetail.Coefficient, Discount = dto.Discount, AutoOpen = dto.AutoOpen, BuyOnline = dto.BuyOnline, Custom = dto.Custom, CustomPrices = dto.CustomPrices, OtherClassify = dto.OtherClassify, DiscounSections = dto.DiscounSections, GiveProducts = dto.GiveProducts, Give = dto.Give, OpenCondition = dto.OpenCondition, ProductServerType = (ProductServerType)dto.ProductServerType, ProductCategoryId = dto.ProductCategoryId, ProductSeriesId = dto.ProductSeriesId, Remark = dto.Remark, StandardProductId = dto.StandardProductId, Status = (ProductStatus)dto.Status, Upgrade = dto.Upgrade, }; finishedProduct.Module = product != null ? product.Moduleid : productPackage != null ? string.Join(",", productPackage.BaseProductPackageRelations.Select(x => x.Moduleid).ToList()) : null; var addVirtualProduct = new AddVirtualProductDto { OutProductCode = dto.ProductCode, Day = finishedProduct.Day, Price = finishedProduct.ProducPrice, Num = finishedProduct.Coefficient, ProductName = finishedProduct.ProductName, BasicPorductCode = dto.ProductCode }; if (product != null) { var result = await _httpClient.PostAsync(_systemConfig.GetAddVirtualProduct(), addVirtualProduct); if (result.Result) { finishedProduct.ProductCode = result.ProductCode; } else { throw new ApiException($"优品接口请求错误:{result.Retmsg}"); } } else { var result = await _httpClient.PostAsync(_systemConfig.GetAddVirtualPackage(), addVirtualProduct); if (result.Result) { finishedProduct.ProductCode = result.ProductCode; } else { throw new ApiException($"优品接口请求错误:{result.Retmsg}"); } } await _zxdRepository.GetRepository().InsertAsync(finishedProduct); } await transaction.CommitAsync(); } catch (Exception ex) { await transaction.RollbackAsync(); await transaction.DisposeAsync(); Log.Error(ex, "批量添加成品产品错误!"); throw; } } /// /// 成品产品分页 /// /// /// public async Task> FinishedProductPage(SearchFinishedProductDto dto) { var query = _zxdRepository.GetRepository().Query() .If(dto.Deptid != null, x => x.Where(x => x.Deptid == dto.Deptid)) .If(!string.IsNullOrEmpty(dto.Name), x => x.Where(x => x.ProductName.Contains(dto.Name))) .If(dto.Price != null, x => x.Where(x => x.ProducPrice == dto.Price)) .If(!string.IsNullOrEmpty(dto.ProductCode), x => x.Where(x => x.ProductCode == dto.ProductCode)) .If(dto.Day != null, x => x.Where(x => x.Day.Contains(dto.Day.Value.ToString()))) .If(!string.IsNullOrEmpty(dto.Module), x => x.Where(x => x.Module.Contains(dto.Module))) .If(dto.Status != null, x => x.Where(x => x.Status == dto.Status)) .Include(x => x.StandardProduct); var total = await query.CountAsync(); var deptments = await _deptmentDomain.GetDeptments(); var data = await query .Select(x => new FinishedProductDto { Id = x.Id, Deptid = x.Deptid, ProducDay = x.Day, AutoOpen = x.AutoOpen ? "是" : "否", ProducPrice = x.ProducPrice, ProductName = x.ProductName, ProductServerType = x.ProductServerType.GetDescription(), StandardProductId = x.StandardProductId, ProductCategoryId = x.ProductCategoryId, ProductSeriesId = x.ProductSeriesId, Status = x.Status.GetDescription() }) .Skip((dto.PageIndex - 1) * dto.PageSize) .Take(dto.PageSize) .ToListAsync(); var productTypeList = await GetProductTypeList(); var standardProductList = await _zxdRepository.GetRepository().Query() .Where(x => data.Select(x => x.StandardProductId).Contains(x.Id)) .ToListAsync(); var productCodes = standardProductList.Where(x => !string.IsNullOrEmpty(x.ProductCode)).Select(x => x.ProductCode).ToList(); var productPackageCodes = standardProductList.Where(x => !string.IsNullOrEmpty(x.ProductPackageCode)).Select(x => x.ProductPackageCode).ToList(); var baseProductList = await _zxdRepository.GetRepository().Query() .Where(x => productCodes.Contains(x.Code)).ToListAsync(); var baseProductPackageList = await _zxdRepository.GetRepository().Query() .Where(x => productPackageCodes.Contains(x.Code)).Include(x => x.BaseProductPackageRelations).ToListAsync(); data.ForEach(x => { x.Department = deptments.First(y => y.Id == x.Deptid).Title; x.ProductSeries = productTypeList.Where(y => y.ProductId == x.ProductSeriesId).Select(y => y.ProductName).FirstOrDefault(); x.ProductCategory = productTypeList.Where(y => y.ProductId == x.ProductCategoryId).Select(y => y.ProductName).FirstOrDefault(); var standardProduct = standardProductList.First(y => y.Id == x.StandardProductId); if (!string.IsNullOrEmpty(standardProduct.ProductCode)) { var baseProduct = baseProductList.First(y => y.Code == standardProduct.ProductCode); x.StandardProductModules = new List { $"{baseProduct.Name}【{baseProduct.Moduleid}】【{baseProduct.Day}】" }; } if (!string.IsNullOrEmpty(standardProduct.ProductPackageCode)) { var baseProductPackage = baseProductPackageList.First(y => y.Code == standardProduct.ProductCode); x.StandardProductModules = baseProductPackage.BaseProductPackageRelations.Select(x => $"{x.ProductName}【{x.Moduleid}】【{x.Day}】").ToList(); } }); return new PageResult(dto.PageIndex, dto.PageSize, total, data); } /// /// 获取赠送产品设置 /// /// /// public async Task> GetActives(string? productCode) { var finishedProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.ProductCode == productCode); if (finishedProduct == null) { throw new ApiException("成品产品不存在或已删除!"); } var activeList = await _zxdRepository.GetRepository().Query() .Where(x => x.ProductCode == finishedProduct.ProductCode) .Where(x => x.Isdelete == 0) .ToListAsync(); if (activeList == null || !activeList.Any()) return new List(); var activeids = activeList.Select(x => x.Id).ToList(); var productGiftList = await _zxdRepository.GetRepository().Query() .Where(x => x.SubProductId == finishedProduct.Id) .Where(x => activeids.Contains(x.Activeid)) .OrderByDescending(x => x.Id) .Select(x => new SubProductGiftDto { GiftDays = x.GiftDays, Activeid = x.Activeid, GiftDaysName = x.GiftDaysName, GiftName = x.GiftName, Id = x.Id, IsDefault = x.IsDefault, Sort = x.Sort, SubProductId = x.SubProductId, SubProductName = x.SubProductName, Type = x.Type }).ToListAsync(); var data = activeList.Select(x => new ProductActiveDto { Id = x.Id, ActiveCode = x.ActiveCode, Name = x.Activename, ActiveType = x.ActiveType, GiftType = string.IsNullOrEmpty(x.Giftype) ? "" : x.Giftype.Replace("[Order]", "【下单时赠送】").Replace("[HandGif]", "【下单后赠送】"), Giftype = x.Giftype, Deptid = x.Deptid, IsFollowOrder = x.IsFollowOrder, GiftList = productGiftList.Where(y => y.Activeid == x.Id).ToList() }).ToList(); return data; } /// /// 创建或修改成品产品的赠送产品 /// /// /// public async Task CreateOrEditActive(CreateOrEditActiveDto dto) { if (dto.Id != null && dto.Id > 0) { var active = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Id == dto.Id); active.IsFollowOrder = dto.IsFollowOrder; active.Giftype = dto.GifType; await _zxdRepository.GetRepository().UpdateAsync(active); return; } var finishedProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.ProductCode == dto.ProductCode); if (finishedProduct == null) { throw new ApiException("成品产品不存在或已删除!"); } var giveProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.ProductCode == dto.ActiveCode); if (giveProduct == null) { throw new ApiException("赠送的成品产品不存在或已删除!"); } var orderActive = new WxOrderActive { ProductName = finishedProduct.ProductName, Activename = giveProduct.ProductName, IsFollowOrder = dto.IsFollowOrder, ProductCode = dto.ProductCode, Deptid = dto.Deptid, ActiveCode = dto.ActiveCode, MinCount = 1, ProductId = finishedProduct.Id, Isdelete = 0, Giftype = dto.GifType, }; await _zxdRepository.GetRepository().InsertAsync(orderActive); } /// /// 删除品产品的赠送产品 /// /// /// /// public async Task DeleteActive(int? id) { if (id == null || id == 0) { throw new ApiException("id不能为空或者为0"); } var active = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Id == id); active.Isdelete = 1; await _zxdRepository.GetRepository().UpdateAsync(active); } /// /// 创建或修改赠送产品 /// /// /// /// public async Task CreateOrEditProductGift(CreateOrEditProducGiftDto dto) { if (dto.Id != null && dto.Id > 0) { var productGift = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Id == dto.Id); productGift.GiftDays = dto.GiftDays; productGift.GiftDaysName = dto.GiftDays == 0 ? "不赠送" : $"赠送{dto.GiftDays}天"; productGift.Sort = dto.Sort; productGift.IsDefault = dto.IsDefault; productGift.GiftName = dto.GiftName; await _zxdRepository.GetRepository().UpdateAsync(productGift); return; } var deptments = await _deptmentDomain.GetDeptments(); var active = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Id == dto.Activeid); var deptment = deptments.FirstOrDefault(x => x.Id == active.Deptid); if (deptment == null || deptment.DeptmentCampains == null || !deptment.DeptmentCampains.Any()) { throw new ApiException($"找不到deptid:{active.Deptid} 的事业部"); } var finishedProduct = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.ProductCode == active.ProductCode); var subProductGift = new WxSzzySubProductGift { Sort = dto.Sort, Deptid = active.Deptid, GiftDays = dto.GiftDays, SubProductId = finishedProduct.Id, GiftDaysName = dto.GiftDays == 0 ? "不赠送" : $"赠送{dto.GiftDays}天", IsDefault = dto.IsDefault, Activeid = dto.Activeid, Channel = deptment.DeptmentCampains.First().StartCampainId, GiftName = dto.GiftName, SubProductName = finishedProduct.ProductName, Type = 1 }; await _zxdRepository.GetRepository().InsertAsync(subProductGift); } /// /// 删除赠送产品 /// /// /// /// public async Task DeleteProductGift(int? id) { if (id == null || id == 0) { throw new ApiException("id不能为空或者为0"); } var productGift = await _zxdRepository.GetRepository().FirstOrDefaultAsync(x => x.Id == id); await _zxdRepository.GetRepository().DeleteAsync(productGift); } } }