using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Model.Dto.WarehouseManagement;
using Model.Page;
using Model.WarehouseManagement;
using Service.Interface.WarehouseManagement;
using SqlSugar;
namespace GD.Service.WarehouseManagement
{
///
/// 商品类别
///
[AppService(ServiceType = typeof(ICategoryService), ServiceLifetime = LifeTime.Transient)]
public class CategoryService : BaseService
{
///
/// 新增商品类别
///
///
///
///
public long AddCategory(Category category, string userName)
{
category.Create_by = userName;
category.Create_time = DateTime.Now;
return InsertReturnBigIdentity(category);
}
///
/// 删除商品类别
///
///
///
public long DeleteByCategoryId(long categoryId)
{
return Delete(categoryId);
}
///
/// 编辑商品类别
///
///
///
///
public long EditCategory(Category category, string userName)
{
category.Update_by = userName;
category.Update_time = DateTime.Now;
return Update(category);
}
///
/// 分页获取所有商品类别
///
///
///
public PagedInfo
{
var expression = Expressionable.Create
.AndIF(!string.IsNullOrEmpty(categoryQueryDto.CreateBy), exp => exp.Create_by.Contains(categoryQueryDto.CreateBy))
.AndIF(categoryQueryDto.ParentId != 0, exp => exp.ParentId == categoryQueryDto.ParentId)
.AndIF(categoryQueryDto.BeginTime != DateTime.MinValue && categoryQueryDto.BeginTime != null, exp => exp.Create_time >= categoryQueryDto.BeginTime)
.AndIF(categoryQueryDto.EndTime != DateTime.MaxValue && categoryQueryDto.EndTime != null, exp => exp.Create_time <= categoryQueryDto.EndTime);
var queryResult = Queryable()
.Where(expression.ToExpression())
.ToList();
var rootId = queryResult
.Where(category =>
{
if (string.IsNullOrEmpty(categoryQueryDto.CategoryName))
{
return false;
}
else
{
//return category.CategoryName.Contains(categoryQueryDto.CategoryName);
return category.CategoryName.FuzzyMatch(categoryQueryDto.CategoryName);
}
})
.FirstOrDefault()?
.CategoryId;
var buildResult = BuildCategoryTree(queryResult, rootId);
if (!string.IsNullOrEmpty(categoryQueryDto.CategoryName)&&rootId==null)
{
buildResult = null;
}
int pageSize = categoryQueryDto.PageSize,
pageNum = categoryQueryDto.PageNum,
totalNum = buildResult?.Count??0;
var pagedResult = buildResult?
.Skip((pageNum - 1) * pageSize)
.Take(pageSize)
.ToList();
//var queryResult = Queryable()
// .Where(expression.ToExpression())
// .ToTree(it => it.ChildCategories, it => it.ParentId, categoryQueryDto.ParentId, it => it.CategoryId);
//int pageSize = categoryQueryDto.PageSize,
// pageNum = categoryQueryDto.PageNum,
// totalNum = queryResult.Count;
//var pagedResult = queryResult
// .Skip((pageNum - 1) * pageSize)
// .Take(pageSize)
// .ToList();
return new PagedInfo
{
PageIndex = pageNum,
PageSize = pageSize,
TotalNum = totalNum,
Result = pagedResult
};
}
public List
{
var queryResult = Queryable()
.ToList();
//return BuildCategoryTree(queryResult);
return queryResult;
}
public List
{
var queryResult = Queryable()
.ToList();
return BuildCategoryTree(queryResult);
}
public Category GetCategoryById(long categoryId)
{
return Queryable()
.Where(epx => epx.CategoryId == categoryId)
.Single();
}
public bool IsExistChild(long categoryId)
{
return Queryable()
.Where(category => category.ParentId == categoryId)
.Any();
}
public bool IsOtherUse(long categoryId)
{
throw new NotImplementedException();
}
///
/// 构建商品类别树
///
///
///
private List
{
var categoryId = rootId != null ? rootId : categories.Min(category => category.ParentId);
var rootCategories = categories
.Where(category => category.ParentId == categoryId)
.ToList();
foreach (var item in rootCategories)
{
item.ChildCategories = RecursionSetSub(item, categories);
}
return rootCategories;
}
///
/// 递归构建商品类别树
///
/// 父商品类别
/// 商品类别列表
///
private List
{
var subCategories = categories
.Where(predicate => predicate.ParentId == category.CategoryId)
.ToList();
foreach (var item in subCategories)
{
item.ChildCategories = RecursionSetSub(item, categories);
}
return subCategories;
}
}
}