266 lines
12 KiB
C#
266 lines
12 KiB
C#
using System;
|
|
using Zxd.Core.Domain.Impl;
|
|
|
|
namespace Zxd.Core.Domain
|
|
{
|
|
public class StandardProductDomain : IStandardProductDomain
|
|
{
|
|
private readonly IBaseRepository<CrmDbContext> _crmRepository;
|
|
private readonly IBaseRepository<ZxdDbContext> _zxdRepository;
|
|
private readonly IRedisManager _redisManager;
|
|
private readonly IDeptmentDomain _deptmentDomain;
|
|
private readonly IMapper _mapper;
|
|
|
|
public StandardProductDomain(IBaseRepository<CrmDbContext> crmRepository,
|
|
IBaseRepository<ZxdDbContext> zxdRepository,
|
|
IRedisManager redisManager,
|
|
IDeptmentDomain deptmentDomain,
|
|
IMapper mapper)
|
|
{
|
|
_crmRepository = crmRepository;
|
|
_zxdRepository = zxdRepository;
|
|
_redisManager = redisManager;
|
|
_deptmentDomain = deptmentDomain;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<List<SelectItem>> GetProductSelect()
|
|
{
|
|
var key = CacheKeys.ProductList;
|
|
var data = new List<SelectItem>();
|
|
if (!await _redisManager.ExistsAsync(key))
|
|
{
|
|
var list = await _crmRepository.GetRepository<Product>().QueryListAsync();
|
|
foreach (var item in list)
|
|
{
|
|
data.Add(new SelectItem(item.Id ?? "", $"{item.Name}/{item.Moduleid}/{item.Day}/{item.Price}"));
|
|
}
|
|
await _redisManager.SetAsync(key, data, TimeSpan.FromHours(1));
|
|
}
|
|
else
|
|
{
|
|
data = await _redisManager.GetListAsync<SelectItem>(key);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<SelectItem>> GetStandardWaySelect()
|
|
{
|
|
var key = CacheKeys.StandardWay;
|
|
var data = new List<SelectItem>();
|
|
if (!await _redisManager.ExistsAsync(key))
|
|
{
|
|
foreach (StandardWay item in Enum.GetValues(typeof(StandardWay)))
|
|
{
|
|
data.Add(new SelectItem(item, item.GetDescription()));
|
|
}
|
|
await _redisManager.SetAsync(key, data, TimeSpan.FromDays(1));
|
|
}
|
|
else
|
|
{
|
|
data = await _redisManager.GetListAsync<SelectItem>(key);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<BaseProductInfoDto> GetBaseProduct(string code)
|
|
{
|
|
if (string.IsNullOrEmpty(code))
|
|
{
|
|
throw new ApiException("产品编码不能为空!");
|
|
}
|
|
|
|
var product = await _crmRepository.GetRepository<Product>().Query()
|
|
.Include(x => x.ProductGroup)
|
|
.Include(x => x.Module)
|
|
.FirstOrDefaultAsync(x => x.Id == code);
|
|
var productPackage = await _crmRepository.GetRepository<ProductPackage>().Query()
|
|
.Include(x => x.ProductGroup)
|
|
.FirstOrDefaultAsync(x => x.Id == code);
|
|
if (product == null && productPackage == null)
|
|
{
|
|
throw new ApiException("产品不存在或已删除!");
|
|
}
|
|
|
|
if (product != null)
|
|
{
|
|
return new BaseProductInfoDto
|
|
{
|
|
Code = product.Id,
|
|
Day = product.Day ?? 0,
|
|
Price = (decimal)product.Price,
|
|
ProductName = product.Name ?? "",
|
|
ProductProperty = "基础产品",
|
|
ProductType = product.ProductGroup?.Name ?? "",
|
|
Status = product.Active == 1 ? "已上线" : "未上线",
|
|
ProductModules = new List<string>() { product.Moduleid ?? "" }
|
|
};
|
|
}
|
|
|
|
if (productPackage != null)
|
|
{
|
|
var productCodes = string.IsNullOrEmpty(productPackage.SubProducts) ? new List<string>()
|
|
: productPackage.SubProducts.Split('|').ToList();
|
|
var data = new BaseProductInfoDto
|
|
{
|
|
Code = productPackage.Id,
|
|
Price = (decimal)productPackage.Price,
|
|
ProductName = productPackage.Name ?? "",
|
|
ProductProperty = "组合产品",
|
|
ProductType = productPackage.ProductGroup?.Name ?? "",
|
|
Status = productPackage.Active == 1 ? "已上线" : "未上线"
|
|
};
|
|
if (productCodes.Any())
|
|
{
|
|
var productList = await _crmRepository.GetRepository<Product>().Query()
|
|
.Where(x => productCodes.Contains(x.Id))
|
|
.ToListAsync();
|
|
data.Day = productList.Sum(x => x.Day ?? 0);
|
|
data.ProductModules = productList.Select(x => x.Moduleid ?? "").ToList();
|
|
}
|
|
return data;
|
|
}
|
|
|
|
return new BaseProductInfoDto();
|
|
}
|
|
|
|
public async Task CreateStandardProduct(CreateStandardProductDto dto)
|
|
{
|
|
if (string.IsNullOrEmpty(dto.ProductCode))
|
|
{
|
|
throw new ApiException("产品编码不能为空!");
|
|
}
|
|
var code = dto.ProductCode;
|
|
var product = await _crmRepository.GetRepository<Product>().Query()
|
|
.Include(x => x.ProductGroup)
|
|
.Include(x => x.Module)
|
|
.FirstOrDefaultAsync(x => x.Id == code);
|
|
var productPackage = await _crmRepository.GetRepository<ProductPackage>().Query()
|
|
.Include(x => x.ProductGroup)
|
|
.FirstOrDefaultAsync(x => x.Id == code);
|
|
if (product == null && productPackage == null)
|
|
{
|
|
throw new ApiException("产品不存在或已删除!");
|
|
}
|
|
var standardProduct = new StandardProduct
|
|
{
|
|
ProductCode = product != null ? dto.ProductCode : null,
|
|
ProductPackageCode = productPackage != null ? dto.ProductCode : null,
|
|
StandardType = (StandardType)dto.StandardType,
|
|
Way = dto.StandardWay,
|
|
CreateTime = DateTime.Now
|
|
};
|
|
|
|
if (product != null)
|
|
{
|
|
// todo 调用优品接口
|
|
}
|
|
else
|
|
{
|
|
// todo 调用优品接口
|
|
}
|
|
|
|
await _zxdRepository.GetRepository<StandardProduct>().InsertAsync(standardProduct);
|
|
}
|
|
|
|
public async Task<PageResult<StandardProductDto>> StandardProductPage(SearchStandardProductDto dto)
|
|
{
|
|
var productCodes = new List<string>();
|
|
var productPackageCodes = new List<string>();
|
|
if (!string.IsNullOrEmpty(dto.ProductName))
|
|
{
|
|
productCodes = await _crmRepository.GetRepository<Product>().Query()
|
|
.Where(x => x.Name.Contains(dto.ProductName))
|
|
.Select(x => x.Id)
|
|
.ToListAsync();
|
|
|
|
productPackageCodes = await _crmRepository.GetRepository<ProductPackage>().Query()
|
|
.Where(x => x.Name.Contains(dto.ProductName))
|
|
.Select(x => x.Id)
|
|
.ToListAsync();
|
|
}
|
|
var query = _zxdRepository.GetRepository<StandardProduct>().Query()
|
|
.If(dto.Id != null, x => x.Where(x => x.Id == dto.Id))
|
|
.If(dto.StandardType != null, x => x.Where(x => x.StandardType == (StandardType)dto.StandardType))
|
|
.If(productCodes.Any(), x => x.Where(x => productCodes.Contains(x.ProductCode)))
|
|
.If(productPackageCodes.Any(), x => x.Where(x => productPackageCodes.Contains(x.ProductPackageCode)))
|
|
.Include(x => x.FinishedProducts);
|
|
|
|
var total = await query.CountAsync();
|
|
var data = await query
|
|
.Select(x => new StandardProductDto
|
|
{
|
|
Id = x.Id,
|
|
CreateTime = x.CreateTime,
|
|
StandardProductType = ((StandardType)x.StandardType).GetDescription(),
|
|
ProductCode = string.IsNullOrEmpty(x.ProductCode) ? x.ProductPackageCode : x.ProductCode,
|
|
ProductProperty = string.IsNullOrEmpty(x.ProductCode) ? "组合产品" : "基础产品",
|
|
FinishedProductCount = x.FinishedProducts == null ? 0 : x.FinishedProducts.Count
|
|
})
|
|
.Skip((dto.PageIndex - 1) * dto.PageSize)
|
|
.Take(dto.PageSize)
|
|
.ToListAsync();
|
|
|
|
var productList = new List<Product>();
|
|
var productPackageList = new List<ProductPackage>();
|
|
var productPackageProductList = new List<Product>();
|
|
if (data.Any())
|
|
{
|
|
productList = await _crmRepository.GetRepository<Product>().Query()
|
|
.Where(x => data.Select(y => y.ProductCode).Contains(x.Id))
|
|
.Include(x => x.Module)
|
|
.Include(x => x.ProductGroup)
|
|
.ToListAsync();
|
|
productPackageList = await _crmRepository.GetRepository<ProductPackage>().Query()
|
|
.Where(x => data.Select(y => y.ProductCode).Contains(x.Id))
|
|
.Include(x => x.ProductGroup)
|
|
.ToListAsync();
|
|
if (productPackageList.Any())
|
|
{
|
|
var productPackageProductCodes = new List<string>();
|
|
productPackageList.ForEach(x =>
|
|
{
|
|
if (x.SubProductCodes != null && x.SubProductCodes.Any())
|
|
{
|
|
productPackageCodes.AddRange(x.SubProductCodes);
|
|
}
|
|
});
|
|
productPackageProductList = await _crmRepository.GetRepository<Product>().Query()
|
|
.Where(x => productPackageProductCodes.Contains(x.Id))
|
|
.Include(x => x.Module)
|
|
.Include(x => x.ProductGroup)
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
data.ForEach(x =>
|
|
{
|
|
var product = productList.FirstOrDefault(y => y.Id == x.ProductCode);
|
|
var productPackage = productPackageList.FirstOrDefault(y => y.Id == x.ProductCode);
|
|
if (product != null)
|
|
{
|
|
x.Day = product.Day.ToString();
|
|
x.Price = $"{product.Price:0:00}元";
|
|
x.StandardProductName = product.Name;
|
|
x.Status = product.Active == 1 ? "已上线" : "未上线";
|
|
x.StandardProductModule = new List<string> { $"{product.Module.Name}【{product.Moduleid}】【{product.Day}】" };
|
|
}
|
|
if (productPackage != null)
|
|
{
|
|
var productPackageProducts = productPackageProductList
|
|
.Where(y => productPackage.SubProductCodes.Contains(y.Id)).ToList();
|
|
x.Day = string.Join("/", productPackageProducts.Select(x => x.Day).ToList());
|
|
x.Price = $"{productPackage.Price:0:00}元";
|
|
x.StandardProductName = productPackage.Name;
|
|
x.Status = productPackage.Active == 1 ? "已上线" : "未上线";
|
|
x.StandardProductModule = productPackageProducts.Select(y => $"{y.Module.Name}【{y.Moduleid}】【{y.Day}】").ToList();
|
|
}
|
|
});
|
|
|
|
return new PageResult<StandardProductDto>(dto.PageIndex, dto.PageSize, total, data);
|
|
}
|
|
}
|
|
}
|
|
|