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.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class PowerStationController : MyBaseController { private readonly IUserRepository userRepository; private readonly IPowerStationRepository powerStationRepository; public PowerStationController( IUserRepository userRepository, IPowerStationRepository powerStationRepository) : base() { this.userRepository = userRepository; this.powerStationRepository = powerStationRepository; } public IActionResult Index() { return View(); } public IActionResult Add() { return View("~/Views/PowerStation/PowerStationAdd.cshtml"); } 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; } /// /// 取得單一電站基本資料 /// /// /// [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.Id != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } 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); if(powerStation == null) { if (post.Id != 0) { apiResult.Code = "9992"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } #region 新增電站資訊 powerStation = new PowerStation() { CompanyId = myUser.CompanyId, 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, CreatedBy = myUser.Id }; List properties = new List() { "CompanyId", "CityId", "AreaId", "Address", "Name", "IsEscrow", "EscrowName", "ElectricityMeterAt", "EstimatedRecoveryTime", "GeneratingCapacity", "PowerRate", "Coordinate", "InverterBrand", "InverterProductModel", "InverterAmount", "PhotovoltaicPanelBrand", "PhotovoltaicPanelProductModel", "PhotovoltaicPanelSpecification", "PhotovoltaicPanelAmount", "CreatedBy", }; var id = await powerStationRepository.AddOneAsync(powerStation, properties); apiResult.Data = id.ToString(); #endregion } else { if(powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } #region 修改電站資訊 #endregion } apiResult.Code = "0000"; apiResult.Msg = "修改成功"; } 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> 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.AddDevice(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>(); var result = Json(new { data = apiResult }); return result; } } }