163 lines
6.4 KiB
C#
163 lines
6.4 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,
|
|
StartAt = post.StartAt
|
|
};
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"Id",
|
|
"EndAt",
|
|
"UpdatedBy",
|
|
"Kwh",
|
|
"Money",
|
|
"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;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult> RecordTable(ElectricitySoldRecordTablePost info)
|
|
{
|
|
List<ElectricitySoldRecordTable> electricitySoldRecordTable = new List<ElectricitySoldRecordTable>();
|
|
ApiResult<List<ElectricitySoldRecordTable>> apiResult = new ApiResult<List<ElectricitySoldRecordTable>>();
|
|
try
|
|
{
|
|
if(info.StationId == null || info.Time == null)
|
|
{
|
|
apiResult.Code = "0000";
|
|
}
|
|
else
|
|
{
|
|
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>";
|
|
a.CreatedDay = Convert.ToDateTime(a.CreatedAt).ToString("yyyy-MM-dd");
|
|
}
|
|
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;
|
|
}
|
|
|
|
public async Task<ApiResult<ElectricitySoldRecord>> GetOnePowerStation(int id)
|
|
{
|
|
ApiResult<ElectricitySoldRecord> apiResult = new ApiResult<ElectricitySoldRecord>();
|
|
ElectricitySoldRecord record = new ElectricitySoldRecord();
|
|
try
|
|
{
|
|
record = await electricitySoldRecordRepository.GetOneAsync(id);
|
|
apiResult.Code = "0000";
|
|
apiResult.Data = record;
|
|
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|