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 { 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> SaveSoldMoney(ElectricitySoldRecord post) { ApiResult apiResult = new ApiResult(); 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 properties = new List() { "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 properties = new List() { "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 RecordTable(ElectricitySoldRecordTablePost info) { List electricitySoldRecordTable = new List(); ApiResult> apiResult = new ApiResult>(); try { if(info.StationId == null || info.Time == null) { apiResult.Code = "0000"; } else { electricitySoldRecordTable = await electricitySoldRecordRepository.RecordTable(info); foreach (ElectricitySoldRecordTable a in electricitySoldRecordTable) { a.Function = @" "; 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> GetOnePowerStation(int id) { ApiResult apiResult = new ApiResult(); 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; } } }