using System; using System.Collections.Generic; using System.Linq; using System.Text; using Dapper; namespace NX.ERP.U8.Mom { public class MomOrderDAL { /// /// /// /// /// /// public IEnumerable GetList(string strWhere = "", object param = null) { using (var conn = U8LoginContext.Instance.Connection) { 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 "; if (!string.IsNullOrEmpty(strWhere)) { strSQL += $" where {strWhere} "; } var lookupDetail = new Dictionary(); var lookupOrder = new Dictionary(); // return conn.Query(strSQL,null); var list = conn.Query(strSQL, (Order, OrderDetail, MoAllocate) => { if (!lookupOrder.TryGetValue(Order.ProductOrderId, out var _order)) { lookupOrder.Add(Order.ProductOrderId, Order); } else { Order = _order; } if (!lookupDetail.TryGetValue(OrderDetail.ProductOrderDtlId, out var momDetail)) { lookupDetail.Add(OrderDetail.ProductOrderDtlId, OrderDetail); Order.ProductDtlLst.Add(OrderDetail); } else { OrderDetail = momDetail; } Order.Allocates.Add(MoAllocate); OrderDetail.ProductAllocateOrderLst.Add(MoAllocate); return Order; }, param, null, true, "id", 10, System.Data.CommandType.Text); return lookupOrder.Values; } } /// /// /// /// /// public IEnumerable QueryOrders(NX.ERP.Model.Request.MomOrder Params) { if (Params == null) { return this.GetOrders(); } List where = new List(); if (!string.IsNullOrEmpty(Params.ProductCode)) { where.Add(" a.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 GetOrders(string strWhere = "", object Param = null) { using (var conn = U8LoginContext.Instance.Connection) { string strSQL = @"select a.MoId as ProductOrderId,a.moCode as ProductOrderNo,a.MakeDate as ProductDate,a.ModifyUser as PreparedBy, a.MoId as id ,a.MoDId as ProductOrderDtlId,a.MoId as ProductOrderId,a.MoCode as ProductOrderNo,a.MakeDate as ProductOrderDate,a.InvCode as ProductCode,a.Qty as Qty FROM v_st_mom_orderdetail a with (nolock) Where ( (1>0) AND (1>0) ) AND ( 1>0 ) and (case when ISNULL(SfcFlag,0)=1 then iquantity+QualifiedInQty else 1 end)<>0 and ISNULL(QCFLAG,0)=0 and ISNULL(Status,0) = 3 and (ISNULL(iQuantity,0))>0 and isnull(MrpQty,0)<>0 and 1=1 "; if (!string.IsNullOrEmpty(strWhere)) { strSQL += $" and {strWhere} "; } var lookupDetail = new Dictionary(); var lookupOrder = new Dictionary(); // return conn.Query(strSQL,null); var list = conn.Query(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 QueryDetails(NX.ERP.Model.Request.MomOrder Params) { if (Params == null) { return this.GetDetails(); } List where = new List(); 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 GetDetails(string strWhere = "", object Param = null) { var strSql = @"select b.MoId as id ,b.MoDId as ProductOrderDtlId,b.MoId as ProductOrderId,b.MoCode as ProductOrderNo,b.MakeDate as ProductOrderDate,b.InvCode as ProductCode,b.Qty as Qty, d.AllocateId as id ,d.AllocateId as AllocateId,b.MoDId as ProductOrderDtlId,d.InvCode as MaterialCode,b.MoId as ProductOrderId, d.IssQty as IssQty,d.Qty FROM v_st_mom_orderdetail b INNER JOIN (SELECT modid as m_modid,allocateid,CREJECTCODE,IssQty,Qty,InvCode from v_st_moallocate where isnull(WIPType,0)=3 and isnull(ByproductFlag,0)=0 and (ISNULL(Qty,0)-ISNULL(IssQty,0)+ISNULL(ReplenishQty,0))>0 and isnull(RequisitionFlag,0) = 0 and 1=1) d ON b.modid= d.m_modid Where b.byproductflag=0 and ISNULL(b.Status,0) = 3 and ( isnull(b.bWorkshopTrans,0) = 0 or (isnull(b.bWorkshopTrans,0) =1 and isnull(b.sfcflag,0) =1)) AND (1>0) AND (1>0) AND ( 1>0 )"; if (!string.IsNullOrEmpty(strWhere)) { strSql += $" and {strWhere} "; } var lookup = new Dictionary(); using (var conn = U8LoginContext.Instance.Connection) { conn.Query(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; } public Model.MOM.MomOrder GetModel(string Code) { return this.GetList("a.moCode=@Code", new { Code }).FirstOrDefault(); } public IEnumerable GetListByLastID(int Id) { return this.GetList("b.moid >= @id", new { Id }); } } }