FIC_Solar/SolarPower/Controllers/PowerStationController.cs
2021-06-18 12:25:45 +08:00

1226 lines
47 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<PowerStationController>
{
private readonly IUserRepository userRepository;
private readonly IPowerStationRepository powerStationRepository;
private string boeFilePath = "/upload/power_station/boe_file/";
private string powerSationSaveAsPath = "";
public PowerStationController(
IUserRepository userRepository,
IPowerStationRepository powerStationRepository) : base()
{
this.userRepository = userRepository;
this.powerStationRepository = powerStationRepository;
powerSationSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "power_station");
}
public IActionResult Index()
{
return View();
}
public IActionResult Add()
{
return View("~/Views/PowerStation/PowerStationAdd.cshtml");
}
public IActionResult Edit()
{
return View("~/Views/PowerStation/PowerStationEdit.cshtml");
}
/// <summary>
/// 取得下拉式公司選單須為Deleted: 0
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult<List<UserSelectItemList>>> GetUserSelectOptionListAsync()
{
ApiResult<List<UserSelectItemList>> apiResult = new ApiResult<List<UserSelectItemList>>();
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;
}
/// <summary>
/// 取裝置類型下拉選單
/// </summary>
/// <returns></returns>
public async Task<ApiResult<List<UserSelectItemList>>> GetDeviceTypeSelectOptionList()
{
ApiResult<List<UserSelectItemList>> apiResult = new ApiResult<List<UserSelectItemList>>();
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;
}
/// <summary>
/// 取得縣市選單
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult<List<CitySelectItemList>>> GetCitySelectOptionList()
{
ApiResult<List<CitySelectItemList>> apiResult = new ApiResult<List<CitySelectItemList>>();
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;
}
/// <summary>
/// 取得縣市選單
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<AreaSelectItemList>>> GetAreaSelectOptionList(int cityId)
{
ApiResult<List<AreaSelectItemList>> apiResult = new ApiResult<List<AreaSelectItemList>>();
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;
}
/// <summary>
/// 取得單一電站基本資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<PowerStation>> GetOnePowerStation(int id)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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;
}
/// <summary>
/// 新增 / 修改 電站基本資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<PowerStation>> SavePowerStationInfo(PostPowerStationInfo post)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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
//取得電站該縣市的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,
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<string> properties = new List<string>()
{
"CompanyId",
"CityId",
"AreaId",
"Address",
"Name",
"Code",
"SerialNumber",
"IsEscrow",
"EscrowName",
"ElectricityMeterAt",
"EstimatedRecoveryTime",
"GeneratingCapacity",
"PowerRate",
"Coordinate",
"InverterBrand",
"InverterProductModel",
"InverterAmount",
"PhotovoltaicPanelBrand",
"PhotovoltaicPanelProductModel",
"PhotovoltaicPanelSpecification",
"PhotovoltaicPanelAmount",
"CreatedBy"
};
var id = await powerStationRepository.AddOneAsync(powerStation, properties);
#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<string> landBuildingProperties = new List<string>()
{
"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)
{
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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 新增 / 修改 能源局與台電資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<PowerStation>> SaveBoETPCInfo([FromForm] PostBoETPCInfo post)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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<string> properties = new List<string>()
{
"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)
{
apiResult.Code = "9999";
string json = System.Text.Json.JsonSerializer.Serialize(post);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
/// <summary>
/// 新增 / 修改 土地與房屋資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<PowerStation>> SaveLandBuildingInfo(PostLandBuildingInfo post)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 新增/修改 運維資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveOperation(OperationInfo post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 運維資料DataTable
/// </summary>
/// <param name="stationId"></param>
/// <returns></returns>
public async Task<ActionResult> OperationTable(int stationId)
{
List<OperationTable> operationTable = new List<OperationTable>();
ApiResult<List<OperationTable>> apiResult = new ApiResult<List<OperationTable>>();
try
{
apiResult.Code = "0000";
operationTable = await powerStationRepository.OperationTable(stationId);
foreach(OperationTable a in operationTable)
{
a.Function = @"
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
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;
}
/// <summary>
/// 取得一筆 運維 資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<OperationInfo>> GetOneOperation(int id)
{
OperationInfo operation = new OperationInfo();
ApiResult<OperationInfo> apiResult = new ApiResult<OperationInfo>();
try
{
apiResult.Code = "0000";
operation = await powerStationRepository.OneOperationInfo(id);
apiResult.Data = operation;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 刪除 運維 資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneOperation(int id)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 新增 / 修改 裝置資料
/// </summary>
/// <param name="Device"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveDevice(DeviceInfo Device)
{
ApiResult<string> apiResult = new ApiResult<string>();
try
{
if (Device.Id == 0)
{
PowerStation powerStation = null;
powerStation = await powerStationRepository.GetOneAsync(Device.PowerStationId);
string Number = await powerStationRepository.GetFinalSerialNumber(Device.PowerStationId, Device.Type);
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<string> properties = new List<string>()
{
"Brand",
"ColName",
"PowerStationId",
"DBName",
"Id",
"Name",
"ProductModel",
"Remark",
"TableName",
"Type",
"UID",
"CreatedBy",
"TypeName",
"SerialNumber"
};
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,
CreatedBy = myUser.Id,
TypeName = Device.TypeName
};
List<string> properties = new List<string>()
{
"Brand",
"ColName",
"PowerStationId",
"DBName",
"Id",
"Name",
"ProductModel",
"Remark",
"CreatedBy",
"TypeName"
};
await powerStationRepository.UpdateDevice(DeviceInfo, properties);
apiResult.Code = "0000";
apiResult.Msg = "儲存成功";
}
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 設備DataTable
/// </summary>
/// <param name="stationId"></param>
/// <returns></returns>
public async Task<ActionResult> DeviceTable(int stationId)
{
List<DeviceTable> deviceTables = new List<DeviceTable>();
ApiResult<List<DeviceTable>> apiResult = new ApiResult<List<DeviceTable>>();
try
{
apiResult.Code = "0000";
deviceTables = await powerStationRepository.DeviceTable(stationId);
foreach (DeviceTable a in deviceTables)
{
a.Function = @"
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
}
apiResult.Data = deviceTables;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
var result = Json(new
{
data = apiResult
});
return result;
}
/// <summary>
/// 軟刪除單一土地房屋資訊
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<PowerStation>> DeleteLandBuildingInfo(int id)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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;
}
/// <summary>
/// 取單一設備資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<DeviceInfo>> GetOneDevice(int id)
{
DeviceInfo Device = new DeviceInfo();
ApiResult<DeviceInfo> apiResult = new ApiResult<DeviceInfo>();
try
{
apiResult.Code = "0000";
Device = await powerStationRepository.OneDeviceInfo(id);
apiResult.Data = Device;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 刪除設備
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneDevice(int id)
{
ApiResult<string> apiResult = new ApiResult<string>();
DeviceInfo Device = new DeviceInfo();
try
{
Device = await powerStationRepository.OneDeviceInfo(id);
if (Device == null)
{
apiResult.Code = "9996";
apiResult.Msg = errorCode.GetString(apiResult.Code);
return apiResult;
}
await powerStationRepository.DeleteOneOtherTable(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;
}
/// <summary>
/// 新增/修改異常
/// </summary>
/// <param name="exceptionModal"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveException(ExceptionModal exceptionModal)
{
ApiResult<string> apiResult = new ApiResult<string>();
try
{
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<string> properties = new List<string>()
{
"Alarm",
"CreatedBy",
"PowerStationId",
"Id",
"LowerLimit",
"Type",
"UpperLimit",
};
await powerStationRepository.AddException(Exception, properties);
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<string> properties = new List<string>()
{
"Alarm",
"CreatedBy",
"PowerStationId",
"Id",
"LowerLimit",
"Type",
"UpperLimit",
};
await powerStationRepository.UpdateException(Exception, properties);
apiResult.Code = "0000";
apiResult.Msg = "儲存成功";
}
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 異常dataTable
/// </summary>
/// <param name="stationId"></param>
/// <returns></returns>
public async Task<ActionResult> ExceptionTable(int stationId)
{
List<ExceptionTable> exceptionTable = new List<ExceptionTable>();
ApiResult<List<ExceptionTable>> apiResult = new ApiResult<List<ExceptionTable>>();
try
{
apiResult.Code = "0000";
exceptionTable = await powerStationRepository.ExceptionTable(stationId);
foreach (ExceptionTable a in exceptionTable)
{
a.Function = @"
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
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();
}
var result = Json(new
{
data = apiResult
});
return result;
}
/// <summary>
/// 取一筆異常設定
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<ExceptionModal>> GetOneException(int id)
{
ExceptionModal Exception = new ExceptionModal();
ApiResult<ExceptionModal> apiResult = new ApiResult<ExceptionModal>();
try
{
apiResult.Code = "0000";
Exception = await powerStationRepository.OneException(id);
apiResult.Data = Exception;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 刪除一筆異常設定
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneException(int id)
{
ApiResult<string> apiResult = new ApiResult<string>();
ExceptionModal Exception = new ExceptionModal();
try
{
Exception = await powerStationRepository.OneException(id);
if (Exception == null)
{
apiResult.Code = "9996";
apiResult.Msg = errorCode.GetString(apiResult.Code);
return apiResult;
}
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;
}
}
}