Files
U8-Import/NX.ERP.U8/Mom/MomOrderDAL.cs

196 lines
8.0 KiB
C#
Raw Normal View History

2021-01-12 14:45:59 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
namespace NX.ERP.U8.Mom
{
2021-01-13 10:21:56 +08:00
public class MomOrderDAL
2021-01-12 14:45:59 +08:00
{
2021-01-22 11:25:03 +08:00
2021-01-12 14:45:59 +08:00
public IEnumerable<Model.MOM.MomOrder> GetList(string strWhere = "", object param = null)
{
using (var conn = new System.Data.SqlClient.SqlConnection(U8LoginContext.Instance.Constr4Net))
{
2021-01-22 11:25:03 +08:00
string strSQL = @"select
a.MoId as ProductOrderId,a.moCode as ProductOrderNo,a.CreateDate as ProductDate,a.CreateUser as PreparedBy,
b.MoId as id ,b.MoDId as ProductOrderDtlId,b.MoId as ProductOrderId,a.MoCode as ProductOrderNo,a.CreateDate as ProductOrderDate,b.InvCode as ProductCode,b.Qty as Qty,
c.AllocateId as id ,c.AllocateId as AllocateId,c.MoDId as ProductOrderDtlId,c.InvCode as MaterialCode,b.MoId as ProductOrderId, c.IssQty as IssQty,c.Qty
from mom_order as a
left join mom_orderdetail as b on a.MoId=b.MoId
left join mom_moallocate as c on b.MoDId=c.MoDId ";
2021-01-12 14:45:59 +08:00
if (!string.IsNullOrEmpty(strWhere))
{
strSQL += $" where {strWhere} ";
}
var lookupDetail = new Dictionary<int, Model.MOM.MomDetail>();
var lookupOrder = new Dictionary<int, Model.MOM.MomOrder>();
2021-01-15 11:30:02 +08:00
// return conn.Query<Model.Purchase.Arrival>(strSQL,null);
2021-01-12 14:45:59 +08:00
var list = conn.Query<Model.MOM.MomOrder, Model.MOM.MomDetail, Model.MOM.MoAllocate, Model.MOM.MomOrder>(strSQL, (Order, OrderDetail, MoAllocate) =>
{
2021-01-22 11:25:03 +08:00
if (!lookupOrder.TryGetValue(Order.ProductOrderId, out var _order))
2021-01-12 14:45:59 +08:00
{
2021-01-22 11:25:03 +08:00
lookupOrder.Add(Order.ProductOrderId, Order);
2021-01-12 14:45:59 +08:00
}
else
{
Order = _order;
}
2021-01-22 11:25:03 +08:00
if (!lookupDetail.TryGetValue(OrderDetail.ProductOrderDtlId, out var momDetail))
2021-01-12 14:45:59 +08:00
{
2021-01-22 11:25:03 +08:00
lookupDetail.Add(OrderDetail.ProductOrderDtlId, OrderDetail);
Order.ProductDtlLst.Add(OrderDetail);
2021-01-12 14:45:59 +08:00
}
else
{
OrderDetail = momDetail;
}
2021-01-15 09:26:17 +08:00
Order.Allocates.Add(MoAllocate);
2021-01-22 11:25:03 +08:00
OrderDetail.ProductAllocateOrderLst.Add(MoAllocate);
2021-01-12 14:45:59 +08:00
return Order;
}, param, null, true, "id", 10, System.Data.CommandType.Text);
return lookupOrder.Values;
}
}
2021-01-22 11:25:03 +08:00
public IEnumerable<Model.MOM.MomOrder> QueryOrders(NX.ERP.Model.Request.MomOrder Params)
{
if (Params == null)
{
return this.GetOrders();
}
List<string> where = new List<string>();
if (!string.IsNullOrEmpty(Params.ProductCode))
{
where.Add(" b.InvCode like @ ProductCode");
}
if (!string.IsNullOrEmpty(Params.ProductOrderNo))
{
where.Add(" a.moCode like @ProductOrderNo ");
}
return this.GetOrders(string.Join(" and ", where.ToArray()), new
{
ProductCode = $"%{Params.ProductCode}%",
ProductOrderNo = $"{Params.ProductOrderNo}"
});
}
public IEnumerable<Model.MOM.MomOrder> GetOrders(string strWhere = "", object Param = null)
{
using (var conn = new System.Data.SqlClient.SqlConnection(U8LoginContext.Instance.Constr4Net))
{
string strSQL = @"select
a.MoId as ProductOrderId,a.moCode as ProductOrderNo,a.CreateDate as ProductDate,a.CreateUser as PreparedBy,
b.MoId as id ,b.MoDId as ProductOrderDtlId,b.MoId as ProductOrderId,a.MoCode as ProductOrderNo,a.CreateDate as ProductOrderDate,b.InvCode as ProductCode,b.Qty as Qty
from mom_order as a
left join mom_orderdetail as b on a.MoId=b.MoId
2021-01-22 14:39:30 +08:00
2021-01-22 11:25:03 +08:00
";
if (!string.IsNullOrEmpty(strWhere))
{
strSQL += $" where {strWhere} ";
}
var lookupDetail = new Dictionary<int, Model.MOM.MomDetail>();
var lookupOrder = new Dictionary<int, Model.MOM.MomOrder>();
// return conn.Query<Model.Purchase.Arrival>(strSQL,null);
var list = conn.Query<Model.MOM.MomOrder, Model.MOM.MomDetail, Model.MOM.MomOrder>(strSQL, (Order, OrderDetail) =>
{
if (!lookupOrder.TryGetValue(Order.ProductOrderId, out var _order))
{
lookupOrder.Add(Order.ProductOrderId, Order);
}
else
{
Order = _order;
}
Order.ProductDtlLst.Add(OrderDetail);
return Order;
}, Param, null, true, "id", 10, System.Data.CommandType.Text);
return lookupOrder.Values;
}
}
public IEnumerable<Model.MOM.MomDetail> QueryDetails(NX.ERP.Model.Request.MomOrder Params)
{
if (Params == null)
{
return this.GetDetails();
}
List<string> where = new List<string>();
if (!string.IsNullOrEmpty(Params.ProductCode))
{
where.Add(" b.InvCode like @ ProductCode");
}
if (!string.IsNullOrEmpty(Params.ProductOrderNo))
{
where.Add(" a.moCode like @ProductOrderNo ");
}
return this.GetDetails(string.Join(" and ", where.ToArray()), new
{
ProductCode = $"%{Params.ProductCode}%",
ProductOrderNo = $"{Params.ProductOrderNo}"
});
}
public IEnumerable<Model.MOM.MomDetail> GetDetails(string strWhere = "", object Param = null)
{
var strSql = @"select
b.MoId as id ,b.MoDId as ProductOrderDtlId,b.MoId as ProductOrderId,a.MoCode as ProductOrderNo,a.CreateDate as ProductOrderDate,b.InvCode as ProductCode,b.Qty as Qty,
c.AllocateId as id ,c.AllocateId as AllocateId,c.MoDId as ProductOrderDtlId,c.InvCode as MaterialCode,b.MoId as ProductOrderId, c.IssQty as IssQty,c.Qty
from mom_order as a
left join mom_orderdetail as b on a.MoId=b.MoId
left join mom_moallocate as c on b.MoDId=c.MoDId ";
2021-01-22 15:15:43 +08:00
if (!string.IsNullOrEmpty(strWhere))
{
strSql += $" where {strWhere} ";
}
2021-01-22 11:25:03 +08:00
var lookup = new Dictionary<int, Model.MOM.MomDetail>();
using (var conn = new System.Data.SqlClient.SqlConnection(U8LoginContext.Instance.Constr4Net))
{
conn.Query<Model.MOM.MomDetail, Model.MOM.MoAllocate, Model.MOM.MomDetail>(strSql, (OrderDetail, MoAllocate) =>
{
if (!lookup.TryGetValue(OrderDetail.ProductOrderDtlId, out var _OrderDetail))
{
lookup.Add(OrderDetail.ProductOrderDtlId, OrderDetail);
}
else
{
OrderDetail = _OrderDetail;
}
OrderDetail.ProductAllocateOrderLst.Add(MoAllocate);
return OrderDetail;
}, Param, null, true, "id", 100, System.Data.CommandType.Text);
}
return lookup.Values;
}
2021-01-12 14:45:59 +08:00
public Model.MOM.MomOrder GetModel(string Code)
{
return this.GetList("a.moCode=@Code", new { Code }).FirstOrDefault();
}
2021-01-13 10:21:56 +08:00
public IEnumerable<Model.MOM.MomOrder> GetListByLastID(int Id)
{
return this.GetList("a.moid >= @id", new { Id });
}
2021-01-12 14:45:59 +08:00
}
}