From b0fc8f1a691c6237a58f356a480cbd18c36c868e Mon Sep 17 00:00:00 2001 From: b110212000 Date: Mon, 30 Aug 2021 15:01:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E8=A1=8C=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E5=AF=84=E9=80=81email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Controllers/OperationController.cs | 282 +++++++++++++----- .../Views/Operation/OperationRecord.cshtml | 140 +++++---- .../PowerStation/PowerStationEdit.cshtml | 49 ++- 3 files changed, 335 insertions(+), 136 deletions(-) diff --git a/SolarPower/Controllers/OperationController.cs b/SolarPower/Controllers/OperationController.cs index 1a98979..4fed12b 100644 --- a/SolarPower/Controllers/OperationController.cs +++ b/SolarPower/Controllers/OperationController.cs @@ -1,6 +1,9 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using SolarPower.Models; +using SolarPower.Models.PowerStation; using SolarPower.Models.Role; using SolarPower.Repository.Interface; using System; @@ -15,15 +18,21 @@ namespace SolarPower.Controllers public class OperationController : MyBaseController { private readonly IOperationRepository operationRepository; + private readonly IPowerStationRepository powerStationRepository; + private readonly IUserRepository userRepository; + private readonly INoticeScheduleRepository noticeScheduleRepository; private string operationRecordFilePath = "/upload/operation_recode/"; private string operationRecordSaveAsPath = ""; - + public IWebHostEnvironment environment; public OperationController( - IOperationRepository operationRepository) : base() + IOperationRepository operationRepository, IWebHostEnvironment environment,IPowerStationRepository powerStationRepository,IUserRepository userRepository,INoticeScheduleRepository noticeScheduleRepository) : base() { + this.noticeScheduleRepository = noticeScheduleRepository; + this.userRepository = userRepository; + this.powerStationRepository = powerStationRepository; this.operationRepository = operationRepository; - + this.environment = environment; operationRecordSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "operation_recode"); } @@ -534,27 +543,25 @@ namespace SolarPower.Controllers finishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } - using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + operationRecord = new OperationRecord() { - operationRecord = new OperationRecord() - { - FormId = "op" + DateTime.Now.ToString("yyyyMMdd") + tempSerialNumber, - SerialNumber = tempSerialNumber, - PowerStationId = post.PowerStationId, - WorkType = (int)OperationRecordWorkTypeEnum.Fix, - ErrorCode = post.ErrorCode, - FixDo = post.FixDo, - FixFirm = post.FixFirm, - Status = post.Status, - //WorkPersonId = post.WorkPersonId, - FinishTime = finishTime, - WorkTime = post.WorkTime, - Notice = post.Notice, - Description = post.Description, - CreatedBy = myUser.Id - }; + FormId = "op" + DateTime.Now.ToString("yyyyMMdd") + tempSerialNumber, + SerialNumber = tempSerialNumber, + PowerStationId = post.PowerStationId, + WorkType = (int)OperationRecordWorkTypeEnum.Fix, + ErrorCode = post.ErrorCode, + FixDo = post.FixDo, + FixFirm = post.FixFirm, + Status = post.Status, + //WorkPersonId = post.WorkPersonId, + FinishTime = finishTime, + WorkTime = post.WorkTime, + Notice = post.Notice, + Description = post.Description, + CreatedBy = myUser.Id + }; - List properties = new List() + List properties = new List() { "FormId", "SerialNumber", @@ -572,33 +579,104 @@ namespace SolarPower.Controllers "CreatedBy" }; - id = await operationRepository.AddOneOperationRecordAsync(operationRecord, properties); + id = await operationRepository.AddOneOperationRecordAsync(operationRecord, properties); - //加入執行人員 - if (post.WorkPersonIds != null) + //加入執行人員 + if (post.WorkPersonIds != null) + { + List insertOperationRecordPersonnels = new List(); + + foreach (var op in post.WorkPersonIds) { - List insertOperationRecordPersonnels = new List(); + OperationRecordPersonnel operationRecordPersonnel = new OperationRecordPersonnel(); + operationRecordPersonnel.OperationRecordId = id; + operationRecordPersonnel.UserId = op; - foreach (var op in post.WorkPersonIds) - { - OperationRecordPersonnel operationRecordPersonnel = new OperationRecordPersonnel(); - operationRecordPersonnel.OperationRecordId = id; - operationRecordPersonnel.UserId = op; + insertOperationRecordPersonnels.Add(operationRecordPersonnel); + } - insertOperationRecordPersonnels.Add(operationRecordPersonnel); - } - - List operationRecordPersonnelProperties = new List() + List operationRecordPersonnelProperties = new List() { "OperationRecordId", "UserId", }; - await operationRepository.AddOperationRecordPersonnelAsync(insertOperationRecordPersonnels, operationRecordPersonnelProperties); - } - scope.Complete(); + await operationRepository.AddOperationRecordPersonnelAsync(insertOperationRecordPersonnels, operationRecordPersonnelProperties); + + } + if (post.WorkPersonIds.Count != 0) + { + var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId); + List noticeSchedules = new List(); + foreach (var person in post.WorkPersonIds) + { + var fixtype = post.WorkType switch + { + 0 => "清洗", + 1 => "巡檢", + 2 => "維修", + _ => "" + }; + var user = await userRepository.GetOneAsync(person); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + UserId = person, + RecipientName = user.Name, + RecipientEmail = user.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + noticeSchedules.Add(schedule); + } + properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + } + + if (post.FixFirm != 0) + { + var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId); + var fixtype = operationRecord.WorkType switch + { + 0 => "清洗", + 1 => "巡檢", + 2 => "維修", + _ => "" + }; + var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); + var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync(post.FixFirm, powerstations.SiteDB, "operation_firm"); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + RecipientName = firm.Name, + RecipientEmail = firm.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); + } + + + //using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress)) + //{ + + // scope.Complete(); + //} + apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; apiResult.Data = id; @@ -606,9 +684,14 @@ namespace SolarPower.Controllers } else { - + //var transactionOption = new TransactionOptions(); + //transactionOption.IsolationLevel = IsolationLevel.ReadUncommitted; + //using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption, TransactionScopeAsyncFlowOption.Enabled)) + //{ + + // scope.Complete(); + //} #region 修改運維作業記錄 - var finishTime = string.Empty; if (post.Status == (int)OperationRecordStatusEnum.Complete) { @@ -650,22 +733,20 @@ namespace SolarPower.Controllers UpdatedBy = myUser.Id }; - - List properties = new List() - { - "Id", - "ErrorCode", - "FixDo", - "FixFirm", - "Status", - "FinishTime", - "WorkTime", - "Notice", - "Description", - //"WorkPersonId", - "UpdatedBy", - }; + { + "Id", + "ErrorCode", + "FixDo", + "FixFirm", + "Status", + "FinishTime", + "WorkTime", + "Notice", + "Description", + //"WorkPersonId", + "UpdatedBy", + }; if (string.IsNullOrEmpty(operationRecord.FormId)) { @@ -686,7 +767,7 @@ namespace SolarPower.Controllers //判斷新進來的執行人員是否要歸類到新增 or 刪除 #region 刪除執行人員編號 - if(post.WorkPersonIds != null && post.WorkPersonIds.Count() > 0) + if (post.WorkPersonIds != null && post.WorkPersonIds.Count() > 0) { //找出要刪除的 List deleteOperationRecordPersonnelIds = origOperationRecordPersonnels.Where(x => !post.WorkPersonIds.Contains(x)).ToList(); @@ -704,7 +785,6 @@ namespace SolarPower.Controllers //刪除執行人員 await operationRepository.DeleteOperationRecordPersonnel(deleteOperationRecordPersonnels); } - #endregion #region 新增人員 @@ -725,23 +805,91 @@ namespace SolarPower.Controllers insertOperationRecordPersonnels.Add(operationPersonnel); } - List operationRecordPersonnelProperties = new List() - { - "OperationRecordId", - "Deleted", - "UserId", - "CreatedBy", - }; - + { + "OperationRecordId", + "Deleted", + "UserId", + "CreatedBy", + }; await operationRepository.AddOperationRecordPersonnelAsync(insertOperationRecordPersonnels, operationRecordPersonnelProperties); } + + #endregion + + if (post.WorkPersonIds.Count != 0) + { + var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId); + List noticeSchedules = new List(); + foreach (var person in post.WorkPersonIds) + { + var fixtype = operationRecord.WorkType switch + { + 0 => "清洗", + 1 => "巡檢", + 2 => "維修", + _ => "" + }; + var user = await userRepository.GetOneAsync(person); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + UserId = person, + RecipientName = user.Name, + RecipientEmail = user.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + noticeSchedules.Add(schedule); + } + properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + } + if (post.FixFirm != 0) + { + var powerstations = await powerStationRepository.GetOneAsync(post.PowerStationId); + var fixtype = post.WorkType switch + { + 0 => "清洗", + 1 => "巡檢", + 2 => "維修", + _ => "" + }; + var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); + var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync(post.FixFirm, powerstations.SiteDB, "operation_firm"); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + RecipientName = firm.Name, + RecipientEmail = firm.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); + + + + } + + #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; apiResult.Data = operationRecord.Id; - #endregion + } } catch (Exception exception) diff --git a/SolarPower/Views/Operation/OperationRecord.cshtml b/SolarPower/Views/Operation/OperationRecord.cshtml index 5c684a3..d2c08bd 100644 --- a/SolarPower/Views/Operation/OperationRecord.cshtml +++ b/SolarPower/Views/Operation/OperationRecord.cshtml @@ -24,8 +24,8 @@
- - + +
@@ -274,7 +274,7 @@ var datepicker; var AllpoweridsType = true; var AllidsType = true; - var status = 1; //1:完成 0:未完成 + var status = 0; //1:完成 0:未完成 var Searchtype = false; //#region Array.Remove @@ -878,72 +878,90 @@ //#region 儲存表單資料 function SaveRecord() { + Swal.fire( + { + title: "儲存", + text: "你確定是否儲存此筆資料?\r\n 儲存成功後將會發送Email至負責人員及廠商", + type: "warning", + icon: 'warning', + showCancelButton: true, + confirmButtonText: "是", + cancelButtonText: "否" + }).then(function (result) { + if (result.value) { + if ($("#record-form").valid()) { + var url = "/Operation/SaveOperationRecord"; - if ($("#record-form").valid()) { - var url = "/Operation/SaveOperationRecord"; + var formData = new FormData(); - var formData = new FormData(); + @*var work_person_ids = $.map($("#work_person_select_modal").val(), function (item) { + return parseInt(item); + }) *@ - @*var work_person_ids = $.map($("#work_person_select_modal").val(), function (item) { - return parseInt(item); - })*@ - - formData.append("Id", selected_id); - formData.append("PowerStationId", $("#power_station_select_modal").val()); - formData.append("WorkType", selected_work_type); - formData.append("ErrorCode", $("#error_code_modal").val()); - formData.append("FixDo", $("#fix_do_modal").val()); - formData.append("FixFirm", $("#fix_firm_select_modal").val()); - formData.append("Status", $("input[name=status_modal]:checked").val()); - $("#work_person_select_modal").val().forEach(function (item) { - formData.append("WorkPersonIds", parseInt(item)); - }); - formData.append("WorkTime", $("#work_time_modal").val()); - formData.append("Notice", $("#notice_textarea_modal").val()); - formData.append("Description", $("#description_textarea_modal").val()); - - $.ajax({ - type: "POST", - url: url, - data: formData, - cache: false, - contentType: false, - processData: false, - success: function (rel) { - if (rel.code != "0000") { - toast_error(rel.msg); - return; - } - - var myDropzone = Dropzone.forElement("#record-file-form"); - - if (myDropzone.files.length > 0) { - - selected_id = rel.data; - - myDropzone.processQueue(); - - myDropzone.on("successmultiple", function (file, rel) { - if (rel.code != "0000") { - toast_error(rel.msg); - return; - } - - toast_ok(rel.msg); - $('#record-form-modal').modal('hide'); - recordFileDropzone.removeAllFiles(); - - operationRecordTable.ajax.reload(); + formData.append("Id", selected_id); + formData.append("PowerStationId", $("#power_station_select_modal").val()); + formData.append("WorkType", selected_work_type); + formData.append("ErrorCode", $("#error_code_modal").val()); + formData.append("FixDo", $("#fix_do_modal").val()); + formData.append("FixFirm", $("#fix_firm_select_modal").val()); + formData.append("Status", $("input[name=status_modal]:checked").val()); + $("#work_person_select_modal").val().forEach(function (item) { + formData.append("WorkPersonIds", parseInt(item)); }); - } else { - $('#record-form-modal').modal('hide'); - myDropzone.removeAllFiles(); + formData.append("WorkTime", $("#work_time_modal").val()); + formData.append("Notice", $("#notice_textarea_modal").val()); + formData.append("Description", $("#description_textarea_modal").val()); - operationRecordTable.ajax.reload(); + $.ajax({ + type: "POST", + url: url, + data: formData, + cache: false, + contentType: false, + processData: false, + success: function (rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + return; + } + + var myDropzone = Dropzone.forElement("#record-file-form"); + + if (myDropzone.files.length > 0) { + + selected_id = rel.data; + + myDropzone.processQueue(); + + myDropzone.on("successmultiple", function (file, rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + return; + } + + toast_ok(rel.msg); + $('#record-form-modal').modal('hide'); + recordFileDropzone.removeAllFiles(); + + operationRecordTable.ajax.reload(); + }); + } else { + $('#record-form-modal').modal('hide'); + myDropzone.removeAllFiles(); + + operationRecordTable.ajax.reload(); + } + } + }); } } }); - } + + + + + + } //#endregion diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 537ec34..f4e31a5 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -853,9 +853,16 @@ $.each(rel.data, function (index, val) { $("#ShareDevice_PowerStationId_modal").append($("