public async Task <IHttpActionResult> getShipDetail([FromUri] ShipParam q) { #region 連接BusinessLogicLibary資料庫並取得資料 try { db0 = getDB0(); var predicate = PredicateBuilder.True <PurchaseDetail>(); predicate = predicate.And(x => x.Purchase.ship_state == (int)IShipState.unshipped & ((x.Purchase.pay_type == (int)IPayType.Remit & x.Purchase.pay_state == (int)IPayState.paid) || (x.Purchase.pay_type == (int)IPayType.PayOnStore & x.Purchase.pay_state == (int)IPayState.paid) || (x.Purchase.pay_type == (int)IPayType.CashOnDelivery))); if (q.keyword != null) { predicate = predicate.And(x => x.purchase_no.Contains(q.keyword) || x.Purchase.receive_name.Contains(q.keyword) || x.p_name.Contains(q.keyword)); } if (q.order_start != null & q.order_end != null) { DateTime start = (DateTime)q.order_start; DateTime end = ((DateTime)q.order_end).AddDays(1); predicate = predicate.And(x => x.Purchase.order_date >= start & x.Purchase.order_date <= end); } //不管怎樣依名子抓 predicate = predicate.And(x => x.p_name == q.p_name); var result = db0.PurchaseDetail.AsExpandable().Where(predicate) .Select(y => new ShipPD() { purchase_no = y.purchase_no, purchase_detail_id = y.purchase_detail_id, //訂單明細-編號 product_detail_id = y.product_detail_id, //產品明細-編號 p_d_pack_name = y.p_d_pack_name, //產品包裝 order_date = y.Purchase.order_date, //下單日期 receive_name = y.Purchase.receive_name, //收件人 customer_name = y.Purchase.Customer.c_name, //購買人 weight = y.ProductDetail.weight, //重量 qty = y.qty //數量 }); IQueryable <ShipPD> resultOrderItems = null; if (q.field != null) { if (q.sort == "asc") { resultOrderItems = result.OrderBy(q.field); } if (q.sort == "desc") { resultOrderItems = result.OrderBy(q.field + " descending"); } } else { resultOrderItems = result.OrderBy(x => x.purchase_no); } ShipData data = new ShipData() { p_name = q.p_name, Detail = await resultOrderItems.ToListAsync(), field = q.field, sort = q.sort }; return(Ok(new { result = true, data = data })); } catch (Exception ex) { return(Ok(new { result = false, message = ex.ToString() })); } finally { db0.Dispose(); } #endregion }
public async Task <IHttpActionResult> getShipList([FromUri] ShipParam q) { #region 連接BusinessLogicLibary資料庫並取得資料 ResultInfo <List <ShipData> > r = new ResultInfo <List <ShipData> >(); try { db0 = getDB0(); var predicate = PredicateBuilder.True <PurchaseDetail>(); predicate = predicate.And(x => x.Purchase.ship_state == (int)IShipState.unshipped & ((x.Purchase.pay_type == (int)IPayType.Remit & x.Purchase.pay_state == (int)IPayState.paid) || (x.Purchase.pay_type == (int)IPayType.PayOnStore & x.Purchase.pay_state == (int)IPayState.paid) || (x.Purchase.pay_type == (int)IPayType.CashOnDelivery))); if (q.keyword != null) { predicate = predicate.And(x => x.purchase_no.Contains(q.keyword) || x.Purchase.receive_name.Contains(q.keyword) || x.p_name.Contains(q.keyword)); } if (q.order_start != null & q.order_end != null) { DateTime start = (DateTime)q.order_start; DateTime end = ((DateTime)q.order_end).AddDays(1); predicate = predicate.And(x => x.Purchase.order_date >= start & x.Purchase.order_date <= end); } var result = db0.PurchaseDetail.AsExpandable().Where(predicate); var resultItems = await result .GroupBy(x => x.p_name) .Select(x => new ShipData() { p_name = x.Key, Detail = x.Select(y => new ShipPD() { purchase_no = y.purchase_no, purchase_detail_id = y.purchase_detail_id, //訂單明細-編號 product_detail_id = y.product_detail_id, //產品明細-編號 p_d_pack_name = y.p_d_pack_name, //產品包裝 order_date = y.Purchase.order_date, //下單日期 receive_name = y.Purchase.receive_name, //收件人 customer_name = y.Purchase.Customer.c_name, //購買人 weight = y.ProductDetail.weight, //重量 qty = y.qty //數量 }).ToList() }) .ToListAsync(); r.result = true; r.data = resultItems; } catch (Exception ex) { r.result = false; r.message = ex.ToString(); } finally { db0.Dispose(); } return(Ok(r)); #endregion }