diff --git a/SolarPower/Controllers/CompanyController.cs b/SolarPower/Controllers/CompanyController.cs index 7296b44..95dcf38 100644 --- a/SolarPower/Controllers/CompanyController.cs +++ b/SolarPower/Controllers/CompanyController.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using SolarPower.Models; using SolarPower.Models.Company; +using SolarPower.Models.Role; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; @@ -14,12 +15,17 @@ namespace SolarPower.Controllers public class CompanyController : MyBaseController { private readonly ICompanyRepository companyRepository; + private readonly IRoleRepository roleRepository; private string logoPath = "/upload/company_logo/"; private string logoSaveAsPath = ""; - public CompanyController(ICompanyRepository companyRepository) : base() + public CompanyController( + ICompanyRepository companyRepository, + IRoleRepository roleRepository + ) : base() { this.companyRepository = companyRepository; + this.roleRepository = roleRepository; var xxx = Directory.GetCurrentDirectory(); @@ -75,6 +81,11 @@ namespace SolarPower.Controllers try { + if(myUser.IsGod != 1 && !IsPlatformLayer(myUser.Role.Layer)) + { //如果只是身分公司管理員 或 公司使用者,就只能看自己公司的資料 + post.SelectedCompanyId = myUser.CompanyId; + } + companies = await companyRepository.GetAllByFilterAsync(post); foreach (var company in companies) @@ -94,18 +105,18 @@ namespace SolarPower.Controllers company.RegisterRatio = registerNumber.ToString() + " / " + company.RegisterUpperLimit.ToString(); - //if(mySimpleCompany.Id == 1) - //{ - company.Function = @" + if (myUser.IsGod == 1 || IsPlatformLayer(myUser.Role.Layer)) + { //只有超級使用者 及 平台 可以使用 + company.Function = @" "; - //} - //else - //{ - // company.Functoin = @" - // "; - //} + } + else + { + company.Function = @" + "; + } } totalRecords = companies.Count(); recFilter = companies.Count(); @@ -133,9 +144,9 @@ namespace SolarPower.Controllers } /// - /// 取得單一系統管理員 + /// 取得單一公司資料 /// - /// + /// /// [HttpPost] public async Task> GetOneCompany(int id) @@ -154,6 +165,15 @@ namespace SolarPower.Controllers apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } + else if(company.Id != myUser.CompanyId) + { + if(myUser.IsGod != 0 && !IsPlatformLayer(myUser.Role.Layer)) + { + apiResult.Code = "9993"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + } apiResult.Code = "0000"; apiResult.Data = company; @@ -208,8 +228,6 @@ namespace SolarPower.Controllers return apiResult; } - - company = new Company() { Name = post.Name, @@ -226,11 +244,15 @@ namespace SolarPower.Controllers "TaxIDNumber", "Phone", "Address", - "RegisterUpperLimit", "CreatedBy", }; - var id = await companyRepository.AddAsync(company, properties); + if (myUser.IsGod == 1 || IsPlatformLayer(myUser.Role.Layer)) + { //超級使用者 或 平台人員可以修改 公司的註冊上限人數 + properties.Add("RegisterUpperLimit"); + } + + var id = await companyRepository.AddOneAsync(company, properties); //處裡公司Logo圖片 if (post.LogoFile != null) @@ -260,6 +282,28 @@ namespace SolarPower.Controllers await companyRepository.UpdateCompany(updateCompany, properties); } + #region 幫別間公司新增"公司管理員"之角色 + Role role = new Role() + { + CompanyId = id, + Name = "公司管理員", + Layer = 2, + CreatedBy = myUser.Id, + }; + + List roleProperties = new List() + { + "CompanyId", + "Name", + "Layer", + "CreatedBy", + }; + + await roleRepository.AddAsync(role, roleProperties); + #endregion + + //TODO 自動新增公司DB及Table,公司DB編號規則 solar_com_(公司編號共四碼),ex:solar_com_0001。 + apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion @@ -282,7 +326,6 @@ namespace SolarPower.Controllers Id = post.Id, Name = post.Name, TaxIDNumber = post.TaxIDNumber, - //Status = post.Status, Phone = post.Phone, Address = post.Address, RegisterUpperLimit = post.RegisterUpperLimit, @@ -296,10 +339,14 @@ namespace SolarPower.Controllers "TaxIDNumber", "Phone", "Address", - "RegisterUpperLimit", "UpdatedBy", }; + if (myUser.IsGod == 1 || IsPlatformLayer(myUser.Role.Layer)) + { //超級使用者 或 平台人員可以修改 公司的註冊上限人數 + properties.Add("RegisterUpperLimit"); + } + await companyRepository.UpdateCompany(update, properties); //處裡公司Logo圖片 diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index b151f80..0bbaa4a 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -20,6 +20,7 @@ using System.Text; using SolarPower.Models.OperatorLogModel; using Newtonsoft.Json; using SolarPower.Models.Company; +using SolarPower.Models.Role; namespace SolarPower.Controllers { @@ -34,7 +35,6 @@ namespace SolarPower.Controllers private IOperatorLogRepository operatorLogRepository => HttpContext?.RequestServices.GetService(); protected MyUser myUser = null; - protected SimpleCompany mySimpleCompany = null; public string controllerName; public string actionName; @@ -103,5 +103,20 @@ namespace SolarPower.Controllers operatorLogRepository.Add(operatorLog, properties); #endregion } + + /// + /// 判斷是否維平台的管理員或平台使用者 + /// + /// + /// + public bool IsPlatformLayer(byte roleLayer) + { + if(roleLayer == (int)RoleLayerEnum.PlatformAdmin || roleLayer == (int)RoleLayerEnum.PlatformUser) + { + return true; + } + + return false; + } } } diff --git a/SolarPower/Controllers/RoleController.cs b/SolarPower/Controllers/RoleController.cs index 62e6a9e..92c0b77 100644 --- a/SolarPower/Controllers/RoleController.cs +++ b/SolarPower/Controllers/RoleController.cs @@ -93,9 +93,9 @@ namespace SolarPower.Controllers } /// - /// 取得單一使用者 + /// 取得單一公司角色 /// - /// + /// /// [HttpPost] public async Task> GetOneRole(int id) @@ -157,8 +157,6 @@ namespace SolarPower.Controllers } #region 新增公司角色 - EDFunction edFunction = new EDFunction(); - role = new Role() { CompanyId = post.SelectedCompanyId, @@ -183,7 +181,7 @@ namespace SolarPower.Controllers } else { - #region 修改使用者 + #region 修改公司角色 UpdateRole update = new UpdateRole() { Id = post.Id, @@ -191,7 +189,6 @@ namespace SolarPower.Controllers UpdatedBy = myUser.Id, }; - List properties = new List() { "Id", diff --git a/SolarPower/Models/ErrorCode.cs b/SolarPower/Models/ErrorCode.cs index 4d70362..248099b 100644 --- a/SolarPower/Models/ErrorCode.cs +++ b/SolarPower/Models/ErrorCode.cs @@ -17,6 +17,7 @@ namespace SolarPower.Models { { "0000", "OK" }, { "0001", "傳入參數錯誤。" }, + { "9993", "無此權限操作"}, { "9994", "查無該公司角色"}, { "9995", "該統一編號已被使用。" }, { "9996", "查無該公司資訊。" }, diff --git a/SolarPower/Repository/Implement/CompanyRepository.cs b/SolarPower/Repository/Implement/CompanyRepository.cs index d458c77..d5f4109 100644 --- a/SolarPower/Repository/Implement/CompanyRepository.cs +++ b/SolarPower/Repository/Implement/CompanyRepository.cs @@ -90,7 +90,7 @@ namespace SolarPower.Repository.Implement if (filter.SelectedCompanyId > 0) { - sql += @" Where Id = @SelectedCompanyId"; + sql += @" AND Id = @SelectedCompanyId"; } else { diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index 1b0bb2c..ed47b6a 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -55,6 +55,39 @@ namespace SolarPower.Repository.Implement } } + /// + /// 新增單一筆資料 + /// + /// + /// + /// + public virtual async Task AddOneAsync(T entity, List properties) + { + int id; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + string sql = GenerateInsertQuery(properties); + + sql += "SELECT LAST_INSERT_ID();"; + + id = (await conn.QueryAsync(sql, entity)).Single(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return id; + } + } + /// /// 透過Id,軟刪除單一筆資料 /// @@ -222,7 +255,7 @@ namespace SolarPower.Repository.Implement insertQuery .Remove(insertQuery.Length - 1, 1) - .Append(")"); + .Append(");"); return insertQuery.ToString(); } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index 7a194ea..b83be3f 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -28,6 +28,15 @@ namespace SolarPower.Repository.Interface /// Task AddAsync(T entity, List properties); + /// + /// 新增單一筆資料 + /// + /// + /// + /// + Task AddOneAsync(T entity, List properties); + + /// /// 修改資料 /// diff --git a/SolarPower/Views/Company/Index.cshtml b/SolarPower/Views/Company/Index.cshtml index 4670445..f4de9ab 100644 --- a/SolarPower/Views/Company/Index.cshtml +++ b/SolarPower/Views/Company/Index.cshtml @@ -98,12 +98,12 @@
- +
- +
@@ -430,8 +430,12 @@ //#region 公司資料表單驗證 $("#company-form").validate({ rules: { + company_name_modal: { + required: true, + }, company_taxIDNumber_modal: { required: true, + maxlength: 8, }, }, }); @@ -443,14 +447,6 @@ if ($("#company-form").valid()) { var url = "/Company/SaveCompany"; - var send_data = { - Id: selected_id, - Name: $("#company_name_modal").val(), - TaxIDNumber: $("#company_taxIDNumber_modal").val(), - Phone: $("#company_phone_modal").val(), - Address: $("#company_address_modal").val(), - } - var formData = new FormData(); var logos = $('#company_logo_modal')[0].files; @@ -459,6 +455,7 @@ formData.append("TaxIDNumber", $("#company_taxIDNumber_modal").val()); formData.append("Phone", $("#company_phone_modal").val()); formData.append("Address", $("#company_address_modal").val()); + formData.append("RegisterUpperLimit", $("#company_registerUpperLimit_modal").val()); if (logos.length > 0) { formData.append("LogoFile", logos[0]) }