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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Query.Implements
{
///
/// 控制信息查询服务实现
///
public sealed class WcsService : IWcsService
{
private readonly IMapper _mapper; // 模型映射
private readonly IDefaultDb _database; // 默认数据库
///
/// 依赖注入
///
public WcsService(
IMapper mapper,
IDefaultDb database)
{
_mapper = mapper;
_database = database;
}
/*
* 实现 Kean.Application.Query.Interfaces.IControlService.GetInstructionCount 方法
*/
public async Task GetInstructionCount(string[] warehouse, string direction, int? function, string device, string barcode, int? state, DateTime? beginFrom, DateTime? beginTo, DateTime? endFrom, DateTime? endTo)
{
return (await GetInstructionSchema(warehouse, direction, function, device, barcode, state, beginFrom, beginTo, endFrom, endTo)
.Single(c => new { Count = Function.Count(c.CONTROL_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IControlService.GetInstructionList 方法
*/
public async Task> GetInstructionList(string[] warehouse, string direction, int? function, string device, string barcode, int? state, DateTime? beginFrom, DateTime? beginTo, DateTime? endFrom, DateTime? endTo, string sort, int? offset, int? limit)
{
return _mapper.Map>(await GetInstructionSchema(warehouse, direction, function, device, barcode, state, beginFrom, beginTo, endFrom, endTo)
.Sort(sort, _mapper)
.OrderBy(c => c.BEGIN_TIME, Infrastructure.Database.Order.Descending)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetInstruction 相关方法的条件
*/
private ISchema GetInstructionSchema(string[] warehouse, string direction, int? function, string device, string barcode, int? state, DateTime? beginFrom, DateTime? beginTo, DateTime? endFrom, DateTime? endTo)
{
var schema = _database.From();
if (warehouse != null)
{
schema = schema.Where(c => warehouse.Contains(c.CONTROL_WAREHOUSE));
}
if (direction != null)
{
schema = schema.Where(c => c.CONTROL_DIRECTION == direction);
}
if (function.HasValue)
{
schema = schema.Where(c => c.CONTROL_FUNCTION == function.Value);
}
if (device != null)
{
schema = schema.Where(c => c.CONTROL_DEVICE.Contains(device));
}
if (barcode != null)
{
schema = schema.Where(c => c.STOCK_BARCODE.Contains(barcode));
}
if (state.HasValue)
{
schema = schema.Where(c => c.CONTROL_STATUS == state.Value);
}
if (beginFrom.HasValue)
{
schema = schema.Where(c => c.BEGIN_TIME >= beginFrom.Value);
}
if (beginTo.HasValue)
{
schema = schema.Where(c => c.BEGIN_TIME <= beginTo.Value.AddDays(1));
}
if (endFrom.HasValue)
{
schema = schema.Where(c => c.END_TIME >= endFrom.Value);
}
if (endTo.HasValue)
{
schema = schema.Where(c => c.END_TIME <= endTo.Value.AddDays(1));
}
return schema;
}
}
}