using Dapper; using SolarPower.Helper; using SolarPower.Models; 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 UserRepository : RepositoryBase, IUserRepository { public UserRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { tableName = "user"; } /// /// 透過Account,取得單一筆資料 /// /// /// public async Task GetOneByAccountAsync(string account) { User result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT * FROM {tableName} WHERE deleted = 0 AND status = @Status AND account = @Account"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Status = UserStatusEnum.Normal, Account = account }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過Email,取得單一筆資料 /// /// /// public async Task GetOneByEmailAsync(string email) { User result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); try { var sql = $"SELECT * FROM {tableName} WHERE deleted = 0 AND status = @Status AND email = @Email"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Status = UserStatusEnum.Normal, Email = email }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 更換密碼 /// /// /// /// public async Task ChangePassword(string password, int id) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE {tableName} SET password = @Password WHERE id = @Id"; await conn.ExecuteAsync(sql, new { Password = password, Id = id }, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } /// /// 取得狀態為正常的使用者基本資料 /// /// /// public MyUser GetMyUserInfoByAccount(string account) { MyUser result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); try { var sql = $"SELECT * FROM {tableName} WHERE deleted = 0 AND status = @Status AND account = @Account"; result = conn.QueryFirstOrDefault(sql, new { Status = UserStatusEnum.Normal, Account = account }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } /// /// 取得使用者基本資料 /// /// /// public async Task GetOneSimpleUser(int id) { SimpleUser 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 UpdatePersonInfo(UpdateUser entity, List properties) { GetProperties = typeof(UpdateUser).GetProperties(); 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 UpdatePassword(UpdatePassword 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> GetAllByFilterAsync(PostUserFilter filter) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT u.*, c.Name AS CompanyName, r.Name AS RoleName FROM {tableName} u LEFT JOIN company c ON u.CompanyId = c.Id LEFT JOIN role r ON u.RoleId = r.Id WHERE u.Deleted = 0"; if (filter.SelectedCompanyId > 0) { sql += @" AND u.CompanyId = @SelectedCompanyId"; } if (!string.IsNullOrEmpty(filter.Name)) { sql += @" AND Name LIKE CONCAT('%', @Name, '%')"; } if (filter.SelectedRoleId > 0) { sql += @" AND u.RoleId = @SelectedRoleId"; } result = (await conn.QueryAsync(sql, filter)).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過公司,查詢使用者列表,0為全部公司的所有人 /// /// /// public async Task> GetUserSelectOptionListAsync(int companyId) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = $"SELECT Id AS Value, Name AS Text FROM {tableName} WHERE Deleted = 0"; if(companyId != 0) { sql+=@" AND CompanyId=@companyId"; } result = (await conn.QueryAsync(sql, new { companyId = companyId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } } }