FIC_Solar/SolarPower/Repository/Implement/CompanyRepository.cs
2021-06-09 15:03:24 +08:00

237 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Dapper;
using SolarPower.Helper;
using SolarPower.Models.Company;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower.Repository.Implement
{
public class CompanyRepository : RepositoryBase<Company>, ICompanyRepository
{
public CompanyRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
tableName = "company";
}
/// <summary>
/// 取得下拉式公司選單須為Deleted: 0
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<List<CompanySelectItemList>> GetCompanySelectOptionListAsync()
{
List<CompanySelectItemList> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $"SELECT Id AS Value, Name AS Text FROM {tableName} WHERE Deleted = 0";
result = (await conn.QueryAsync<CompanySelectItemList>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
/// <summary>
/// 取得狀態為正常的公司基本資料
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
public SimpleCompany GetOneNormalSimpleCompanyById(int id)
{
SimpleCompany result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
conn.Open();
try
{
var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0 AND Status = @Status AND Id = @Id";
result = conn.QueryFirstOrDefault<SimpleCompany>(sql, new { Status = CompanyStatusEnum.Normal, Id = id });
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
/// <summary>
/// 透過搜尋條件,查詢過濾後的公司
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<List<CompanyDataTable>> GetAllByFilterAsync(PostCompanyFilter filter)
{
List<CompanyDataTable> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0";
if (filter.SelectedCompanyId > 0)
{
sql += @" Where Id = @SelectedCompanyId";
}
else
{
if (!string.IsNullOrEmpty(filter.Name))
{
sql += @" AND Name LIKE CONCAT('%', @Name, '%')";
}
if (!string.IsNullOrEmpty(filter.Phone))
{
sql += @" AND Phone LIKE CONCAT('%', @Phone, '%')";
}
if (!string.IsNullOrEmpty(filter.TaxIDNumber))
{
sql += @" AND TaxIDNumber LIKE CONCAT('%', @TaxIDNumber, '%')";
}
}
result = (await conn.QueryAsync<CompanyDataTable>(sql, filter)).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
/// <summary>
/// 透過搜尋條件,查詢過濾後的公司
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<int> GetNormalUserNumberByCompanyIdAsync(int id)
{
int result = 0;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $"SELECT COUNT(*) FROM user WHERE Deleted = 0 AND Status = @Status AND CompanyId = @CompanyId";
result = (await conn.QueryAsync<int>(sql, new { Status = CompanyStatusEnum.Normal, CompanyId = id })).FirstOrDefault();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
/// <summary>
/// 取得單一公司資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<Company> GetOneCompany(int id)
{
Company result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
conn.Open();
try
{
var sql = $"SELECT * FROM {tableName} WHERE deleted = 0 AND id = @Id";
result = await conn.QueryFirstOrDefaultAsync<Company>(sql, new { Id = id });
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
/// <summary>
/// 修改公司資料
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task UpdateCompany(UpdateCompany 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>
/// 透過統編,取得單一公司基本資料
/// </summary>
/// <param name="taxIDNumber"></param>
/// <returns></returns>
public async Task<SimpleCompany> GetOneNormalSimpleCompanyByTaxIDNumber(string taxIDNumber)
{
SimpleCompany result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
conn.Open();
try
{
var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0 AND TaxIDNumber = @TaxIDNumber";
result = await conn.QueryFirstOrDefaultAsync<SimpleCompany>(sql, new { TaxIDNumber = taxIDNumber });
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
}
}