FIC_Solar/SolarPower/Controllers/ElectricitySoldRecordController.cs
2021-08-03 12:01:00 +08:00

134 lines
5.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower.Controllers
{
public class ElectricitySoldRecordController : MyBaseController<ElectricitySoldRecordController>
{
private readonly IElectricitySoldRecordRepository electricitySoldRecordRepository;
private readonly IPowerStationRepository powerStationRepository;
public ElectricitySoldRecordController(IElectricitySoldRecordRepository electricitySoldRecordRepository, IPowerStationRepository powerStationRepository) : base()
{
this.electricitySoldRecordRepository = electricitySoldRecordRepository;
this.powerStationRepository = powerStationRepository;
}
public IActionResult Index()
{
return View();
}
public async Task<ApiResult<string>> SaveSoldMoney(ElectricitySoldRecord post)
{
ApiResult<string> apiResult = new ApiResult<string>();
PowerStation powerStation = null;
try
{
powerStation = await powerStationRepository.GetOneAsync(post.PowerstationId);
if (post.Id == 0)
{
ElectricitySoldRecord record = new ElectricitySoldRecord()
{
EndAt = post.EndAt,
CreatedBy = myUser.Id,
Kwh = post.Kwh,
Money = post.Money,
PowerstationId = post.PowerstationId,
StartAt = post.StartAt
};
List<string> properties = new List<string>()
{
"EndAt",
"CreatedBy",
"Kwh",
"Money",
"PowerstationId",
"StartAt"
};
await electricitySoldRecordRepository.AddAsync(record, properties);
apiResult.Code = "0000";
apiResult.Msg = "新增成功";
}
else
{
ElectricitySoldRecord record = new ElectricitySoldRecord()
{
Id = post.Id,
EndAt = post.EndAt,
UpdatedBy = myUser.Id,
Kwh = post.Kwh,
Money = post.Money,
PowerstationId = post.PowerstationId,
StartAt = post.StartAt
};
List<string> properties = new List<string>()
{
"Id",
"EndAt",
"UpdatedBy",
"Kwh",
"Money",
"PowerstationId",
"StartAt"
};
await electricitySoldRecordRepository.Update(record, properties);
apiResult.Code = "0000";
apiResult.Msg = "儲存成功";
}
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = errorCode.GetString(apiResult.Code);
string json = System.Text.Json.JsonSerializer.Serialize(post);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
public async Task<ActionResult> RecordTable(ElectricitySoldRecordTablePost info)
{
List<ElectricitySoldRecordTable> electricitySoldRecordTable = new List<ElectricitySoldRecordTable>();
ApiResult<List<ElectricitySoldRecordTable>> apiResult = new ApiResult<List<ElectricitySoldRecordTable>>();
try
{
electricitySoldRecordTable = await electricitySoldRecordRepository.RecordTable(info);
foreach (ElectricitySoldRecordTable a in electricitySoldRecordTable)
{
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.Code = "0000";
apiResult.Data = electricitySoldRecordTable;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "info=" + System.Text.Json.JsonSerializer.Serialize(info));
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
var result = Json(new
{
data = apiResult
});
return result;
}
}
}