diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index 3877051..cdeca76 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -125,5 +125,34 @@ namespace SolarPower.Controllers return false; } + + /// + /// 取得最新的流水號 + /// + /// 當前的 + /// + /// 0: PadLeft;0: PadRight + /// + public string GetLastSerialNumber(string current = "", int pad = 4, byte direction = 0) + { + var tempSerialNumber = 0; + if (!string.IsNullOrEmpty(current)) + { + tempSerialNumber = Convert.ToInt32(current) + 1; + } + else + { + tempSerialNumber = 1; + } + + if (direction == 0) + { + return tempSerialNumber.ToString().Trim().PadLeft(pad, '0'); + } + else + { + return tempSerialNumber.ToString().Trim().PadRight(pad, '0'); + } + } } } diff --git a/SolarPower/Controllers/OperationController.cs b/SolarPower/Controllers/OperationController.cs index 076db2f..593ee6a 100644 --- a/SolarPower/Controllers/OperationController.cs +++ b/SolarPower/Controllers/OperationController.cs @@ -27,14 +27,18 @@ namespace SolarPower.Controllers { return View(); } - + /// + /// 取得電站Option + /// + /// + /// public async Task GetPowerStationSelectOption(int post) { ApiResult> apiResult = new ApiResult>(); try { var PowerStationIdLists = new List(); - PowerStationIdLists = await operationRepository.GetPowerStationIdList(post) ; + PowerStationIdLists = await operationRepository.GetPowerStationIdList(myUser.Id) ; apiResult.Code = "0000"; apiResult.Data = PowerStationIdLists; } @@ -53,5 +57,181 @@ namespace SolarPower.Controllers return result; } + /// + /// 儲存計畫 + /// + /// + /// + public async Task> SaveOperationPlan(OperationCreatePlanModal post) + { + 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 = "新增成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + /// + /// 定時計畫datatable + /// + /// + /// + public async Task OperationPlanTable(List id) + { + List OperationPlanTable = new List(); + ApiResult> apiResult = new ApiResult>(); + try + { + apiResult.Code = "0000"; + OperationPlanTable = await operationRepository.OperationPlanTable(id); + foreach (OperationPlanTable a in OperationPlanTable) + { + if(a.Type == 0) + { + a.TypeName = "清洗"; + } + else + { + a.TypeName = "巡檢"; + } + + if(a.ScheduleType == 0) + { + a.Schedule = "每" + a.ScheduleNum.ToString() + "天" ; + } + else if (a.ScheduleType == 1) + { + a.Schedule = "每" + a.ScheduleNum.ToString() + "周"; + } + else if (a.ScheduleType == 2) + { + a.Schedule = "每" + a.ScheduleNum.ToString() + "月"; + } + else if (a.ScheduleType == 3) + { + a.Schedule = "每" + a.ScheduleNum.ToString() + "季"; + } + else if (a.ScheduleType == 4) + { + a.Schedule = "每" + a.ScheduleNum.ToString() + "年"; + } + + a.StartTimeString = a.StartTime.ToString("yyyy-MM-dd"); + var crst = a.CreatedAt.Split(" "); + a.CreateTimeString = crst[0]; + + if(a.EmailType == 0) + { + a.EmailTypeName = "當天"; + } + else if(a.EmailType == 1) + { + a.EmailTypeName = "前一天"; + } + else if (a.EmailType == 2) + { + a.EmailTypeName = "前兩天"; + } + else if (a.EmailType == 3) + { + a.EmailTypeName = "前三天"; + } + + a.Function = @" + + "; + } + + apiResult.Data = OperationPlanTable; + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = exception.ToString(); + } + var result = Json(new + { + data = apiResult + }); + + return result; + } + /// + /// 刪除定時計畫 + /// + /// + /// + public async Task> DeleteOneOperationPlan(int Id) + { + ApiResult apiResult = new ApiResult(); + OperationCreatePlan operation = new OperationCreatePlan(); + try + { + operation = await operationRepository.GetOneOperation(Id); + + if (operation == null) + { + apiResult.Code = "9998"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + await operationRepository.DeleteOneOtherTable(Id, "operation_plan_create"); + + apiResult.Code = "0000"; + apiResult.Msg = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + Id); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } } } diff --git a/SolarPower/Helper/DatabaseHelper.cs b/SolarPower/Helper/DatabaseHelper.cs index b359893..56f1936 100644 --- a/SolarPower/Helper/DatabaseHelper.cs +++ b/SolarPower/Helper/DatabaseHelper.cs @@ -37,7 +37,7 @@ namespace SolarPower.Helper var passwordStr = ed.DESDecrypt(dbConfig.Password); //var connStr = $"server={serverStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;"; - var connStr = @"server=127.0.0.1;database=solar_power;user=root;password=000000;charset=utf8;"; + var connStr = @"server=127.0.0.1;port=3308;database=solar_power;user=root;password=00000000;charset=utf8;"; this._connectionString = connStr; } @@ -45,7 +45,6 @@ namespace SolarPower.Helper public IDbConnection GetConnection() { var conn = new MySqlConnection(this._connectionString); - return conn; } } diff --git a/SolarPower/Models/Operation.cs b/SolarPower/Models/Operation.cs index 2da6e2c..4ddacba 100644 --- a/SolarPower/Models/Operation.cs +++ b/SolarPower/Models/Operation.cs @@ -26,4 +26,34 @@ namespace SolarPower.Models public string Text { get; set; } public string Value { get; set; } } + public class OperationCreatePlanModal : Created + { + 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 Description { get; set; } + public int EmailType { get; set; } + } + public class OperationCreatePlan : OperationCreatePlanModal + { + public string PlanId { get; set; } + public string SerialNumber { get; set; } + } + public class OperationPlanTable : OperationCreatePlan + { + public string TypeName { get; set; } + public string PowerStationName { get; set; } + public string Schedule { get; set; } + public string EmailTypeName { get; set; } + public string CreatedPerson { get; set; } + public string Function { get; set; }//功能 + public string ScheduleDay { get; set; } + public string StartTimeString { get; set; } + public string CreateTimeString { get; set; } + } } diff --git a/SolarPower/Repository/Implement/OperationRepository.cs b/SolarPower/Repository/Implement/OperationRepository.cs index dec8901..063c5ad 100644 --- a/SolarPower/Repository/Implement/OperationRepository.cs +++ b/SolarPower/Repository/Implement/OperationRepository.cs @@ -23,7 +23,7 @@ namespace SolarPower.Repository.Implement { try { - var sql = @$"SELECT tn.Id AS Value, tn.Name AS Text FROM {tableName} tn LEFT JOIN power_station_operation_personnel pp + var sql = @$"SELECT tn.Id AS Value, tn.Name AS Text FROM power_station tn LEFT JOIN power_station_operation_personnel pp ON tn.Id=pp.PowerStationId WHERE pp.UserId = @userid"; result = (await conn.QueryAsync(sql, new { userid = UserId })).ToList(); @@ -35,5 +35,86 @@ namespace SolarPower.Repository.Implement return result; } } + + public async Task AddOperationPlan(OperationCreatePlan OperationPlan, List properties) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + string sql = GenerateInsertQuery(properties); + + await conn.ExecuteAsync(sql, OperationPlan); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + } + } + public async Task> OperationPlanTable(List id) + { + List result; + var count = 0; + string Wheresql = "oc.PowerStationId = "; + foreach (int too in id) + { + + if(count == id.Count-1) + { + Wheresql += too.ToString(); + } + else + { + Wheresql += too.ToString() + " OR oc.PowerStationId = "; + } + count++; + } + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT *,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(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + public async Task GetOneOperation(int id) + { + OperationCreatePlan result; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + var sql = @"SELECT * FROM operation_plan_create WHERE Deleted =0 AND Id = @Id"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return result; + } + } } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index 7f20ed9..8338fb5 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -368,5 +368,31 @@ namespace SolarPower.Repository.Implement return updateQuery.ToString(); } + /// + /// 取資料庫當前流水號 + /// + /// + /// + /// + public async Task GetCurrentSerialNumber(string Table_name, string where) + { + string Num; + using IDbConnection conn = _databaseHelper.GetConnection(); + conn.Open(); + try + { + var sql = $"SELECT SerialNumber FROM {Table_name} WHERE {where} ORDER BY SerialNumber DESC"; + Num = await conn.QueryFirstOrDefaultAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return Num; + } } } diff --git a/SolarPower/Repository/Interface/IOperationRepository.cs b/SolarPower/Repository/Interface/IOperationRepository.cs index 8b245ac..284afb2 100644 --- a/SolarPower/Repository/Interface/IOperationRepository.cs +++ b/SolarPower/Repository/Interface/IOperationRepository.cs @@ -6,8 +6,12 @@ using System.Threading.Tasks; namespace SolarPower.Repository.Interface { - public interface IOperationRepository + public interface IOperationRepository : IRepositoryBase { Task> GetPowerStationIdList(int UserId); + Task AddOperationPlan(OperationCreatePlan OperationPlan, List properties); + Task> OperationPlanTable(List id); + Task GetOneOperation(int id); } + } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index 42f719d..94c1b52 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -65,6 +65,13 @@ namespace SolarPower.Repository.Interface /// /// Task DeleteOneOtherTable(int id, string tablename); + /// + /// 取資料庫最後流水號 + /// + /// + /// + /// + Task GetCurrentSerialNumber(string Table_name, string where); } } diff --git a/SolarPower/Views/Operation/Index.cshtml b/SolarPower/Views/Operation/Index.cshtml index 05e4898..a4d9d53 100644 --- a/SolarPower/Views/Operation/Index.cshtml +++ b/SolarPower/Views/Operation/Index.cshtml @@ -23,7 +23,6 @@
-
@@ -103,79 +102,28 @@ 匯出 - + 計劃
- +
- + + - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
計劃單號 項目 電站排程排程週期每次預期/天 開始時間填寫表單描述 email 通知 建立人 建立時間功能
20210630003巡檢新竹交大站每次5天內 / 1周王小明巡檢單01 -
- - -
-
鋼鐵人2021-03-5
20210630003清洗新竹交大站每次5天內 / 5周鋼鐵人清洗單02 -
- - -
-
浩克2021-03-5
20210630003清洗新竹交大站每次5天內 / 5周鋼鐵人清洗單02 -
- - -
-
浩克2021-03-5
@@ -197,89 +145,90 @@