using Dapper; using SolarPower.Helper; using SolarPower.Models.PowerStation; using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.Json; namespace SolarPower.Repository.Implement { public class PowerStationRepository : RepositoryBase, IPowerStationRepository { public PowerStationRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { tableName = "power_station"; } public override async Task GetOneAsync(int id) { //base.GetOneAsync(id); PowerStation 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}); if(result!= null) { var sql_land_building = @"SELECT * FROM land_building WHERE Deleted = 0 AND PowerStationId = @PowerStationId"; result.LandBuildings = (await conn.QueryAsync(sql_land_building, new { PowerStationId = result.Id })).ToList(); } } catch (Exception exception) { throw exception; } finally { conn.Close(); } return result; } } public async Task AddOperation(OperationInfo operation, List properties) { using (IDbConnection conn = _databaseHelper.GetConnection()) { int count; conn.Open(); try { string sql = GenerateInsertQueryWithCustomTable(properties, "operation_firm"); count = await conn.ExecuteAsync(sql, operation); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return count; } } /// /// 運維DataTable /// /// /// public async Task> OperationTable (int stationId) { using (IDbConnection conn = _databaseHelper.GetConnection()) { List operation = new List(); conn.Open(); try { string sql = @$"SELECT operation_firm.Name, operation_firm.PowerStationId, operation_firm.Id,operation_firm.ContactPerson,operation_firm.Phone,operation_firm.Email,user.Name AS CreatedName,operation_firm.CreatedAt,operation_firm.Type FROM operation_firm LEFT JOIN user ON operation_firm.CreatedBy = user.id WHERE operation_firm.Deleted = 0 AND operation_firm.PowerStationId = @StationId"; operation = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return operation; } } /// /// 選取單一運維 /// /// /// public async Task OneOperationInfo (int id) { using (IDbConnection conn = _databaseHelper.GetConnection()) { OperationInfo operation; conn.Open(); try { string sql = @$"SELECT * FROM operation_firm WHERE Id = @Id"; operation = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); } catch (Exception exception) { throw exception; } finally { conn.Close(); } return operation; } } public async Task UpdateOperation(OperationInfo operation , List properties) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); var trans = conn.BeginTransaction(); try { var updateQuery = new StringBuilder($"UPDATE operation_firm 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"); await conn.ExecuteAsync(updateQuery.ToString(), operation, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } /// /// 裝置類型下拉選單 /// /// public async Task> DeviceType() { List result = new List(); using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); var trans = conn.BeginTransaction(); try { string sql = @$"SELECT * FROM variable WHERE name = @name"; var json = await conn.QueryFirstOrDefaultAsync(sql, new { name = "Type" }); Root jsonfor = JsonSerializer.Deserialize(json.value); foreach(Models.PowerStation.Type a in jsonfor.Type) { UserSelectItemList KeyValue = new UserSelectItemList { Value = a.EName, Text = a.Name }; result.Add(KeyValue); } trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } return result; } /// /// 新增裝置資料 /// /// /// /// public async Task AddDevice(Device DeviceInfo, List properties) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); try { string sql = GenerateInsertQueryWithCustomTable(properties, "device"); await conn.ExecuteAsync(sql, DeviceInfo); } catch (Exception exception) { throw exception; } finally { conn.Close(); } } /// /// 修改裝置資料 /// /// /// /// public async Task UpdateDevice(Device DeviceInfo, List properties) { using IDbConnection conn = _databaseHelper.GetConnection(); conn.Open(); var trans = conn.BeginTransaction(); try { var updateQuery = GenerateUpdateQueryOtherTable(properties, "device"); await conn.ExecuteAsync(updateQuery.ToString(), DeviceInfo, trans); trans.Commit(); } catch (Exception exception) { trans.Rollback(); throw exception; } finally { conn.Close(); } } } }