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 u.*, c.Name AS CompanyName FROM `{tableName}` u LEFT JOIN company c ON u.CompanyId = c.Id WHERE u.Deleted = 0 AND u.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, opc.SPStationAmount FROM `{tableName}` u LEFT JOIN company c ON u.CompanyId = c.Id LEFT JOIN `role` r ON u.RoleId = r.Id LEFT JOIN (SELECT op.UserId, COUNT(*) AS SPStationAmount FROM power_station_operation_personnel op WHERE op.Deleted = 0 GROUP BY op.UserId) opc ON u.Id = opc.UserId WHERE u.Deleted = 0"; if (filter.SelectedCompanyId > 0) { sql += @" AND u.CompanyId = @SelectedCompanyId"; } if (!string.IsNullOrEmpty(filter.Name)) { sql += @" AND u.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; } } /// /// 透過使用者Id,取得該使用者所有管理的電站 /// /// /// public async Task> GetUserPowerStationAsync(int userId) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT op.Id, ps.Code, ps.Name AS PowerStationName, op.EmailDayReport, op.EmailMonthReport, op.EmailComplexReport, op.EmailException, CASE ps.IsEscrow WHEN 1 THEN CONCAT(ps.EscrowName, '(代管)') WHEN 0 THEN c.Name END AS EscrowName FROM power_station_operation_personnel op LEFT JOIN power_station ps ON op.PowerStationId = ps.Id LEFT JOIN company c ON ps.CompanyId = c.Id LEFT JOIN user us ON us.Id = @UserId WHERE op.Deleted = 0 AND op.UserId = @UserId"; result = (await conn.QueryAsync(sql, new { UserId = userId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過Id,取得該使用者單一管理的電站 /// /// /// public async Task GetOneUserPowerStationAsync(int id) { UserPowerStation result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT op.Id, ps.Code, ps.Name, CASE ps.IsEscrow WHEN 1 THEN CONCAT(ps.EscrowName, '(代管)') WHEN 0 THEN c.Name END AS EscrowName FROM power_station_operation_personnel op LEFT JOIN power_station ps ON op.PowerStationId = ps.Id LEFT JOIN company c ON ps.CompanyId = c.Id WHERE op.Deleted = 0 AND op.Id = @Id"; result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } return result; } } /// /// 透過編號,軟刪除使用者電站 /// /// /// public async Task DeleteOneUserPowerStationAsync(int id) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var sql = $"UPDATE power_station_operation_personnel 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(); } } } } public async Task> GetCompanyPowerStationAsync(int companyId, int userId, bool isPlatformLayer) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT ps.Id, ps.Code, ps.Name AS PowerStationName, CASE ps.IsEscrow WHEN 1 THEN CONCAT(ps.EscrowName, '(代管)') WHEN 0 THEN c.Name END AS EscrowName FROM power_station ps LEFT JOIN company c ON ps.CompanyId = c.Id WHERE ps.Id NOT IN (SELECT psop.PowerStationId FROM power_station_operation_personnel psop WHERE psop.UserId = @UserId AND psop.Deleted = 0) AND ps.Deleted = 0 "; if (!isPlatformLayer) { sql += " AND ps.CompanyId = @CompanyId"; } result = (await conn.QueryAsync(sql, new { CompanyId = companyId, UserId = userId })).ToList(); } catch (Exception exception) { throw exception; } return result; } } public async Task DeleteOneGetEmail(IdAndTypeByEmail post) { using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var changetype = ""; switch (post.Type) { case 0: changetype = "EmailDayReport"; break; case 1: changetype = "EmailMonthReport"; break; case 2: changetype = "EmailComplexReport"; break; case 3: changetype = "EmailException"; break; } var sql = @$"UPDATE power_station_operation_personnel SET {changetype} = {post.Check} WHERE Id = @Id"; await conn.ExecuteAsync(sql, new { Id = post.Id }, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } } } }