349 lines
10 KiB
C#
349 lines
10 KiB
C#
|
||
using Dapper;
|
||
using SolarPower.Helper;
|
||
using SolarPower.Repository.Interface;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SolarPower.Repository.Implement
|
||
{
|
||
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
|
||
{
|
||
protected readonly IDatabaseHelper _databaseHelper;
|
||
protected string tableName;
|
||
protected IEnumerable<PropertyInfo> GetProperties = typeof(T).GetProperties();
|
||
|
||
public RepositoryBase(IDatabaseHelper databaseHelper)
|
||
{
|
||
this._databaseHelper = databaseHelper;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增資料
|
||
/// </summary>
|
||
/// <param name="entity"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<int> AddAsync(T entity, List<string> properties)
|
||
{
|
||
int count;
|
||
using (IDbConnection conn = _databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
try
|
||
{
|
||
string sql = GenerateInsertQuery(properties);
|
||
|
||
count = await conn.ExecuteAsync(sql, entity);
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
|
||
return count;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增單一筆資料
|
||
/// </summary>
|
||
/// <param name="entity"></param>
|
||
/// <param name="properties"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<int> AddOneAsync(T entity, List<string> properties)
|
||
{
|
||
int id;
|
||
using (IDbConnection conn = _databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
try
|
||
{
|
||
string sql = GenerateInsertQuery(properties);
|
||
|
||
sql += "SELECT LAST_INSERT_ID();";
|
||
|
||
id = (await conn.QueryAsync<int>(sql, entity)).Single();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
|
||
return id;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 透過Id,軟刪除單一筆資料
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task DeleteOne(int id)
|
||
{
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
using (var trans = conn.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var sql = $"UPDATE {tableName} SET deleted = 1 WHERE id = @Id";
|
||
|
||
await conn.ExecuteAsync(sql, new { Id = id }, trans);
|
||
|
||
trans.Commit();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
trans.Rollback();
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 透過Id,軟刪除單一筆資料(不同資料表)
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="tablename"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task DeleteOneOtherTable(int id,string table_name)
|
||
{
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
using (var trans = conn.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var sql = $"UPDATE {table_name} SET deleted = 1 WHERE id = @Id";
|
||
|
||
await conn.ExecuteAsync(sql, new { Id = id }, trans);
|
||
|
||
trans.Commit();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
trans.Rollback();
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取得所有資料
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public virtual async Task<List<T>> GetAllAsync()
|
||
{
|
||
List<T> result;
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
try
|
||
{
|
||
var sql = $"SELECT * FROM {tableName}";
|
||
|
||
result = (await conn.QueryAsync<T>(sql)).ToList();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
throw exception;
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取得單一筆資料
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<T> GetOneAsync(int id)
|
||
{
|
||
T result;
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
try
|
||
{
|
||
var sql = $"SELECT * FROM {tableName} WHERE id = @Id";
|
||
|
||
result = await conn.QueryFirstOrDefaultAsync<T>(sql, new { Id = id });
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
throw exception;
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 透過Id,實際刪除單一筆資料
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task PurgeOneAsync(int id)
|
||
{
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
using (var trans = conn.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var sql = $"DELETE FROM {tableName} WHERE id = @Id";
|
||
|
||
await conn.ExecuteAsync(sql, new { Id = id }, trans);
|
||
|
||
trans.Commit();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
trans.Rollback();
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改資料
|
||
/// </summary>
|
||
/// <param name="entity"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task Update(T entity, List<string> properties)
|
||
{
|
||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||
{
|
||
conn.Open();
|
||
using (var trans = conn.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var sql = GenerateUpdateQuery(properties);
|
||
|
||
await conn.ExecuteAsync(sql, entity, trans);
|
||
|
||
trans.Commit();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
trans.Rollback();
|
||
throw exception;
|
||
}
|
||
finally
|
||
{
|
||
conn.Close();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 產生Insert語句
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected string GenerateInsertQuery(List<string> properties)
|
||
{
|
||
var insertQuery = new StringBuilder($"INSERT INTO {tableName} ");
|
||
|
||
insertQuery.Append("(");
|
||
|
||
properties.ForEach(prop => { insertQuery.Append($"{prop},"); });
|
||
|
||
insertQuery
|
||
.Remove(insertQuery.Length - 1, 1)
|
||
.Append(") VALUES (");
|
||
|
||
properties.ForEach(prop => { insertQuery.Append($"@{prop},"); });
|
||
|
||
insertQuery
|
||
.Remove(insertQuery.Length - 1, 1)
|
||
.Append(");");
|
||
|
||
return insertQuery.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 產生Insert語句,可選擇自己要加入資料表
|
||
/// </summary>
|
||
/// <param name="properties"></param>
|
||
/// <param name="table_name">欲新增至目標資料表</param>
|
||
/// <returns></returns>
|
||
protected string GenerateInsertQueryWithCustomTable(List<string> properties, string table_name)
|
||
{
|
||
var insertQuery = new StringBuilder($"INSERT INTO {table_name} ");
|
||
|
||
insertQuery.Append("(");
|
||
|
||
properties.ForEach(prop => { insertQuery.Append($"{prop},"); });
|
||
|
||
insertQuery
|
||
.Remove(insertQuery.Length - 1, 1)
|
||
.Append(") VALUES (");
|
||
|
||
properties.ForEach(prop => { insertQuery.Append($"@{prop},"); });
|
||
|
||
insertQuery
|
||
.Remove(insertQuery.Length - 1, 1)
|
||
.Append(")");
|
||
|
||
return insertQuery.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 產生Update語句
|
||
/// </summary>
|
||
/// <param name="properties"></param>
|
||
/// <returns></returns>
|
||
protected string GenerateUpdateQuery(List<string> properties)
|
||
{
|
||
var updateQuery = new StringBuilder($"UPDATE {tableName} SET ");
|
||
|
||
properties.ForEach(property =>
|
||
{
|
||
if (!property.Equals("Id"))
|
||
{
|
||
updateQuery.Append($"{property}=@{property},");
|
||
}
|
||
});
|
||
|
||
updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma
|
||
updateQuery.Append(" WHERE id = @Id");
|
||
|
||
return updateQuery.ToString();
|
||
}
|
||
}
|
||
}
|