You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tinyPos/TinyPOS/TinyPosEntity/Service/ProductService.cs

137 lines
4.0 KiB

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<Product> 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<Product> Find(List<TransactionItem> transactionItemList)
{
var subQry = from ti in transactionItemList
select ti.Product.ProductId;
List<int> 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<Product> 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);
}
}
}
}