diff --git a/SolarPower/Controllers/SensorTypeController.cs b/SolarPower/Controllers/SensorTypeController.cs new file mode 100644 index 0000000..fb7d95e --- /dev/null +++ b/SolarPower/Controllers/SensorTypeController.cs @@ -0,0 +1,132 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using SolarPower.Repository.Interface; +using SolarPower.Services.Interface; +using System.IO; +using SolarPower.Models; +using SolarPower.Models.SensorType; + +namespace SolarPower.Controllers +{ + public class SensorTypeController : MyBaseController + { + private readonly ISensorTypeRepository sensorRepository; + private string boeFilePath = "/upload/power_station/boe_file/"; + private string stationImageFilePath = "/upload/power_station/"; + private string powerSationSaveAsPath = ""; + + public SensorTypeController(ISensorTypeRepository sensorRepository) : base() + { + this.sensorRepository = sensorRepository; + + powerSationSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "power_station"); + } + + public IActionResult Index() + { + sensorRepository.GetAllSensorAsync(); + return View(); + } + + + /// + /// 新增 / 修改 設備資料 + /// + /// + /// + [HttpPost] + public async Task> SaveSensorType(SensorTypeInfo post) + { + ApiResult apiResult = new ApiResult(); + + SensorType sensor = null; + + try + { + sensor = await sensorRepository.GetOneAsync(post.Id); + + if (sensor == null) + { + + if (post.Id != 0) + { + apiResult.Code = "9998";//待定 + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + #region 新增設備 + sensor = new SensorType() + { + UId = post.UId, + Enabled = post.Enabled, + SensorName = post.SensorName, + SensorNameEn = post.SensorNameEn, + CreatedBy = myUser.Id, + }; + + List properties = new List() + { + "UId", + "Enabled", + "SensorName", + "SensorNameEn", + "CreatedBy", + }; + + var id = await sensorRepository.AddOneAsync(sensor, properties); + + + //var website_url = await powerStationRepository.GetOneVariableByName("WebSiteUrl"); + + + //var sendSubject = "新增設備成功"; + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + #endregion + } + else + { + #region 修改設備 + SensorType update = new SensorType() + { + Id = sensor.Id, + Enabled = post.Enabled, + SensorName = post.SensorName, + SensorNameEn = post.SensorNameEn, + UpdatedBy = myUser.Id + }; + + + List properties = new List() + { + "Id", + "Enabled", + "SensorName", + "SensorNameEn", + "UpdatedBy", + }; + + await sensorRepository.UpdateSensorInfo(update, properties); + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + #endregion + } + } + 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; + } + + } +} diff --git a/SolarPower/Models/SensorType.cs b/SolarPower/Models/SensorType.cs new file mode 100644 index 0000000..3508bc3 --- /dev/null +++ b/SolarPower/Models/SensorType.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SolarPower.Models.SensorType +{ + //Base Class。如由其餘需求,使用繼承 + public class SensorType : SensorTypeInfo + { + //public int Id { get; set; } //編號 + public byte Deleted { get; set; } //是否刪除 + //public int UId { get; set; } //設備編號 + //public int Enabled { get; set; } //啟用 + //public string SensorName { get; set; } //sensor名稱 + //public string SensorNameEn { get; set; } //sensor英文名稱 + } + + /// + /// 設備 + /// + public class SensorTypeInfo : UserInfo + { + public int UId { get; set; }//設備編號 + public string SensorName { get; set; }//傳感器名稱 + public string SensorNameEn { get; set; }//傳感器英文名稱 + public int Enabled { get; set; }//啟用 + } + +} diff --git a/SolarPower/Repository/Implement/SensorTypeRepository.cs b/SolarPower/Repository/Implement/SensorTypeRepository.cs new file mode 100644 index 0000000..0a616c6 --- /dev/null +++ b/SolarPower/Repository/Implement/SensorTypeRepository.cs @@ -0,0 +1,142 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using SolarPower.Models; +using SolarPower.Models.SensorType; +using SolarPower.Repository.Interface; +using Dapper; +using SolarPower.Helper; +using System.Data; + +namespace SolarPower.Repository.Implement +{ + public class SensorTypeRepository : RepositoryBase, ISensorTypeRepository + { + public SensorTypeRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) + { + tableName = "sensor_type"; + } + public async Task> GetAllSensorAsync() + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT Id, Deleted, Enabled, UID, SensorName, SensorNameEn FROM solar_master.sensor_type WHERE deleted = 0"; + + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 新增裝置資料 + /// + /// + /// + /// + public async Task AddSensor(SensorType sensorInfo, List properties) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + //var trans = conn.BeginTransaction(); + using (var trans = conn.BeginTransaction()) + { + try + { + //string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "device"); + //await conn.ExecuteAsync(sql, DeviceInfo); + //trans.Commit(); + + //var sql2 = GenerateUpdateQuery(properties); + var sql = GenerateInsertQueryWithCustomTable(properties, "sensor_type"); + await conn.ExecuteAsync(sql, sensorInfo, trans); + trans.Commit(); + + //string sql = $"INSERT INTO company_auth_page (CompanyId, AuthCode, CreatedBy) VALUES (@CompanyId, @AuthCode, @CreatedBy)"; + //await conn.ExecuteAsync(sql, trans); + //trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + + } + + /// + /// 透過sensorNameEn,取得單一筆資料 + /// + /// + /// + public async Task GetOneByEnglishNameAsync(string sensorNameEn) + { + SensorType result; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + try + { + var sql = $"SELECT * FROM solar_master.sensor_type WHERE deleted = 0 AND sensorNameEn = @SensorNameEn"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { SensorName_En = sensorNameEn }); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 修改設備資料 + /// + /// + /// + public async Task UpdateSensorInfo(SensorType entity, List properties) + { + GetProperties = typeof(SensorType).GetProperties(); + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = GenerateUpdateQuery(properties); + + await conn.ExecuteAsync(sql, entity, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + } +} diff --git a/SolarPower/Repository/Interface/ISensorTypeRepository.cs b/SolarPower/Repository/Interface/ISensorTypeRepository.cs new file mode 100644 index 0000000..2f6ec21 --- /dev/null +++ b/SolarPower/Repository/Interface/ISensorTypeRepository.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using SolarPower.Models; +using SolarPower.Models.SensorType; +using SolarPower.Repository.Interface; + +namespace SolarPower.Repository.Interface +{ + public interface ISensorTypeRepository : IRepositoryBase + { + Task> GetAllSensorAsync(); + + /// + /// 新增 設備 + /// + /// + /// + Task AddSensor(SensorType SensorInfo, List properties); + + /// + /// 透過sensorNameEn,取得單一筆資料 + /// + /// + /// + Task GetOneByEnglishNameAsync(string sensorNameEn); + + /// + /// 修改設備資料 + /// + /// + /// + /// + Task UpdateSensorInfo(SensorType entity, List properties); + + + } +} diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index 1dd5028..7706b29 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -82,6 +82,7 @@ namespace SolarPower services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); #endregion double loginExpireMinute = this.Configuration.GetValue("LoginExpireMinute"); diff --git a/SolarPower/Views/SensorType/Index.cshtml b/SolarPower/Views/SensorType/Index.cshtml new file mode 100644 index 0000000..8def1b3 --- /dev/null +++ b/SolarPower/Views/SensorType/Index.cshtml @@ -0,0 +1,249 @@ +@{ + ViewData["MainNum"] = "7"; + ViewData["SubNum"] = "3"; + ViewData["Title"] = "裝置管理"; +} +@using SolarPower.Models.Role +@model RoleLayerEnum + + + +
+

+ @ViewData["Title"] +

+
+ +
+
+
+
+
+ @*只有平台人員可以新增公司*@ + @if (ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformAdmin || ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformUser) + { + + } +
+
+ + + + + + + + + + + + + +
設備編號啟用Sensor名稱Sensor英文名稱建立時間功能
+
+
+
+
+
+
+
+ + + +@section Scripts { + +} \ No newline at end of file diff --git a/SolarPower/Views/Shared/_Layout.cshtml b/SolarPower/Views/Shared/_Layout.cshtml index 3f3945f..b769f2d 100644 --- a/SolarPower/Views/Shared/_Layout.cshtml +++ b/SolarPower/Views/Shared/_Layout.cshtml @@ -394,7 +394,7 @@ } - @if (ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformAdmin || ViewBag.auths.Contains("Company") || ViewBag.auths.Contains("User") || ViewBag.auths.Contains("Role") || ViewBag.auths.Contains("User"))@*TODO修改定時任務權限*@ + @if (ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformAdmin || ViewBag.auths.Contains("Company") || ViewBag.auths.Contains("User") || ViewBag.auths.Contains("Role") || ViewBag.auths.Contains("User") || ViewBag.auths.Contains("SensorType"))@*TODO修改定時任務權限*@ {
  • @@ -418,22 +418,30 @@
  • } + @*@if (ViewBag.myUser.Role.Layer == (int)RoleLayerEnum.PlatformAdmin || ViewBag.auths.Contains("SensorType")) + { +
  • + + 裝置管理 + +
  • + }*@ @*@if (ViewBag.auths.Contains("User")) - { -
  • - - 功能清單 - -
  • - } - @if (ViewBag.auths.Contains("User")) - { -
  • - - 定時任務設定 - -
  • - }*@ + { +
  • + + 功能清單 + +
  • + } + @if (ViewBag.auths.Contains("User")) + { +
  • + + 定時任務設定 + +
  • + }*@ }