diff --git a/SolarPower/Controllers/OperationController.cs b/SolarPower/Controllers/OperationController.cs index 086dbc0..d63e536 100644 --- a/SolarPower/Controllers/OperationController.cs +++ b/SolarPower/Controllers/OperationController.cs @@ -71,41 +71,98 @@ namespace SolarPower.Controllers ApiResult apiResult = new ApiResult(); try { - var now = DateTime.Now.ToString("yyyy-MM-dd"); - var finalid = await operationRepository.GetCurrentSerialNumber("operation_plan_create", $"PowerStationId = {post.PowerStationId} AND CreatedAt LIKE '%{now}%'"); - var newSerialNumber = GetLastSerialNumber(finalid); - var OperationPlan = new OperationCreatePlan() - { - EmailType = post.EmailType, - ScheduleNum = post.ScheduleNum, - Description = post.Description, - WorkDay = post.WorkDay, - ScheduleType = post.ScheduleType, - SerialNumber = newSerialNumber, - StartTime = post.StartTime, - PowerStationId = post.PowerStationId, - Type = post.Type, - PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber, - CreatedBy = myUser.Id - }; - List properties = new List() - { - "EmailType", - "ScheduleNum", - "Description", - "WorkDay", - "ScheduleType", - "SerialNumber", - "StartTime", - "PowerStationId", - "Type", - "PlanId", - "CreatedBy" - }; - await operationRepository.AddOperationPlan(OperationPlan, properties); - apiResult.Code = "0000"; - apiResult.Msg = "新增成功"; + if (post.Id == 0) + { + var now = DateTime.Now.ToString("yyyy-MM-dd"); + var finalid = await operationRepository.GetCurrentSerialNumber("operation_plan_create", $"PowerStationId = {post.PowerStationId} AND CreatedAt LIKE '%{now}%'"); + var newSerialNumber = GetLastSerialNumber(finalid); + var OperationPlan = new OperationCreatePlan() + { + EmailType = post.EmailType, + ScheduleNum = post.ScheduleNum, + Description = post.Description, + WorkDay = post.WorkDay, + ScheduleType = post.ScheduleType, + SerialNumber = newSerialNumber, + StartTime = post.StartTime, + PowerStationId = post.PowerStationId, + Type = post.Type, + PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber, + CreatedBy = myUser.Id + }; + List properties = new List() + { + "EmailType", + "ScheduleNum", + "Description", + "WorkDay", + "ScheduleType", + "SerialNumber", + "StartTime", + "PowerStationId", + "Type", + "PlanId", + "CreatedBy" + }; + await operationRepository.AddOperationPlan(OperationPlan, properties); + + var record = new PlanToRecord() + { + WorkType = post.Type, + PowerStationId = post.PowerStationId, + StartTime = post.StartTime, + CreatedBy = myUser.Id, + EndTime = post.EndTime + }; + List properties2 = new List() + { + "WorkType", + "PowerStationId", + "StartTime", + "CreatedBy", + "EndTime" + }; + await operationRepository.AddToRecord(record, properties2); + + apiResult.Code = "0000"; + apiResult.Msg = "新增成功"; + } + else + { + var OperationPlan = new OperationCreatePlan() + { + Id = post.Id, + EmailType = post.EmailType, + ScheduleNum = post.ScheduleNum, + Description = post.Description, + WorkDay = post.WorkDay, + ScheduleType = post.ScheduleType, + StartTime = post.StartTime, + PowerStationId = post.PowerStationId, + Type = post.Type, + UpdatedBy = myUser.Id + }; + List properties = new List() + { + "Id", + "EmailType", + "ScheduleNum", + "Description", + "WorkDay", + "ScheduleType", + "StartTime", + "PowerStationId", + "Type", + "UpdatedBy" + }; + await operationRepository.UpdateOperationPlan(OperationPlan, properties); + apiResult.Code = "0000"; + apiResult.Msg = "編輯成功"; + } + + + } catch (Exception exception) { @@ -162,7 +219,7 @@ namespace SolarPower.Controllers a.Schedule = "每" + a.ScheduleNum.ToString() + "年"; } - a.StartTimeString = a.StartTime.ToString("yyyy-MM-dd"); + a.StartTimeString = a.StartTime; var crst = a.CreatedAt.Split(" "); a.CreateTimeString = crst[0]; @@ -239,6 +296,25 @@ namespace SolarPower.Controllers return apiResult; } + public async Task> GetOneOperationPlan(int Id) + { + ApiResult apiResult = new ApiResult(); + OperationCreatePlan operationCreatePlan = new OperationCreatePlan(); + try + { + apiResult.Code = "0000"; + operationCreatePlan = await operationRepository.GetOneOperation(Id); + apiResult.Data = operationCreatePlan; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + Id); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + return apiResult; + } /// /// 運維作業記錄列表 diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 9a4913e..16f33b7 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -1840,6 +1840,51 @@ namespace SolarPower.Controllers Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } + return apiResult; + } + /// + /// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量 + /// + /// + public async Task>> GetSolarCitySummary() + { + ApiResult> apiResult = new ApiResult>(); + List solaramount = new List(); + try + { + apiResult.Code = "0000"; + solaramount = await powerStationRepository.GetSolarCitySummary(myUser); + apiResult.Data = solaramount; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + + return apiResult; + } + + /// + /// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量 + /// + /// + public async Task>> GetSolarByCity(List cityid) + { + ApiResult> apiResult = new ApiResult>(); + List solaramount = new List(); + try + { + apiResult.Code = "0000"; + solaramount = await powerStationRepository.GetSolarByCity(myUser, cityid); + apiResult.Data = solaramount; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + return apiResult; } } diff --git a/SolarPower/Models/Operation.cs b/SolarPower/Models/Operation.cs index 3e59c34..a217ee4 100644 --- a/SolarPower/Models/Operation.cs +++ b/SolarPower/Models/Operation.cs @@ -28,14 +28,43 @@ namespace SolarPower.Models } public class OperationCreatePlanModal : Created { + private string startTime = "", endTime = ""; public int Id { get; set; } public int PowerStationId { get; set; } public int Type { get; set; } public int ScheduleNum { get; set; } public int ScheduleType { get; set; } public int WorkDay { get; set; } - public DateTime StartTime { get; set; } - public DateTime EndTime { get; set; } + public string StartTime + { + get + { + if (!string.IsNullOrEmpty(startTime)) + { + return Convert.ToDateTime(startTime).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { startTime = value; } + } + public string EndTime + { + get + { + if (!string.IsNullOrEmpty(endTime)) + { + return Convert.ToDateTime(endTime).ToString("yyyy-MM-dd"); + } + else + { + return null; + } + } + set { endTime = value; } + } public string Description { get; set; } public int EmailType { get; set; } } @@ -43,6 +72,10 @@ namespace SolarPower.Models { public string PlanId { get; set; } public string SerialNumber { get; set; } + + private string updatedAt; + public int UpdatedBy { get; set; } //修改者 + public string UpdatedAt { get { return Convert.ToDateTime(updatedAt).ToString("yyyy-MM-dd HH:mm:ss"); } set { updatedAt = value; } } //修改時間 } public class OperationPlanTable : OperationCreatePlan { @@ -57,6 +90,14 @@ namespace SolarPower.Models public string CreateTimeString { get; set; } } + public class PlanToRecord : Created + { + public int PowerStationId { get; set; } + public int WorkType { get; set; } + public string StartTime { get; set; } + public string EndTime { get; set; } + } + /// /// 運維作業記錄的搜尋條件 /// @@ -77,7 +118,8 @@ namespace SolarPower.Models public string PowerStationName { get; set; } //電站名稱 public string FormId { get; set; } //表單號 public byte WorkType { get; set; } //工作項目 - public string WorkTypeText { + public string WorkTypeText + { get { Dictionary pairs = new Dictionary() @@ -92,7 +134,8 @@ namespace SolarPower.Models } //工作項目名稱 public string FixDo { get; set; } //維修項目 public byte Status { get; set; } //狀態 - public string StatusText { + public string StatusText + { get { Dictionary pairs = new Dictionary() @@ -107,7 +150,8 @@ namespace SolarPower.Models } } //狀態名稱 public string WorkPersonName { get; set; } //處理人員 - public string StartTime { + public string StartTime + { get { if (!string.IsNullOrEmpty(startTime)) @@ -121,7 +165,8 @@ namespace SolarPower.Models } set { startTime = value; } } //開始時間 - public string EndTime { + public string EndTime + { get { if (!string.IsNullOrEmpty(endTime)) @@ -135,7 +180,8 @@ namespace SolarPower.Models } set { endTime = value; } } // 結束時間 - public string OperationPredict { + public string OperationPredict + { get { return StartTime + " ~ " + EndTime; diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 84fe51f..316e244 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -457,4 +457,14 @@ namespace SolarPower.Models.PowerStation public string Image { get; set; } } + /// + /// 電站縣市分佈 + /// + public class SolarCityAmount + { + public int CityId { get; set; } + public string City { get; set; } + public int Amount { get; set; } + } + } diff --git a/SolarPower/Repository/Implement/OperationRepository.cs b/SolarPower/Repository/Implement/OperationRepository.cs index d7f3115..f34ca08 100644 --- a/SolarPower/Repository/Implement/OperationRepository.cs +++ b/SolarPower/Repository/Implement/OperationRepository.cs @@ -80,7 +80,9 @@ namespace SolarPower.Repository.Implement { try { - var sql = @$"SELECT *,ps.Name AS PowerStationName ,us.Name AS CreatedPerson FROM operation_plan_create oc LEFT JOIN power_station ps + var sql = @$"SELECT oc.Id,oc.PlanId,oc.PowerStationId,oc.Type, + oc.ScheduleNum,oc.ScheduleType,oc.WorkDay,oc.StartTime, + oc.EmailType,oc.Description,oc.CreatedAt,ps.Name AS PowerStationName ,us.Name AS CreatedPerson FROM operation_plan_create oc LEFT JOIN power_station ps ON oc.PowerStationId = ps.Id LEFT JOIN user us ON us.Id = oc.CreatedBy WHERE {Wheresql} AND oc.Deleted = 0"; result = (await conn.QueryAsync(sql)).ToList(); @@ -117,6 +119,57 @@ namespace SolarPower.Repository.Implement } } + public async Task UpdateOperationPlan(OperationCreatePlan OperationPlan, List properties) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + var sql = GenerateUpdateQuery(properties); + await conn.ExecuteAsync(sql, OperationPlan, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + + } + } + + public async Task AddToRecord(PlanToRecord record, List properties2) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + string sql = GenerateInsertQueryWithCustomTable(properties2, "operation_record"); + await conn.ExecuteAsync(sql, record); + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + + } + } + /// /// 透過搜尋條件,查詢過濾後的運維作業記錄 /// diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 68141b0..2eae156 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.Json; +using SolarPower.Models; namespace SolarPower.Repository.Implement { @@ -620,7 +621,7 @@ namespace SolarPower.Repository.Implement try { string sql = @$"SELECT * FROM variable WHERE name = @name"; - var json = await conn.QueryFirstOrDefaultAsync(sql, new { name = "Type" }); + var json = await conn.QueryFirstOrDefaultAsync(sql, new { name = "Type" }); Root jsonfor = JsonSerializer.Deserialize(json.value); foreach (Models.PowerStation.Type a in jsonfor.Type) { @@ -1275,6 +1276,90 @@ namespace SolarPower.Repository.Implement } } } - + public async Task> GetSolarCitySummary(MyUser User) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + List solaramount = new List(); + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + if (User.Role.Layer == 0 || User.Role.Layer == 1) + { + var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id GROUP BY power_station.CityId"; + solaramount = (await conn.QueryAsync(sql)).ToList(); + trans.Commit(); + } + else if (User.Role.Layer == 2) + { + var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id WHERE CompanyId = @CompanyId GROUP BY power_station.CityId ORDER BY power_station.CityId "; + solaramount = (await conn.QueryAsync(sql, new { CompanyId = User.CompanyId })).ToList(); + trans.Commit(); + } + else + { + var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE UserId = @UserId GROUP BY power_station.CityId ORDER BY power_station.CityId"; + solaramount = (await conn.QueryAsync(sql, new { UserId = User.Id })).ToList(); + trans.Commit(); + } + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + return solaramount; + } + public async Task> GetSolarByCity(MyUser User, List CityId) + { + using IDbConnection conn = _databaseHelper.GetConnection(); + List powerstation = new List(); + conn.Open(); + var trans = conn.BeginTransaction(); + try + { + var ids = ""; + foreach(var id in CityId) + { + ids = ids + id + ","; + } + if (ids.Length != 0) + { + ids = ids.Substring(0, ids.Length - 1); + } + if (User.Role.Layer == 0 || User.Role.Layer == 1) + { + var sql = "SELECT * FROM power_station WHERE CityId IN(@Ids)"; + powerstation = (await conn.QueryAsync(sql, new { Ids = ids })).ToList(); + trans.Commit(); + } + else if (User.Role.Layer == 2) + { + var sql = "SELECT * FROM power_station WHERE CityId IN(@Ids) AND CompanyId=@CompanyId"; + powerstation = (await conn.QueryAsync(sql, new { Ids = ids ,CompanyId = User.CompanyId })).ToList(); + trans.Commit(); + } + else + { + var sql = "SELECT power_station.* FROM power_station LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE CityId IN(@Ids) AND Userid = @UserId"; + powerstation = (await conn.QueryAsync(sql, new { UserId = User.Id })).ToList(); + trans.Commit(); + } + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + return powerstation; + } } } diff --git a/SolarPower/Repository/Interface/IOperationRepository.cs b/SolarPower/Repository/Interface/IOperationRepository.cs index 3dd0106..ef999a4 100644 --- a/SolarPower/Repository/Interface/IOperationRepository.cs +++ b/SolarPower/Repository/Interface/IOperationRepository.cs @@ -12,6 +12,9 @@ namespace SolarPower.Repository.Interface Task AddOperationPlan(OperationCreatePlan OperationPlan, List properties); Task> OperationPlanTable(List id); Task GetOneOperation(int id); + Task UpdateOperationPlan(OperationCreatePlan OperationPlan, List properties); + Task AddToRecord(PlanToRecord record, List properties2); + /// /// 透過搜尋條件,查詢過濾後的運維作業記錄 diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index ffcb98a..d9ab904 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -1,4 +1,5 @@ -using SolarPower.Models.PowerStation; +using SolarPower.Models; +using SolarPower.Models.PowerStation; using SolarPower.Models.User; using System; using System.Collections.Generic; @@ -325,5 +326,20 @@ namespace SolarPower.Repository.Interface /// /// Task DeleteOnePowerStationSingleLine(int id, string db_name); + + /// + /// 取得電站分佈縣市以及數量 + /// + /// + /// + Task> GetSolarCitySummary(MyUser User); + + /// + /// 透過縣市取得電站卡片資訊 + /// + /// + /// + /// + Task> GetSolarByCity(MyUser User, List CityId); } } diff --git a/SolarPower/Views/Operation/Index.cshtml b/SolarPower/Views/Operation/Index.cshtml index a4d9d53..e9e9ef8 100644 --- a/SolarPower/Views/Operation/Index.cshtml +++ b/SolarPower/Views/Operation/Index.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["MainNum"] = "5"; + ViewData["MainNum"] = "6"; ViewData["SubNum"] = "1"; ViewData["Title"] = "運維管理"; } @@ -359,13 +359,14 @@ var url = "/Operation/SaveOperationPlan"; var send_data = { + id: selected_id, PowerStationId: $("#operation_powerStationselect_modal").val(), Type: $("#operation_type_modal").val(), ScheduleNum: $("#operation_scheduleNum_modal").val(), ScheduleType: $("#operation_scheduleType_modal").val(), WorkDay: $('#operation_workDay_modal').val(), StartTime: $("#operation_startTime_modal").val(), - EndTime: $("#operation_endTime_modal").val(), + EndTime: document.getElementById("operation_endTime_modal").innerHTML, Description: $("#operation_description_modal").val(), EmailType: $('#operation_emailType_modal').val() } @@ -413,6 +414,44 @@ }); }); //#endregion + //#region 取一筆定時計畫 + $('#OperationPlan_table').on("click", "button.edit-btn", function () { + $("#Operation-modal .modal-title").html("定時計劃 - 編輯"); + selected_id = $(this).parents('tr').attr('data-id'); + + //取得單一異常設定資料 + var url = "/Operation/GetOneOperationPlan/"; + + var send_data = { + id: selected_id + } + + + $.post(url, send_data, function (rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + return; + } + + $("#operation_powerStationselect_modal").val(rel.data.powerStationId); + $("#operation_type_modal").val(rel.data.type); + $("#operation_scheduleNum_modal").val(rel.data.scheduleNum); + $("#operation_scheduleType_modal").val(rel.data.scheduleType); + $("#operation_workDay_modal").val(rel.data.workDay); + var start = rel.data.startTime.split(' '); + $("#operation_startTime_modal").val(start[0]); + $("#operation_description_modal").val(rel.data.description); + $("#operation_emailType_modal").val(rel.data.emailType); + + var dat = new Date(start); + var day = new Date(dat.setDate(dat.getDate() + Number(rel.data.workDay))).toISOString().split("T"); + document.getElementById("operation_endTime_modal").innerHTML = day[0]; + + $("#Operation-modal").modal(); + }, 'json'); + + }); + //#endregion } \ No newline at end of file diff --git a/SolarPower/Views/PowerStation/Index.cshtml b/SolarPower/Views/PowerStation/Index.cshtml index dba4d51..a3f8fe4 100644 --- a/SolarPower/Views/PowerStation/Index.cshtml +++ b/SolarPower/Views/PowerStation/Index.cshtml @@ -410,7 +410,49 @@ $(function () { var localurl = this.location.href; - //電站區域Tab要佳LI + + //#region 電站區域Tab + + + //var url = "/PowerStation/GetSolarCitySummary"; + //$.post(url, function (rel) { + // if (rel.code != "0000") { + // toast_error(rel.msg); + // return; + // } + // else { + // $.each(rel.data, function (index, val) { + // alert(val.city + val.amount); + // }); + // return; + // } + + + //}, 'json'); + + var url = "/PowerStation/GetSolarByCity"; + var ids = [1, 3]; + var send_data = { + cityid: ids + } + $.post(url, send_data, function (rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + return; + } + else { + $.each(rel.data, function (index, val) { + alert(val.name); + }); + return; + } + + + }, 'json'); + + //#endregion + + //電站區域 $('#templateAreaTab').find('li').attr('id', 'AreaTabnewtaipei').clone().appendTo($('#areaTab')); $('#AreaTabnewtaipei').find('a').attr('href', '#tab-newtaipei'); $('#AreaTabnewtaipei').find('a').attr('class', 'nav-link fs-lg px-4');