FIC_Solar/SolarPower/Controllers/PowerStationController.cs
Kai 459347dcb2 1. 運維作業紀錄 修改 圖片上傳
2. 電站 運維人員 改回主DB
2021-06-26 16:39:42 +08:00

1941 lines
78 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 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");
}
/// <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;
}
[HttpPost]
public async Task<ApiResult<List<OperationPersonnelSelectItemList>>> GetOperationPersonnelSelectOptionList(int powerStationId)
{
ApiResult<List<OperationPersonnelSelectItemList>> apiResult = new ApiResult<List<OperationPersonnelSelectItemList>>();
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;
}
/// <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);
//取得該公司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<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",
"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<string> landBuildingProperties = new List<string>()
{
"Address",
"PowerStationId",
"CreatedBy"
};
await powerStationRepository.AddOneLandBuildingInfo(landBuilding, landBuildingProperties, company.SiteDB);
#endregion
#region
//找出要新增的
if (post.OperationPersonnelIds != null)
{
List<PowerStationOperationPersonnel> insertOperationPersonnels = new List<PowerStationOperationPersonnel>();
foreach (var op in post.OperationPersonnelIds)
{
PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel();
operationPersonnel.PowerStationId = id;
operationPersonnel.UserId = op;
operationPersonnel.CreatedBy = myUser.Id;
insertOperationPersonnels.Add(operationPersonnel);
}
List<string> operationPersonnelProperties = new List<string>()
{
"PowerStationId",
"UserId",
"CreatedBy",
};
await powerStationRepository.AddOperationPersonnelAsync(insertOperationPersonnels, operationPersonnelProperties);
}
#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, powerStation.SiteDB);
List<int> origOperationPersonnels = null; //原先的運維人員
origOperationPersonnels = await powerStationRepository.GetOperationPersonnelIdsByPowerStatioinId(powerStation.Id);
//判斷新進來的資料是否要歸類到新增 or 刪除
#region
//找出要刪除的
List<int> deleteOperationPersonnelIds = origOperationPersonnels.Where(x => !post.OperationPersonnelIds.Contains(x)).ToList();
List<PowerStationOperationPersonnel> deleteOperationPersonnels = new List<PowerStationOperationPersonnel>();
foreach (var opId in deleteOperationPersonnelIds)
{
PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel();
operationPersonnel.PowerStationId = powerStation.Id;
operationPersonnel.UserId = opId;
deleteOperationPersonnels.Add(operationPersonnel);
}
//刪除運維人員
await powerStationRepository.DeleteOperationPersonnel(deleteOperationPersonnels);
#endregion
#region
//找出要新增的
if (post.OperationPersonnelIds != null)
{
List<int> insertOperationPersonnelIds = post.OperationPersonnelIds.Where(x => !origOperationPersonnels.Contains(x)).ToList();
List<PowerStationOperationPersonnel> insertOperationPersonnels = new List<PowerStationOperationPersonnel>();
foreach (var op in insertOperationPersonnelIds)
{
PowerStationOperationPersonnel operationPersonnel = new PowerStationOperationPersonnel();
operationPersonnel.PowerStationId = powerStation.Id;
operationPersonnel.UserId = op;
operationPersonnel.CreatedBy = myUser.Id;
insertOperationPersonnels.Add(operationPersonnel);
}
List<string> operationPersonnelProperties = new List<string>()
{
"PowerStationId",
"UserId",
"CreatedBy",
};
await powerStationRepository.AddOperationPersonnelAsync(insertOperationPersonnels, operationPersonnelProperties);
}
#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;
}
/// <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, 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;
}
/// <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, 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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 新增/修改 運維資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveOperation(OperationInfo post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <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>>();
PowerStation powerStation = null;
try
{
powerStation = await powerStationRepository.GetOneAsync(stationId);
operationTable = await powerStationRepository.OperationTable(stationId, powerStation.SiteDB);
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.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;
}
/// <summary>
/// 取得一筆 運維 資料
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<OperationInfo>> GetOneOperation(PostPowerStationIdAndSelectedId post)
{
ApiResult<OperationInfo> apiResult = new ApiResult<OperationInfo>();
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;
}
/// <summary>
/// 刪除 運維 資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneOperation(PostPowerStationIdAndSelectedId post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 新增 / 修改 裝置資料
/// </summary>
/// <param name="Device"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveDevice(DeviceInfo Device)
{
ApiResult<string> apiResult = new ApiResult<string>();
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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <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
{
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 = @"
<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();
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId=" + stationId);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
var result = Json(new
{
data = apiResult
});
return result;
}
/// <summary>
/// 軟刪除單一土地房屋資訊
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<PowerStation>> DeleteLandBuildingInfo(PostPowerStationIdAndSelectedId post)
{
ApiResult<PowerStation> apiResult = new ApiResult<PowerStation>();
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;
}
/// <summary>
/// 取單一設備資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<DeviceInfo>> GetOneDevice(PostPowerStationIdAndSelectedId id)
{
DeviceInfo Device = new DeviceInfo();
ApiResult<DeviceInfo> apiResult = new ApiResult<DeviceInfo>();
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;
}
/// <summary>
/// 刪除設備
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneDevice(PostPowerStationIdAndSelectedId id)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 新增/修改異常
/// </summary>
/// <param name="exceptionModal"></param>
/// <returns></returns>
public async Task<ApiResult<string>> SaveException(ExceptionModal exceptionModal)
{
ApiResult<string> apiResult = new ApiResult<string>();
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<string> properties = new List<string>()
{
"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<string> properties = new List<string>()
{
"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;
}
/// <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
{
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 = @"
<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();
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "stationId=" + stationId);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
var result = Json(new
{
data = apiResult
});
return result;
}
/// <summary>
/// 取一筆異常設定
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<ExceptionModal>> GetOneException(PostPowerStationIdAndSelectedId id)
{
ExceptionModal Exception = new ExceptionModal();
ApiResult<ExceptionModal> apiResult = new ApiResult<ExceptionModal>();
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;
}
/// <summary>
/// 刪除一筆異常設定
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeleteOneException(PostPowerStationIdAndSelectedId id)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 取得所有電站圖片
/// </summary>
/// <param name="powerStationId"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<PowerStationImage>>> GetAllPowerStationImage(int powerStationId)
{
ApiResult<List<PowerStationImage>> apiResult = new ApiResult<List<PowerStationImage>>();
List<PowerStationImage> 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;
}
/// <summary>
/// 新增 電站圖片
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<PowerStationImage>>> SavePowerStationImages([FromForm] PostPowerStationImage post)
{
ApiResult<List<PowerStationImage>> apiResult = new ApiResult<List<PowerStationImage>>();
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<PowerStationImage> 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<PowerStationImage>();
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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 軟刪除 單一電站圖片
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeletePowerStationImage(PostPowerStationIdAndSelectedId post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 變更主要卡片顯示
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public async Task<ApiResult<string>> ChangeMainDisplay(PostChangeMainDisplay post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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<string> properties = new List<string>();
//找出原本的圖片
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<string>()
{
"Id",
"IsMainDisplay",
"UpdatedBy"
};
await powerStationRepository.UpdatePowerStationImage(updata, properties, powerStation.SiteDB);
}
// 更新被選擇的圖維卡片顯示圖
updata = new UpdataPowerStationImage()
{
Id = post.TargetImageId,
IsMainDisplay = 1,
UpdatedBy = myUser.Id
};
properties = new List<string>()
{
"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;
}
/// <summary>
/// 取得所有單線圖
/// </summary>
/// <param name="powerStationId"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<PowerStationSingleLine>>> GetAllPowerStationSingleLine(int powerStationId)
{
ApiResult<List<PowerStationSingleLine>> apiResult = new ApiResult<List<PowerStationSingleLine>>();
List<PowerStationSingleLine> 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;
}
/// <summary>
/// 新增 單線圖
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<List<PowerStationSingleLine>>> SavePowerStationSingleLine([FromForm] PostPowerStationSingleLine post)
{
ApiResult<List<PowerStationSingleLine>> apiResult = new ApiResult<List<PowerStationSingleLine>>();
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<PowerStationSingleLine> 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<PowerStationSingleLine>();
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<string> properties = new List<string>()
{
"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;
}
/// <summary>
/// 軟刪除 單一單線圖
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult<string>> DeletePowerStationSingleLine(PostPowerStationIdAndSelectedId post)
{
ApiResult<string> apiResult = new ApiResult<string>();
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;
}
/// <summary>
/// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量
/// </summary>
/// <returns></returns>
public async Task<ApiResult<List<SolarCityAmount>>> GetSolarCitySummary()
{
ApiResult<List<SolarCityAmount>> apiResult = new ApiResult<List<SolarCityAmount>>();
List<SolarCityAmount> solaramount = new List<SolarCityAmount>();
try
{
apiResult.Code = "0000";
solaramount = await powerStationRepository.GetSolarCitySummary(myUser);
apiResult.Data = solaramount;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
/// <summary>
/// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量
/// </summary>
/// <returns></returns>
public async Task<ApiResult<List<PowerStation>>> GetSolarByCity(List<int> cityid)
{
ApiResult<List<PowerStation>> apiResult = new ApiResult<List<PowerStation>>();
List<PowerStation> solaramount = new List<PowerStation>();
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;
}
}
}