using AutoMapper;
using Kean.Application.Query.Interfaces;
using Kean.Application.Query.ViewModels;
using Kean.Infrastructure.Database;
using Kean.Infrastructure.Database.Repository.Default;
using Kean.Infrastructure.Database.Repository.Default.Entities;
using Kean.Infrastructure.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Query.Implements
{
///
/// 物料信息查询服务实现
///
public sealed class MaterialService : IMaterialService
{
private readonly IMapper _mapper; // 模型映射
private readonly IDefaultDb _database; // 默认数据库
///
/// 依赖注入
///
public MaterialService(
IMapper mapper,
IDefaultDb database)
{
_mapper = mapper;
_database = database;
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetCategoryTree 方法
*/
public async Task> GetCategoryTree()
{
return new Tree(_mapper.Map>(await _database.From()
.Where(c => c.CLASS_FLAG == true)
.Select()), "Id", "Parent");
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetCategorys 方法
*/
public async Task> GetCategorys()
{
return _mapper.Map>(await _database.From().Select());
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetMaterialItem 方法
*/
public async Task GetMaterialItem(int id)
{
return _mapper.Map(await _database.From()
.Where(g => g.GOODS_ID == id && g.GOODS_FLAG == true)
.Single());
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetMaterialCount 方法
*/
public async Task GetMaterialCount(string code, string name, string group, int[] category)
{
return (await GetMaterialSchema(code, name, group, category)
.Single(g => new { Count = Function.Count(g.GOODS_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetMaterialList 方法
*/
public async Task> GetMaterialList(string code, string name, string group, int[] category, string sort, int? offset, int? limit)
{
return _mapper.Map>(await GetMaterialSchema(code, name, group, category)
.Sort(sort, _mapper)
.OrderBy(r => r.GOODS_ID, Infrastructure.Database.Order.Ascending)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetMaterial 相关方法的条件
*/
private ISchema GetMaterialSchema(string code, string name, string group, int[] category)
{
var schema = _database.From()
.Where(g => g.GOODS_FLAG == true);
if (code != null)
{
schema = schema.Where(g => g.GOODS_CODE.Contains(code));
}
if (name != null)
{
schema = schema.Where(g => g.GOODS_NAME.Contains(name));
}
if (group != null)
{
schema = schema.Where(g => g.TYPE_ID == group);
}
if (category != null)
{
schema = schema.Where(g => category.Contains(g.CLASS_ID.Value));
}
return schema;
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetSafetyCount 方法
*/
public async Task GetSafetyCount(string code, string name, int? warehouse)
{
return (await GetSafetySchema(code, name, warehouse)
.Single(s => new { Count = Function.Count(s.SAFETY_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetSafetyList 方法
*/
public async Task> GetSafetyList(string code, string name, int? warehouse, string sort, int? offset, int? limit)
{
return _mapper.Map>(await GetSafetySchema(code, name, warehouse)
.Sort(sort, _mapper)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetSafety 相关方法的条件
*/
private ISchema GetSafetySchema(string code, string name, int? warehouse)
{
var schema = _database.From()
.Where(s => s.GOODS_FLAG == true);
if (code != null)
{
schema = schema.Where(s => s.GOODS_CODE.Contains(code));
}
if (name != null)
{
schema = schema.Where(s => s.GOODS_NAME.Contains(name));
}
if (warehouse != null)
{
var query = _database.From().Where(s => s.WAREHOUSE_ID == warehouse.Value).Query(s => s.SAFETY_ID);
schema = schema.Where(s => query.Contains(s.SAFETY_ID));
}
return schema;
}
/*
* 实现 Kean.Application.Query.Interfaces.IMaterialService.GetMaterial 方法
*/
public async Task GetMaterial(string code, bool bLike)
{
var schema = _database.From();
if (bLike)
{
schema.Where(r => r.GOODS_CODE.StartsWith(code));
}
else
{
schema.Where(r => r.GOODS_CODE == code);
}
var material = await schema.Single();
return _mapper.Map(material);
}
}
}