using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TinyPosEntity.Entity; namespace TinyPosEntity.Service { public class ProductService { static ProductService instance; public static ProductService Instance { get { Init(); return instance; } } private static void Init() { if (instance == null) instance = new ProductService(); } public void Create(Product targetObject) { using (EntityContext context = new EntityContext()) { context.Products.Add(targetObject); context.SaveChanges(); } } public void Create(List targetObject) { using (EntityContext context = new EntityContext()) { foreach (Product type in targetObject) { context.Products.Add(type); } context.SaveChanges(); } } public Product Find(Product targetObject) { using (EntityContext context = new EntityContext()) { var qry = from p in context.Products where p.Name.Equals(targetObject.Barcode) select p; if (qry.Count() < 1) return null; return qry.ToArray()[0]; } } public Product Find(string barcode) { using (EntityContext context = new EntityContext()) { var qry = from p in context.Products where p.Name.Equals(barcode) select p; if (qry.Count() < 1) return null; return qry.ToArray()[0]; } } public List Find(List transactionItemList) { var subQry = from ti in transactionItemList select ti.Product.ProductId; List productIdList = subQry.ToList(); if (productIdList == null || productIdList.Count < 1) return null; using (EntityContext context = new EntityContext()) { var qry = from p in context.Products where productIdList.Contains(p.ProductId) orderby p.ProductId ascending select p; if (qry.Count() < 1) return null; return qry.ToList(); } } public List Find(DateTime fromDateTime, DateTime toDateTime) { using (EntityContext context = new EntityContext()) { var qry = from p in context.Products join ti in context.TransactionItems on p equals ti.Product join t in context.Transactions on ti.Transaction equals t where (t.Timestamp >= fromDateTime && t.Timestamp <= toDateTime) orderby p.ProductId ascending select p; if (qry.Count() < 1) return null; return qry.ToList(); } } public void Remove(Product targetObject) { using (EntityContext context = new EntityContext()) { context.Database.ExecuteSqlCommand( "DELETE FROM dbo.Products WHERE ProductId = {0}", targetObject.ProductId); } } public void Remove(string barcode) { using (EntityContext context = new EntityContext()) { context.Database.ExecuteSqlCommand( "DELETE FROM dbo.Products WHERE Barcode = {0}", barcode); } } } }