544 lines
20 KiB
C#
544 lines
20 KiB
C#
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<PowerStationController>
|
||
{
|
||
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");
|
||
}
|
||
|
||
/// <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>
|
||
/// <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.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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增 / 修改 電站基本資料
|
||
/// </summary>
|
||
/// <param name="post"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult<string>> SavePowerStationInfo(PostPowerStationInfo post)
|
||
{
|
||
ApiResult<string> apiResult = new ApiResult<string>();
|
||
|
||
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<string> properties = new List<string>()
|
||
{
|
||
"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;
|
||
}
|
||
/// <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)
|
||
{
|
||
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<string> properties = new List<string>()
|
||
{
|
||
"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<string> properties = new List<string>()
|
||
{
|
||
"Brand",
|
||
"ColName",
|
||
"PowerStationId",
|
||
"DBName",
|
||
"Id",
|
||
"Name",
|
||
"ProductModel",
|
||
"Remark",
|
||
"TableName",
|
||
"Type",
|
||
"UID",
|
||
"CreatedBy"
|
||
};
|
||
await powerStationRepository.UpdateDevice(DeviceInfo, properties);
|
||
apiResult.Code = "0000";
|
||
apiResult.Msg = "儲存成功";
|
||
}
|
||
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
apiResult.Code = "9999";
|
||
apiResult.Msg = exception.ToString();
|
||
}
|
||
return apiResult;
|
||
}
|
||
|
||
public async Task<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;
|
||
}
|
||
}
|
||
}
|