diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 540ee3f..7e7c324 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -9,6 +9,7 @@ using SolarPower.Repository.Interface; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; @@ -18,6 +19,8 @@ namespace SolarPower.Controllers { private readonly IUserRepository userRepository; private readonly IPowerStationRepository powerStationRepository; + private string boeFilePath = "/upload/power_station/boe_file/"; + private string powerSationSaveAsPath = ""; public PowerStationController( IUserRepository userRepository, @@ -25,6 +28,8 @@ namespace SolarPower.Controllers { this.userRepository = userRepository; this.powerStationRepository = powerStationRepository; + + powerSationSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "power_station"); } public IActionResult Index() { @@ -52,7 +57,7 @@ namespace SolarPower.Controllers try { EDFunction edFunction = new EDFunction(); - var companyId= Convert.ToInt32(edFunction.AESDecrypt(HttpContext.Session.GetString("CompanyId"))); //將公司id透過AES解密 + var companyId = Convert.ToInt32(edFunction.AESDecrypt(HttpContext.Session.GetString("CompanyId"))); //將公司id透過AES解密 var userSelectItemLists = await userRepository.GetUserSelectOptionListAsync(companyId); apiResult.Code = "0000"; @@ -67,6 +72,76 @@ namespace SolarPower.Controllers apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } + /// + /// 取裝置類型下拉選單 + /// + /// + public async Task>> GetDeviceTypeSelectOptionList() + { + ApiResult> apiResult = new ApiResult>(); + try + { + var userSelectItemLists = await powerStationRepository.DeviceType(); + apiResult.Code = "0000"; + apiResult.Data = userSelectItemLists; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + return apiResult; + } + + /// + /// 取得縣市選單 + /// + /// + [HttpGet] + public async Task>> GetCitySelectOptionList() + { + ApiResult> apiResult = new ApiResult>(); + try + { + var citySelectItemLists = await powerStationRepository.GetCitySelectOptionListAsync(); + + apiResult.Code = "0000"; + apiResult.Data = citySelectItemLists; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + /// + /// 取得縣市選單 + /// + /// + [HttpPost] + public async Task>> GetAreaSelectOptionList(int cityId) + { + ApiResult> apiResult = new ApiResult>(); + try + { + var areaSelectItemLists = await powerStationRepository.GetAreaSelectOptionListAsync(cityId); + + apiResult.Code = "0000"; + apiResult.Data = areaSelectItemLists; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } /// /// 取得單一電站基本資料 @@ -90,13 +165,19 @@ namespace SolarPower.Controllers apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } - else if (powerStation.Id != myUser.CompanyId) + else if (powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } + //替能源局換檔案路徑 + if (!string.IsNullOrEmpty(powerStation.BoEFile)) + { + powerStation.BoEFile = boeFilePath + powerStation.BoEFile; + } + apiResult.Code = "0000"; apiResult.Data = powerStation; @@ -117,9 +198,9 @@ namespace SolarPower.Controllers /// /// /// - public async Task> SavePowerStationInfo(PostPowerStationInfo post) + public async Task> SavePowerStationInfo(PostPowerStationInfo post) { - ApiResult apiResult = new ApiResult(); + ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; @@ -127,7 +208,7 @@ namespace SolarPower.Controllers { powerStation = await powerStationRepository.GetOneAsync(post.Id); - if(powerStation == null) + if (powerStation == null) { if (post.Id != 0) { @@ -137,12 +218,32 @@ namespace SolarPower.Controllers } #region 新增電站資訊 - powerStation = new PowerStation() { + + //取得電站該縣市的Zipcode + var zipcode = await powerStationRepository.GetCityAreaZipcodeAsync(post.AreaId); + //取得電站該縣市地區最後流水號 + var lastSerialNumber = await powerStationRepository.GetLastSerialNumberByCityAreaIdAsync(post.CityId, post.AreaId); + var tempSerialNumber = 0; + if (!string.IsNullOrEmpty(lastSerialNumber)) + { + tempSerialNumber = Convert.ToInt32(lastSerialNumber) + 1; + } + else + { + tempSerialNumber = 1; + } + + var codeFormat = "{0}-{1}-{2}"; + + powerStation = new PowerStation() + { CompanyId = myUser.CompanyId, CityId = post.CityId, AreaId = post.AreaId, Address = post.Address, Name = post.Name, + Code = String.Format(codeFormat, zipcode.City, zipcode.Area, tempSerialNumber.ToString().PadLeft(4, '0')), + SerialNumber = tempSerialNumber.ToString().PadLeft(4, '0'), IsEscrow = post.IsEscrow, EscrowName = post.EscrowName, ElectricityMeterAt = post.ElectricityMeterAt, @@ -167,6 +268,8 @@ namespace SolarPower.Controllers "AreaId", "Address", "Name", + "Code", + "SerialNumber", "IsEscrow", "EscrowName", "ElectricityMeterAt", @@ -181,30 +284,215 @@ namespace SolarPower.Controllers "PhotovoltaicPanelProductModel", "PhotovoltaicPanelSpecification", "PhotovoltaicPanelAmount", - "CreatedBy", + "CreatedBy" }; var id = await powerStationRepository.AddOneAsync(powerStation, properties); - apiResult.Data = id.ToString(); + #region 新增土地與房屋資訊 + var city = await powerStationRepository.GetOneCityByIdAsync(post.CityId); + var area = await powerStationRepository.GetOneAreaByIdAsync(post.AreaId); + + LandBuilding landBuilding = new LandBuilding() + { + Address = city.Name + area.Name + post.Address, + PowerStationId = id, + CreatedBy = myUser.Id + }; + + List landBuildingProperties = new List() + { + "Address", + "PowerStationId", + "CreatedBy", + "CreatedBy" + }; + await powerStationRepository.AddOneLandBuildingInfo(landBuilding, landBuildingProperties); + #endregion + + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + apiResult.Data = await powerStationRepository.GetOneAsync(id); #endregion } else { - if(powerStation.CompanyId != myUser.CompanyId) + if (powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } - #region 修改電站資訊 + + UpdatePowerStationInfo update = new UpdatePowerStationInfo() + { + Id = post.Id, + CityId = post.CityId, + AreaId = post.AreaId, + Address = post.Address, + Name = post.Name, + IsEscrow = post.IsEscrow, + EscrowName = post.EscrowName, + ElectricityMeterAt = post.ElectricityMeterAt, + EstimatedRecoveryTime = post.EstimatedRecoveryTime, + GeneratingCapacity = post.GeneratingCapacity, + PowerRate = post.PowerRate, + Coordinate = post.Coordinate, + InverterBrand = post.InverterBrand, + InverterProductModel = post.InverterProductModel, + InverterAmount = post.InverterAmount, + PhotovoltaicPanelBrand = post.PhotovoltaicPanelBrand, + PhotovoltaicPanelProductModel = post.PhotovoltaicPanelProductModel, + PhotovoltaicPanelSpecification = post.PhotovoltaicPanelSpecification, + PhotovoltaicPanelAmount = post.PhotovoltaicPanelAmount, + UpdatedBy = myUser.Id + }; + + List properties = new List() + { + "Id", + "CityId", + "AreaId", + "Address", + "Name", + "IsEscrow", + "EscrowName", + "ElectricityMeterAt", + "EstimatedRecoveryTime", + "GeneratingCapacity", + "PowerRate", + "Coordinate", + "InverterBrand", + "InverterProductModel", + "InverterAmount", + "PhotovoltaicPanelBrand", + "PhotovoltaicPanelProductModel", + "PhotovoltaicPanelSpecification", + "PhotovoltaicPanelAmount", + "UpdatedBy", + }; + + await powerStationRepository.UpdatePowerStationInfo(update, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + apiResult.Data = await powerStationRepository.GetOneAsync(powerStation.Id); #endregion } + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + string json = System.Text.Json.JsonSerializer.Serialize(post); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + /// + /// 新增 / 修改 能源局與台電資料 + /// + /// + /// + [HttpPost] + public async Task> SaveBoETPCInfo([FromForm] PostBoETPCInfo post) + { + ApiResult apiResult = new ApiResult(); + + PowerStation powerStation = null; + + try + { + powerStation = await powerStationRepository.GetOneAsync(post.Id); + + if (powerStation == null) + { + if (post.Id != 0) + { + apiResult.Code = "9992"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + } + else + { + if (powerStation.CompanyId != myUser.CompanyId) + { + apiResult.Code = "9993"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + #region 修改能源局與台電 + + //處理檔案 + var boeFileName = ""; + if (post.BoEFile != null) + { + var split = post.BoEFile.FileName.Split("."); + boeFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + split[split.Length - 1]; + + var fullPath = Path.Combine(powerSationSaveAsPath, "boe_file", boeFileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + post.BoEFile.CopyTo(stream); + } + } + + UpdateBoETPCInfo update = new UpdateBoETPCInfo() + { + Id = post.Id, + BoEFileName = post.BoEFile != null ? post.BoEFile.FileName : null, //原本檔名 + BoEFile = boeFileName, //自訂檔名 + BoEDiscountRate = post.BoEDiscountRate, + BoEDeviceRegisterNumber = post.BoEDeviceRegisterNumber, + BoERentRatio = post.BoERentRatio, + TPCContractNumber = post.TPCContractNumber, + TPCContractAt = post.TPCContractAt, + TPCSellDeadline = post.TPCSellDeadline, + TPCMeterReading = post.TPCMeterReading, + TPCPurchaseElectricityAt = post.TPCPurchaseElectricityAt, + TPCSellElectricityAt = post.TPCSellElectricityAt, + UpdatedBy = myUser.Id + }; + + List properties = new List() + { + "Id", + "BoEFileName", + "BoEFile", + "BoEDiscountRate", + "BoEDeviceRegisterNumber", + "BoERentRatio", + "TPCContractNumber", + "TPCContractAt", + "TPCSellDeadline", + "TPCMeterReading", + "TPCPurchaseElectricityAt", + "TPCSellElectricityAt", + "UpdatedBy", + }; + + await powerStationRepository.UpdateBoETPCInfo(update, properties); + #endregion + } + + powerStation = await powerStationRepository.GetOneAsync(powerStation.Id); + //替能源局換檔案路徑 + if (!string.IsNullOrEmpty(powerStation.BoEFile)) + { + powerStation.BoEFile = boeFilePath + powerStation.BoEFile; + } apiResult.Code = "0000"; apiResult.Msg = "修改成功"; + apiResult.Data = powerStation; } catch (Exception exception) { @@ -216,5 +504,479 @@ namespace SolarPower.Controllers return apiResult; } + + /// + /// 新增 / 修改 土地與房屋資料 + /// + /// + /// + public async Task> SaveLandBuildingInfo(PostLandBuildingInfo post) + { + ApiResult apiResult = new ApiResult(); + + PowerStation powerStation = null; + LandBuilding landBuilding = null; + + try + { + powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); + + if (powerStation == null) + { + if (post.PowerStationId != 0) + { + apiResult.Code = "9992"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + } + else + { + if (powerStation.CompanyId != myUser.CompanyId) + { + apiResult.Code = "9993"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + landBuilding = await powerStationRepository.GetOneLandBuildingInfo(post.Id); + + if (landBuilding == null) + { + if (post.Id != 0) + { + apiResult.Code = "9991"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + #region 新增土地房屋資訊 + + //取得公司在該縣市區域當前電站的流水編碼 + landBuilding = new LandBuilding() + { + PowerStationId = powerStation.Id, + Address = post.Address, + LeaseNotarizationAt = post.LeaseNotarizationAt, + Landowner = post.Landowner, + Purpose = post.Purpose, + LeaseRate = post.LeaseRate, + Coordinate = post.Coordinate, + Phone = post.Phone, + CreatedBy = myUser.Id + }; + + List properties = new List() + { + "PowerStationId", + "Address", + "LeaseNotarizationAt", + "Landowner", + "Purpose", + "LeaseRate", + "Coordinate", + "Phone", + "CreatedBy" + }; + + var id = await powerStationRepository.AddOneLandBuildingInfo(landBuilding, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + apiResult.Data = await powerStationRepository.GetOneAsync(powerStation.Id); + #endregion + } + else + { + #region 修改土地房屋資訊 + UpdateLandBuilding update = new UpdateLandBuilding() + { + Id = post.Id, + Address = post.Address, + LeaseNotarizationAt = post.LeaseNotarizationAt, + Landowner = post.Landowner, + Purpose = post.Purpose, + LeaseRate = post.LeaseRate, + Coordinate = post.Coordinate, + Phone = post.Phone, + UpdatedBy = myUser.Id + }; + + List properties = new List() + { + "Id", + "Address", + "LeaseNotarizationAt", + "Landowner", + "Purpose", + "LeaseRate", + "Coordinate", + "Phone", + "UpdatedBy", + }; + + await powerStationRepository.UpdateLandBuildingInfo(update, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "修改成功"; + apiResult.Data = await powerStationRepository.GetOneAsync(powerStation.Id); + #endregion + } + } + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + string json = System.Text.Json.JsonSerializer.Serialize(post); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 新增/修改 運維資料 + /// + /// + /// + public async Task> SaveOperation(OperationInfo post) + { + ApiResult apiResult = new ApiResult(); + try + { + if (post.Id == 0) + { + OperationInfo operation = new OperationInfo() + { + Id = post.Id, + Email = post.Email, + Name = post.Name, + Phone = post.Phone, + CreatedBy = myUser.Id, + ContactPerson = post.ContactPerson, + PowerStationId = post.PowerStationId, + Type = post.Type + }; + List properties = new List() + { + "Id", + "Email", + "Name", + "Phone", + "CreatedBy", + "ContactPerson", + "PowerStationId", + "Type" + }; + await powerStationRepository.AddOperation(operation, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + OperationInfo operation = new OperationInfo() + { + Id = post.Id, + Email = post.Email, + Name = post.Name, + Phone = post.Phone, + CreatedBy = myUser.Id, + ContactPerson = post.ContactPerson, + PowerStationId = post.PowerStationId, + Type = post.Type + }; + List properties = new List() + { + "Id", + "Email", + "Name", + "Phone", + "CreatedBy", + "ContactPerson", + "PowerStationId", + "Type" + }; + await powerStationRepository.UpdateOperation(operation, properties); + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + } + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + + return apiResult; + } + /// + /// 運維資料DataTable + /// + /// + /// + public async Task OperationTable(int stationId) + { + + List operationTable = new List(); + ApiResult> apiResult = new ApiResult>(); + try + { + apiResult.Code = "0000"; + operationTable = await powerStationRepository.OperationTable(stationId); + foreach(OperationTable a in operationTable) + { + a.Function = @" + + "; + if (a.Type == 0) + { + a.TypeName = "施工"; + } + else if(a.Type == 1) + { + a.TypeName = "清洗"; + } + else if (a.Type == 2) + { + a.TypeName = "運維"; + } + } + + apiResult.Data = operationTable; + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + var result = Json(new + { + data = apiResult + }); + + return result; + } + /// + /// 取得一筆 運維 資料 + /// + /// + /// + public async Task> GetOneOperation(int id) + { + OperationInfo operation = new OperationInfo(); + ApiResult apiResult = new ApiResult(); + try + { + apiResult.Code = "0000"; + operation = await powerStationRepository.OneOperationInfo(id); + apiResult.Data = operation; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + + return apiResult; + } + /// + /// 刪除 運維 資料 + /// + /// + /// + public async Task> DeleteOneOperation(int id) + { + ApiResult apiResult = new ApiResult(); + OperationInfo operation = new OperationInfo(); + try + { + operation = await powerStationRepository.OneOperationInfo(id); + + if (operation == null) + { + apiResult.Code = "9996"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + await powerStationRepository.DeleteOneOtherTable(operation.Id, "operation_firm"); + + apiResult.Code = "0000"; + apiResult.Msg = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + /// + /// 新增 / 修改 裝置資料 + /// + /// + /// + public async Task> SaveDevice(DeviceInfo Device) + { + ApiResult apiResult = new ApiResult(); + try + { + if (Device.Id == 0) + { + Device DeviceInfo = new Device() + { + Brand = Device.Brand, + ColName = Device.ColName, + PowerStationId = Device.PowerStationId, + DBName = Device.DBName, + Id = Device.Id, + Name = Device.Name, + ProductModel = Device.ProductModel, + Remark = Device.Remark, + TableName = Device.TableName, + Type = Device.Type, + UID = Device.PowerStationId + "-" + Device.Type, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "Brand", + "ColName", + "PowerStationId", + "DBName", + "Id", + "Name", + "ProductModel", + "Remark", + "TableName", + "Type", + "UID", + "CreatedBy" + }; + await powerStationRepository.AddDevice(DeviceInfo,properties); + + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + Device DeviceInfo = new Device() + { + Brand = Device.Brand, + ColName = Device.ColName, + PowerStationId = Device.PowerStationId, + DBName = Device.DBName, + Id = Device.Id, + Name = Device.Name, + ProductModel = Device.ProductModel, + Remark = Device.Remark, + TableName = Device.TableName, + Type = Device.Type, + UID = Device.PowerStationId + "-" + Device.Type, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "Brand", + "ColName", + "PowerStationId", + "DBName", + "Id", + "Name", + "ProductModel", + "Remark", + "TableName", + "Type", + "UID", + "CreatedBy" + }; + await powerStationRepository.UpdateDevice(DeviceInfo, properties); + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + } + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + return apiResult; + } + + public async Task DeviceTable(int stationId) + { + List deviceTables = new List(); + ApiResult> apiResult = new ApiResult>(); + try + { + apiResult.Code = "0000"; + deviceTables = await powerStationRepository.DeviceTable(stationId); + foreach (DeviceTable a in deviceTables) + { + a.Function = @" + + "; + } + apiResult.Data = deviceTables; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + var result = Json(new + { + data = apiResult + }); + return result; + } + + /// + /// 軟刪除單一土地房屋資訊 + /// + /// + /// + [HttpPost] + public async Task> DeleteLandBuildingInfo(int id) + { + ApiResult apiResult = new ApiResult(); + + + LandBuilding landBuilding; + try + { + landBuilding = await powerStationRepository.GetOneLandBuildingInfo(id); + + if (landBuilding == null) + { + apiResult.Code = "9996"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + await powerStationRepository.DeleteOneLandBuildingInfo(landBuilding.Id); + + apiResult.Code = "0000"; + apiResult.Msg = "刪除成功"; + apiResult.Data = await powerStationRepository.GetOneAsync(landBuilding.PowerStationId); + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } } } diff --git a/SolarPower/Models/ErrorCode.cs b/SolarPower/Models/ErrorCode.cs index 46d7dfd..4744706 100644 --- a/SolarPower/Models/ErrorCode.cs +++ b/SolarPower/Models/ErrorCode.cs @@ -17,6 +17,7 @@ namespace SolarPower.Models { { "0000", "OK" }, { "0001", "傳入參數錯誤。" }, + { "9991", "查無該土地房屋資訊"}, { "9992", "查無該電站資訊"}, { "9993", "無此權限操作"}, { "9994", "查無該公司角色"}, diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index ac9c806..67bce18 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.AspNetCore.Http; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -7,16 +8,33 @@ namespace SolarPower.Models.PowerStation { public class PowerStation : Created { + private string electricityMeterAt = "", tpcContractAt = "", tpcPurchaseElectricityAt = "", tpcSellElectricityAt = ""; + public int Id { get; set; } public int CompanyId { get; set; } public int CityId { get; set; } //縣市 public int AreaId { get; set; } //地區 public string Address { get; set; } //地址 public string Name { get; set; } //名稱 - //public string Code { get; set; } + public string Code { get; set; } //電站代碼 + public string SerialNumber { get; set; } //四碼流水號 public byte IsEscrow { get; set; } //是否被代管 public string EscrowName { get; set; } //被代管公司 - public string ElectricityMeterAt { get; set; } //台電掛錶日 + public string ElectricityMeterAt //台電掛錶日 + { + get + { + if (!string.IsNullOrEmpty(electricityMeterAt)) + { + return Convert.ToDateTime(electricityMeterAt).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { electricityMeterAt = value; } + } public int EstimatedRecoveryTime { get; set; } //預計回收年限 public double GeneratingCapacity { get; set; } //發電容量 public double PowerRate { get; set; } //授電費率 @@ -28,30 +46,91 @@ namespace SolarPower.Models.PowerStation public string PhotovoltaicPanelProductModel { get; set; } //光電板型號 public string PhotovoltaicPanelSpecification { get; set; } //光電板規格 public int PhotovoltaicPanelAmount { get; set; } //光電板規格 + public string BoEFileName { get; set; } //能源局原檔案名 public string BoEFile { get; set; } //能源局檔案 public int BoEDiscountRate { get; set; } //能源局折扣率 public string BoEDeviceRegisterNumber { get; set; } //能源局設備登記編號 public int BoERentRatio { get; set; } //能源局租金比例 public string TPCContractNumber { get; set; } //台電契約編號 - public string TPCContractAt { get; set; } //台電簽約日期 + public string TPCContractAt //台電簽約日期 + { + get + { + if (!string.IsNullOrEmpty(tpcContractAt)) + { + return Convert.ToDateTime(tpcContractAt).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { tpcContractAt = value; } + } public int TPCSellDeadline { get; set; } //台電售電期限(年) - public string TPCPurchaseElectricityAt { get; set; } //台電正式購電日 - public string TPCSellElectricityAt { get; set; } //台電正式售電日 + public int TPCMeterReading { get; set; } //台電每期抄錶日 + public string TPCPurchaseElectricityAt //台電正式購電日 + { + get + { + if (!string.IsNullOrEmpty(tpcPurchaseElectricityAt)) + { + return Convert.ToDateTime(tpcPurchaseElectricityAt).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { tpcPurchaseElectricityAt = value; } + } + public string TPCSellElectricityAt //台電正式售電日 + { + get + { + if (!string.IsNullOrEmpty(tpcPurchaseElectricityAt)) + { + return Convert.ToDateTime(tpcPurchaseElectricityAt).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { tpcPurchaseElectricityAt = value; } + } public List LandBuildings { get; set; } //土地房屋資料 - + public string CreatorName { get; set; } //創建者名稱 } public class LandBuilding : Created { + private string leaseNotarizationAt; + public int Id { get; set; } public int PowerStationId { get; set; } public string Address { get; set; } - public string LeaseNotarizationAt { get; set; } //租約公證日期 + public string LeaseNotarizationAt //租約公證日期 + { + get + { + if (!string.IsNullOrEmpty(leaseNotarizationAt)) + { + return Convert.ToDateTime(leaseNotarizationAt).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { leaseNotarizationAt = value; } + } public string Landowner { get; set; } //地主姓名 public string Purpose { get; set; } //房屋用途 public int LeaseRate { get; set; } //租金比例 public string Coordinate { get; set; } //經緯度 public string Phone { get; set; } //電話 + public string CreatorName { get; set; } //創建者名稱 } @@ -78,4 +157,179 @@ namespace SolarPower.Models.PowerStation public string PhotovoltaicPanelSpecification { get; set; } //光電板規格 public int PhotovoltaicPanelAmount { get; set; } //光電板規格 } + + public class UpdatePowerStationInfo : Updated + { + public int CityId { get; set; } //縣市 + public int AreaId { get; set; } //地區 + public string Address { get; set; } //地址 + public string Name { get; set; } //名稱 + public byte IsEscrow { get; set; } //是否被代管 + public string EscrowName { get; set; } //被代管公司 + public string ElectricityMeterAt { get; set; } //台電掛錶日 + public int EstimatedRecoveryTime { get; set; } //預計回收年限 + public double GeneratingCapacity { get; set; } //發電容量 + public double PowerRate { get; set; } //授電費率 + public string Coordinate { get; set; } //座標 + public string InverterBrand { get; set; } //逆變器廠牌 + public string InverterProductModel { get; set; } //逆變器型號 + public int InverterAmount { get; set; } //逆變器數量 + public string PhotovoltaicPanelBrand { get; set; } //光電板廠牌 + public string PhotovoltaicPanelProductModel { get; set; } //光電板型號 + public string PhotovoltaicPanelSpecification { get; set; } //光電板規格 + public int PhotovoltaicPanelAmount { get; set; } //光電板規格 + } + + public class PostBoETPCInfo + { + public int Id { get; set; } + public IFormFile BoEFile { get; set; } + public int BoEDiscountRate { get; set; } + public string BoEDeviceRegisterNumber { get; set; } + public int BoERentRatio { get; set; } + public string TPCContractNumber { get; set; } + public string TPCContractAt { get; set; } + public int TPCSellDeadline { get; set; } + public int TPCMeterReading { get; set; } + public string TPCPurchaseElectricityAt { get; set; } + public string TPCSellElectricityAt { get; set; } + } + + public class UpdateBoETPCInfo : Updated + { + public string BoEFileName { get; set; } + public string BoEFile { get; set; } + public int BoEDiscountRate { get; set; } + public string BoEDeviceRegisterNumber { get; set; } + public int BoERentRatio { get; set; } + public string TPCContractNumber { get; set; } + public string TPCContractAt { get; set; } + public int TPCSellDeadline { get; set; } + public int TPCMeterReading { get; set; } + public string TPCPurchaseElectricityAt { get; set; } + public string TPCSellElectricityAt { get; set; } + } + + public class PostLandBuildingInfo + { + public int Id { get; set; } + public int PowerStationId { get; set; } + public string Address { get; set; } + public string LeaseNotarizationAt { get; set; } + public string Landowner { get; set; } + public string Purpose { get; set; } + public int LeaseRate { get; set; } + public string Coordinate { get; set; } + public string Phone { get; set; } + } + + public class UpdateLandBuilding : Updated + { + public string Address { get; set; } + public string LeaseNotarizationAt { get; set; } + public string Landowner { get; set; } + public string Purpose { get; set; } + public int LeaseRate { get; set; } + public string Coordinate { get; set; } + public string Phone { get; set; } + } + + public class CitySelectItemList + { + public string Text { get; set; } + public string Value { get; set; } + } + + public class AreaSelectItemList + { + public string Text { get; set; } + public string Value { get; set; } + } + + public class Zipcode + { + public string City { get; set; } + public string Area { get; set; } + } + + public class City + { + public int Id { get; set; } + public string Name { get; set; } + public string ZipCode { get; set; } + public int Priority { get; set; } + } + + public class Area + { + public int Id { get; set; } + public int CityId { get; set; } + public string Name { get; set; } + public string ZipCode { get; set; } + } + + + public class OperationInfo : Created + { + public int Id { get; set; } + public int PowerStationId { get; set; }//電廠id + public int Type { get; set; }//廠商類別 + public string Name { get; set; }//名稱 + public string ContactPerson { get; set; }//聯絡人 + public string Phone { get; set; }//電話 + public string Email { get; set; }//Email + } + public class OperationStationId + { + public int stationId { get; set; } + } + public class OperationTable : OperationInfo + { + public string CreatedName { get; set; }//建立者名稱 + public string Function { get; set; }//功能 + public string TypeName { get; set; } + } + /// + /// 設備裝置下拉選單 + /// + public class Type + { + public string Name { get; set; } + public string EName { get; set; } + } + public class Root + { + public List Type { get; set; } + } + public class Variable + { + public string name { get; set; } + public string value { get; set; } + } + /// + /// 設備 + /// + public class DeviceInfo + { + public int Id { get; set; } + public int PowerStationId { get; set; }//所屬電站編號 + public string Name { get; set; }//名稱 + public string Type { get; set; }//類型 + public string Brand { get; set; }//廠牌 + public string ProductModel { get; set; }//型號 + public string DBName { get; set; } + public string TableName { get; set; } + public string ColName { get; set; } + public string Remark { get; set; } + } + public class Device : DeviceInfo + { + public string UID { get; set; }//設備編號 + public int CreatedBy { get; set; }//建立者 + } + public class DeviceTable : DeviceInfo + { + public string UID { get; set; }//設備編號 + public string Function { get; set; }//功能 + } } diff --git a/SolarPower/Models/Share.cs b/SolarPower/Models/Share.cs index 1bccedd..1dad64f 100644 --- a/SolarPower/Models/Share.cs +++ b/SolarPower/Models/Share.cs @@ -186,101 +186,58 @@ namespace SolarPower.Models /// 回傳結果 /// /// 資料型別 - public class ApiResult + public class ApiResult { public string Code { get; set; } public string Msg { get; set; } - public S1 Data { get; set; } + public T Data { get; set; } } - public class DapperConnection + /// + /// 資料夾 + /// + public class FolderFunction { /// - /// 新增SQL字串 + /// 創建資料夾 /// - /// Table名稱 - /// Add新增欄位名稱 - /// - //public string InsertQueryTxt(string TableName, List TableValue) - //{ - // var sqlId = ""; - // var sqlValue = ""; + /// + /// 0:找到資料夾也不刪除 1:找到資料夾並且刪除 + public void CreateFolder(string folderPath, int type) + { + DirectoryInfo dInfo = new DirectoryInfo(folderPath); - // foreach (var value in TableValue) - // { - // sqlId += value + ","; - // sqlValue += "@" + value + ","; - // } + if (dInfo.Exists) //找到 + { + if (type == 1) + { + dInfo.Delete(true); //如果資料夾裡面有檔案給bool參數就可以直接刪除,沒給bool參數會報錯誤 + } - // var connTxt = "INSERT INTO " + TableName + "(" + sqlId.Substring(0, sqlId.Length - 1) + ") VALUES" + "(" + sqlValue.Substring(0, sqlValue.Length - 1) + ");"; - // return connTxt; - //} + dInfo.Create(); + } + else //沒找到 + { + dInfo.Create(); //建立新的資料夾 + } + } /// - /// 修改SQL字串 + /// 刪除檔案 /// - /// Table名稱 - /// Update變動欄位名稱 - /// where條件 - /// - //public string UpdateQueryTxt(string TableName, List TableValue, string sWhere = "") - //{ - // var sqlTxt = ""; - - // foreach (var value in TableValue) - // { - // sqlTxt += value + "=@" + value + ","; - // } - - // var connTxt = "UPDATE " + TableName + " SET " + sqlTxt.Substring(0, sqlTxt.Length - 1); - - // if (sWhere != "") - // { - // connTxt += " WHERE " + sWhere; - // } - - // return connTxt; - //} - - //private string GenerateInsertQuery() - //{ - // var insertQuery = new StringBuilder($"INSERT INTO {_tableName} "); - - // insertQuery.Append("("); - - // var properties = GenerateListOfProperties(GetProperties); - // properties.ForEach(prop => { insertQuery.Append($"[{prop}],"); }); - - // insertQuery - // .Remove(insertQuery.Length - 1, 1) - // .Append(") VALUES ("); - - // properties.ForEach(prop => { insertQuery.Append($"@{prop},"); }); - - // insertQuery - // .Remove(insertQuery.Length - 1, 1) - // .Append(")"); - - // return insertQuery.ToString(); - //} - - //private string GenerateUpdateQuery() - //{ - // var updateQuery = new StringBuilder($"UPDATE {_tableName} SET "); - // var properties = GenerateListOfProperties(GetProperties); - - // 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"); - - // return updateQuery.ToString(); - //} + /// + public void DeleteFile(string filePath) + { + System.IO.FileInfo file = new System.IO.FileInfo(filePath); + if (File.Exists(filePath)) //找到 + { + //do something + file.Delete(); + } + else //沒找到 + { + //do something + } + } } } diff --git a/SolarPower/Models/User.cs b/SolarPower/Models/User.cs index 78477d7..c380ccd 100644 --- a/SolarPower/Models/User.cs +++ b/SolarPower/Models/User.cs @@ -10,7 +10,7 @@ namespace SolarPower.Models.User Suspend = 0, //停權 Normal = 1, //正常 } - + //Base Class。如由其餘需求,使用繼承 public class User : Created { diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index c86f08e..029c9e2 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1,12 +1,15 @@ 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 { @@ -17,6 +20,159 @@ namespace SolarPower.Repository.Implement tableName = "power_station"; } + /// + /// 查詢縣市列表 + /// + /// + public async Task> GetCitySelectOptionListAsync() + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT Id AS Value, Name AS Text FROM city"; + + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 查詢縣市列表 + /// + /// + /// + public async Task> GetAreaSelectOptionListAsync(int cityId) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT Id AS Value, Name AS Text FROM Area WHERE CityId = @CityId"; + + result = (await conn.QueryAsync(sql, new { CityId = cityId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 透過編號取得,縣市資訊 + /// + /// + /// + public async Task GetOneCityByIdAsync(int cityId) + { + City result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT * FROM city WHERE Id=@Id"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = cityId }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 透過編號取得,地區資訊 + /// + /// + /// + public async Task GetOneAreaByIdAsync(int areaId) + { + Area result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT * FROM area WHERE Id=@Id"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = areaId }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 取得縣市地區代碼 + /// + /// + /// + public async Task GetCityAreaZipcodeAsync(int areaId) + { + Zipcode result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT a.ZipCode AS Area, c.ZipCode AS City FROM area a + LEFT JOIN city c ON a.cityId = c.Id + WHERE a.Id = @AreaId"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { AreaId = areaId }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 透過縣市地區編號,取得該縣市地區最後的流水號 + /// + /// + /// + /// + public async Task GetLastSerialNumberByCityAreaIdAsync(int cityId, int areaId) + { + string result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT SerialNumber FROM {tableName} + WHERE CityId = @CityId && AreaId = @AreaId ORDER BY SerialNumber DESC"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { CityId = cityId, AreaId = areaId }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + + /// + /// 透過電站編號,取得單一電站資訊(覆寫) + /// + /// + /// public override async Task GetOneAsync(int id) { //base.GetOneAsync(id); @@ -27,13 +183,17 @@ namespace SolarPower.Repository.Implement conn.Open(); try { - var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0 AND Id = @Id"; + var sql = @$"SELECT ps.*, u.Name AS CreatorName FROM {tableName} ps + LEFT JOIN user u ON ps.CreatedBy = u.Id + WHERE ps.Deleted = 0 AND ps.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"; + var sql_land_building = @$"SELECT lb.*, u.Name AS CreatorName FROM land_building lb + LEFT JOIN user u ON lb.CreatedBy = u.Id + WHERE lb.Deleted = 0 AND PowerStationId = @PowerStationId"; result.LandBuildings = (await conn.QueryAsync(sql_land_building, new { PowerStationId = result.Id })).ToList(); } } @@ -48,5 +208,438 @@ namespace SolarPower.Repository.Implement return result; } } + + /// + /// 修改電站基本資訊 + /// + /// + /// + public async Task UpdatePowerStationInfo(UpdatePowerStationInfo 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 UpdateBoETPCInfo(UpdateBoETPCInfo 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 GetOneLandBuildingInfo(int id) + { + LandBuilding result; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + var sql = @"SELECT * FROM land_building 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 AddOneLandBuildingInfo(LandBuilding entity, List properties) + { + int id; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + string sql = GenerateInsertQueryWithCustomTable(properties, "land_building"); + + sql += "SELECT LAST_INSERT_ID();"; + + id = (await conn.QueryAsync(sql, entity)).Single(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return id; + } + } + + /// + /// 更新 土地房屋資訊 + /// + /// + /// + /// + public async Task UpdateLandBuildingInfo(UpdateLandBuilding entity, List properties) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = GenerateUpdateQueryWithCustomTable(properties, "land_building"); + + await conn.ExecuteAsync(sql, entity, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + /// + /// 軟刪除土地房屋資訊 + /// + /// + /// + public async Task DeleteOneLandBuildingInfo(int id) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = $"UPDATE land_building 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 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 = GenerateUpdateQueryWithCustomTable(properties, "device"); + await conn.ExecuteAsync(updateQuery.ToString(), DeviceInfo, trans); + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + /// + /// 裝置dataTable + /// + /// + /// + public async Task> DeviceTable(int stationId) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + List Device = new List(); + try + { + string sql = @$"SELECT * FROM device WHERE Deleted = 0 AND PowerStationId = @StationId"; + Device = (await conn.QueryAsync(sql, new { StationId = stationId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return Device; + } + } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index ed47b6a..7f20ed9 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -87,6 +87,7 @@ namespace SolarPower.Repository.Implement return id; } } + /// /// 透過Id,軟刪除單一筆資料 @@ -121,6 +122,40 @@ namespace SolarPower.Repository.Implement } } } + /// + /// 透過Id,軟刪除單一筆資料(不同資料表) + /// + /// + /// + /// + public virtual async Task DeleteOneOtherTable(int id,string table_name) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = $"UPDATE {table_name} 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(); + } + + } + } + } /// /// 取得所有資料 @@ -282,7 +317,7 @@ namespace SolarPower.Repository.Implement insertQuery .Remove(insertQuery.Length - 1, 1) - .Append(")"); + .Append(");"); return insertQuery.ToString(); } @@ -309,5 +344,29 @@ namespace SolarPower.Repository.Implement return updateQuery.ToString(); } + + /// + /// 產生Update語句,可選擇自己要加入資料表 + /// + /// + /// 欲新增至目標資料表 + /// + protected string GenerateUpdateQueryWithCustomTable(List properties, string table_name) + { + var updateQuery = new StringBuilder($"UPDATE {table_name} 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"); + + return updateQuery.ToString(); + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 1673730..d542640 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -1,4 +1,5 @@ using SolarPower.Models.PowerStation; +using SolarPower.Models.User; using System; using System.Collections.Generic; using System.Linq; @@ -8,5 +9,127 @@ namespace SolarPower.Repository.Interface { public interface IPowerStationRepository : IRepositoryBase { + + /// + /// 查詢縣市列表 + /// + /// + /// + Task> GetCitySelectOptionListAsync(); + + /// + /// 查詢地區列表 + /// + /// + /// + Task> GetAreaSelectOptionListAsync(int cityId); + + /// + /// 透過編號取得,縣市資訊 + /// + /// + /// + Task GetOneCityByIdAsync(int cityId); + + /// + /// 透過編號取得,地區資訊 + /// + /// + /// + Task GetOneAreaByIdAsync(int areaId); + + /// + /// 取得縣市地區代碼 + /// + /// + /// + Task GetCityAreaZipcodeAsync(int areaId); + + /// + /// 透過縣市地區編號,取得該縣市地區最後的流水號 + /// + /// + /// + /// + Task GetLastSerialNumberByCityAreaIdAsync(int cityId, int areaId); + + /// + /// 修改電站基本資訊 + /// + /// + /// + /// + Task UpdatePowerStationInfo(UpdatePowerStationInfo entity, List properties); + + /// + /// 修改能源局與台電資訊 + /// + /// + /// + /// + Task UpdateBoETPCInfo(UpdateBoETPCInfo entity, List properties); + + /// + /// 取得 土地房屋資訊 + /// + /// + /// + Task GetOneLandBuildingInfo(int id); + + /// + /// 新增 土地房屋資訊 + /// + /// + /// + /// + Task AddOneLandBuildingInfo(LandBuilding entity, List properties); + + /// + /// 更新 土地房屋資訊 + /// + /// + /// + /// + Task UpdateLandBuildingInfo(UpdateLandBuilding entity, List properties); + + /// + /// 軟刪除土地房屋資訊 + /// + /// + /// + Task DeleteOneLandBuildingInfo(int id); + Task AddOperation(OperationInfo operation, List properties); + /// + /// 運維dataTable + /// + /// + /// + Task> OperationTable (int stationId); + Task OneOperationInfo (int stationId); + Task UpdateOperation(OperationInfo operation, List properties); + /// + /// 裝置類型下拉式選單 + /// + /// + Task> DeviceType(); + /// + /// 新增 裝置 + /// + /// + /// + Task AddDevice(Device DeviceInfo, List properties); + /// + /// 修改 裝置 + /// + /// + /// + /// + Task UpdateDevice(Device DeviceInfo, List properties); + /// + /// 設備datatable + /// + /// + /// + Task> DeviceTable(int stationId); } } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index b83be3f..42f719d 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -58,5 +58,13 @@ namespace SolarPower.Repository.Interface /// /// Task PurgeOneAsync(int id); + /// + /// 透過Id,軟刪除單一筆資料(不同資料表) + /// + /// + /// + /// + Task DeleteOneOtherTable(int id, string tablename); + } } diff --git a/SolarPower/SolarPower.csproj b/SolarPower/SolarPower.csproj index cc2bb36..268efd8 100644 --- a/SolarPower/SolarPower.csproj +++ b/SolarPower/SolarPower.csproj @@ -18,6 +18,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SolarPower/Views/Login/Index.cshtml b/SolarPower/Views/Login/Index.cshtml index bd8bf5d..f58a482 100644 --- a/SolarPower/Views/Login/Index.cshtml +++ b/SolarPower/Views/Login/Index.cshtml @@ -12,7 +12,7 @@ - + diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 5a93d45..9836ba5 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -24,7 +24,10 @@ - 新竹巨城站 + + + 新增電站 +