using Dapper; using SolarPower.Helper; using SolarPower.Models; using SolarPower.Models.Company; using SolarPower.Models.Role; using SolarPower.Models.User; 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, ICompanyRepository { public CompanyRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { tableName = "company"; } /// /// 取得下拉式公司選單,須為Deleted: 0 /// /// /// public async Task> GetCompanySelectOptionListAsync() { List 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(sql)).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 取得當前使用者所在的公司資訊 /// /// /// public MyCompany GetMyCompanyInfoById(int id) { MyCompany 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(sql, new { Status = CompanyStatusEnum.Normal, Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 透過搜尋條件,查詢過濾後的公司 /// /// /// public async Task> GetAllByFilterAsync(PostCompanyFilter filter) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0"; if (filter.SelectedCompanyId > 0) { sql += @" AND 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(sql, filter)).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過搜尋條件,查詢過濾後的公司 /// /// /// public async Task 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(sql, new { Status = CompanyStatusEnum.Normal, CompanyId = id })).FirstOrDefault(); } catch (Exception exception) { throw exception; } return result; } } /// /// 取得單一公司資料 /// /// /// public async Task 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(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 修改公司資料 /// /// /// public async Task UpdateCompany(UpdateCompany entity, List 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(); } } } } /// /// 透過統編,取得單一公司基本資料 /// /// /// public async Task 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(sql, new { TaxIDNumber = taxIDNumber }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 透過公司編號,取得該公司的註冊人數 /// /// /// public async Task GetRegisterNumberByCompanyId(int companyId) { int result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); try { var sql = $"SELECT COUNT(*) FROM user WHERE Deleted = 0 AND Status = @Status AND CompanyId = @CompanyId"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Status = UserStatusEnum.Normal, CompanyId = companyId }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 透過公司編號,取得該公司的權限池 /// /// /// public async Task> GetCompanyAuthByCompanyId(int companyId) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); try { var sql = @$"SELECT ap.*, CASE WHEN cap_id.CompanyId IS NOT NULL THEN 1 ELSE 0 END AS CheckAuth FROM auth_page ap LEFT JOIN (SELECT * FROM company_auth_page WHERE CompanyId = @CompanyId) cap_id ON ap.AuthCode = cap_id.AuthCode "; result = (await conn.QueryAsync(sql, new { CompanyId = companyId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 實際刪除公司權限池 /// /// /// public async Task PurgeCompanyAuth(List companyAuths) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"DELETE FROM company_auth_page WHERE CompanyId = @CompanyId AND AuthCode = @AuthCode"; await conn.ExecuteAsync(sql, companyAuths, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 實際刪除公司權限池 /// /// /// public async Task PurgeCompanyRoleAuth(int companyId, List companyAuths) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { //找出該公司底下有該權限的角色 var auths = companyAuths.Select(x => x.AuthCode).ToList(); var sqlRole = @$"SELECT ra.Id, ra.AuthCode FROM role_auth ra LEFT JOIN role r ON ra.Id = r.Id WHERE r.CompanyId = @CompanyId AND ra.AuthCode IN @AuthCode"; var roles = (await conn.QueryAsync(sqlRole, new { CompanyId = companyId, AuthCode = auths }, trans)).ToList(); var sql = $"DELETE FROM role_auth WHERE Id = @Id AND AuthCode = @AuthCode"; await conn.ExecuteAsync(sql, roles, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 新增公司權限池 /// /// /// /// public async Task AddCompanyAuthAsync(List entity, List properties) { int count; using (IDbConnection conn = _databaseHelper.GetConnection()) { conn.Open(); try { string sql = $"INSERT INTO company_auth_page (CompanyId, AuthCode, CreatedBy) VALUES (@CompanyId, @AuthCode, @CreatedBy)"; count = await conn.ExecuteAsync(sql, entity); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } } }