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.Database.Repository.Default.Entities.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Query.Implements
{
///
/// 库房查询服务实现
///
public class WarehouseService : IWarehouseService
{
private readonly IMapper _mapper; // 模型映射
private readonly IDefaultDb _database; // 默认数据库
///
/// 依赖注入
///
public WarehouseService(
IMapper mapper,
IDefaultDb database)
{
_mapper = mapper;
_database = database;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetWarehouseCount 方法
*/
public async Task GetWarehouseCount()
{
return (await _database.From()
.Single(w => new { Count = Function.Count(w.WAREHOUSE_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetWarehouseList 方法
*/
public async Task> GetWarehouseList(string sort, int? offset, int? limit)
{
return _mapper.Map>(await _database.From()
.Sort(sort, _mapper)
.Page(offset, limit)
.Select());
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetAreaCount 方法
*/
public async Task GetAreaCount(int[] id, int? warehouse)
{
return (await GetAreaSchema(id, warehouse)
.Single(a => new { Count = Function.Count(a.AREA_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetAreaList 方法
*/
public async Task> GetAreaList(int[] id, int? warehouse, string sort, int? offset, int? limit)
{
return _mapper.Map>(await GetAreaSchema(id, warehouse)
.Sort(sort, _mapper)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetArea 相关方法的条件
*/
private ISchema GetAreaSchema(int[] id, int? warehouse)
{
var schema = _database.From();
if (id != null)
{
schema = schema.Where(a => id.Contains(a.AREA_ID));
}
if (warehouse.HasValue)
{
schema = schema.Where(a => a.WAREHOUSE_ID == warehouse);
}
return schema;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetCellCount 方法
*/
public async Task GetCellCount(int[] area, string type, bool? @in, bool? @out, string name, string state, string laneway, string cellTag)
{
return (await GetCellSchema(area, type, @in, @out, name, state, laneway, cellTag)
.Single(c => new { Count = Function.Count(c.CELL_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetCellList 方法
*/
public async Task> GetCellList(int[] area, string type, bool? @in, bool? @out, string name, string state, string sort, int? offset, int? limit, string laneway, string cellTag)
{
return _mapper.Map>(await GetCellSchema(area, type, @in, @out, name, state, laneway, cellTag)
.Sort(sort, _mapper)
.OrderBy(r => r.CELL_ID, Infrastructure.Database.Order.Ascending)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetCell 相关方法的条件
*/
private ISchema GetCellSchema(int[] area, string type, bool? @in, bool? @out, string name, string state, string laneway, string cellTag)
{
var schema = _database.From().Where(c => c.CELL_FLAG == true);
if (area != null)
{
schema = schema.Where(c => area.Contains(c.AREA_ID));
}
if (type != null)
{
schema = schema.Where(c => c.CELL_TYPE == type);
}
if (@in.HasValue)
{
schema = schema.Where(c => c.CELL_IN == @in);
}
if (@out.HasValue)
{
schema = schema.Where(c => c.CELL_OUT == @out);
}
if (name != null)
{
schema = schema.Where(c => c.CELL_NAME == name);
}
if (state != null)
{
schema = schema.Where(c => c.RUN_STATUS == state);
}
if (cellTag != null)
{
schema = schema.Where(c => c.CELL_TAG.Contains(cellTag));
}
if (laneway != null)
{
int way = 0;
try
{
way = Convert.ToInt32(laneway);
}
catch { }
schema = schema.Where(c => c.CELL_LANEWAY == way);
}
return schema;
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetEmergencyList 方法
*/
public async Task> GetEmergencyList(string stationCode, string stationModel, string inout)
{
var schema = _database.From()
.Where(r=>r.INOUT != null);
if (stationCode != null)
{
schema = schema.Where(r => r.STATION_CODE== stationCode);
}
if (stationModel != null)
{
schema = schema.Where(r => r.STATION_MODEL == stationModel);
}
if (inout != null)
{
schema = schema.Where(r => r.INOUT == inout);
}
return _mapper.Map>(await schema
.OrderBy(r=>r.STATION_CODE, Infrastructure.Database.Order.Ascending)
.Select());
}
/*
* 实现 Kean.Application.Query.Interfaces.IWarehouseService.GetEmergencyCount 方法
*/
public async Task GetEmergencyCount(string stationCode, string stationModel, string inout)
{
var schema = _database.From()
.Where(r => r.INOUT != null);
if (stationCode != null)
{
schema = schema.Where(r => r.STATION_CODE == stationCode);
}
if (stationModel != null)
{
schema = schema.Where(r => r.STATION_MODEL == stationModel);
}
if (inout != null)
{
schema = schema.Where(r => r.INOUT == inout);
}
return (await schema
.Single(w => new { Count = Function.Count(w.STATION_CODE) }))
.Count;
}
}
}