110 lines
5.2 KiB
C#
110 lines
5.2 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data.SqlClient;
|
|||
|
using Tanshu.Accounts.Contracts;
|
|||
|
using Tanshu.Data.DAO;
|
|||
|
using Tanshu.Accounts.DAOFactory;
|
|||
|
|
|||
|
namespace Tanshu.Accounts.SqlDAO
|
|||
|
{
|
|||
|
public class CustomerDAO : BaseDAO, ICustomerDAO
|
|||
|
{
|
|||
|
public CustomerDAO(IConnectionDAO connection)
|
|||
|
: base(connection)
|
|||
|
{ }
|
|||
|
|
|||
|
public void Insert(CustomerBO customer)
|
|||
|
{
|
|||
|
using (SqlCommand cmd = new SqlCommand(@"
|
|||
|
SET @CustomerID = NEWID();
|
|||
|
SELECT @Code = ISNULL(MAX(Code), 0) + 1 FROM Customers;
|
|||
|
INSERT INTO Customers (CustomerID, Code, Name, LedgerID, Address, Important, Phone, Remarks) VALUES (@CustomerID, @Code, @Name, @LedgerID, @Address, @Important, @Phone, @Remarks)"))
|
|||
|
{
|
|||
|
cmd.Parameters.Add("@CustomerID", System.Data.SqlDbType.UniqueIdentifier);
|
|||
|
cmd.Parameters["@CustomerID"].Direction = System.Data.ParameterDirection.Output;
|
|||
|
cmd.Parameters.Add("@Code", System.Data.SqlDbType.Int);
|
|||
|
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
|
|||
|
|
|||
|
cmd.Parameters.AddWithValue("@Name", customer.Name);
|
|||
|
cmd.Parameters.AddWithValue("@LedgerID", customer.LedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@Address", customer.Address);
|
|||
|
cmd.Parameters.AddWithValue("@Important", customer.Important);
|
|||
|
cmd.Parameters.AddWithValue("@Phone", customer.Phone);
|
|||
|
cmd.Parameters.AddWithValue("@Remarks", customer.Remarks);
|
|||
|
connection.ExecuteNonQuery(cmd);
|
|||
|
customer.CustomerID = (Guid)cmd.Parameters["@CustomerID"].Value;
|
|||
|
customer.Code = (int)cmd.Parameters["@Code"].Value;
|
|||
|
}
|
|||
|
}
|
|||
|
public void Update(CustomerBO customer)
|
|||
|
{
|
|||
|
using (SqlCommand cmd = new SqlCommand(@"UPDATE Customers SET Code = @Code, Name = @Name, LedgerID = @LedgerID, Address = @Address, Important = @Important, Phone = @Phone, Remarks = @Remarks WHERE CustomerID = @CustomerID"))
|
|||
|
{
|
|||
|
cmd.Parameters.AddWithValue("@CustomerID", customer.CustomerID);
|
|||
|
cmd.Parameters.AddWithValue("@Code", customer.Code);
|
|||
|
cmd.Parameters.AddWithValue("@Name", customer.Name);
|
|||
|
cmd.Parameters.AddWithValue("@LedgerID", customer.LedgerID);
|
|||
|
cmd.Parameters.AddWithValue("@Address", customer.Address);
|
|||
|
cmd.Parameters.AddWithValue("@Important", customer.Important);
|
|||
|
cmd.Parameters.AddWithValue("@Phone", customer.Phone);
|
|||
|
cmd.Parameters.AddWithValue("@Remarks", customer.Remarks);
|
|||
|
connection.ExecuteNonQuery(cmd);
|
|||
|
}
|
|||
|
}
|
|||
|
public void Delete(Guid customerID)
|
|||
|
{
|
|||
|
using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID"))
|
|||
|
{
|
|||
|
cmd.Parameters.AddWithValue("@CustomerID", customerID);
|
|||
|
connection.ExecuteNonQuery(cmd);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public CustomerBO GetCustomer(Guid customerID)
|
|||
|
{
|
|||
|
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers Where CustomerID=@CustomerID ");
|
|||
|
cmd.Parameters.AddWithValue("@CustomerID", customerID);
|
|||
|
return BusinessObjectDAO<CustomerBO>.GetBusinessObject(connection.ExecuteReader(cmd));
|
|||
|
}
|
|||
|
public List<CustomerBO> GetFilteredCustomers(Dictionary<string, string> filter)
|
|||
|
{
|
|||
|
string query = "";
|
|||
|
string[] filters = filter["Universal"].Split(' ');
|
|||
|
if ((filters != null) && (filters.Length > 0))
|
|||
|
{
|
|||
|
query = "SELECT CustomerID, Code, Name, LedgerID, Address, Important, Phone, Remarks FROM Customers WHERE ";
|
|||
|
foreach (string fil in filters)
|
|||
|
{
|
|||
|
query += string.Format("(Name LIKE '%{0}%' OR Address LIKE '%{0}%' OR Phone LIKE '%{0}%' OR Remarks LIKE '%{0}%') AND ", fil);
|
|||
|
}
|
|||
|
query = query.Substring(0, query.Length - 5);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
query = "SELECT CustomerID, Code, Name, LedgerID, Address, Important, Phone, Remarks FROM Customers";
|
|||
|
}
|
|||
|
return BusinessObjectDAO<CustomerBO>.GetBusinessObjects(connection.ExecuteReader(query));
|
|||
|
}
|
|||
|
public List<LedgerBO> GetCustomerLedgers()
|
|||
|
{
|
|||
|
return BusinessObjectDAO<LedgerBO>.GetBusinessObjects(connection.ExecuteReader("SELECT * FROM Ledgers WHERE Type IN ('B', 'D') ORDER BY Name"));
|
|||
|
}
|
|||
|
public List<CustomerBO> GetCustomers()
|
|||
|
{
|
|||
|
Dictionary<string, string> filter = new Dictionary<string, string>();
|
|||
|
filter.Add("Universal", "");
|
|||
|
return GetFilteredCustomers(filter);
|
|||
|
}
|
|||
|
public List<CustomerBO> GetCustomers(Guid customerID)
|
|||
|
{
|
|||
|
string query = "SELECT CustomerID, Code, Name, LedgerID, Address, Important, Phone, Remarks FROM Customers WHERE CustomerID = @CustomerID";
|
|||
|
using (SqlCommand cmd = new SqlCommand(query))
|
|||
|
{
|
|||
|
cmd.Parameters.AddWithValue("@CustomerID", customerID);
|
|||
|
return BusinessObjectDAO<CustomerBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|