using System; using System.Collections.Generic; using System.Data.SqlClient; using Tanshu.Accounts.Contracts; using Tanshu.Data.DAO; using System.Data; namespace Tanshu.Accounts.SqlDAO { public class ProductDAO : BaseDAO { public ProductDAO(IConnectionDAO connection) : base(connection) { } public bool Insert(ProductBO product) { product.ProductID = Guid.NewGuid(); SqlCommand cmd = new SqlCommand(@" SELECT @Code = ISNULL(MAX(Code), 0) + 1 FROM Products; INSERT INTO Products (ProductID, Code, Name, ShortName, BarCode, Units, ProductGroupID, VatID ,SalePrice , ServiceTaxID , Discontinued, MinimumLevel, MaximumLevel, SortOrder) VALUES (@ProductID, @Code, @Name, '', '', @Units, @ProductGroupID, @VatID, @SalePrice ,@ServiceTaxID ,@Discontinued, @MinimumLevel, @MaximumLevel, @SortOrder);"); { cmd.Parameters.AddWithValue("@ProductID", product.ProductID); cmd.Parameters.Add("@Code", SqlDbType.Int); cmd.Parameters["@Code"].Direction = ParameterDirection.Output; cmd.Parameters.AddWithValue("@Name", product.Name); cmd.Parameters.AddWithValue("@Units", product.Units); cmd.Parameters.AddWithValue("@ProductGroupID", product.ProductGroupID); cmd.Parameters.AddWithValue("@VatID", product.VatID); cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice); cmd.Parameters.AddWithValue("@ServiceTaxID", product.ServiceTaxID); cmd.Parameters.AddWithValue("@Discontinued", product.Discontinued); cmd.Parameters.AddWithValue("@MinimumLevel", product.MinimumLevel); cmd.Parameters.AddWithValue("@MaximumLevel", product.MaximumLevel); cmd.Parameters.AddWithValue("@SortOrder", product.SortOrder); connection.ExecuteNonQuery(cmd); product.Code = (int)cmd.Parameters["@Code"].Value; return true; } } public ProductBO GetProduct(Guid productID) { SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE ProductID = @ProductID"); cmd.Parameters.AddWithValue("@ProductID", productID); return BusinessObjectDAO.GetBusinessObject(connection.ExecuteReader(cmd)); } public bool Delete(Guid productID) { SqlCommand cmd = new SqlCommand("DELETE FROM Products WHERE ProductID = @ProductID"); cmd.Parameters.AddWithValue("@ProductID", productID); connection.ExecuteNonQuery(cmd); return true; } public bool Update(ProductBO product) { SqlCommand cmd = new SqlCommand(@"UPDATE Products SET Code = @Code ,Name = @Name ,Units = @Units ,ProductGroupID = @ProductGroupID ,VatID = @VatID ,SalePrice = @SalePrice ,ServiceTaxID = @ServiceTaxID ,Discontinued = @Discontinued ,MinimumLevel = @MinimumLevel ,MaximumLevel = @MaximumLevel, SortOrder=@SortOrder WHERE ProductID = @ProductID;"); cmd.Parameters.AddWithValue("@ProductID", product.ProductID); cmd.Parameters.AddWithValue("@Code", product.Code); cmd.Parameters.AddWithValue("@Name", product.Name); cmd.Parameters.AddWithValue("@Units", product.Units); cmd.Parameters.AddWithValue("@ProductGroupID", product.ProductGroupID); cmd.Parameters.AddWithValue("@VatID", product.VatID); cmd.Parameters.AddWithValue("@SalePrice", product.SalePrice); cmd.Parameters.AddWithValue("@ServiceTaxID", product.ServiceTaxID); cmd.Parameters.AddWithValue("@Discontinued", product.Discontinued); cmd.Parameters.AddWithValue("@MinimumLevel", product.MinimumLevel); cmd.Parameters.AddWithValue("@MaximumLevel", product.MaximumLevel); cmd.Parameters.AddWithValue("@SortOrder", product.SortOrder); connection.ExecuteNonQuery(cmd); return true; } public ProductBO GetProduct(string nameAndUnits) { SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE Name + ' (' + Units + ')' = @Name"); cmd.Parameters.AddWithValue("@Name", nameAndUnits); return BusinessObjectDAO.GetBusinessObject(connection.ExecuteReader(cmd)); } public List GetProducts() { //SELECT p.ProductID, p.Picture, p.Code, p.Name, p.Units, p.ProductGroupID, p.VatID, p.SalePrice, p.ServiceTaxID, p.Discontinued, p.MinimumLevel, p.MaximumLevel, p.SortOrder, pt.Name AS Type FROM Products p INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID ORDER BY p.Name SqlCommand cmd = new SqlCommand("SELECT p.ProductID, p.Picture, p.Code, p.Name, p.Units, p.ProductGroupID, p.VatID, p.SalePrice, p.ServiceTaxID, p.Discontinued, p.MinimumLevel, p.MaximumLevel, p.SortOrder, pt.Name AS Type FROM Products p INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID ORDER BY p.Name"); return BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader(cmd)); } public List GetFilteredProducts(Dictionary filter) { List list = new List(); string query = GetProductQuery(filter); using (IDataReader dr = connection.ExecuteReader(query)) { while (dr.Read()) { list.Add(new ProductDisplaySmallBO { Category = dr.GetString(4), Code = dr.GetInt32(1), Name = dr.GetString(2), Price = dr.GetDecimal(3), ProductID = dr.GetGuid(0) }); } dr.Close(); } return list; } private string GetProductQuery(Dictionary filter) { string query = @" SELECT p.ProductID, p.Code, p.Name + ' (' + p.Units + ') ' + ShortName AS Name, p.SalePrice * (1 + t.Rate) AS Price, pt.Name AS Category FROM Products p INNER JOIN Tax t ON p.VatID = t.TaxID INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID WHERE pt.IsForSale = 1 AND p.SalePrice > 0 AND p.Discontinued = 0 "; string[] n = filter["Name"].Split(" ".ToCharArray()); foreach (string n1 in n) if (n1 != null && n1 != string.Empty) query += string.Format("AND p.Name + ' (' + p.Units + ') ' + ShortName LIKE '%{0}%' \r\n", n1); n = filter["Type"].ToLower().Split(" ".ToCharArray()); foreach (string n1 in n) if (n1 != null && n1 != string.Empty) query += string.Format("AND pt.Name LIKE '%{0}%' \r\n", n1); query += "ORDER BY p.SortOrder DESC;"; return query; } public void UpdateShortName() { List list = BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader("SELECT * FROM Products")); foreach (var item in list) { using (SqlCommand cmd = new SqlCommand("UPDATE Products SET ShortName = @ShortName WHERE ProductID = @ProductID AND ShortName <> @ShortName")) { cmd.Parameters.AddWithValue("@ShortName", GetShortName(item.Name)); cmd.Parameters.AddWithValue("@ProductID", item.ProductID); connection.ExecuteScalar(cmd); } } } private string GetShortName(string name) { string[] t1 = name.Split(" ".ToCharArray()); if (t1.Length == 0) return ""; name = ""; foreach (string t in t1) { if ((!t.StartsWith("(")) && (t != "")) name += t.Substring(0, 1); } return name; } public List GetProductGroups() { SqlCommand cmd = new SqlCommand("SELECT * FROM ProductGroups ORDER BY Name"); return BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader(cmd)); } public List GetProducts(string name, int skip, int count) { string query = @" WITH ProductsRN AS (SELECT *, ROW_NUMBER() OVER(ORDER BY Name) AS RowNum FROM Products WHERE Name LIKE '%{0}%') SELECT TOP {1} p.*, pt.Name AS Type FROM ProductsRN p INNER JOIN ProductGroups pt ON p.ProductGroupID = pt.ProductGroupID WHERE RowNum > {2} "; query = string.Format(query, name, count, skip); return BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader(query)); } public List GetTaxes() { return BusinessObjectDAO.GetBusinessObjects(connection.ExecuteReader("SELECT * FROM Tax;")); } } }