diff --git a/SolarPower/Controllers/ElectricitySoldRecordController.cs b/SolarPower/Controllers/ElectricitySoldRecordController.cs index e209466..6b5b7b6 100644 --- a/SolarPower/Controllers/ElectricitySoldRecordController.cs +++ b/SolarPower/Controllers/ElectricitySoldRecordController.cs @@ -68,7 +68,6 @@ namespace SolarPower.Controllers UpdatedBy = myUser.Id, Kwh = post.Kwh, Money = post.Money, - PowerstationId = post.PowerstationId, StartAt = post.StartAt }; List properties = new List() @@ -78,7 +77,6 @@ namespace SolarPower.Controllers "UpdatedBy", "Kwh", "Money", - "PowerstationId", "StartAt" }; await electricitySoldRecordRepository.Update(record, properties); @@ -99,21 +97,30 @@ namespace SolarPower.Controllers return apiResult; } + [HttpPost] public async Task RecordTable(ElectricitySoldRecordTablePost info) { List electricitySoldRecordTable = new List(); ApiResult> apiResult = new ApiResult>(); try { - electricitySoldRecordTable = await electricitySoldRecordRepository.RecordTable(info); - foreach (ElectricitySoldRecordTable a in electricitySoldRecordTable) + if(info.StationId == null || info.Time == null) { - a.Function = @" + 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; } - apiResult.Code = "0000"; - apiResult.Data = electricitySoldRecordTable; } catch (Exception exception) { @@ -129,5 +136,27 @@ namespace SolarPower.Controllers }); 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; + } } } diff --git a/SolarPower/Repository/Implement/ElectricitySoldRecordRepository.cs b/SolarPower/Repository/Implement/ElectricitySoldRecordRepository.cs index cf69909..d11d164 100644 --- a/SolarPower/Repository/Implement/ElectricitySoldRecordRepository.cs +++ b/SolarPower/Repository/Implement/ElectricitySoldRecordRepository.cs @@ -1,8 +1,10 @@ -using SolarPower.Helper; +using Dapper; +using SolarPower.Helper; using SolarPower.Models; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Threading.Tasks; @@ -16,26 +18,61 @@ namespace SolarPower.Repository.Implement } public async Task> RecordTable (ElectricitySoldRecordTablePost post) { - List a = new List(); + string sql = ""; + List ids = new List(); + foreach(var id in post.StationId) + { + ids.Add(Convert.ToInt32(id.Value)); + } + + switch (post.SearchType) { - //case 0: - // post.Time.Replace("-","~").Replace("") - // sql = ""; - // break; - //case 1: - // sql = ""; - // break; - //case 2: - // sql = ""; - // break; - //case 3: - // sql = ""; - // break; + case 0: + post.Time = post.Time.Replace(" ", ""); + post.Time = post.Time.Replace("-", "~"); + post.Time = post.Time.Replace("/", "-"); + var time = post.Time.Split("~"); + sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es + LEFT JOIN power_station ps ON es.PowerstationId = ps.Id + WHERE es.StartAt BETWEEN '{time[0]}' AND '{time[1]}' AND es.PowerstationId IN @ids"; + break; + case 1: + post.Time = post.Time.Replace(" ", ""); + post.Time = post.Time.Replace("-", "~"); + post.Time = post.Time.Replace("/", "-"); + var time1 = post.Time.Split("~"); + sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es + LEFT JOIN power_station ps ON es.PowerstationId = ps.Id + WHERE es.StartAt BETWEEN '{time1[0]}' AND '{time1[1]}' AND es.PowerstationId IN @ids"; + sql = ""; + break; + case 2: + sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es + LEFT JOIN power_station ps ON es.PowerstationId = ps.Id + WHERE DATE_FORMAT(es.StartAt , '%Y-%m') = '{post.Time}' AND es.PowerstationId IN @ids"; + break; + case 3: + sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es + LEFT JOIN power_station ps ON es.PowerstationId = ps.Id + WHERE DATE_FORMAT(es.StartAt , '%Y') = '{post.Time}' AND es.PowerstationId IN @ids"; + break; + } + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + List a = new List(); + try + { + a = (await conn.QueryAsync(sql,new { ids = ids})).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return a; } - return a; } } } diff --git a/SolarPower/Views/ElectricitySoldRecord/Index.cshtml b/SolarPower/Views/ElectricitySoldRecord/Index.cshtml index a854a67..eadd9bf 100644 --- a/SolarPower/Views/ElectricitySoldRecord/Index.cshtml +++ b/SolarPower/Views/ElectricitySoldRecord/Index.cshtml @@ -243,9 +243,10 @@ } \ No newline at end of file