using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SolarPower.Models; using SolarPower.Models.PowerStation; using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class PowerStationController : MyBaseController { private readonly IUserRepository userRepository; private readonly ICompanyRepository companyRepository; private readonly IPowerStationRepository powerStationRepository; private string boeFilePath = "/upload/power_station/boe_file/"; private string stationImageFilePath = "/upload/power_station/"; private string powerSationSaveAsPath = ""; public PowerStationController( IUserRepository userRepository, ICompanyRepository companyRepository, IPowerStationRepository powerStationRepository) : base() { this.userRepository = userRepository; this.companyRepository = companyRepository; this.powerStationRepository = powerStationRepository; powerSationSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "power_station"); } public IActionResult Index() { return View(); } public IActionResult Edit() { return View("~/Views/PowerStation/PowerStationEdit.cshtml"); } /// /// 取得下拉式公司選單,須為Deleted: 0 /// /// [HttpGet] public async Task>> GetUserSelectOptionListAsync() { ApiResult> apiResult = new ApiResult>(); try { EDFunction edFunction = new EDFunction(); var companyId = Convert.ToInt32(edFunction.AESDecrypt(HttpContext.Session.GetString("CompanyId"))); //將公司id透過AES解密 var userSelectItemLists = await userRepository.GetUserSelectOptionListAsync(companyId); apiResult.Code = "0000"; apiResult.Data = userSelectItemLists; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } 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; } [HttpPost] public async Task>> GetOperationPersonnelSelectOptionList(int powerStationId) { ApiResult> apiResult = new ApiResult>(); try { var personnelSelectItemLists = await powerStationRepository.GetOperationPersonnelSelectOptionListAsync(powerStationId); apiResult.Code = "0000"; apiResult.Data = personnelSelectItemLists; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 取得單一電站基本資料 /// /// /// [HttpPost] public async Task> GetOnePowerStation(int id) { ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(id); if (powerStation == null) { 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; } //替能源局換檔案路徑 if (!string.IsNullOrEmpty(powerStation.BoEFile)) { powerStation.BoEFile = boeFilePath + powerStation.BoEFile; } apiResult.Code = "0000"; apiResult.Data = powerStation; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 新增 / 修改 電站基本資料 /// /// /// public async Task> SavePowerStationInfo(PostPowerStationInfo post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.Id); //取得該公司DB Name var company = await companyRepository.GetOneAsync(myUser.CompanyId); if (powerStation == null) { if (post.Id != 0) { apiResult.Code = "9992"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } #region 新增電站資訊 //取得電站該縣市的Zipcode var zipcode = await powerStationRepository.GetCityAreaZipcodeAsync(post.AreaId); //取得電站該縣市地區最後流水號 var currentSerialNumber = await powerStationRepository.GetLastSerialNumberByCityAreaIdAsync(post.CityId, post.AreaId); var tempSerialNumber = GetLastSerialNumber(currentSerialNumber); 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), SerialNumber = tempSerialNumber, 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, SiteDB = company.SiteDB, CreatedBy = myUser.Id }; List properties = new List() { "CompanyId", "CityId", "AreaId", "Address", "Name", "Code", "SerialNumber", "IsEscrow", "EscrowName", "ElectricityMeterAt", "EstimatedRecoveryTime", "GeneratingCapacity", "PowerRate", "Coordinate", "InverterBrand", "InverterProductModel", "InverterAmount", "PhotovoltaicPanelBrand", "PhotovoltaicPanelProductModel", "PhotovoltaicPanelSpecification", "PhotovoltaicPanelAmount", "SiteDB", "CreatedBy" }; var id = await powerStationRepository.AddOnePowerStationAsync(powerStation, properties, company.SiteDB); #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" }; await powerStationRepository.AddOneLandBuildingInfo(landBuilding, landBuildingProperties, company.SiteDB); #endregion #region 新增運維人員 //找出要新增的 if (post.OperationPersonnelIds != null) { List insertOperationPersonnels = new List(); foreach (var op in post.OperationPersonnelIds) { PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel(); operationPersonnel.PowerStationId = id; operationPersonnel.UserId = op; operationPersonnel.CreatedBy = myUser.Id; insertOperationPersonnels.Add(operationPersonnel); } List operationPersonnelProperties = new List() { "PowerStationId", "UserId", "CreatedBy", }; await powerStationRepository.AddOperationPersonnelAsync(insertOperationPersonnels, operationPersonnelProperties, company.SiteDB); } #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; apiResult.Data = await powerStationRepository.GetOneAsync(id); #endregion } else { 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, powerStation.SiteDB); List origOperationPersonnels = null; //原先的運維人員 origOperationPersonnels = await powerStationRepository.GetOperationPersonnelIdsByPowerStatioinId(powerStation.Id, powerStation.SiteDB); //判斷新進來的資料是否要歸類到新增 or 刪除 #region 刪除電站運維人員編號 //找出要刪除的 List deleteOperationPersonnelIds = origOperationPersonnels.Where(x => !post.OperationPersonnelIds.Contains(x)).ToList(); List deleteOperationPersonnels = new List(); foreach (var opId in deleteOperationPersonnelIds) { PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel(); operationPersonnel.PowerStationId = powerStation.Id; operationPersonnel.UserId = opId; deleteOperationPersonnels.Add(operationPersonnel); } //刪除運維人員 await powerStationRepository.DeleteOperationPersonnel(deleteOperationPersonnels, powerStation.SiteDB); #endregion #region 新增電站運維人員 //找出要新增的 if (post.OperationPersonnelIds != null) { List insertOperationPersonnelIds = post.OperationPersonnelIds.Where(x => !origOperationPersonnels.Contains(x)).ToList(); List insertOperationPersonnels = new List(); foreach (var op in insertOperationPersonnelIds) { PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel(); operationPersonnel.PowerStationId = powerStation.Id; operationPersonnel.UserId = op; operationPersonnel.CreatedBy = myUser.Id; insertOperationPersonnels.Add(operationPersonnel); } List operationPersonnelProperties = new List() { "PowerStationId", "UserId", "CreatedBy", }; await powerStationRepository.AddOperationPersonnelAsync(insertOperationPersonnels, operationPersonnelProperties, powerStation.SiteDB); } #endregion 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, powerStation.SiteDB); #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) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } 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, powerStation.SiteDB); 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, powerStation.SiteDB); 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, powerStation.SiteDB); 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(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); 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, powerStation.SiteDB); 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, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } 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; } /// /// 運維資料DataTable /// /// /// public async Task OperationTable(int stationId) { List operationTable = new List(); ApiResult> apiResult = new ApiResult>(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(stationId); operationTable = await powerStationRepository.OperationTable(stationId, powerStation.SiteDB); 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.Code = "0000"; apiResult.Data = operationTable; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId = " + stationId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } /// /// 取得一筆 運維 資料 /// /// /// public async Task> GetOneOperation(PostPowerStationIdAndSelectedId post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); apiResult.Code = "0000"; var operation = await powerStationRepository.OneOperationInfo(post.SelectedId, powerStation.SiteDB); apiResult.Data = operation; } 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> DeleteOneOperation(PostPowerStationIdAndSelectedId post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); var operation = await powerStationRepository.OneOperationInfo(post.SelectedId, powerStation.SiteDB); if (operation == null) { apiResult.Code = "9996"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await powerStationRepository.DeleteOneByIdWithCustomDBNameAndTable(operation.Id, powerStation.SiteDB, "operation_firm"); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } 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> SaveDevice(DeviceInfo Device) { ApiResult apiResult = new ApiResult(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(Device.PowerStationId); if (Device.Id == 0) { string Number = await powerStationRepository.GetFinalSerialNumber(Device.PowerStationId, Device.Type,powerStation.SiteDB); var tempSerialNumber = 0; if (!string.IsNullOrEmpty(Number)) { tempSerialNumber = Convert.ToInt32(Number) + 1; } else { tempSerialNumber = 1; } 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 = powerStation.Code + "-" + Device.Type + "-" + tempSerialNumber.ToString().PadLeft(3, '0'), CreatedBy = myUser.Id, TypeName = Device.TypeName, SerialNumber = tempSerialNumber.ToString().PadLeft(3, '0') }; List properties = new List() { "Brand", "ColName", "PowerStationId", "DBName", "Id", "Name", "ProductModel", "Remark", "TableName", "Type", "UID", "CreatedBy", "TypeName", "SerialNumber" }; await powerStationRepository.AddDevice(DeviceInfo, properties, powerStation.SiteDB); 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, CreatedBy = myUser.Id, TypeName = Device.TypeName }; List properties = new List() { "Brand", "ColName", "PowerStationId", "DBName", "Id", "Name", "ProductModel", "Remark", "CreatedBy", "TypeName" }; await powerStationRepository.UpdateDevice(DeviceInfo, properties, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Device=" + Device); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 設備DataTable /// /// /// public async Task DeviceTable(int stationId) { List deviceTables = new List(); ApiResult> apiResult = new ApiResult>(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(stationId); apiResult.Code = "0000"; deviceTables = await powerStationRepository.DeviceTable(stationId, powerStation.SiteDB); foreach (DeviceTable a in deviceTables) { a.Function = @" "; } apiResult.Data = deviceTables; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId=" + stationId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } /// /// 軟刪除單一土地房屋資訊 /// /// /// [HttpPost] public async Task> DeleteLandBuildingInfo(PostPowerStationIdAndSelectedId post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation = null; LandBuilding landBuilding; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); landBuilding = await powerStationRepository.GetOneLandBuildingInfo(post.SelectedId, powerStation.SiteDB); if (landBuilding == null) { apiResult.Code = "9991"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await powerStationRepository.DeleteOneLandBuildingInfo(landBuilding.Id, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; apiResult.Data = await powerStationRepository.GetOneAsync(landBuilding.PowerStationId); } 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> GetOneDevice(PostPowerStationIdAndSelectedId id) { DeviceInfo Device = new DeviceInfo(); ApiResult apiResult = new ApiResult(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); apiResult.Code = "0000"; Device = await powerStationRepository.OneDeviceInfo(id.SelectedId, powerStation.SiteDB); apiResult.Data = Device; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 刪除設備 /// /// /// public async Task> DeleteOneDevice(PostPowerStationIdAndSelectedId id) { ApiResult apiResult = new ApiResult(); DeviceInfo Device = new DeviceInfo(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); Device = await powerStationRepository.OneDeviceInfo(id.SelectedId, powerStation.SiteDB); if (Device == null) { apiResult.Code = "9996"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } //TODO //await powerStationRepository.DeleteOneByIdWithCustomDBNameAndTable(Device.Id, "device"); 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> SaveException(ExceptionModal exceptionModal) { ApiResult apiResult = new ApiResult(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(exceptionModal.PowerStationId); if (exceptionModal.Id == 0) { ExceptionModal Exception = new ExceptionModal() { Alarm = exceptionModal.Alarm, CreatedBy = myUser.Id, PowerStationId = exceptionModal.PowerStationId, Id = exceptionModal.Id, LowerLimit = exceptionModal.LowerLimit, Type = exceptionModal.Type, UpperLimit = exceptionModal.UpperLimit }; List properties = new List() { "Alarm", "CreatedBy", "PowerStationId", "Id", "LowerLimit", "Type", "UpperLimit", }; await powerStationRepository.AddException(Exception, properties,powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } else { ExceptionModal Exception = new ExceptionModal() { Alarm = exceptionModal.Alarm, CreatedBy = myUser.Id, PowerStationId = exceptionModal.PowerStationId, Id = exceptionModal.Id, LowerLimit = exceptionModal.LowerLimit, Type = exceptionModal.Type, UpperLimit = exceptionModal.UpperLimit }; List properties = new List() { "Alarm", "CreatedBy", "PowerStationId", "Id", "LowerLimit", "Type", "UpperLimit", }; await powerStationRepository.UpdateException(Exception, properties,powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "ExceptionModal=" + exceptionModal); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 異常dataTable /// /// /// public async Task ExceptionTable(int stationId) { List exceptionTable = new List(); ApiResult> apiResult = new ApiResult>(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(stationId); apiResult.Code = "0000"; exceptionTable = await powerStationRepository.ExceptionTable(stationId,powerStation.SiteDB); foreach (ExceptionTable a in exceptionTable) { a.Function = @" "; if (a.Type == 1) { a.TypeName = "PR值"; } if (a.Alarm == 1) { a.AlarmName = "email通知"; } } apiResult.Data = exceptionTable; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId=" + stationId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } var result = Json(new { data = apiResult }); return result; } /// /// 取一筆異常設定 /// /// /// public async Task> GetOneException(PostPowerStationIdAndSelectedId id) { ExceptionModal Exception = new ExceptionModal(); ApiResult apiResult = new ApiResult(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); apiResult.Code = "0000"; Exception = await powerStationRepository.OneException(id.SelectedId,powerStation.SiteDB); apiResult.Data = Exception; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 刪除一筆異常設定 /// /// /// public async Task> DeleteOneException(PostPowerStationIdAndSelectedId id) { ApiResult apiResult = new ApiResult(); ExceptionModal Exception = new ExceptionModal(); try { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); Exception = await powerStationRepository.OneException(id.SelectedId,powerStation.SiteDB); if (Exception == null) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } //TODO //await powerStationRepository.DeleteOneOtherTable(Exception.Id, "power_station_exception"); 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; } /// /// 取得所有電站圖片 /// /// /// [HttpPost] public async Task>> GetAllPowerStationImage(int powerStationId) { ApiResult> apiResult = new ApiResult>(); List powerStationImages = null; try { var powerStation = await powerStationRepository.GetOneAsync(powerStationId); if (powerStation == null) { 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; } powerStationImages = await powerStationRepository.GetAllPowerStationImageAsync(powerStationId, powerStation.SiteDB); foreach (var stationImage in powerStationImages) { stationImage.Image = Path.Combine(stationImageFilePath, powerStation.Id.ToString()) + "/" + stationImage.Image; } } apiResult.Code = "0000"; apiResult.Data = powerStationImages; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "PowerStationId=" + powerStationId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 新增 電站圖片 /// /// /// [HttpPost] public async Task>> SavePowerStationImages([FromForm] PostPowerStationImage post) { ApiResult> apiResult = new ApiResult>(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); if (powerStation == null) { 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 新增圖片 List powerStationImages; if (post.StationImages != null && post.StationImages.Length > 0) { FolderFunction folderFunction = new FolderFunction(); var imageSaveAsPath = Path.Combine(powerSationSaveAsPath, powerStation.Id.ToString()); folderFunction.CreateFolder(imageSaveAsPath, 0); powerStationImages = new List(); foreach (var image in post.StationImages) { var split = image.FileName.Split("."); var fileName = Guid.NewGuid() + "." + split[split.Length - 1]; var fullPath = Path.Combine(imageSaveAsPath, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { image.CopyTo(stream); } PowerStationImage powerStationImage = new PowerStationImage() { PowerStationId = powerStation.Id, Image = fileName, CreatedBy = myUser.Id }; powerStationImages.Add(powerStationImage); } List properties = new List() { "PowerStationId", "Image", "CreatedBy" }; await powerStationRepository.AddPowerStationImageAsync(powerStationImages, properties, powerStation.SiteDB); } #endregion #region 重新取得圖片資訊 powerStationImages = null; powerStationImages = await powerStationRepository.GetAllPowerStationImageAsync(powerStation.Id, powerStation.SiteDB); foreach (var stationImage in powerStationImages) { stationImage.Image = Path.Combine(stationImageFilePath, powerStation.Id.ToString()) + "/" + stationImage.Image; } #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; apiResult.Data = powerStationImages; } } 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> DeletePowerStationImage(PostPowerStationIdAndSelectedId post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation; PowerStationImage powerStationImage; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); powerStationImage = await powerStationRepository.GetOnePowerStationImageAsync(post.SelectedId, powerStation.SiteDB); if (powerStationImage == null) { apiResult.Code = "9990"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } if (powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await powerStationRepository.DeleteOnePowerStationImage(powerStationImage.Id, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } 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> ChangeMainDisplay(PostChangeMainDisplay post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation; PowerStationImage powerStationImage; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); powerStationImage = await powerStationRepository.GetOnePowerStationImageAsync(post.TargetImageId, powerStation.SiteDB); if (powerStationImage == null) { apiResult.Code = "9990"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } if (powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } UpdataPowerStationImage updata = new UpdataPowerStationImage(); List properties = new List(); //找出原本的圖片 var origMainDisplay = await powerStationRepository.GetMainDisplayAsync(post.PowerStationId, powerStation.SiteDB); if (origMainDisplay != null) { updata = new UpdataPowerStationImage() { Id=origMainDisplay.Id, IsMainDisplay = 0, UpdatedBy = myUser.Id }; properties = new List() { "Id", "IsMainDisplay", "UpdatedBy" }; await powerStationRepository.UpdatePowerStationImage(updata, properties, powerStation.SiteDB); } // 更新被選擇的圖維卡片顯示圖 updata = new UpdataPowerStationImage() { Id = post.TargetImageId, IsMainDisplay = 1, UpdatedBy = myUser.Id }; properties = new List() { "Id", "IsMainDisplay", "UpdatedBy" }; await powerStationRepository.UpdatePowerStationImage(updata, properties, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "修改卡片顯示圖成功"; } 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>> GetAllPowerStationSingleLine(int powerStationId) { ApiResult> apiResult = new ApiResult>(); List powerStationSingleLines = null; try { var powerStation = await powerStationRepository.GetOneAsync(powerStationId); if (powerStation == null) { 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; } powerStationSingleLines = await powerStationRepository.GetAllPowerStationSingleLineAsync(powerStationId, powerStation.SiteDB); foreach (var singleLine in powerStationSingleLines) { singleLine.Image = Path.Combine(stationImageFilePath, powerStation.Id.ToString()) + "/" + singleLine.Image; } } apiResult.Code = "0000"; apiResult.Data = powerStationSingleLines; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "PowerStationId=" + powerStationId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 新增 單線圖 /// /// /// [HttpPost] public async Task>> SavePowerStationSingleLine([FromForm] PostPowerStationSingleLine post) { ApiResult> apiResult = new ApiResult>(); PowerStation powerStation = null; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); if (powerStation == null) { 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 新增圖片 List powerStationSingleLines; if (post.SingleLineImages != null && post.SingleLineImages.Length > 0) { FolderFunction folderFunction = new FolderFunction(); var imageSaveAsPath = Path.Combine(powerSationSaveAsPath, powerStation.Id.ToString()); folderFunction.CreateFolder(imageSaveAsPath, 0); powerStationSingleLines = new List(); foreach (var image in post.SingleLineImages) { var split = image.FileName.Split("."); var fileName = Guid.NewGuid() + "." + split[split.Length - 1]; var fullPath = Path.Combine(imageSaveAsPath, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { image.CopyTo(stream); } PowerStationSingleLine powerStationSingleLine = new PowerStationSingleLine() { PowerStationId = powerStation.Id, Image = fileName, CreatedBy = myUser.Id }; powerStationSingleLines.Add(powerStationSingleLine); } List properties = new List() { "PowerStationId", "Image", "CreatedBy" }; await powerStationRepository.AddPowerStationSingleLineAsync(powerStationSingleLines, properties, powerStation.SiteDB); } #endregion #region 重新取得圖片資訊 powerStationSingleLines = null; powerStationSingleLines = await powerStationRepository.GetAllPowerStationSingleLineAsync(powerStation.Id, powerStation.SiteDB); foreach (var singleLine in powerStationSingleLines) { singleLine.Image = Path.Combine(stationImageFilePath, powerStation.Id.ToString()) + "/" + singleLine.Image; } #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; apiResult.Data = powerStationSingleLines; } } 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> DeletePowerStationSingleLine(PostPowerStationIdAndSelectedId post) { ApiResult apiResult = new ApiResult(); PowerStation powerStation; PowerStationSingleLine powerStationSingleLine; try { powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); if (powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } powerStationSingleLine = await powerStationRepository.GetOnePowerStationSingleLineAsync(post.SelectedId, powerStation.SiteDB); if (powerStationSingleLine == null) { apiResult.Code = "9990"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await powerStationRepository.DeleteOnePowerStationSingleLine(powerStationSingleLine.Id, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } 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>> GetSolarCitySummary() { ApiResult> apiResult = new ApiResult>(); List solaramount = new List(); try { apiResult.Code = "0000"; solaramount = await powerStationRepository.GetSolarCitySummary(myUser); apiResult.Data = solaramount; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); } return apiResult; } /// /// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量 /// /// public async Task>> GetSolarByCity(List cityid) { ApiResult> apiResult = new ApiResult>(); List solaramount = new List(); try { apiResult.Code = "0000"; solaramount = await powerStationRepository.GetSolarByCity(myUser, cityid); apiResult.Data = solaramount; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = exception.ToString(); } return apiResult; } } }