山东雷驰
 
 
 
 

227 lines
7.4 KiB

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.Soap.Hithium.Models;
using Kean.Infrastructure.Utilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Query.Implements
{
/// <summary>
/// 订单信息查询服务实现
/// </summary>
public sealed class OrderService : IOrderService
{
private readonly IMapper _mapper; // 模型映射
private readonly IDefaultDb _database; // 默认数据库
private readonly Kean.Infrastructure.Soap.Hithium.WmsAPIService _wmsService; // 海辰WMS
/// <summary>
/// 依赖注入
/// </summary>
public OrderService(
IMapper mapper,
Kean.Infrastructure.Soap.Hithium.WmsAPIService wmsService,
IDefaultDb database)
{
_mapper = mapper;
_database = database;
_wmsService = wmsService;
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetTypeList 方法
*/
public async Task<IEnumerable<Ordtyp>> GetTypeList()
{
return _mapper.Map<IEnumerable<Ordtyp>>(await _database.From<T_PLAN_TYPE>()
.Where(t => t.TYPE_FLAG == true)
.Select());
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetActionList 方法
*/
public async Task<IEnumerable<string>> GetActionList()
{
return await System.Threading.Tasks.Task.FromResult(new string[]
{
"edit",
"goto",
"cancel",
"finish",
"inbound",
"outbound"
//...
});
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetFlowList 方法
*/
public async Task<IEnumerable<Flow>> GetFlowList()
{
var nodes = (await _database.From<T_PLAN_FLOW_NODE>()
.Select())
.GroupBy(n => n.FLOW_ID)
.ToDictionary(g => g.Key, g => _mapper.Map<IEnumerable<Flow.Node>>(g));
var paths = (await _database.From<T_PLAN_FLOW_PATH>()
.Select())
.GroupBy(p => p.FLOW_ID)
.ToDictionary(g => g.Key, g => _mapper.Map<IEnumerable<Flow.Path>>(g));
return _mapper.Map<IEnumerable<Flow>>(await _database.From<T_PLAN_FLOW>()
.Where(f => f.FLOW_FLAG == true)
.Select())
.Select(f =>
{
f.Nodes = nodes.ContainsKey(f.Id) ? nodes[f.Id] : new Flow.Node[0];
f.Paths = paths.ContainsKey(f.Id) ? paths[f.Id] : new Flow.Path[0];
return f;
});
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetOrderCount 方法
*/
public async Task<int> GetOrderCount(
int? type,
int? flow,
int? node,
string no,
string creater,
DateTime? createFrom,
DateTime? createTo)
{
return (await GetOrderSchema(type, flow, node, no, creater, createFrom, createTo)
.Single(p => new { Count = Function.Count(p.PLAN_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetOrderList 方法
*/
public async Task<IEnumerable<ViewModels.Order>> GetOrderList(
int? type,
int? flow,
int? node,
string no,
string creater,
DateTime? createFrom,
DateTime? createTo,
string sort,
int? offset,
int? limit)
{
var schema = _database.From<T_PLAN_FLOW_NODE>();
if (flow.HasValue)
{
schema = schema.Where(n => n.FLOW_ID == flow.Value);
}
var action = (await schema.Select()).ToDictionary(
i => i.NODE_ID,
i => JsonHelper.Deserialize<IDictionary<int, string[][]>>(i.NODE_DETAIL));
return (await GetOrderSchema(type, flow, node, no, creater, createFrom, createTo)
.Sort<T_PLAN_MAIN, ViewModels.Order>(sort, _mapper)
.Page(offset, limit)
.Select())
.Select(i =>
{
var vm = _mapper.Map<ViewModels.Order>(i);
vm.Action = action[i.PLAN_NODE][i.PLAN_TYPE];
return vm;
});
}
/*
* 组织 GetOrder 相关方法的条件
*/
private ISchema<T_PLAN_MAIN> GetOrderSchema(
int? type,
int? flow,
int? node,
string no,
string creater,
DateTime? createFrom,
DateTime? createTo)
{
var schema = _database.From<T_PLAN_MAIN>();
if (type.HasValue)
{
schema = schema.Where(p => p.PLAN_TYPE == type.Value);
}
if (flow.HasValue)
{
schema = schema.Where(p => p.PLAN_FLOW == flow.Value);
}
if (node.HasValue)
{
schema = schema.Where(p => p.PLAN_NODE == node.Value);
}
if (no != null)
{
schema = schema.Where(p => p.PLAN_CODE.Contains(no));
}
if (creater != null)
{
schema = schema.Where(p => p.PLAN_CREATER.Contains(creater));
}
if (createFrom.HasValue)
{
schema = schema.Where(p => p.PLAN_CREATE_TIME >= createFrom.Value);
}
if (createTo.HasValue)
{
schema = schema.Where(p => p.PLAN_CREATE_TIME <= createTo.Value.AddDays(1));
}
return schema;
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetLineCount 方法
*/
public async Task<int> GetLineCount(
int? id)
{
return (await GetLineSchema(id)
.Single(p => new { Count = Function.Count(p.PLAN_LIST_ID) }))
.Count;
}
/*
* 实现 Kean.Application.Query.Interfaces.IOrderService.GetLineList 方法
*/
public async Task<IEnumerable<ViewModels.Order>> GetLineList(
int? id,
string sort,
int? offset,
int? limit)
{
return _mapper.Map<IEnumerable<ViewModels.Order>>(await GetLineSchema(id)
.Sort<V_PLAN_LIST, ViewModels.Order>(sort, _mapper)
.Page(offset, limit)
.Select());
}
/*
* 组织 GetLine 相关方法的条件
*/
private ISchema<V_PLAN_LIST> GetLineSchema(
int? id)
{
var schema = _database.From<V_PLAN_LIST>();
if (id.HasValue)
{
schema = schema.Where(p => p.PLAN_ID == id.Value);
}
return schema;
}
}
}